ScriptProvider.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
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         // 1st try inlined
00044         std::string inlined( (script_r.*inlined_r)() );
00045         if ( ! inlined.empty() )
00046         {
00047           // Take care the TmpFile goes out of scope BEFORE the
00048           // ofstream opens the file again.
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           // otherwise try download
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             // no script
00066             return ManagedFile();
00067           }
00068         }
00069 
00070         // HERE: got the script
00071         filesystem::chmod( ret, 0700 );
00072         return ret;
00073       }
00074 
00076     } // namespace
00078 
00080     //
00081     //  METHOD NAME : ScriptProvider::ScriptProvider
00082     //  METHOD TYPE : Ctor
00083     //
00084     ScriptProvider::ScriptProvider( repo::RepoMediaAccess & access_r )
00085       : _access( access_r )
00086     {}
00087 
00089     //
00090     //  METHOD NAME : ScriptProvider::~ScriptProvider
00091     //  METHOD TYPE : Dtor
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   } // namespace repo
00124 } // namespace zypp

Generated on Tue Sep 25 19:23:05 2007 for libzypp by  doxygen 1.5.3