backup
[platform/upstream/libzypp.git] / zypp / RepoStatus.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/RepoStatus.cc
10  *
11 */
12 #include <iostream>
13 #include <sstream>
14 //#include "zypp/base/Logger.h"
15 #include "zypp/RepoStatus.h"
16 #include "zypp/PathInfo.h"
17
18 using namespace std;
19
20 ///////////////////////////////////////////////////////////////////
21 namespace zypp
22 { /////////////////////////////////////////////////////////////////
23
24   ///////////////////////////////////////////////////////////////////
25   //
26   //    CLASS NAME : RepoStatus::Impl
27   //
28   /** RepoStatus implementation. */
29   struct RepoStatus::Impl
30   {
31
32   public:
33     
34     string checksum;
35     Date timestamp;
36     
37     /** Offer default Impl. */
38     static shared_ptr<Impl> nullimpl()
39     {
40       static shared_ptr<Impl> _nullimpl( new Impl );
41       return _nullimpl;
42     }
43
44   private:
45     friend Impl * rwcowClone<Impl>( const Impl * rhs );
46     /** clone for RWCOW_pointer */
47     Impl * clone() const
48     { return new Impl( *this ); }
49   };
50   ///////////////////////////////////////////////////////////////////
51
52   /** \relates RepoStatus::Impl Stream output */
53   inline std::ostream & operator<<( std::ostream & str, const RepoStatus::Impl & obj )
54   {
55     return str << "RepoStatus::Impl";
56   }
57
58   ///////////////////////////////////////////////////////////////////
59   //
60   //    CLASS NAME : RepoStatus
61   //
62   ///////////////////////////////////////////////////////////////////
63
64   ///////////////////////////////////////////////////////////////////
65   //
66   //    METHOD NAME : RepoStatus::RepoStatus
67   //    METHOD TYPE : Ctor
68   //
69   RepoStatus::RepoStatus()
70     : _pimpl( new Impl() )
71   {}
72
73   ///////////////////////////////////////////////////////////////////
74   //
75   //    METHOD NAME : RepoStatus::~RepoStatus
76   //    METHOD TYPE : Dtor
77   //
78   RepoStatus::~RepoStatus()
79   {}
80
81   RepoStatus::RepoStatus( const Pathname &path )
82     : _pimpl( new Impl() )
83   {
84       PathInfo info(path);
85       if ( info.isExist() )
86       {
87         _pimpl->checksum = filesystem::sha1sum(path);
88         _pimpl->timestamp = Date(info.mtime());
89       }
90   }
91   
92   bool RepoStatus::empty() const
93   {
94     return _pimpl->checksum.empty();
95   }
96
97   RepoStatus & RepoStatus::setChecksum( const string &checksum )
98   {
99     _pimpl->checksum = checksum;
100     return *this;
101   }
102
103   RepoStatus & RepoStatus::setTimestamp( const Date &timestamp )
104   {
105     _pimpl->timestamp = timestamp;
106     return *this;
107   }
108   
109   string RepoStatus::checksum() const
110   { return _pimpl->checksum; }
111
112   Date RepoStatus::timestamp() const
113   { return _pimpl->timestamp; }
114   
115   RepoStatus operator&&( const RepoStatus &lhs, const RepoStatus &rhs )
116   {
117     RepoStatus result;
118     string combinedcs = (lhs.checksum() + rhs.checksum());
119     stringstream ss(combinedcs);
120     CheckSum newcs(CheckSum::sha1(ss));
121     result.setChecksum(newcs.checksum());
122     result.setTimestamp(lhs.timestamp());
123     if ( rhs.timestamp() > lhs.timestamp() )
124       result.setTimestamp(rhs.timestamp());
125     return result;
126   }
127
128   /******************************************************************
129   **
130   **    FUNCTION NAME : operator<<
131   **    FUNCTION TYPE : std::ostream &
132   */
133   std::ostream & operator<<( std::ostream & str, const RepoStatus & obj )
134   {
135     return str << *obj._pimpl;
136   }
137
138   /////////////////////////////////////////////////////////////////
139 } // namespace zypp
140 ///////////////////////////////////////////////////////////////////