Start work on tests for Cumulus.
[cumulus.git] / metadata.h
1 /* Cumulus: Efficient Filesystem Backup to the Cloud
2  * Copyright (C) 2007-2008 The Cumulus Developers
3  * See the AUTHORS file for a list of contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 /* Handling of metadata written to backup snapshots.  This manages the writing
21  * of file metadata into new backup snapshots, including breaking the metadata
22  * log apart across separate objects.  Eventually this should include unified
23  * handling of the statcache, and re-use of metadata between snapshots.
24  */
25
26 #ifndef _LBS_METADATA_H
27 #define _LBS_METADATA_H
28
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <list>
33 #include <string>
34 #include <sstream>
35
36 #include "store.h"
37 #include "ref.h"
38 #include "util.h"
39
40 extern bool flag_full_metadata;
41
42 /* Metadata for a single inode, ready to be written out. */
43 struct MetadataItem {
44     int offset;
45     std::string text;
46
47     bool reused;
48     ObjectReference ref;
49 };
50
51 class MetadataWriter {
52 public:
53     MetadataWriter(TarSegmentStore *store, const char *path,
54                    const char *snapshot_name, const char *snapshot_scheme);
55     void add(dictionary info);
56     ObjectReference close();
57
58     bool find(const std::string& path);
59     ObjectReference old_ref() const {
60         return ObjectReference::parse(old_metadata_loc);
61     }
62
63     bool is_unchanged(const struct stat *stat_buf);
64
65     dictionary get_old_metadata() const { return old_metadata; }
66     std::list<ObjectReference> get_blocks();
67     std::string get_checksum() { return old_metadata["checksum"]; }
68
69 private:
70     void metadata_flush();
71     void read_statcache();
72
73     // Where are objects eventually written to?
74     TarSegmentStore *store;
75
76     // File descriptors for reading/writing local statcache data
77     std::string statcache_path, statcache_tmp_path;
78     FILE *statcache_in, *statcache_out;
79
80     // Metadata not yet written out to the segment store
81     size_t chunk_size;
82     std::list<MetadataItem> items;
83     std::ostringstream metadata_root;
84
85     // Statcache information read back in from a previous run
86     bool old_metadata_eof;
87     dictionary old_metadata;
88     std::string old_metadata_loc;   // Reference to where the metadata is found
89 };
90
91 #endif // _LBS_METADATA_H