libzypp
10.5.0
|
00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 00005 | / /__ | | | | | | | 00006 | /_____||_| |_| |_| | 00007 | | 00008 \---------------------------------------------------------------------*/ 00012 #include <iostream> 00013 #include <fstream> 00014 00015 #include "zypp/base/LogTools.h" 00016 #include "zypp/base/IOStream.h" 00017 #include "zypp/base/String.h" 00018 00019 #include "zypp/PathInfo.h" 00020 #include "zypp/TmpPath.h" 00021 #include "zypp/Date.h" 00022 00023 #include "zypp/target/SoftLocksFile.h" 00024 00025 using std::endl; 00026 00028 namespace zypp 00029 { 00030 00031 namespace target 00032 { 00033 00034 void SoftLocksFile::load( const Pathname & file_r, Data & data_r ) 00035 { 00036 PathInfo pi( file_r ); 00037 if ( ! pi.isFile() ) 00038 { 00039 WAR << "Can't read " << pi << endl; 00040 return; 00041 } 00042 std::ifstream infile( file_r.c_str() ); 00043 for( iostr::EachLine in( infile ); in; in.next() ) 00044 { 00045 std::string l( str::trim(*in) ); 00046 if ( ! l.empty() && l[0] != '#' ) 00047 { 00048 data_r.insert( IdString(l) ); 00049 } 00050 } 00051 MIL << "Read " << pi << endl; 00052 } 00053 00054 void SoftLocksFile::store( const Pathname & file_r, const Data & data_r ) 00055 { 00056 filesystem::TmpFile tmp( filesystem::TmpFile::makeSibling( file_r ) ); 00057 filesystem::chmod( tmp.path(), 0644 ); 00058 00059 std::ofstream outs( tmp.path().c_str() ); 00060 outs << "# zypp::SoftLocksFile generated " << Date::now() << endl; 00061 dumpRange( outs, data_r.begin(), data_r.end(), "#", "\n", "\n", "\n", "#\n" ); 00062 outs.close(); 00063 00064 if ( outs.good() ) 00065 { 00066 filesystem::rename( tmp.path(), file_r ); 00067 MIL << "Wrote " << PathInfo(file_r) << endl; 00068 } 00069 else 00070 { 00071 ERR << "Can't write " << PathInfo(tmp.path()) << endl; 00072 } 00073 } 00074 00075 /****************************************************************** 00076 ** 00077 ** FUNCTION NAME : operator<< 00078 ** FUNCTION TYPE : std::ostream & 00079 */ 00080 std::ostream & operator<<( std::ostream & str, const SoftLocksFile & obj ) 00081 { 00082 str << obj.file() << ' '; 00083 if ( obj._dataPtr ) 00084 str << obj.data(); 00085 else 00086 str << "(unloaded)"; 00087 return str; 00088 } 00089 00091 } // namespace target 00094 } // namespace zypp