a657ec9463d28bee9a1c94088fe788eb5fabfbca
[cumulus.git] / tarstore.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  * is built on top of libtar, and represents segments as TAR files and objects
7  * as files within them. */
8
9 #ifndef _LBS_TARSTORE_H
10 #define _LBS_TARSTORE_H
11
12 #include <stdint.h>
13 #include <libtar.h>
14
15 #include <list>
16 #include <set>
17 #include <string>
18 #include <iostream>
19 #include <sstream>
20
21 #include "store.h"
22
23 /* A simple wrapper around a single TAR file to represent a segment.  Objects
24  * may only be written out all at once, since the tar header must be written
25  * first; incremental writing is not supported. */
26 class Tarfile {
27 public:
28     Tarfile(const std::string &path, const std::string &segment);
29     ~Tarfile();
30
31     void write_object(int id, const char *data, size_t len);
32
33     // Return an estimate of the size of the file.
34     size_t size_estimate() { return size; }
35
36     void internal_write_object(const std::string &path,
37                                const char *data, size_t len);
38
39 private:
40     size_t size;
41     std::string segment_name;
42     std::ostringstream checksums;
43     TAR *t;
44 };
45
46 class TarSegmentStore {
47 public:
48     // New segments will be stored in the given directory.
49     TarSegmentStore(const std::string &path) { this->path = path; }
50     ~TarSegmentStore() { sync(); }
51
52     // Writes an object to segment in the store, and returns the name
53     // (segment/object) to refer to it.  The optional parameter group can be
54     // used to control object placement; objects with different group
55     // parameters are kept in separate segments.
56     std::string write_object(const char *data, size_t len,
57                              const std::string &group = "",
58                              const std::list<std::string> &refs = norefs);
59
60     // Ensure all segments have been fully written.
61     void sync();
62
63 private:
64     struct segment_info {
65         Tarfile *file;
66         std::string name;           // UUID
67         std::set<std::string> refs; // Other segments this one refers to
68         int count;                  // Objects written to this segment
69     };
70
71     std::string path;
72     std::map<std::string, struct segment_info *> segments;
73
74     // An empty list which can be used as an argument to write_object to
75     // indicate that this object depends on no others.
76     static std::list<std::string> norefs;
77
78     // Ensure that all segments in the given group have been fully written.
79     void close_segment(const std::string &group);
80
81     // Parse an object reference string and return just the segment name
82     // portion.
83     std::string object_reference_to_segment(const std::string &object);
84 };
85
86 #endif // _LBS_TARSTORE_H