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 file provides interfaces for
6 * reading and writing objects and segments. */
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;
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 {
33 explicit IOException(const std::string &err) { error = err; }
34 virtual ~IOException() throw () { }
35 std::string getError() const { return error; }
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. */
44 virtual ~OutputStream() { }
46 // Write the given data buffer
47 void write(const void *data, size_t len);
49 // Return the total number of bytes written so far
50 int64_t get_pos() const { return bytes_written; }
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);
59 void write_s32(int32_t val) { write_u32((uint32_t)val); }
60 void write_s64(int64_t val) { write_u64((uint64_t)val); }
62 void write_varint(uint64_t val);
64 void write_string(const std::string &s);
65 void write_dictionary(const dictionary &d);
68 // Function which actually causes a write: must be overridden by
70 virtual void write_internal(const void *data, size_t len) = 0;
73 int64_t bytes_written;
76 /* An OutputStream implementation which writes data to memory and returns the
77 * result as a string. */
78 class StringOutputStream : public OutputStream {
81 std::string contents() const { return buf.str(); }
84 virtual void write_internal(const void *data, size_t len);
87 std::stringstream buf;
90 /* An OutputStream implementation which writes data via the C stdio layer. */
91 class FileOutputStream : public OutputStream {
93 explicit FileOutputStream(FILE *file);
94 virtual ~FileOutputStream();
97 virtual void write_internal(const void *data, size_t len);
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 {
107 explicit WrapperOutputStream(OutputStream &o);
108 virtual ~WrapperOutputStream() { }
111 virtual void write_internal(const void *data, size_t len);
117 /* Like WrapperOutputStream, but additionally computes a checksum of data as it
119 class ChecksumOutputStream : public OutputStream {
121 explicit ChecksumOutputStream(OutputStream &o);
122 virtual ~ChecksumOutputStream() { }
124 /* Once a checksum is computed, no further data should be written to the
126 const uint8_t *finish_and_checksum();
127 size_t checksum_size() const { return csum.checksum_size(); }
130 virtual void write_internal(const void *data, size_t len);
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);
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 {
155 SegmentWriter(OutputStream *output, struct uuid u);
158 struct uuid get_uuid() const { return id; }
160 // Start writing out a new object to this segment.
161 OutputStream *new_object();
162 void finish_object();
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);
169 typedef std::vector<std::pair<int64_t, int64_t> > object_table;
171 ChecksumOutputStream *out; // Output stream with checksumming enabled
172 OutputStream *raw_out; // Raw output stream, without checksumming
175 int64_t object_start_offset;
176 OutputStream *object_stream;
178 object_table objects;
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. */
187 // New segments will be stored in the given directory.
188 SegmentStore(const std::string &path);
190 SegmentWriter *new_segment();
193 std::string directory;
196 #endif // _LBS_STORE_H