Imported Upstream version 14.32.2
[platform/upstream/libzypp.git] / zypp / base / WatchFile.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/WatchFile.h
10  *
11 */
12 #ifndef ZYPP_BASE_WATCHFILE_H
13 #define ZYPP_BASE_WATCHFILE_H
14
15 #include <iosfwd>
16
17 #include "zypp/PathInfo.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   ///////////////////////////////////////////////////////////////////
24   //
25   //    CLASS NAME : WatchFile
26   //
27   /** Remember a files attributes to detect content changes.
28    *
29    * Repeatedly call \ref hasChanged to check whether the content has
30    * changed since the last call. Creation or deletion of the file will
31    * be reported as change as well.
32    *
33    * Per default the ctor stats the file, so \ref hasChanged will detect
34    * changes done after \ref WatchFile was created.
35    *
36    * You may omit the initial stat by passing \c NO_INIT as second argument
37    * to the ctor. \ref WatchFile  will behave as if the file did not exist
38    * at the time \ref WatchFile was created.
39    *
40    * \code
41    * static WatchFile sysconfigFile( "/etc/sysconfig/SuSEfirewall2",
42    *                                 WatchFile::NO_INIT );
43    * if ( sysconfigFile.hasChanged() )
44    * {
45    *   // reload the file...
46    * }
47    * \endcode
48   */
49   class WatchFile
50   {
51     public:
52       enum Initial { NO_INIT, INIT };
53
54     public:
55       /** */
56       WatchFile( const Pathname & path_r = Pathname(),
57                  Initial mode            = INIT )
58       : _path( path_r )
59       {
60         PathInfo pi( mode == INIT ? path_r : Pathname() );
61         _size  = pi.size();
62         _mtime = pi.mtime();
63       }
64
65       const Pathname & path() const
66       { return _path; }
67
68       bool hasChanged()
69       {
70         PathInfo pi( _path );
71         if ( _size != pi.size() || _mtime != pi.mtime() )
72         {
73           _size = pi.size();
74           _mtime = pi.mtime();
75           return true;
76         }
77         return false;
78       }
79
80     private:
81       Pathname _path;
82       off_t  _size;
83       time_t _mtime;
84   };
85   ///////////////////////////////////////////////////////////////////
86
87   /////////////////////////////////////////////////////////////////
88 } // namespace zypp
89 ///////////////////////////////////////////////////////////////////
90 #endif // ZYPP_BASE_WATCHFILE_H