Append checksums to segments to allow some verification.
[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 file provides interfaces for
6  * reading and writing objects and segments. */
7
8 #ifndef _LBS_STORE_H
9 #define _LBS_STORE_H
10
11 #include <stdint.h>
12
13 #include <exception>
14 #include <map>
15 #include <string>
16 #include <sstream>
17 #include <vector>
18
19 #include "sha1.h"
20
21 /* In memory datatype to represent key/value pairs of information, such as file
22  * metadata.  Currently implemented as map<string, string>. */
23 typedef std::map<std::string, std::string> dictionary;
24
25 /* IOException will be thrown if an error occurs while reading or writing in
26  * one of the I/O wrappers.  Depending upon the context; this may be fatal or
27  * not--typically, errors reading/writing the store will be serious, but errors
28  * reading an individual file are less so. */
29 class IOException : public std::exception {
30 private:
31     std::string error;
32 public:
33     explicit IOException(const std::string &err) { error = err; }
34     virtual ~IOException() throw () { }
35     std::string getError() const { return error; }
36 };
37
38 /* OutputStream is an abstract interface for writing data without seeking.
39  * Output could be to a file, to an object within a segment, or even to a
40  * memory buffer to help serialize data. */
41 class OutputStream {
42 public:
43     OutputStream();
44     virtual ~OutputStream() { }
45
46     // Write the given data buffer
47     void write(const void *data, size_t len);
48
49     // Return the total number of bytes written so far
50     int64_t get_pos() const { return bytes_written; }
51
52     // Convenience functions for writing other data types.  Values are always
53     // written out in little-endian order.
54     void write_u8(uint8_t val);
55     void write_u16(uint16_t val);
56     void write_u32(uint32_t val);
57     void write_u64(uint64_t val);
58
59     void write_s32(int32_t val) { write_u32((uint32_t)val); }
60     void write_s64(int64_t val) { write_u64((uint64_t)val); }
61
62     void write_varint(uint64_t val);
63
64     void write_string(const std::string &s);
65     void write_dictionary(const dictionary &d);
66
67 protected:
68     // Function which actually causes a write: must be overridden by
69     // implementation.
70     virtual void write_internal(const void *data, size_t len) = 0;
71
72 private:
73     int64_t bytes_written;
74 };
75
76 /* An OutputStream implementation which writes data to memory and returns the
77  * result as a string. */
78 class StringOutputStream : public OutputStream {
79 public:
80     StringOutputStream();
81     std::string contents() const { return buf.str(); }
82
83 protected:
84     virtual void write_internal(const void *data, size_t len);
85
86 private:
87     std::stringstream buf;
88 };
89
90 /* An OutputStream implementation which writes data via the C stdio layer. */
91 class FileOutputStream : public OutputStream {
92 public:
93     explicit FileOutputStream(FILE *file);
94     virtual ~FileOutputStream();
95
96 protected:
97     virtual void write_internal(const void *data, size_t len);
98
99 private:
100     FILE *f;
101 };
102
103 /* An OutputStream which is simply sends writes to another OutputStream, but
104  * does provide separate tracking of bytes written. */
105 class WrapperOutputStream : public OutputStream {
106 public:
107     explicit WrapperOutputStream(OutputStream &o);
108     virtual ~WrapperOutputStream() { }
109
110 protected:
111     virtual void write_internal(const void *data, size_t len);
112
113 private:
114     OutputStream &real;
115 };
116
117 /* Like WrapperOutputStream, but additionally computes a checksum of data as it
118  * is written. */
119 class ChecksumOutputStream : public OutputStream {
120 public:
121     explicit ChecksumOutputStream(OutputStream &o);
122     virtual ~ChecksumOutputStream() { }
123
124     /* Once a checksum is computed, no further data should be written to the
125      * stream. */
126     const uint8_t *finish_and_checksum();
127     size_t checksum_size() const { return csum.checksum_size(); }
128
129 protected:
130     virtual void write_internal(const void *data, size_t len);
131
132 private:
133     OutputStream &real;
134     SHA1Checksum csum;
135 };
136
137 /* Simple wrappers that encode integers using a StringOutputStream and return
138  * the encoded result. */
139 std::string encode_u16(uint16_t val);
140 std::string encode_u32(uint32_t val);
141 std::string encode_u64(uint64_t val);
142
143 struct uuid {
144     uint8_t bytes[16];
145 };
146
147 /* A class which is used to pack multiple objects into a single segment, with a
148  * lookup table to quickly locate each object.  Call new_object() to get an
149  * OutputStream to which a new object may be written, and optionally
150  * finish_object() when finished writing the current object.  Only one object
151  * may be written to a segment at a time; if multiple objects must be written
152  * concurrently, they must be to different segments. */
153 class SegmentWriter {
154 public:
155     SegmentWriter(OutputStream *output, struct uuid u);
156     ~SegmentWriter();
157
158     struct uuid get_uuid() const { return id; }
159
160     // Start writing out a new object to this segment.
161     OutputStream *new_object();
162     void finish_object();
163
164     // Utility functions for generating and formatting UUIDs for display.
165     static struct uuid generate_uuid();
166     static std::string format_uuid(const struct uuid u);
167
168 private:
169     typedef std::vector<std::pair<int64_t, int64_t> > object_table;
170
171     ChecksumOutputStream *out;  // Output stream with checksumming enabled
172     OutputStream *raw_out;      // Raw output stream, without checksumming
173     struct uuid id;
174
175     int64_t object_start_offset;
176     OutputStream *object_stream;
177
178     object_table objects;
179 };
180
181 /* A SegmentStore, as the name suggests, is used to store the contents of many
182  * segments.  The SegmentStore internally tracks where data should be placed
183  * (such as a local directory or remote storage), and allows new segments to be
184  * easily created as needed. */
185 class SegmentStore {
186 public:
187     // New segments will be stored in the given directory.
188     SegmentStore(const std::string &path);
189
190     SegmentWriter *new_segment();
191
192 private:
193     std::string directory;
194 };
195
196 #endif // _LBS_STORE_H