Really fix the umask problem.
[cumulus.git] / store.cc
1 /* LBS: An LFS-inspired filesystem backup system
2  * Copyright (C) 2007  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 #include <assert.h>
10 #include <stdio.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/resource.h>
14 #include <sys/wait.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <time.h>
18
19 #include <algorithm>
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <string>
24 #include <iostream>
25
26 #include "store.h"
27 #include "ref.h"
28
29 using std::max;
30 using std::list;
31 using std::map;
32 using std::set;
33 using std::string;
34
35 /* Default filter program is bzip2 */
36 const char *filter_program = "bzip2 -c";
37 const char *filter_extension = ".bz2";
38
39 static void cloexec(int fd)
40 {
41     long flags = fcntl(fd, F_GETFD);
42
43     if (flags < 0)
44         return;
45
46     fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
47 }
48
49 Tarfile::Tarfile(const string &path, const string &segment)
50     : size(0),
51       segment_name(segment)
52 {
53     real_fd = open(path.c_str(), O_WRONLY | O_CREAT, 0666);
54     if (real_fd < 0)
55         throw IOException("Error opening output file");
56
57     filter_fd = spawn_filter(real_fd);
58
59     if (tar_fdopen(&t, filter_fd, (char *)path.c_str(), NULL,
60                    O_WRONLY | O_CREAT, 0666, TAR_VERBOSE | TAR_GNU) == -1)
61         throw IOException("Error opening Tarfile");
62 }
63
64 Tarfile::~Tarfile()
65 {
66     /* Close the tar file... */
67     tar_append_eof(t);
68
69     if (tar_close(t) != 0)
70         throw IOException("Error closing Tarfile");
71
72     /* ...and wait for filter process to finish. */
73     int status;
74     waitpid(filter_pid, &status, 0);
75
76     if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
77         throw IOException("Filter process error");
78     }
79
80     close(real_fd);
81 }
82
83 /* Launch a child process which can act as a filter (compress, encrypt, etc.)
84  * on the TAR output.  The file descriptor to which output should be written
85  * must be specified; the return value is the file descriptor which will be
86  * attached to the standard input of the filter program. */
87 int Tarfile::spawn_filter(int fd_out)
88 {
89     int fds[2];
90
91     /* Create a pipe for communicating with the filter process. */
92     if (pipe(fds) < 0) {
93         throw IOException("Unable to create pipe for filter");
94     }
95
96     /* Create a child process which can exec() the filter program. */
97     filter_pid = fork();
98     if (filter_pid < 0)
99         throw IOException("Unable to fork filter process");
100
101     if (filter_pid > 0) {
102         /* Parent process */
103         close(fds[0]);
104         cloexec(fds[1]);
105     } else {
106         /* Child process.  Rearrange file descriptors.  stdin is fds[0], stdout
107          * is fd_out, stderr is unchanged. */
108         close(fds[1]);
109
110         if (dup2(fds[0], 0) < 0)
111             exit(1);
112         close(fds[0]);
113
114         if (dup2(fd_out, 1) < 0)
115             exit(1);
116         close(fd_out);
117
118         /* Exec the filter program. */
119         execlp("/bin/sh", "/bin/sh", "-c", filter_program, NULL);
120
121         /* Should not reach here except for error cases. */
122         fprintf(stderr, "Could not exec filter: %m\n");
123         exit(1);
124     }
125
126     return fds[1];
127 }
128
129 void Tarfile::write_object(int id, const char *data, size_t len)
130 {
131     char buf[64];
132     sprintf(buf, "%08x", id);
133     string path = segment_name + "/" + buf;
134
135     internal_write_object(path, data, len);
136 }
137
138 void Tarfile::internal_write_object(const string &path,
139                                     const char *data, size_t len)
140 {
141     memset(&t->th_buf, 0, sizeof(struct tar_header));
142
143     th_set_type(t, S_IFREG | 0600);
144     th_set_user(t, 0);
145     th_set_group(t, 0);
146     th_set_mode(t, 0600);
147     th_set_size(t, len);
148     th_set_mtime(t, time(NULL));
149     th_set_path(t, const_cast<char *>(path.c_str()));
150     th_finish(t);
151
152     if (th_write(t) != 0)
153         throw IOException("Error writing tar header");
154
155     size += T_BLOCKSIZE;
156
157     if (len == 0)
158         return;
159
160     size_t blocks = (len + T_BLOCKSIZE - 1) / T_BLOCKSIZE;
161     size_t padding = blocks * T_BLOCKSIZE - len;
162
163     for (size_t i = 0; i < blocks - 1; i++) {
164         if (tar_block_write(t, &data[i * T_BLOCKSIZE]) == -1)
165             throw IOException("Error writing tar block");
166     }
167
168     char block[T_BLOCKSIZE];
169     memset(block, 0, sizeof(block));
170     memcpy(block, &data[T_BLOCKSIZE * (blocks - 1)], T_BLOCKSIZE - padding);
171     if (tar_block_write(t, block) == -1)
172         throw IOException("Error writing final tar block");
173
174     size += blocks * T_BLOCKSIZE;
175 }
176
177 /* Estimate the size based on the size of the actual output file on disk.
178  * However, it might be the case that the filter program is buffering all its
179  * data, and might potentially not write a single byte until we have closed
180  * our end of the pipe.  If we don't do so until we see data written, we have
181  * a problem.  So, arbitrarily pick an upper bound on the compression ratio
182  * that the filter will achieve (128:1), and return a size estimate which is
183  * the larger of a) bytes actually seen written to disk, and b) input
184  * bytes/128. */
185 size_t Tarfile::size_estimate()
186 {
187     struct stat statbuf;
188
189     if (fstat(real_fd, &statbuf) == 0)
190         return max((int64_t)statbuf.st_size, (int64_t)(size / 128));
191
192     /* Couldn't stat the file on disk, so just return the actual number of
193      * bytes, before compression. */
194     return size;
195 }
196
197 static const size_t SEGMENT_SIZE = 4 * 1024 * 1024;
198
199 static map<string, int64_t> group_sizes;
200
201 ObjectReference TarSegmentStore::write_object(const char *data, size_t len,
202                                               const std::string &group)
203 {
204     struct segment_info *segment;
205
206     // Find the segment into which the object should be written, looking up by
207     // group.  If no segment exists yet, create one.
208     if (segments.find(group) == segments.end()) {
209         segment = new segment_info;
210
211         segment->name = generate_uuid();
212
213         string filename = path + "/" + segment->name + ".tar";
214         filename += filter_extension;
215         segment->file = new Tarfile(filename, segment->name);
216
217         segment->count = 0;
218
219         segments[group] = segment;
220     } else {
221         segment = segments[group];
222     }
223
224     int id = segment->count;
225     char id_buf[64];
226     sprintf(id_buf, "%08x", id);
227
228     segment->file->write_object(id, data, len);
229     segment->count++;
230
231     group_sizes[group] += len;
232
233     ObjectReference ref(segment->name, id_buf);
234
235     // If this segment meets or exceeds the size target, close it so that
236     // future objects will go into a new segment.
237     if (segment->file->size_estimate() >= SEGMENT_SIZE)
238         close_segment(group);
239
240     return ref;
241 }
242
243 void TarSegmentStore::sync()
244 {
245     while (!segments.empty())
246         close_segment(segments.begin()->first);
247 }
248
249 void TarSegmentStore::dump_stats()
250 {
251     printf("Data written:\n");
252     for (map<string, int64_t>::iterator i = group_sizes.begin();
253          i != group_sizes.end(); ++i) {
254         printf("    %s: %lld\n", i->first.c_str(), i->second);
255     }
256 }
257
258 void TarSegmentStore::close_segment(const string &group)
259 {
260     struct segment_info *segment = segments[group];
261
262     delete segment->file;
263     segments.erase(segments.find(group));
264     delete segment;
265 }
266
267 string TarSegmentStore::object_reference_to_segment(const string &object)
268 {
269     return object;
270 }
271
272 LbsObject::LbsObject()
273     : group(""), data(NULL), data_len(0), written(false)
274 {
275 }
276
277 LbsObject::~LbsObject()
278 {
279 }
280
281 void LbsObject::write(TarSegmentStore *store)
282 {
283     assert(data != NULL);
284     assert(!written);
285
286     ref = store->write_object(data, data_len, group);
287     written = true;
288 }
289
290 void LbsObject::checksum()
291 {
292     assert(written);
293
294     SHA1Checksum hash;
295     hash.process(data, data_len);
296     ref.set_checksum(hash.checksum_str());
297 }