Initial support for filtering TAR files through an external program.
[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  * is built on top of libtar, and represents segments as TAR files and objects
7  * as files within them. */
8
9 #ifndef _LBS_STORE_H
10 #define _LBS_STORE_H
11
12 #include <stdint.h>
13 #include <libtar.h>
14
15 #include <list>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <iostream>
20 #include <sstream>
21
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 /* A simple wrapper around a single TAR file to represent a segment.  Objects
45  * may only be written out all at once, since the tar header must be written
46  * first; incremental writing is not supported. */
47 class Tarfile {
48 public:
49     Tarfile(const std::string &path, const std::string &segment);
50     ~Tarfile();
51
52     int spawn_filter(int fd_out);
53     void write_object(int id, const char *data, size_t len);
54
55     // Return an estimate of the size of the file.
56     size_t size_estimate();
57
58     void internal_write_object(const std::string &path,
59                                const char *data, size_t len);
60
61 private:
62     size_t size;
63     std::string segment_name;
64     TAR *t;
65
66     /* Filter support. */
67     int real_fd, filter_fd;
68     pid_t filter_pid;
69 };
70
71 class TarSegmentStore {
72 public:
73     // New segments will be stored in the given directory.
74     TarSegmentStore(const std::string &path) { this->path = path; }
75     ~TarSegmentStore() { sync(); }
76
77     // Writes an object to segment in the store, and returns the name
78     // (segment/object) to refer to it.  The optional parameter group can be
79     // used to control object placement; objects with different group
80     // parameters are kept in separate segments.
81     ObjectReference write_object(const char *data, size_t len,
82                                  const std::string &group = "");
83
84     // Ensure all segments have been fully written.
85     void sync();
86
87 private:
88     struct segment_info {
89         Tarfile *file;
90         std::string name;           // UUID
91         int count;                  // Objects written to this segment
92     };
93
94     std::string path;
95     std::map<std::string, struct segment_info *> segments;
96
97     // Ensure that all segments in the given group have been fully written.
98     void close_segment(const std::string &group);
99
100     // Parse an object reference string and return just the segment name
101     // portion.
102     std::string object_reference_to_segment(const std::string &object);
103 };
104
105 /* An in-memory representation of an object, which can be incrementally built
106  * before it is written out to a segment. */
107 class LbsObject {
108 public:
109     LbsObject();
110     ~LbsObject();
111
112     // If an object is placed in a group, it will be written out to segments
113     // only containing other objects in the same group.  A group name is simply
114     // a string.
115     //std::string get_group() const { return group; }
116     void set_group(const std::string &g) { group = g; }
117
118     // Data in an object must be written all at once, and cannot be generated
119     // incrementally.  Data can be an arbitrary block of binary data of any
120     // size.  The pointer to the data need only remain valid until write() is
121     // called.
122     void set_data(const char *d, size_t len) { data = d; data_len = len; }
123
124     // Write an object to a segment, thus making it permanent.  This function
125     // can be called at most once.
126     void write(TarSegmentStore *store);
127
128     // Compute the checksum of an object, and include it in the object
129     // reference.  This should be called after write(), and the data specified
130     // by set_data() must remain valid through the call to checksum().
131     void checksum();
132
133     // An object is assigned a permanent name once it has been written to a
134     // segment.  Until that time, its name cannot be determined.
135     std::string get_name() const { return ref.to_string(); }
136     ObjectReference get_ref() { return ref; }
137
138 private:
139     std::string group;
140     const char *data;
141     size_t data_len;
142
143     bool written;
144     ObjectReference ref;
145 };
146
147 #endif // _LBS_STORE_H