Report compressed size of data written in a backup as well as uncompressed.
[cumulus.git] / store.h
1 /* LBS: An LFS-inspired filesystem backup system
2  * Copyright (C) 2006-2008  Michael Vrable
3  *
4  * Backup data is stored in a collection of objects, which are grouped together
5  * into segments for storage purposes.  This implementation of the object store
6  * represents segments as TAR files and objects as files within them. */
7
8 #ifndef _LBS_STORE_H
9 #define _LBS_STORE_H
10
11 #include <stdint.h>
12
13 #include <list>
14 #include <map>
15 #include <set>
16 #include <string>
17 #include <iostream>
18 #include <sstream>
19
20 #include "localdb.h"
21 #include "remote.h"
22 #include "sha1.h"
23 #include "ref.h"
24
25 class LbsObject;
26
27 /* In memory datatype to represent key/value pairs of information, such as file
28  * metadata.  Currently implemented as map<string, string>. */
29 typedef std::map<std::string, std::string> dictionary;
30
31 /* IOException will be thrown if an error occurs while reading or writing in
32  * one of the I/O wrappers.  Depending upon the context; this may be fatal or
33  * not--typically, errors reading/writing the store will be serious, but errors
34  * reading an individual file are less so. */
35 class IOException : public std::exception {
36 private:
37     std::string error;
38 public:
39     explicit IOException(const std::string &err) { error = err; }
40     virtual ~IOException() throw () { }
41     std::string getError() const { return error; }
42 };
43
44 /* Simplified TAR header--we only need to store regular files, don't need to
45  * handle long filenames, etc. */
46 static const int TAR_BLOCK_SIZE = 512;
47
48 struct tar_header
49 {
50     char name[100];
51     char mode[8];
52     char uid[8];
53     char gid[8];
54     char size[12];
55     char mtime[12];
56     char chksum[8];
57     char typeflag;
58     char linkname[100];
59     char magic[8];
60     char uname[32];
61     char gname[32];
62     char devmajor[8];
63     char devminor[8];
64     char prefix[155];
65     char padding[12];
66 };
67
68 /* A simple wrapper around a single TAR file to represent a segment.  Objects
69  * may only be written out all at once, since the tar header must be written
70  * first; incremental writing is not supported. */
71 class Tarfile {
72 public:
73     Tarfile(RemoteFile *file, const std::string &segment);
74     ~Tarfile();
75
76     void write_object(int id, const char *data, size_t len);
77
78     // Return an estimate of the size of the file.
79     size_t size_estimate();
80
81 private:
82     size_t size;
83     std::string segment_name;
84
85     RemoteFile *file;
86
87     /* Filter support. */
88     int real_fd, filter_fd;
89     pid_t filter_pid;
90
91     // Write data to the tar file
92     void tar_write(const char *data, size_t size);
93 };
94
95 class TarSegmentStore {
96 public:
97     // New segments will be stored in the given directory.
98     TarSegmentStore(RemoteStore *remote,
99                     LocalDb *db = NULL)
100         { this->remote = remote; this->db = db; }
101     ~TarSegmentStore() { sync(); }
102
103     // Writes an object to segment in the store, and returns the name
104     // (segment/object) to refer to it.  The optional parameter group can be
105     // used to control object placement; objects with different group
106     // parameters are kept in separate segments.
107     ObjectReference write_object(const char *data, size_t len,
108                                  const std::string &group = "");
109
110     // Ensure all segments have been fully written.
111     void sync();
112
113     // Dump statistics to stdout about how much data has been written
114     void dump_stats();
115
116 private:
117     struct segment_info {
118         Tarfile *file;
119         std::string group;
120         std::string name;           // UUID
121         int count;                  // Objects written to this segment
122         int size;                   // Combined size of objects written
123         std::string basename;       // Name of segment without directory
124         RemoteFile *rf;
125     };
126
127     RemoteStore *remote;
128     std::map<std::string, struct segment_info *> segments;
129     LocalDb *db;
130
131     // Ensure that all segments in the given group have been fully written.
132     void close_segment(const std::string &group);
133
134     // Parse an object reference string and return just the segment name
135     // portion.
136     std::string object_reference_to_segment(const std::string &object);
137 };
138
139 /* An in-memory representation of an object, which can be incrementally built
140  * before it is written out to a segment. */
141 class LbsObject {
142 public:
143     LbsObject();
144     ~LbsObject();
145
146     // If an object is placed in a group, it will be written out to segments
147     // only containing other objects in the same group.  A group name is simply
148     // a string.
149     //std::string get_group() const { return group; }
150     void set_group(const std::string &g) { group = g; }
151
152     // Data in an object must be written all at once, and cannot be generated
153     // incrementally.  Data can be an arbitrary block of binary data of any
154     // size.  The pointer to the data need only remain valid until write() is
155     // called.
156     void set_data(const char *d, size_t len) { data = d; data_len = len; }
157
158     // Write an object to a segment, thus making it permanent.  This function
159     // can be called at most once.
160     void write(TarSegmentStore *store);
161
162     // Compute the checksum of an object, and include it in the object
163     // reference.  This should be called after write(), and the data specified
164     // by set_data() must remain valid through the call to checksum().
165     void checksum();
166
167     // An object is assigned a permanent name once it has been written to a
168     // segment.  Until that time, its name cannot be determined.
169     std::string get_name() const { return ref.to_string(); }
170     ObjectReference get_ref() { return ref; }
171
172 private:
173     std::string group;
174     const char *data;
175     size_t data_len;
176
177     bool written;
178     ObjectReference ref;
179 };
180
181 /* Program through which segment data is piped before being written to file. */
182 extern const char *filter_program;
183
184 /* Extension which should be appended to segments written out (.tar is already
185  * included; this adds to it) */
186 extern const char *filter_extension;
187
188 /* Launch a process to filter data written to a file descriptor.  fd_out is the
189  * file descriptor where the filtered data should be written.  program is the
190  * filter program to execute (a single string which will be interpreted by
191  * /bin/sh).  The return value is a file descriptor to which the data to be
192  * filtered should be written.  The process ID of the filter process is stored
193  * at address filter_pid if non-NULL. */
194 int spawn_filter(int fd_out, const char *program, pid_t *filter_pid);
195
196 #endif // _LBS_STORE_H