Create a third_party directory for files copied from other projects.
[cumulus.git] / store.h
1 /* Cumulus: Smart Filesystem Backup to Dumb Servers
2  *
3  * Copyright (C) 2006-2008  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 /* Backup data is stored in a collection of objects, which are grouped together
22  * into segments for storage purposes.  This implementation of the object store
23  * represents segments as TAR files and objects as files within them. */
24
25 #ifndef _LBS_STORE_H
26 #define _LBS_STORE_H
27
28 #include <stdint.h>
29
30 #include <list>
31 #include <map>
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 /* A simple wrapper around a single TAR file to represent a segment.  Objects
73  * may only be written out all at once, since the tar header must be written
74  * first; incremental writing is not supported. */
75 class Tarfile {
76 public:
77     Tarfile(RemoteFile *file, const std::string &segment);
78     ~Tarfile();
79
80     void write_object(int id, const char *data, size_t len);
81
82     // Return an estimate of the size of the file.
83     size_t size_estimate();
84
85 private:
86     size_t size;
87     std::string segment_name;
88
89     RemoteFile *file;
90
91     /* Filter support. */
92     int real_fd, filter_fd;
93     pid_t filter_pid;
94
95     // Write data to the tar file
96     void tar_write(const char *data, size_t size);
97 };
98
99 class TarSegmentStore {
100 public:
101     // New segments will be stored in the given directory.
102     TarSegmentStore(RemoteStore *remote,
103                     LocalDb *db = NULL)
104         { this->remote = remote; this->db = db; }
105     ~TarSegmentStore() { sync(); }
106
107     // Writes an object to segment in the store, and returns the name
108     // (segment/object) to refer to it.  The optional parameter group can be
109     // used to control object placement; objects with different group
110     // parameters are kept in separate segments.
111     ObjectReference write_object(const char *data, size_t len,
112                                  const std::string &group = "");
113
114     // Ensure all segments have been fully written.
115     void sync();
116
117     // Dump statistics to stdout about how much data has been written
118     void dump_stats();
119
120 private:
121     struct segment_info {
122         Tarfile *file;
123         std::string group;
124         std::string name;           // UUID
125         int count;                  // Objects written to this segment
126         int size;                   // Combined size of objects written
127         std::string basename;       // Name of segment without directory
128         RemoteFile *rf;
129     };
130
131     RemoteStore *remote;
132     std::map<std::string, struct segment_info *> segments;
133     LocalDb *db;
134
135     // Ensure that all segments in the given group have been fully written.
136     void close_segment(const std::string &group);
137
138     // Parse an object reference string and return just the segment name
139     // portion.
140     std::string object_reference_to_segment(const std::string &object);
141 };
142
143 /* An in-memory representation of an object, which can be incrementally built
144  * before it is written out to a segment. */
145 class LbsObject {
146 public:
147     LbsObject();
148     ~LbsObject();
149
150     // If an object is placed in a group, it will be written out to segments
151     // only containing other objects in the same group.  A group name is simply
152     // a string.
153     //std::string get_group() const { return group; }
154     void set_group(const std::string &g) { group = g; }
155
156     // Data in an object must be written all at once, and cannot be generated
157     // incrementally.  Data can be an arbitrary block of binary data of any
158     // size.  The pointer to the data need only remain valid until write() is
159     // called.
160     void set_data(const char *d, size_t len) { data = d; data_len = len; }
161
162     // Write an object to a segment, thus making it permanent.  This function
163     // can be called at most once.
164     void write(TarSegmentStore *store);
165
166     // Compute the checksum of an object, and include it in the object
167     // reference.  This should be called after write(), and the data specified
168     // by set_data() must remain valid through the call to checksum().
169     void checksum();
170
171     // An object is assigned a permanent name once it has been written to a
172     // segment.  Until that time, its name cannot be determined.
173     std::string get_name() const { return ref.to_string(); }
174     ObjectReference get_ref() { return ref; }
175
176 private:
177     std::string group;
178     const char *data;
179     size_t data_len;
180
181     bool written;
182     ObjectReference ref;
183 };
184
185 /* Program through which segment data is piped before being written to file. */
186 extern const char *filter_program;
187
188 /* Extension which should be appended to segments written out (.tar is already
189  * included; this adds to it) */
190 extern const char *filter_extension;
191
192 /* Launch a process to filter data written to a file descriptor.  fd_out is the
193  * file descriptor where the filtered data should be written.  program is the
194  * filter program to execute (a single string which will be interpreted by
195  * /bin/sh).  The return value is a file descriptor to which the data to be
196  * filtered should be written.  The process ID of the filter process is stored
197  * at address filter_pid if non-NULL. */
198 int spawn_filter(int fd_out, const char *program, pid_t *filter_pid);
199
200 #endif // _LBS_STORE_H