Miscellaneous changes.
[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_TARSTORE_H
10 #define _LBS_TARSTORE_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
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 /* 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. */
46 class Tarfile {
47 public:
48     Tarfile(const std::string &path, const std::string &segment);
49     ~Tarfile();
50
51     void write_object(int id, const char *data, size_t len);
52
53     // Return an estimate of the size of the file.
54     size_t size_estimate() { return size; }
55
56     void internal_write_object(const std::string &path,
57                                const char *data, size_t len);
58
59 private:
60     size_t size;
61     std::string segment_name;
62     std::ostringstream checksums;
63     TAR *t;
64 };
65
66 class TarSegmentStore {
67 public:
68     // New segments will be stored in the given directory.
69     TarSegmentStore(const std::string &path) { this->path = path; }
70     ~TarSegmentStore() { sync(); }
71
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);
79
80     // Ensure all segments have been fully written.
81     void sync();
82
83 private:
84     struct segment_info {
85         Tarfile *file;
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
89     };
90
91     std::string path;
92     std::map<std::string, struct segment_info *> segments;
93
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;
97
98     // Ensure that all segments in the given group have been fully written.
99     void close_segment(const std::string &group);
100
101     // Parse an object reference string and return just the segment name
102     // portion.
103     std::string object_reference_to_segment(const std::string &object);
104 };
105
106 /* An in-memory representation of an object, which can be incrementally built
107  * before it is written out to a segment. */
108 class LbsObject {
109 public:
110     LbsObject();
111     ~LbsObject();
112
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
115     // a string.
116     //std::string get_group() const { return group; }
117     void set_group(const std::string &g) { group = g; }
118
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
122     // called.
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; }
126
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);
130
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; }
134
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);
140
141 private:
142     std::string group;
143     const char *data;
144     size_t data_len;
145
146     bool written;
147     std::string name;
148
149     std::set<std::string> refs;
150 };
151
152 #endif // _LBS_TARSTORE_H