libzypp 17.31.23
RepoStatus.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <sstream>
14#include <fstream>
15#include <optional>
16#include <set>
17#include <zypp/base/Logger.h>
18#include <zypp/base/String.h>
19#include <zypp/RepoStatus.h>
20#include <zypp/RepoInfo.h>
21#include <zypp/PathInfo.h>
22
23using std::endl;
24
26namespace zypp
27{
29 namespace
30 {
32 void recursiveTimestamp( const Pathname & dir_r, time_t & max_r )
33 {
34 std::list<std::string> dircontent;
35 if ( filesystem::readdir( dircontent, dir_r, false/*no dots*/ ) != 0 )
36 return; // readdir logged the error
37
38 for_( it, dircontent.begin(), dircontent.end() )
39 {
40 PathInfo pi( dir_r + *it, PathInfo::LSTAT );
41 if ( pi.isDir() )
42 {
43 if ( pi.mtime() > max_r )
44 max_r = pi.mtime();
45 recursiveTimestamp( pi.path(), max_r );
46 }
47 }
48 }
49 } // namespace
51
53 //
54 // CLASS NAME : RepoStatus::Impl
55 //
58 {
59 using Checksums = std::set<std::string>;
60
61 public:
69 void assignFromCtor( std::string && checksum_r, Date && timestamp_r )
70 {
71 if ( !checksum_r.empty() ) {
72 static const std::string magic( "43" );
73 checksum_r += magic;
74 _checksums.insert( std::move(checksum_r) );
75 }
76 _timestamp = std::move(timestamp_r);
77 }
78
80 void inject( std::string && checksum_r, Date && timestamp_r )
81 {
82 if ( !checksum_r.empty() ) {
83 _checksums.insert( std::move(checksum_r) );
84 _cachedchecksum.reset();
85 }
86
87 if ( timestamp_r > _timestamp )
88 _timestamp = timestamp_r;
89 }
90
92 void injectFrom( const Impl & rhs )
93 {
94 if ( &rhs == this ) // no self insert
95 return;
96
97 if ( !rhs._checksums.empty() ) {
98 _checksums.insert( rhs._checksums.begin(), rhs._checksums.end() );
99 _cachedchecksum.reset();
100 }
101
102 if ( rhs._timestamp > _timestamp )
104 }
105
106 bool empty() const
107 { return _checksums.empty(); }
108
109 std::string checksum() const
110 {
111 std::string ret;
112 if ( _checksums.empty() )
113 return ret;
114
115 if ( _checksums.size() == 1 )
116 ret = *_checksums.begin();
117 else {
118 if ( !_cachedchecksum ) {
119 std::stringstream ss;
120 for ( std::string_view c : _checksums )
121 ss << c;
123 }
124 ret = *_cachedchecksum;
125 }
126 return ret;
127 }
128
130 { return _timestamp; }
131
133 std::ostream & dumpOn( std::ostream & str ) const
134 { return str << ( empty() ? "NO_REPOSTATUS" : checksum() ) << " " << time_t(_timestamp); }
135
136 private:
139
140 mutable std::optional<std::string> _cachedchecksum;
141
142 private:
143 friend Impl * rwcowClone<Impl>( const Impl * rhs );
145 Impl * clone() const
146 { return new Impl( *this ); }
147 };
149
151 //
152 // CLASS NAME : RepoStatus
153 //
155
157 : _pimpl( new Impl() )
158 {}
159
161 : _pimpl( new Impl() )
162 {
163 PathInfo info( path_r );
164 if ( info.isExist() )
165 {
166 if ( info.isFile() )
167 {
168 _pimpl->assignFromCtor( filesystem::sha1sum( path_r ), Date( info.mtime() ) );
169 }
170 else if ( info.isDir() )
171 {
172 time_t t = info.mtime();
173 recursiveTimestamp( path_r, t );
174 _pimpl->assignFromCtor( CheckSum::sha1FromString( str::numstring( t ) ).checksum(), Date( t ) );
175 }
176 }
177 }
178
180 : _pimpl( new Impl() )
181 {
182 _pimpl->assignFromCtor( CheckSum::sha1FromString( info_r.url().asString() ).checksum(), Date() );
183 }
184
185 RepoStatus::RepoStatus( std::string checksum_r, Date timestamp_r )
186 : _pimpl( new Impl() )
187 {
188 _pimpl->assignFromCtor( std::move(checksum_r), std::move(timestamp_r) );
189 }
190
192 {}
193
195 {
196 RepoStatus ret;
197 std::ifstream file( path_r.c_str() );
198 if ( !file )
199 {
200 WAR << "No cookie file " << path_r << endl;
201 }
202 else
203 {
204 // line := "[checksum] time_t" !!! strip time from line
205 std::string line { str::getline( file ) };
206 Date stmp { str::strtonum<time_t>( str::stripLastWord( line ) ) };
207 ret._pimpl->inject( std::move(line), std::move(stmp) ); // raw inject to avoid magic being added
208 }
209 return ret;
210 }
211
212 void RepoStatus::saveToCookieFile( const Pathname & path_r ) const
213 {
214 std::ofstream file(path_r.c_str());
215 if (!file) {
216 ZYPP_THROW (Exception( "Can't open " + path_r.asString() ) );
217 }
218 file << _pimpl->checksum() << " " << time_t(_pimpl->timestamp()) << endl;
219 file.close();
220 }
221
222 bool RepoStatus::empty() const
223 { return _pimpl->empty(); }
224
226 { return _pimpl->timestamp(); }
227
228 std::ostream & operator<<( std::ostream & str, const RepoStatus & obj )
229 { return obj._pimpl->dumpOn( str ); }
230
231 RepoStatus operator&&( const RepoStatus & lhs, const RepoStatus & rhs )
232 {
233 RepoStatus result { lhs };
234 result._pimpl->injectFrom( *rhs._pimpl );
235 return result;
236 }
237
238 bool operator==( const RepoStatus & lhs, const RepoStatus & rhs )
239 { return lhs._pimpl->checksum() == rhs._pimpl->checksum(); }
240
242} // namespace zypp
static CheckSum sha1FromString(const std::string &input_r)
Definition: CheckSum.h:105
static CheckSum sha1(const std::string &checksum)
Definition: CheckSum.h:75
std::string checksum() const
Definition: CheckSum.cc:170
Store and operate on date (time_t).
Definition: Date.h:33
Base class for Exception.
Definition: Exception.h:146
What is known about a repository.
Definition: RepoInfo.h:72
Url url() const
Pars pro toto: The first repository url.
Definition: RepoInfo.h:131
Track changing files or directories.
Definition: RepoStatus.h:41
RWCOW_pointer< Impl > _pimpl
Implementation.
Definition: RepoStatus.h:90
static RepoStatus fromCookieFile(const Pathname &path)
Reads the status from a cookie file.
Definition: RepoStatus.cc:194
Date timestamp() const
The time the data were changed the last time.
Definition: RepoStatus.cc:225
bool empty() const
Whether the status is empty (empty checksum)
Definition: RepoStatus.cc:222
RepoStatus()
Default ctor.
Definition: RepoStatus.cc:156
void saveToCookieFile(const Pathname &path_r) const
Save the status information to a cookie file.
Definition: RepoStatus.cc:212
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:497
Wrapper class for stat/lstat.
Definition: PathInfo.h:221
time_t mtime() const
Definition: PathInfo.h:376
bool isExist() const
Return whether valid stat info exists.
Definition: PathInfo.h:281
const char * c_str() const
String representation.
Definition: Pathname.h:110
const std::string & asString() const
String representation.
Definition: Pathname.h:91
String related utilities and Regular expression matching.
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition: PathInfo.cc:605
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Definition: PathInfo.cc:1041
std::string stripLastWord(std::string &line, const bool rtrim_first)
Definition: String.cc:296
std::string numstring(char n, int w=0)
Definition: String.h:289
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
Definition: String.cc:478
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
RepoStatus operator&&(const RepoStatus &lhs, const RepoStatus &rhs)
Definition: RepoStatus.cc:231
RepoStatus implementation.
Definition: RepoStatus.cc:58
Date timestamp() const
Definition: RepoStatus.cc:129
void assignFromCtor(std::string &&checksum_r, Date &&timestamp_r)
Assign data called from RepoStatus ctor (adds magic).
Definition: RepoStatus.cc:69
std::string checksum() const
Definition: RepoStatus.cc:109
void injectFrom(const Impl &rhs)
Inject the raw data from rhs.
Definition: RepoStatus.cc:92
std::ostream & dumpOn(std::ostream &str) const
Dump to log file (not to/from CookieFile).
Definition: RepoStatus.cc:133
std::set< std::string > Checksums
Definition: RepoStatus.cc:59
std::optional< std::string > _cachedchecksum
Definition: RepoStatus.cc:140
void inject(std::string &&checksum_r, Date &&timestamp_r)
Inject raw data (no magic added).
Definition: RepoStatus.cc:80
Impl * clone() const
clone for RWCOW_pointer
Definition: RepoStatus.cc:145
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
#define WAR
Definition: Logger.h:97