1 /* LBS: An LFS-inspired filesystem backup system
2 * Copyright (C) 2007 Michael Vrable
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. */
11 #include <sys/types.h>
13 #include <sys/resource.h>
35 /* Default filter program is bzip2 */
36 const char *filter_program = "bzip2 -c";
37 const char *filter_extension = ".bz2";
39 static void cloexec(int fd)
41 long flags = fcntl(fd, F_GETFD);
46 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
49 Tarfile::Tarfile(const string &path, const string &segment)
53 real_fd = open(path.c_str(), O_WRONLY | O_CREAT, 0666);
55 throw IOException("Error opening output file");
57 filter_fd = spawn_filter(real_fd);
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");
66 /* Close the tar file... */
69 if (tar_close(t) != 0)
70 throw IOException("Error closing Tarfile");
72 /* ...and wait for filter process to finish. */
74 waitpid(filter_pid, &status, 0);
76 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
77 throw IOException("Filter process error");
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)
91 /* Create a pipe for communicating with the filter process. */
93 throw IOException("Unable to create pipe for filter");
96 /* Create a child process which can exec() the filter program. */
99 throw IOException("Unable to fork filter process");
101 if (filter_pid > 0) {
106 /* Child process. Rearrange file descriptors. stdin is fds[0], stdout
107 * is fd_out, stderr is unchanged. */
110 if (dup2(fds[0], 0) < 0)
114 if (dup2(fd_out, 1) < 0)
118 /* Exec the filter program. */
119 execlp("/bin/sh", "/bin/sh", "-c", filter_program, NULL);
121 /* Should not reach here except for error cases. */
122 fprintf(stderr, "Could not exec filter: %m\n");
129 void Tarfile::write_object(int id, const char *data, size_t len)
132 sprintf(buf, "%08x", id);
133 string path = segment_name + "/" + buf;
135 internal_write_object(path, data, len);
138 void Tarfile::internal_write_object(const string &path,
139 const char *data, size_t len)
141 memset(&t->th_buf, 0, sizeof(struct tar_header));
143 th_set_type(t, S_IFREG | 0600);
146 th_set_mode(t, 0600);
148 th_set_mtime(t, time(NULL));
149 th_set_path(t, const_cast<char *>(path.c_str()));
152 if (th_write(t) != 0)
153 throw IOException("Error writing tar header");
160 size_t blocks = (len + T_BLOCKSIZE - 1) / T_BLOCKSIZE;
161 size_t padding = blocks * T_BLOCKSIZE - len;
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");
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");
174 size += blocks * T_BLOCKSIZE;
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
185 size_t Tarfile::size_estimate()
189 if (fstat(real_fd, &statbuf) == 0)
190 return max((int64_t)statbuf.st_size, (int64_t)(size / 128));
192 /* Couldn't stat the file on disk, so just return the actual number of
193 * bytes, before compression. */
197 static const size_t SEGMENT_SIZE = 4 * 1024 * 1024;
199 static map<string, int64_t> group_sizes;
201 ObjectReference TarSegmentStore::write_object(const char *data, size_t len,
202 const std::string &group)
204 struct segment_info *segment;
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;
211 segment->name = generate_uuid();
213 string filename = path + "/" + segment->name + ".tar";
214 filename += filter_extension;
215 segment->file = new Tarfile(filename, segment->name);
219 segments[group] = segment;
221 segment = segments[group];
224 int id = segment->count;
226 sprintf(id_buf, "%08x", id);
228 segment->file->write_object(id, data, len);
231 group_sizes[group] += len;
233 ObjectReference ref(segment->name, id_buf);
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);
243 void TarSegmentStore::sync()
245 while (!segments.empty())
246 close_segment(segments.begin()->first);
249 void TarSegmentStore::dump_stats()
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);
258 void TarSegmentStore::close_segment(const string &group)
260 struct segment_info *segment = segments[group];
262 delete segment->file;
263 segments.erase(segments.find(group));
267 string TarSegmentStore::object_reference_to_segment(const string &object)
272 LbsObject::LbsObject()
273 : group(""), data(NULL), data_len(0), written(false)
277 LbsObject::~LbsObject()
281 void LbsObject::write(TarSegmentStore *store)
283 assert(data != NULL);
286 ref = store->write_object(data, data_len, group);
290 void LbsObject::checksum()
295 hash.process(data, data_len);
296 ref.set_checksum(hash.checksum_str());