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>
33 static char *const filter_program[] = {"bzip2", "-c", NULL};
35 static void cloexec(int fd)
37 long flags = fcntl(fd, F_GETFD);
42 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
45 Tarfile::Tarfile(const string &path, const string &segment)
49 real_fd = open(path.c_str(), O_WRONLY | O_CREAT, 0600);
51 throw IOException("Error opening output file");
53 filter_fd = spawn_filter(real_fd);
55 if (tar_fdopen(&t, filter_fd, (char *)path.c_str(), NULL,
56 O_WRONLY | O_CREAT, 0600, TAR_VERBOSE | TAR_GNU) == -1)
57 throw IOException("Error opening Tarfile");
62 /* Close the tar file... */
65 if (tar_close(t) != 0)
66 throw IOException("Error closing Tarfile");
68 /* ...and wait for filter process to finish. */
70 waitpid(filter_pid, &status, 0);
72 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
73 throw IOException("Filter process error");
79 /* Launch a child process which can act as a filter (compress, encrypt, etc.)
80 * on the TAR output. The file descriptor to which output should be written
81 * must be specified; the return value is the file descriptor which will be
82 * attached to the standard input of the filter program. */
83 int Tarfile::spawn_filter(int fd_out)
87 /* Create a pipe for communicating with the filter process. */
89 throw IOException("Unable to create pipe for filter");
92 /* Create a child process which can exec() the filter program. */
95 throw IOException("Unable to fork filter process");
102 /* Child process. Rearrange file descriptors. stdin is fds[0], stdout
103 * is fd_out, stderr is unchanged. */
106 if (dup2(fds[0], 0) < 0)
110 if (dup2(fd_out, 1) < 0)
114 /* Exec the filter program. */
115 execvp(filter_program[0], filter_program);
117 /* Should not reach here except for error cases. */
118 fprintf(stderr, "Could not exec filter: %m\n");
125 void Tarfile::write_object(int id, const char *data, size_t len)
128 sprintf(buf, "%08x", id);
129 string path = segment_name + "/" + buf;
131 internal_write_object(path, data, len);
134 void Tarfile::internal_write_object(const string &path,
135 const char *data, size_t len)
137 memset(&t->th_buf, 0, sizeof(struct tar_header));
139 th_set_type(t, S_IFREG | 0600);
142 th_set_mode(t, 0600);
144 th_set_mtime(t, time(NULL));
145 th_set_path(t, const_cast<char *>(path.c_str()));
148 if (th_write(t) != 0)
149 throw IOException("Error writing tar header");
156 size_t blocks = (len + T_BLOCKSIZE - 1) / T_BLOCKSIZE;
157 size_t padding = blocks * T_BLOCKSIZE - len;
159 for (size_t i = 0; i < blocks - 1; i++) {
160 if (tar_block_write(t, &data[i * T_BLOCKSIZE]) == -1)
161 throw IOException("Error writing tar block");
164 char block[T_BLOCKSIZE];
165 memset(block, 0, sizeof(block));
166 memcpy(block, &data[T_BLOCKSIZE * (blocks - 1)], T_BLOCKSIZE - padding);
167 if (tar_block_write(t, block) == -1)
168 throw IOException("Error writing final tar block");
170 size += blocks * T_BLOCKSIZE;
173 /* Estimate the size based on the size of the actual output file on disk.
174 * However, it might be the case that the filter program is buffering all its
175 * data, and might potentially not write a single byte until we have closed
176 * our end of the pipe. If we don't do so until we see data written, we have
177 * a problem. So, arbitrarily pick an upper bound on the compression ratio
178 * that the filter will achieve (128:1), and return a size estimate which is
179 * the larger of a) bytes actually seen written to disk, and b) input
181 size_t Tarfile::size_estimate()
185 if (fstat(real_fd, &statbuf) == 0)
186 return max((int64_t)statbuf.st_size, (int64_t)(size / 128));
188 /* Couldn't stat the file on disk, so just return the actual number of
189 * bytes, before compression. */
193 static const size_t SEGMENT_SIZE = 4 * 1024 * 1024;
195 ObjectReference TarSegmentStore::write_object(const char *data, size_t len,
196 const std::string &group)
198 struct segment_info *segment;
200 // Find the segment into which the object should be written, looking up by
201 // group. If no segment exists yet, create one.
202 if (segments.find(group) == segments.end()) {
203 segment = new segment_info;
205 segment->name = generate_uuid();
207 string filename = path + "/" + segment->name + ".tar.bz2";
208 segment->file = new Tarfile(filename, segment->name);
212 segments[group] = segment;
214 segment = segments[group];
217 int id = segment->count;
219 sprintf(id_buf, "%08x", id);
221 segment->file->write_object(id, data, len);
224 ObjectReference ref(segment->name, id_buf);
226 // If this segment meets or exceeds the size target, close it so that
227 // future objects will go into a new segment.
228 if (segment->file->size_estimate() >= SEGMENT_SIZE)
229 close_segment(group);
234 void TarSegmentStore::sync()
236 while (!segments.empty())
237 close_segment(segments.begin()->first);
240 void TarSegmentStore::close_segment(const string &group)
242 struct segment_info *segment = segments[group];
244 delete segment->file;
245 segments.erase(segments.find(group));
249 string TarSegmentStore::object_reference_to_segment(const string &object)
254 LbsObject::LbsObject()
255 : group(""), data(NULL), data_len(0), written(false)
259 LbsObject::~LbsObject()
263 void LbsObject::write(TarSegmentStore *store)
265 assert(data != NULL);
268 ref = store->write_object(data, data_len, group);
272 void LbsObject::checksum()
277 hash.process(data, data_len);
278 ref.set_checksum(hash.checksum_str());