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 * is built on top of libtar, and represents segments as TAR files and objects
7 * as files within them. */
9 #ifndef _LBS_TARSTORE_H
10 #define _LBS_TARSTORE_H
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 /* A simple wrapper around a single TAR file to represent a segment. Objects
44 * may only be written out all at once, since the tar header must be written
45 * first; incremental writing is not supported. */
48 Tarfile(const std::string &path, const std::string &segment);
51 void write_object(int id, const char *data, size_t len);
53 // Return an estimate of the size of the file.
54 size_t size_estimate() { return size; }
56 void internal_write_object(const std::string &path,
57 const char *data, size_t len);
61 std::string segment_name;
62 std::ostringstream checksums;
66 class TarSegmentStore {
68 // New segments will be stored in the given directory.
69 TarSegmentStore(const std::string &path) { this->path = path; }
70 ~TarSegmentStore() { sync(); }
72 // Writes an object to segment in the store, and returns the name
73 // (segment/object) to refer to it. The optional parameter group can be
74 // used to control object placement; objects with different group
75 // parameters are kept in separate segments.
76 std::string write_object(const char *data, size_t len,
77 const std::string &group = "",
78 const std::list<std::string> &refs = norefs);
80 // Ensure all segments have been fully written.
86 std::string name; // UUID
87 std::set<std::string> refs; // Other segments this one refers to
88 int count; // Objects written to this segment
92 std::map<std::string, struct segment_info *> segments;
94 // An empty list which can be used as an argument to write_object to
95 // indicate that this object depends on no others.
96 static std::list<std::string> norefs;
98 // Ensure that all segments in the given group have been fully written.
99 void close_segment(const std::string &group);
101 // Parse an object reference string and return just the segment name
103 std::string object_reference_to_segment(const std::string &object);
106 /* An in-memory representation of an object, which can be incrementally built
107 * before it is written out to a segment. */
113 // If an object is placed in a group, it will be written out to segments
114 // only containing other objects in the same group. A group name is simply
116 //std::string get_group() const { return group; }
117 void set_group(const std::string &g) { group = g; }
119 // Data in an object must be written all at once, and cannot be generated
120 // incrementally. Data can be an arbitrary block of binary data of any
121 // size. The pointer to the data need only remain valid until write() is
123 //const char *get_data() const { return data; }
124 //size_t get_data_len() const { return data_len; }
125 void set_data(const char *d, size_t len) { data = d; data_len = len; }
127 // Write an object to a segment, thus making it permanent. This function
128 // can be called at most once.
129 void write(TarSegmentStore *store);
131 // An object is assigned a permanent name once it has been written to a
132 // segment. Until that time, its name cannot be determined.
133 std::string get_name() const { return name; }
135 // Logically, one object may reference other objects (such as a metadata
136 // listing referncing actual file data blocks). Such references should be
137 // noted explicitly. It may eventually be used to build up a tree of
138 // checksums for later verifying integrity.
139 void add_reference(const LbsObject *o);
150 #endif // _LBS_TARSTORE_H