libzypp  13.10.6
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 "zypp/base/Logger.h"
16 #include "zypp/base/String.h"
17 #include "zypp/RepoStatus.h"
18 #include "zypp/PathInfo.h"
19 
20 using namespace std;
21 
23 namespace zypp
24 {
25 
27  //
28  // CLASS NAME : RepoStatus::Impl
29  //
32  {
33 
34  public:
35 
36  string checksum;
38 
40  static shared_ptr<Impl> nullimpl()
41  {
42  static shared_ptr<Impl> _nullimpl( new Impl );
43  return _nullimpl;
44  }
45 
46  private:
47  friend Impl * rwcowClone<Impl>( const Impl * rhs );
49  Impl * clone() const
50  { return new Impl( *this ); }
51  };
53 
55  inline std::ostream & operator<<( std::ostream & str, const RepoStatus::Impl & obj )
56  {
57  return str << obj.checksum << " " << (time_t) obj.timestamp;
58  }
59 
61  //
62  // CLASS NAME : RepoStatus
63  //
65 
67  //
68  // METHOD NAME : RepoStatus::RepoStatus
69  // METHOD TYPE : Ctor
70  //
71  RepoStatus::RepoStatus()
72  : _pimpl( new Impl() )
73  {}
74 
75  RepoStatus::RepoStatus( const Pathname &path )
76  : _pimpl( new Impl() )
77  {
78  PathInfo info(path);
79  if ( info.isExist() )
80  {
81  _pimpl->timestamp = Date(info.mtime());
82  if ( info.isFile() )
83  _pimpl->checksum = filesystem::sha1sum(path);
84  else // non files
85  _pimpl->checksum = str::numstring(info.mtime());
86  }
87  }
88 
90  //
91  // METHOD NAME : RepoStatus::~RepoStatus
92  // METHOD TYPE : Dtor
93  //
95  {}
96 
97  RepoStatus RepoStatus::fromCookieFile( const Pathname &cookiefile )
98  {
99  std::ifstream file(cookiefile.c_str());
100  if (!file) {
101  WAR << "No cookie file " << cookiefile << endl;
102  return RepoStatus();
103  }
104 
105  RepoStatus status;
106  std::string buffer;
107  file >> buffer;
108  status.setChecksum(buffer);
109  file >> buffer;
110  status.setTimestamp(Date(str::strtonum<time_t>(buffer)));
111  return status;
112  }
113 
114  void RepoStatus::saveToCookieFile( const Pathname &cookiefile ) const
115  {
116  std::ofstream file(cookiefile.c_str());
117  if (!file) {
118  ZYPP_THROW (Exception( "Can't open " + cookiefile.asString() ) );
119  }
120  file << this->checksum() << " " << (int) this->timestamp() << endl << endl;
121  file.close();
122  }
123 
124  bool RepoStatus::empty() const
125  {
126  return _pimpl->checksum.empty();
127  }
128 
130  {
131  _pimpl->checksum = checksum;
132  return *this;
133  }
134 
136  {
137  _pimpl->timestamp = timestamp;
138  return *this;
139  }
140 
141  string RepoStatus::checksum() const
142  { return _pimpl->checksum; }
143 
145  { return _pimpl->timestamp; }
146 
147  RepoStatus operator&&( const RepoStatus & lhs, const RepoStatus & rhs )
148  {
149  if ( lhs.empty() )
150  return rhs;
151  if ( rhs.empty() )
152  return lhs;
153 
154  std::string lchk( lhs.checksum() );
155  std::string rchk( rhs.checksum() );
156  // order strings to assert && is kommutativ
157  stringstream ss( lchk < rchk ? lchk+rchk : rchk+lchk );
158 
159  RepoStatus result;
160  result.setChecksum( CheckSum::sha1(ss).checksum() );
161  result.setTimestamp( lhs.timestamp() < rhs.timestamp() ? rhs.timestamp() : lhs.timestamp() );
162  return result;
163  }
164 
165  /******************************************************************
166  **
167  ** FUNCTION NAME : operator<<
168  ** FUNCTION TYPE : std::ostream &
169  */
170  std::ostream & operator<<( std::ostream & str, const RepoStatus & obj )
171  {
172  return str << *obj._pimpl;
173  }
174 
176 } // namespace zypp
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
Date timestamp() const
timestamp of the repository.
Definition: RepoStatus.cc:144
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Definition: PathInfo.cc:970
std::string checksum() const
Checksum of the repository.
Definition: RepoStatus.cc:141
RepoStatus & setTimestamp(const Date &timestamp)
set the repository timestamp
Definition: RepoStatus.cc:135
bool empty() const
Is the status empty?
Definition: RepoStatus.cc:124
RepoStatus operator&&(const RepoStatus &lhs, const RepoStatus &rhs)
combines 2 repostatus with a checksum based on both checksums and the newest timestamp ...
Definition: RepoStatus.cc:147
RepoStatus()
Default ctor.
Definition: RepoStatus.cc:71
static RepoStatus fromCookieFile(const Pathname &path)
reads the status from a file which contains the checksum and timestamp in each line.
Definition: RepoStatus.cc:97
Store and operate on date (time_t).
Definition: Date.h:31
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
static shared_ptr< Impl > nullimpl()
Offer default Impl.
Definition: RepoStatus.cc:40
#define WAR
Definition: Logger.h:48
std::string numstring(char n, int w=0)
Definition: String.h:219
Impl * clone() const
clone for RWCOW_pointer
Definition: RepoStatus.cc:49
void saveToCookieFile(const Pathname &path) const
save the status information to a cookie file
Definition: RepoStatus.cc:114
RepoStatus & setChecksum(const std::string &checksum)
set the repository checksum
Definition: RepoStatus.cc:129
RepoStatus implementation.
Definition: RepoStatus.cc:31
~RepoStatus()
Dtor.
Definition: RepoStatus.cc:94
Base class for Exception.
Definition: Exception.h:143
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
Definition: PathInfo.cc:980
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: RepoStatus.h:126
std::ostream & operator<<(std::ostream &str, const RepoStatus::Impl &obj)
Definition: RepoStatus.cc:55
static CheckSum sha1(const std::string &checksum)
Definition: CheckSum.h:69
Local facts about a repository This class represents the status of a repository on the system...
Definition: RepoStatus.h:37