00001
00002
00003
00004
00005
00006
00007
00008
00012 #include <iostream>
00013 #include <fstream>
00014
00015 #include "zypp/repo/ScriptProvider.h"
00016 #include "zypp/PathInfo.h"
00017 #include "zypp/TmpPath.h"
00018 #include "zypp/Script.h"
00019
00020 using std::endl;
00021
00023 namespace zypp
00024 {
00025
00026 namespace repo
00027 {
00028
00030 namespace
00031 {
00032
00033 typedef std::string (Script::*inlined)() const;
00034 typedef OnMediaLocation (Script::*location)() const;
00035
00037 ManagedFile doProvideScript( repo::RepoMediaAccess & access_r,
00038 const Script & script_r,
00039 inlined inlined_r, location location_r )
00040 {
00041 ManagedFile ret;
00042
00043
00044 std::string inlined( (script_r.*inlined_r)() );
00045 if ( ! inlined.empty() )
00046 {
00047
00048
00049 ret = ManagedFile( filesystem::TmpFile( filesystem::TmpPath::defaultLocation(),
00050 "zypp-script-"+script_r.name() ),
00051 filesystem::unlink );
00052 std::ofstream str( ret.value().c_str() );
00053 str << inlined << endl;
00054 }
00055 else
00056 {
00057
00058 OnMediaLocation location( (script_r.*location_r)() );
00059 if ( ! location.filename().empty() )
00060 {
00061 ret = access_r.provideFile( script_r.repository(), location );
00062 }
00063 else
00064 {
00065
00066 return ManagedFile();
00067 }
00068 }
00069
00070
00071 filesystem::chmod( ret, 0700 );
00072 return ret;
00073 }
00074
00076 }
00078
00080
00081
00082
00083
00084 ScriptProvider::ScriptProvider( repo::RepoMediaAccess & access_r )
00085 : _access( access_r )
00086 {}
00087
00089
00090
00091
00092
00093 ScriptProvider::~ScriptProvider()
00094 {}
00095
00096 ManagedFile ScriptProvider::provideDoScript( const Script_constPtr & script_r ) const
00097 {
00098 ManagedFile ret;
00099 if ( script_r )
00100 {
00101 return doProvideScript( _access, *script_r,
00102 &Script::doScriptInlined,
00103 &Script::doScriptLocation );
00104 }
00105 return ret;
00106 }
00107
00108 ManagedFile ScriptProvider::provideUndoScript( const Script_constPtr & script_r ) const
00109 {
00110 ManagedFile ret;
00111 if ( script_r )
00112 {
00113 return doProvideScript( _access, *script_r,
00114 &Script::undoScriptInlined,
00115 &Script::undoScriptLocation );
00116 }
00117 return ret;
00118 }
00119
00121 }
00124 }