libzypp
10.5.0
|
00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 00005 | / /__ | | | | | | | 00006 | /_____||_| |_| |_| | 00007 | | 00008 \---------------------------------------------------------------------*/ 00012 #include <iostream> 00013 #include <sstream> 00014 #include <fstream> 00015 #include "zypp/base/Logger.h" 00016 #include "zypp/base/String.h" 00017 #include "zypp/RepoStatus.h" 00018 #include "zypp/PathInfo.h" 00019 00020 using namespace std; 00021 00023 namespace zypp 00024 { 00025 00027 // 00028 // CLASS NAME : RepoStatus::Impl 00029 // 00031 struct RepoStatus::Impl 00032 { 00033 00034 public: 00035 00036 string checksum; 00037 Date timestamp; 00038 00040 static shared_ptr<Impl> nullimpl() 00041 { 00042 static shared_ptr<Impl> _nullimpl( new Impl ); 00043 return _nullimpl; 00044 } 00045 00046 private: 00047 friend Impl * rwcowClone<Impl>( const Impl * rhs ); 00049 Impl * clone() const 00050 { return new Impl( *this ); } 00051 }; 00053 00055 inline std::ostream & operator<<( std::ostream & str, const RepoStatus::Impl & obj ) 00056 { 00057 return str << obj.checksum << " " << (time_t) obj.timestamp; 00058 } 00059 00061 // 00062 // CLASS NAME : RepoStatus 00063 // 00065 00067 // 00068 // METHOD NAME : RepoStatus::RepoStatus 00069 // METHOD TYPE : Ctor 00070 // 00071 RepoStatus::RepoStatus() 00072 : _pimpl( new Impl() ) 00073 {} 00074 00075 RepoStatus::RepoStatus( const Pathname &path ) 00076 : _pimpl( new Impl() ) 00077 { 00078 PathInfo info(path); 00079 if ( info.isExist() ) 00080 { 00081 _pimpl->timestamp = Date(info.mtime()); 00082 if ( info.isFile() ) 00083 _pimpl->checksum = filesystem::sha1sum(path); 00084 else // non files 00085 _pimpl->checksum = str::numstring(info.mtime()); 00086 } 00087 } 00088 00090 // 00091 // METHOD NAME : RepoStatus::~RepoStatus 00092 // METHOD TYPE : Dtor 00093 // 00094 RepoStatus::~RepoStatus() 00095 {} 00096 00097 RepoStatus RepoStatus::fromCookieFile( const Pathname &cookiefile ) 00098 { 00099 std::ifstream file(cookiefile.c_str()); 00100 if (!file) { 00101 WAR << "No cookie file " << cookiefile << endl; 00102 return RepoStatus(); 00103 } 00104 00105 RepoStatus status; 00106 std::string buffer; 00107 file >> buffer; 00108 status.setChecksum(buffer); 00109 file >> buffer; 00110 status.setTimestamp(Date(str::strtonum<time_t>(buffer))); 00111 return status; 00112 } 00113 00114 void RepoStatus::saveToCookieFile( const Pathname &cookiefile ) const 00115 { 00116 std::ofstream file(cookiefile.c_str()); 00117 if (!file) { 00118 ZYPP_THROW (Exception( "Can't open " + cookiefile.asString() ) ); 00119 } 00120 file << this->checksum() << " " << (int) this->timestamp() << endl << endl; 00121 file.close(); 00122 } 00123 00124 bool RepoStatus::empty() const 00125 { 00126 return _pimpl->checksum.empty(); 00127 } 00128 00129 RepoStatus & RepoStatus::setChecksum( const string &checksum ) 00130 { 00131 _pimpl->checksum = checksum; 00132 return *this; 00133 } 00134 00135 RepoStatus & RepoStatus::setTimestamp( const Date ×tamp ) 00136 { 00137 _pimpl->timestamp = timestamp; 00138 return *this; 00139 } 00140 00141 string RepoStatus::checksum() const 00142 { return _pimpl->checksum; } 00143 00144 Date RepoStatus::timestamp() const 00145 { return _pimpl->timestamp; } 00146 00147 RepoStatus operator&&( const RepoStatus & lhs, const RepoStatus & rhs ) 00148 { 00149 if ( lhs.empty() ) 00150 return rhs; 00151 if ( rhs.empty() ) 00152 return lhs; 00153 00154 std::string lchk( lhs.checksum() ); 00155 std::string rchk( rhs.checksum() ); 00156 // order strings to assert && is kommutativ 00157 stringstream ss( lchk < rchk ? lchk+rchk : rchk+lchk ); 00158 00159 RepoStatus result; 00160 result.setChecksum( CheckSum::sha1(ss).checksum() ); 00161 result.setTimestamp( lhs.timestamp() < rhs.timestamp() ? rhs.timestamp() : lhs.timestamp() ); 00162 return result; 00163 } 00164 00165 /****************************************************************** 00166 ** 00167 ** FUNCTION NAME : operator<< 00168 ** FUNCTION TYPE : std::ostream & 00169 */ 00170 std::ostream & operator<<( std::ostream & str, const RepoStatus & obj ) 00171 { 00172 return str << *obj._pimpl; 00173 } 00174 00176 } // namespace zypp