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