Replace boost::scoped_ptr with std::unique_ptr.
[cumulus.git] / store.h
1 /* Cumulus: Efficient Filesystem Backup to the Cloud
2  * Copyright (C) 2006-2008 The Cumulus Developers
3  * See the AUTHORS file for a list of contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 /* Backup data is stored in a collection of objects, which are grouped together
21  * into segments for storage purposes.  This implementation of the object store
22  * represents segments as TAR files and objects as files within them. */
23
24 #ifndef _LBS_STORE_H
25 #define _LBS_STORE_H
26
27 #include <stdint.h>
28
29 #include <list>
30 #include <map>
31 #include <memory>
32 #include <set>
33 #include <string>
34 #include <iostream>
35 #include <sstream>
36
37 #include "localdb.h"
38 #include "remote.h"
39 #include "ref.h"
40 #include "third_party/sha1.h"
41
42 class LbsObject;
43
44 /* In memory datatype to represent key/value pairs of information, such as file
45  * metadata.  Currently implemented as map<string, string>. */
46 typedef std::map<std::string, std::string> dictionary;
47
48 /* Simplified TAR header--we only need to store regular files, don't need to
49  * handle long filenames, etc. */
50 static const int TAR_BLOCK_SIZE = 512;
51
52 struct tar_header
53 {
54     char name[100];
55     char mode[8];
56     char uid[8];
57     char gid[8];
58     char size[12];
59     char mtime[12];
60     char chksum[8];
61     char typeflag;
62     char linkname[100];
63     char magic[8];
64     char uname[32];
65     char gname[32];
66     char devmajor[8];
67     char devminor[8];
68     char prefix[155];
69     char padding[12];
70 };
71
72 class FileFilter {
73 public:
74     // It is valid for program to be NULL or empty; if so, no filtering is
75     // done.
76     static FileFilter *New(int fd, const char *program);
77
78     // Wait for the filter process to terminate.
79     int wait();
80
81     // Accessors for the file descriptors.
82     int get_raw_fd() const { return fd_raw; }
83     int get_wrapped_fd() const { return fd_wrapped; }
84
85 private:
86     FileFilter(int raw, int wrapped, pid_t pid);
87
88     // Launch a process to filter data written to a file descriptor.  fd_out is
89     // the file descriptor where the filtered data should be written.  program
90     // is the filter program to execute (a single string which will be
91     // interpreted by /bin/sh).  The return value is a file descriptor to which
92     // the data to be filtered should be written.  The process ID of the filter
93     // process is stored at address filter_pid if non-NULL.
94     static int spawn_filter(int fd_out, const char *program, pid_t *filter_pid);
95
96     // The original file descriptor passed when creating the FileFilter object.
97     int fd_raw;
98
99     // The wrapped file descriptor: writes here are piped through the filter
100     // program.
101     int fd_wrapped;
102
103     // The filter process if one was launched, or -1 if there is no filter
104     // program.
105     pid_t pid;
106 };
107
108 /* A simple wrapper around a single TAR file to represent a segment.  Objects
109  * may only be written out all at once, since the tar header must be written
110  * first; incremental writing is not supported. */
111 class Tarfile {
112 public:
113     Tarfile(RemoteFile *file, const std::string &segment);
114     ~Tarfile();
115
116     void write_object(int id, const char *data, size_t len);
117
118     // Return an estimate of the size of the file.
119     size_t size_estimate();
120
121 private:
122     size_t size;
123     std::string segment_name;
124
125     RemoteFile *file;
126     std::unique_ptr<FileFilter> filter;
127
128     // Write data to the tar file
129     void tar_write(const char *data, size_t size);
130 };
131
132 class TarSegmentStore {
133 public:
134     // New segments will be stored in the given directory.
135     TarSegmentStore(RemoteStore *remote,
136                     LocalDb *db = NULL)
137         { this->remote = remote; this->db = db; }
138     ~TarSegmentStore() { sync(); }
139
140     // Writes an object to segment in the store, and returns the name
141     // (segment/object) to refer to it.  The optional parameter group can be
142     // used to control object placement; objects with different group
143     // parameters are kept in separate segments.
144     ObjectReference write_object(const char *data, size_t len,
145                                  const std::string &group = "",
146                                  const std::string &checksum = "",
147                                  double age = 0.0);
148
149     // Ensure all segments have been fully written.
150     void sync();
151
152     // Dump statistics to stdout about how much data has been written
153     void dump_stats();
154
155 private:
156     struct segment_info {
157         Tarfile *file;
158         std::string group;
159         std::string name;           // UUID
160         int count;                  // Objects written to this segment
161         int data_size;              // Combined size of objects written
162         std::string basename;       // Name of segment without directory
163         RemoteFile *rf;
164     };
165
166     RemoteStore *remote;
167     std::map<std::string, struct segment_info *> segments;
168     LocalDb *db;
169
170     // Ensure that all segments in the given group have been fully written.
171     void close_segment(const std::string &group);
172
173     // Parse an object reference string and return just the segment name
174     // portion.
175     std::string object_reference_to_segment(const std::string &object);
176 };
177
178 /* An in-memory representation of an object, which can be incrementally built
179  * before it is written out to a segment. */
180 class LbsObject {
181 public:
182     LbsObject();
183     ~LbsObject();
184
185     // If an object is placed in a group, it will be written out to segments
186     // only containing other objects in the same group.  A group name is simply
187     // a string.
188     //std::string get_group() const { return group; }
189     void set_group(const std::string &g) { group = g; }
190
191     // Data in an object must be written all at once, and cannot be generated
192     // incrementally.  Data can be an arbitrary block of binary data of any
193     // size.  The pointer to the data need only remain valid until write() is
194     // called.  If checksum is non-NULL then it is assumed to contain a hash
195     // value for the data; this provides an optimization in case the caller has
196     // already checksummed the data.  Otherwise the set_data will compute a
197     // hash of the data itself.
198     void set_data(const char *d, size_t len, const char *checksum);
199
200     // Explicitly sets the age of the data, for later garbage-collection or
201     // repacking purposes.  If not set, the age defaults to the current time.
202     // The age is stored in the database as a floating point value, expressing
203     // the time in Julian days.
204     void set_age(double age) { this->age = age; }
205
206     // Write an object to a segment, thus making it permanent.  This function
207     // can be called at most once.
208     void write(TarSegmentStore *store);
209
210     // An object is assigned a permanent name once it has been written to a
211     // segment.  Until that time, its name cannot be determined.
212     ObjectReference get_ref() { return ref; }
213
214 private:
215     std::string group;
216     double age;
217     const char *data;
218     size_t data_len;
219     std::string checksum;
220
221     bool written;
222     ObjectReference ref;
223 };
224
225 /* Program through which segment data is piped before being written to file. */
226 extern const char *filter_program;
227
228 /* Extension which should be appended to segments written out (.tar is already
229  * included; this adds to it) */
230 extern const char *filter_extension;
231
232 #endif // _LBS_STORE_H