Initial implementation of metadata log sharing.
[cumulus.git] / statcache.h
1 /* LBS: An LFS-inspired filesystem backup system Copyright (C) 2007  Michael
2  * Vrable
3  *
4  * To speed backups, we maintain a "stat cache" containing selected information
5  * about all regular files, including modification times and the list of blocks
6  * that comprised the file in the last backup.  If the file has not changed
7  * according to a stat() call, we may re-use the information contained in the
8  * stat cache instead of re-reading the entire file.  It is always safe to
9  * discard information from the stat cache; this will only cause a file to be
10  * re-read to determine that it contains the same data as before.
11  *
12  * The stat cache is stored in a file called "statcache" in the local backup
13  * directory.  During a backup, a new statcache file is written out with a
14  * suffix based on the current time; at the end of a successful backup this
15  * file is renamed over the original statcache file.
16  *
17  * The information in the statcache file is stored in sorted order as we
18  * traverse the filesystem, so that we can read and write it in a purely
19  * streaming manner.  (This is why we don't include the information in the
20  * SQLite local database; doing so is likely less efficient.)
21  */
22
23 #ifndef _LBS_STATCACHE_H
24 #define _LBS_STATCACHE_H
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29
30 #include <iostream>
31 #include <list>
32 #include <string>
33
34 #include "ref.h"
35
36 class StatCache {
37 public:
38     void Open(const char *path, const char *snapshot_name,
39               const char *snapshot_scheme);
40     void Close();
41     bool Find(const std::string &path, const struct stat *stat_buf);
42     void Save(const std::string &path, struct stat *stat_buf,
43               const std::string &checksum,
44               const std::list<std::string> &blocks);
45
46     std::string get_checksum() const { return old_checksum; }
47     const std::list<ObjectReference> &get_blocks() const
48         { return old_contents; }
49
50 private:
51     void ReadNext();
52
53     std::string oldpath, newpath;
54     std::ifstream *oldcache;
55     std::ofstream *newcache;
56
57     /* Information about one file read from the old cache. */
58     bool end_of_cache;
59     bool old_is_validated;
60     int64_t old_mtime, old_ctime, old_inode, old_size;
61     std::string old_name, old_checksum;
62     std::list<ObjectReference> old_contents;
63 };
64
65 #endif // _LBS_STATCACHE_H