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 if (_do_script != "")
00067 {
00068 if ( !_tmp_do_script )
00069 _tmp_do_script.reset(new filesystem::TmpFile(getZYpp()->tmpPath(), "zypp-yum-do-script-"));
00070
00071 Pathname pth = _tmp_do_script->path();
00072
00073 ofstream st(pth.asString().c_str());
00074
00075 if ( !st )
00076 {
00077 ZYPP_THROW(Exception(N_("Can't write the patch script to a temporary file.")));
00078 }
00079
00080 st << _do_script << endl;
00081 return pth;
00082 }
00083 else if (_do_location != "" && _do_location != "/")
00084 {
00085 Pathname script = source().provideFile(_do_location, _do_media);
00086 if (! YUMSourceImpl::checkCheckSum(script, _do_checksum.type(), _do_checksum.checksum()))
00087 {
00088 ZYPP_THROW(Exception(N_("Failed check for the script file check sum")));
00089 }
00090 return script;
00091 }
00092 else
00093 {
00094 return Pathname();
00095 }
00096 }
00098 Pathname YUMScriptImpl::undo_script() const
00099 {
00100 if (_undo_script != "")
00101 {
00102 if ( !_tmp_undo_script )
00103 _tmp_undo_script.reset(new filesystem::TmpFile(getZYpp()->tmpPath(), "zypp-yum-undo-script-"));
00104
00105 Pathname pth = _tmp_undo_script->path();
00106
00107 ofstream st(pth.asString().c_str());
00108
00109 if ( !st )
00110 {
00111 ZYPP_THROW(Exception(N_("Can't write the patch script to a temporary file.")));
00112 }
00113
00114 st << _undo_script << endl;
00115 return pth;
00116 }
00117 else if (_undo_location != "" && _undo_location != "/")
00118 {
00119 Pathname script = source().provideFile(_undo_location, _undo_media);
00120 if (! YUMSourceImpl::checkCheckSum(script, _undo_checksum.type(), _undo_checksum.checksum()))
00121 {
00122 ZYPP_THROW(Exception(N_("Failed check for the script file check sum")));
00123 }
00124 return script;
00125 }
00126 else return Pathname();
00127 }
00129 bool YUMScriptImpl::undo_available() const {
00130 return _undo_script != ""
00131 || (_undo_location != "" && _undo_location != "/");
00132 }
00133
00134 Source_Ref YUMScriptImpl::source() const
00135 { return _source; }
00136
00137
00138
00139 }
00141 }
00144 }