Compute SHA-1 checksums of regular files to be stored with index data.
[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
18 typedef std::map<std::string, std::string> dictionary;
19
20 class IOException : public std::exception {
21 private:
22     std::string error;
23 public:
24     explicit IOException(const std::string &err) { error = err; }
25     virtual ~IOException() throw () { }
26     std::string getError() const { return error; }
27 };
28
29 class OutputStream {
30 public:
31     virtual ~OutputStream() { }
32     virtual void write(const void *data, size_t len) = 0;
33
34     /* Convenience functions for writing other data types.  Values are always
35      * written out in little-endian order. */
36     void write_u8(uint8_t val);
37     void write_u16(uint16_t val);
38     void write_u32(uint32_t val);
39     void write_u64(uint64_t val);
40
41     void write_s32(int32_t val) { write_u32((uint32_t)val); }
42     void write_s64(int64_t val) { write_u64((uint64_t)val); }
43
44     void write_varint(uint64_t val);
45
46     void write_string(const std::string &s);
47     void write_dictionary(const dictionary &d);
48 };
49
50 class StringOutputStream : public OutputStream {
51 private:
52     std::stringstream buf;
53 public:
54     StringOutputStream();
55
56     virtual void write(const void *data, size_t len);
57     std::string contents() const { return buf.str(); }
58 };
59
60 class FileOutputStream : public OutputStream {
61 private:
62     FILE *f;
63 public:
64     explicit FileOutputStream(FILE *file);
65     virtual ~FileOutputStream();
66
67     virtual void write(const void *data, size_t len);
68 };
69
70 std::string encode_u16(uint16_t val);
71 std::string encode_u32(uint32_t val);
72 std::string encode_u64(uint64_t val);
73
74 #endif // _LBS_STORE_H