1 /* LBS: An LFS-inspired filesystem backup system
2 * Copyright (C) 2006 Michael Vrable
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. */
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;
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 {
38 explicit IOException(const std::string &err) { error = err; }
39 virtual ~IOException() throw () { }
40 std::string getError() const { return error; }
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;
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. */
72 Tarfile(const std::string &path, const std::string &segment);
75 void write_object(int id, const char *data, size_t len);
77 // Return an estimate of the size of the file.
78 size_t size_estimate();
82 std::string segment_name;
85 int real_fd, filter_fd;
88 // Write data to the tar file
89 void tar_write(const char *data, size_t size);
92 class TarSegmentStore {
94 // New segments will be stored in the given directory.
95 TarSegmentStore(const std::string &path,
97 { this->path = path; this->db = db; }
98 ~TarSegmentStore() { sync(); }
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 = "");
107 // Ensure all segments have been fully written.
110 // Dump statistics to stdout about how much data has been written
114 struct segment_info {
116 std::string name; // UUID
117 int count; // Objects written to this segment
118 int size; // Combined size of objects written
119 std::string basename; // Name of segment without directory
120 std::string fullname; // Full path to stored segment
124 std::map<std::string, struct segment_info *> segments;
127 // Ensure that all segments in the given group have been fully written.
128 void close_segment(const std::string &group);
130 // Parse an object reference string and return just the segment name
132 std::string object_reference_to_segment(const std::string &object);
135 /* An in-memory representation of an object, which can be incrementally built
136 * before it is written out to a segment. */
142 // If an object is placed in a group, it will be written out to segments
143 // only containing other objects in the same group. A group name is simply
145 //std::string get_group() const { return group; }
146 void set_group(const std::string &g) { group = g; }
148 // Data in an object must be written all at once, and cannot be generated
149 // incrementally. Data can be an arbitrary block of binary data of any
150 // size. The pointer to the data need only remain valid until write() is
152 void set_data(const char *d, size_t len) { data = d; data_len = len; }
154 // Write an object to a segment, thus making it permanent. This function
155 // can be called at most once.
156 void write(TarSegmentStore *store);
158 // Compute the checksum of an object, and include it in the object
159 // reference. This should be called after write(), and the data specified
160 // by set_data() must remain valid through the call to checksum().
163 // An object is assigned a permanent name once it has been written to a
164 // segment. Until that time, its name cannot be determined.
165 std::string get_name() const { return ref.to_string(); }
166 ObjectReference get_ref() { return ref; }
177 /* Program through which segment data is piped before being written to file. */
178 extern const char *filter_program;
180 /* Extension which should be appended to segments written out (.tar is already
181 * included; this adds to it) */
182 extern const char *filter_extension;
184 /* Launch a process to filter data written to a file descriptor. fd_out is the
185 * file descriptor where the filtered data should be written. program is the
186 * filter program to execute (a single string which will be interpreted by
187 * /bin/sh). The return value is a file descriptor to which the data to be
188 * filtered should be written. The process ID of the filter process is stored
189 * at address filter_pid if non-NULL. */
190 int spawn_filter(int fd_out, const char *program, pid_t *filter_pid);
192 #endif // _LBS_STORE_H