/
RepoStatus.cc
177 lines (152 loc) · 4.82 KB
/
RepoStatus.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*---------------------------------------------------------------------\
| ____ _ __ __ ___ |
| |__ / \ / / . \ . \ |
| / / \ V /| _/ _/ |
| / /__ | | | | | | |
| /_____||_| |_| |_| |
| |
\---------------------------------------------------------------------*/
/** \file zypp/RepoStatus.cc
*
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include "zypp/base/Logger.h"
#include "zypp/base/String.h"
#include "zypp/RepoStatus.h"
#include "zypp/PathInfo.h"
using namespace std;
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
// CLASS NAME : RepoStatus::Impl
//
/** RepoStatus implementation. */
struct RepoStatus::Impl
{
public:
string checksum;
Date timestamp;
/** Offer default Impl. */
static shared_ptr<Impl> nullimpl()
{
static shared_ptr<Impl> _nullimpl( new Impl );
return _nullimpl;
}
private:
friend Impl * rwcowClone<Impl>( const Impl * rhs );
/** clone for RWCOW_pointer */
Impl * clone() const
{ return new Impl( *this ); }
};
///////////////////////////////////////////////////////////////////
/** \relates RepoStatus::Impl Stream output */
inline std::ostream & operator<<( std::ostream & str, const RepoStatus::Impl & obj )
{
return str << obj.checksum << " " << (time_t) obj.timestamp;
}
///////////////////////////////////////////////////////////////////
//
// CLASS NAME : RepoStatus
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : RepoStatus::RepoStatus
// METHOD TYPE : Ctor
//
RepoStatus::RepoStatus()
: _pimpl( new Impl() )
{}
RepoStatus::RepoStatus( const Pathname &path )
: _pimpl( new Impl() )
{
PathInfo info(path);
if ( info.isExist() )
{
_pimpl->timestamp = Date(info.mtime());
if ( info.isFile() )
_pimpl->checksum = filesystem::sha1sum(path);
else // non files
_pimpl->checksum = str::numstring(info.mtime());
}
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : RepoStatus::~RepoStatus
// METHOD TYPE : Dtor
//
RepoStatus::~RepoStatus()
{}
RepoStatus RepoStatus::fromCookieFile( const Pathname &cookiefile )
{
std::ifstream file(cookiefile.c_str());
if (!file) {
WAR << "No cookie file " << cookiefile << endl;
return RepoStatus();
}
RepoStatus status;
std::string buffer;
file >> buffer;
status.setChecksum(buffer);
file >> buffer;
status.setTimestamp(Date(str::strtonum<time_t>(buffer)));
return status;
}
void RepoStatus::saveToCookieFile( const Pathname &cookiefile ) const
{
std::ofstream file(cookiefile.c_str());
if (!file) {
ZYPP_THROW (Exception( "Can't open " + cookiefile.asString() ) );
}
file << this->checksum() << " " << (int) this->timestamp() << endl << endl;
file.close();
}
bool RepoStatus::empty() const
{
return _pimpl->checksum.empty();
}
RepoStatus & RepoStatus::setChecksum( const string &checksum )
{
_pimpl->checksum = checksum;
return *this;
}
RepoStatus & RepoStatus::setTimestamp( const Date ×tamp )
{
_pimpl->timestamp = timestamp;
return *this;
}
string RepoStatus::checksum() const
{ return _pimpl->checksum; }
Date RepoStatus::timestamp() const
{ return _pimpl->timestamp; }
RepoStatus operator&&( const RepoStatus & lhs, const RepoStatus & rhs )
{
if ( lhs.empty() )
return rhs;
if ( rhs.empty() )
return lhs;
std::string lchk( lhs.checksum() );
std::string rchk( rhs.checksum() );
// order strings to assert && is kommutativ
stringstream ss( lchk < rchk ? lchk+rchk : rchk+lchk );
RepoStatus result;
result.setChecksum( CheckSum::sha1(ss).checksum() );
result.setTimestamp( lhs.timestamp() < rhs.timestamp() ? rhs.timestamp() : lhs.timestamp() );
return result;
}
/******************************************************************
**
** FUNCTION NAME : operator<<
** FUNCTION TYPE : std::ostream &
*/
std::ostream & operator<<( std::ostream & str, const RepoStatus & obj )
{
return str << *obj._pimpl;
}
/////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////