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. */
15 OutputStream::OutputStream()
20 void OutputStream::write(const void *data, size_t len)
22 write_internal(data, len);
26 void OutputStream::write_u8(uint8_t val)
31 void OutputStream::write_u16(uint16_t val)
36 buf[1] = (val >> 8) & 0xff;
40 void OutputStream::write_u32(uint32_t val)
45 buf[1] = (val >> 8) & 0xff;
46 buf[2] = (val >> 16) & 0xff;
47 buf[3] = (val >> 24) & 0xff;
51 void OutputStream::write_u64(uint64_t val)
56 buf[1] = (val >> 8) & 0xff;
57 buf[2] = (val >> 16) & 0xff;
58 buf[3] = (val >> 24) & 0xff;
59 buf[4] = (val >> 32) & 0xff;
60 buf[5] = (val >> 40) & 0xff;
61 buf[6] = (val >> 48) & 0xff;
62 buf[7] = (val >> 56) & 0xff;
66 /* Writes an integer to an output stream using a variable-sized representation:
67 * seven bits are written at a time (little-endian), and the eigth bit of each
68 * byte is set if more data follows. */
69 void OutputStream::write_varint(uint64_t val)
72 uint8_t remainder = (val & 0x7f);
80 /* Write an arbitrary string by first writing out the length, followed by the
82 void OutputStream::write_string(const string &s)
84 size_t len = s.length();
89 void OutputStream::write_dictionary(const dictionary &d)
91 size_t size = d.size();
96 for (dictionary::const_iterator i = d.begin(); i != d.end(); ++i) {
97 write_string(i->first);
98 write_string(i->second);
102 assert(written == size);
105 StringOutputStream::StringOutputStream()
106 : buf(std::ios_base::out)
110 void StringOutputStream::write_internal(const void *data, size_t len)
112 buf.write((const char *)data, len);
114 throw IOException("error writing to StringOutputStream");
117 FileOutputStream::FileOutputStream(FILE *file)
122 FileOutputStream::~FileOutputStream()
127 void FileOutputStream::write_internal(const void *data, size_t len)
131 res = fwrite(data, 1, len, f);
133 throw IOException("write error");
137 WrapperOutputStream::WrapperOutputStream(OutputStream &o)
142 void WrapperOutputStream::write_internal(const void *data, size_t len)
144 real.write(data, len);
147 /* Provide checksumming of a data stream. */
148 ChecksumOutputStream::ChecksumOutputStream(OutputStream &o)
153 void ChecksumOutputStream::write_internal(const void *data, size_t len)
155 real.write(data, len);
156 csum.process(data, len);
159 const uint8_t *ChecksumOutputStream::finish_and_checksum()
161 return csum.checksum();
164 /* Utility functions, for encoding data types to strings. */
165 string encode_u16(uint16_t val)
167 StringOutputStream s;
172 string encode_u32(uint32_t val)
174 StringOutputStream s;
179 string encode_u64(uint64_t val)
181 StringOutputStream s;
186 SegmentWriter::SegmentWriter(OutputStream *output, struct uuid u)
191 /* All output data will be checksummed except the very last few bytes,
192 * which are the checksum itself. */
193 out = new ChecksumOutputStream(*raw_out);
195 /* Write out the segment header first. */
196 static const char signature[] = "LBSSEG0\n";
197 out->write(signature, strlen(signature));
198 out->write(id.bytes, sizeof(struct uuid));
201 SegmentWriter::~SegmentWriter()
206 // Write out the object table which gives the sizes and locations of all
207 // objects, and then add the trailing signature, which indicates the end of
208 // the segment and gives the offset of the object table.
209 int64_t index_offset = out->get_pos();
211 for (object_table::const_iterator i = objects.begin();
212 i != objects.end(); ++i) {
213 out->write_s64(i->first);
214 out->write_s64(i->second);
217 static const char signature2[] = "LBSEND";
218 out->write(signature2, strlen(signature2));
219 out->write_s64(index_offset);
220 out->write_u32(objects.size());
222 /* Finally, append a checksum to the end of the file, so that its integrity
223 * (against accidental, not malicious, corruption) can be verified. */
224 const uint8_t *csum = out->finish_and_checksum();
225 raw_out->write(csum, out->checksum_size());
227 /* The SegmentWriter takes ownership of the OutputStream it is writing to,
228 * and destroys it automatically when done with the segment. */
233 OutputStream *SegmentWriter::new_object()
238 object_start_offset = out->get_pos();
239 object_stream = new WrapperOutputStream(*out);
241 return object_stream;
244 void SegmentWriter::finish_object()
246 assert(object_stream != NULL);
248 // store (start, length) information for locating this object
249 objects.push_back(std::make_pair(object_start_offset,
250 object_stream->get_pos()));
252 delete object_stream;
253 object_stream = NULL;
256 struct uuid SegmentWriter::generate_uuid()
260 uuid_generate(u.bytes);
265 string SegmentWriter::format_uuid(const struct uuid u)
267 // A UUID only takes 36 bytes, plus the trailing '\0', so this is safe.
270 uuid_unparse_lower(u.bytes, buf);
275 SegmentStore::SegmentStore(const string &path)
280 SegmentWriter *SegmentStore::new_segment()
282 struct uuid id = SegmentWriter::generate_uuid();
283 string filename = directory + "/" + SegmentWriter::format_uuid(id);
285 FILE *f = fopen(filename.c_str(), "wb");
287 throw IOException("Unable to open new segment");
289 return new SegmentWriter(new FileOutputStream(f), id);