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