libzypp: zypp/RepoStatus.cc Source File

libzypp 17.25.10

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
23 using std::endl;
24
26 namespace 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 //
57 struct RepoStatus::Impl
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 )
103 _timestamp = rhs._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;
122 _cachedchecksum = CheckSum::sha1(ss).checksum();
123 }
124 ret = *_cachedchecksum;
125 }
126 return ret;
127 }
128
129 Date timestamp() const
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:
137 Checksums _checksums;
138 Date _timestamp;
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
156 RepoStatus::RepoStatus()
157 : _pimpl( new Impl() )
158 {}
159
160 RepoStatus::RepoStatus( const Pathname & path_r )
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
179 RepoStatus::RepoStatus( const RepoInfo & info_r )
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
191 RepoStatus::~RepoStatus()
192 {}
193
194 RepoStatus RepoStatus::fromCookieFile( const Pathname & path_r )
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
225 Date RepoStatus::timestamp() const
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
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
Logger.h
WAR
#define WAR
Definition: Logger.h:92
PathInfo.h
RepoInfo.h
RepoStatus.h
String.h
zypp::CheckSum::sha1FromString
static CheckSum sha1FromString(const std::string &input_r)
Definition: CheckSum.h:105
zypp::CheckSum::sha1
static CheckSum sha1(const std::string &checksum)
Definition: CheckSum.h:75
zypp::CheckSum::checksum
std::string checksum() const
Definition: CheckSum.cc:170
zypp::Date
Store and operate on date (time_t).
Definition: Date.h:33
zypp::Exception
Base class for Exception.
Definition: Exception.h:146
zypp::RepoInfo
What is known about a repository.
Definition: RepoInfo.h:72
zypp::RepoInfo::url
Url url() const
Pars pro toto: The first repository url.
Definition: RepoInfo.h:131
zypp::RepoStatus
Track changing files or directories.
Definition: RepoStatus.h:41
zypp::RepoStatus::_pimpl
RWCOW_pointer< Impl > _pimpl
Implementation.
Definition: RepoStatus.h:88
zypp::RepoStatus::fromCookieFile
static RepoStatus fromCookieFile(const Pathname &path)
Reads the status from a cookie file.
Definition: RepoStatus.cc:194
zypp::RepoStatus::timestamp
Date timestamp() const
The time the data were changed the last time.
Definition: RepoStatus.cc:225
zypp::RepoStatus::~RepoStatus
~RepoStatus()
Dtor.
Definition: RepoStatus.cc:191
zypp::RepoStatus::empty
bool empty() const
Whether the status is empty (empty checksum)
Definition: RepoStatus.cc:222
zypp::RepoStatus::RepoStatus
RepoStatus()
Default ctor.
Definition: RepoStatus.cc:156
zypp::RepoStatus::saveToCookieFile
void saveToCookieFile(const Pathname &path_r) const
Save the status information to a cookie file.
Definition: RepoStatus.cc:212
zypp::Url::asString
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:492
zypp::filesystem::PathInfo
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:221
zypp::filesystem::PathInfo::mtime
time_t mtime() const
Definition: PathInfo.h:376
zypp::filesystem::PathInfo::isDir
bool isDir() const
Definition: PathInfo.h:290
zypp::filesystem::PathInfo::isFile
bool isFile() const
Definition: PathInfo.h:289
zypp::filesystem::PathInfo::LSTAT
@ LSTAT
Definition: PathInfo.h:226
zypp::filesystem::PathInfo::isExist
bool isExist() const
Return whether valid stat info exists.
Definition: PathInfo.h:281
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:45
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition: Pathname.h:91
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition: Pathname.h:110
str
String related utilities and Regular expression matching.
zypp::filesystem::checksum
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
Definition: PathInfo.cc:1013
zypp::filesystem::readdir
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition: PathInfo.cc:598
zypp::filesystem::sha1sum
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Definition: PathInfo.cc:1003
zypp::str::stripLastWord
std::string stripLastWord(std::string &line, const bool rtrim_first)
Definition: String.cc:296
zypp::str::numstring
std::string numstring(char n, int w=0)
Definition: String.h:286
zypp::str::getline
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
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
zypp::operator==
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...
Definition: SetRelationMixin.h:84
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
zypp::operator&&
RepoStatus operator&&(const RepoStatus &lhs, const RepoStatus &rhs)
Definition: RepoStatus.cc:231
zypp::RepoStatus::Impl
RepoStatus implementation.
Definition: RepoStatus.cc:58
zypp::RepoStatus::Impl::timestamp
Date timestamp() const
Definition: RepoStatus.cc:129
zypp::RepoStatus::Impl::assignFromCtor
void assignFromCtor(std::string &&checksum_r, Date &&timestamp_r)
Assign data called from RepoStatus ctor (adds magic).
Definition: RepoStatus.cc:69
zypp::RepoStatus::Impl::checksum
std::string checksum() const
Definition: RepoStatus.cc:109
zypp::RepoStatus::Impl::clone
Impl * clone() const
clone for RWCOW_pointer
Definition: RepoStatus.cc:145
zypp::RepoStatus::Impl::injectFrom
void injectFrom(const Impl &rhs)
Inject the raw data from rhs.
Definition: RepoStatus.cc:92
zypp::RepoStatus::Impl::empty
bool empty() const
Definition: RepoStatus.cc:106
zypp::RepoStatus::Impl::dumpOn
std::ostream & dumpOn(std::ostream &str) const
Dump to log file (not to/from CookieFile).
Definition: RepoStatus.cc:133
zypp::RepoStatus::Impl::Checksums
std::set< std::string > Checksums
Definition: RepoStatus.cc:59
zypp::RepoStatus::Impl::_cachedchecksum
std::optional< std::string > _cachedchecksum
Definition: RepoStatus.cc:140
zypp::RepoStatus::Impl::_checksums
Checksums _checksums
Definition: RepoStatus.cc:137
zypp::RepoStatus::Impl::inject
void inject(std::string &&checksum_r, Date &&timestamp_r)
Inject raw data (no magic added).
Definition: RepoStatus.cc:80
zypp::RepoStatus::Impl::_timestamp
Date _timestamp
Definition: RepoStatus.cc:138