00001
00002
00003
00004
00005
00006
00007
00008
00012 #include "zypp/source/yum/YUMScriptImpl.h"
00013 #include "zypp/Arch.h"
00014 #include "zypp/Edition.h"
00015 #include "zypp/base/Gettext.h"
00016 #include "zypp/ZYppFactory.h"
00017
00018 #include "zypp/source/yum/YUMSourceImpl.h"
00019
00020 #include <fstream>
00021
00022
00023 using namespace std;
00024 using namespace zypp::detail;
00025
00027 namespace zypp
00028 {
00029
00030 namespace source
00031 {
00032 namespace yum
00033 {
00034
00036
00037
00038
00040
00043 YUMScriptImpl::YUMScriptImpl(
00044 Source_Ref source_r,
00045 const zypp::parser::yum::YUMPatchScript & parsed
00046 )
00047 : _do_script(parsed.do_script)
00048 , _undo_script(parsed.undo_script)
00049 , _do_location(parsed.do_location)
00050 , _undo_location(parsed.undo_location)
00051 , _do_media(1)
00052 , _undo_media(1)
00053 , _do_checksum(parsed.do_checksum_type, parsed.do_checksum)
00054 , _undo_checksum(parsed.undo_checksum_type, parsed.undo_checksum)
00055 , _source(source_r)
00056 {
00057 unsigned do_media = strtol(parsed.do_media.c_str(), 0, 10);
00058 if (do_media > 0)
00059 _do_media = do_media;
00060 unsigned undo_media = strtol(parsed.undo_media.c_str(), 0, 10);
00061 if (undo_media > 0)
00062 _undo_media = undo_media;
00063 }
00064
00065 Pathname YUMScriptImpl::do_script() const
00066 {
00067 if (_do_script != "")
00068 {
00069 if ( !_tmp_do_script )
00070 _tmp_do_script.reset(new filesystem::TmpFile(getZYpp()->tmpPath(), "zypp-yum-do-script-"));
00071
00072 Pathname pth = _tmp_do_script->path();
00073
00074 ofstream st(pth.asString().c_str());
00075
00076 if ( !st )
00077 {
00078 ZYPP_THROW(Exception(N_("Can't write the patch script to a temporary file.")));
00079 }
00080
00081 st << _do_script << endl;
00082 return pth;
00083 }
00084 else if (_do_location != "" && _do_location != "/")
00085 {
00086 Pathname script = source().provideFile(_do_location, _do_media);
00087 if (! filesystem::is_checksum(script, _do_checksum))
00088 {
00089 ZYPP_THROW(Exception(N_("Failed check for the script file check sum")));
00090 }
00091 return script;
00092 }
00093 else
00094 {
00095 return Pathname();
00096 }
00097 }
00099 Pathname YUMScriptImpl::undo_script() const
00100 {
00101 if (_undo_script != "")
00102 {
00103 if ( !_tmp_undo_script )
00104 _tmp_undo_script.reset(new filesystem::TmpFile(getZYpp()->tmpPath(), "zypp-yum-undo-script-"));
00105
00106 Pathname pth = _tmp_undo_script->path();
00107
00108 ofstream st(pth.asString().c_str());
00109
00110 if ( !st )
00111 {
00112 ZYPP_THROW(Exception(N_("Can't write the patch script to a temporary file.")));
00113 }
00114
00115 st << _undo_script << endl;
00116 return pth;
00117 }
00118 else if (_undo_location != "" && _undo_location != "/")
00119 {
00120 Pathname script = source().provideFile(_undo_location, _undo_media);
00121 if (! filesystem::is_checksum(script, _undo_checksum) )
00122 {
00123 ZYPP_THROW(Exception(N_("Failed check for the script file check sum")));
00124 }
00125 return script;
00126 }
00127 else return Pathname();
00128 }
00130 bool YUMScriptImpl::undo_available() const
00131 {
00132 return _undo_script != ""
00133 || (_undo_location != "" && _undo_location != "/");
00134 }
00135
00136 Source_Ref YUMScriptImpl::source() const
00137 {
00138 return _source;
00139 }
00140
00141
00142
00143 }
00145 }
00148 }