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>
31 static char *const filter_program[] = {"bzip2", "-c", NULL};
33 static void cloexec(int fd)
35 long flags = fcntl(fd, F_GETFD);
40 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
43 Tarfile::Tarfile(const string &path, const string &segment)
47 real_fd = open(path.c_str(), O_WRONLY | O_CREAT, 0600);
49 throw IOException("Error opening output file");
51 filter_fd = spawn_filter(real_fd);
53 if (tar_fdopen(&t, filter_fd, (char *)path.c_str(), NULL,
54 O_WRONLY | O_CREAT, 0600, TAR_VERBOSE | TAR_GNU) == -1)
55 throw IOException("Error opening Tarfile");
60 /* Close the tar file... */
63 if (tar_close(t) != 0)
64 throw IOException("Error closing Tarfile");
66 /* ...and wait for filter process to finish. */
68 waitpid(filter_pid, &status, 0);
70 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
71 throw IOException("Filter process error");
77 /* Launch a child process which can act as a filter (compress, encrypt, etc.)
78 * on the TAR output. The file descriptor to which output should be written
79 * must be specified; the return value is the file descriptor which will be
80 * attached to the standard input of the filter program. */
81 int Tarfile::spawn_filter(int fd_out)
85 /* Create a pipe for communicating with the filter process. */
87 throw IOException("Unable to create pipe for filter");
90 /* Create a child process which can exec() the filter program. */
93 throw IOException("Unable to fork filter process");
100 /* Child process. Rearrange file descriptors. stdin is fds[0], stdout
101 * is fd_out, stderr is unchanged. */
104 if (dup2(fds[0], 0) < 0)
108 if (dup2(fd_out, 1) < 0)
112 /* Exec the filter program. */
113 execvp(filter_program[0], filter_program);
115 /* Should not reach here except for error cases. */
116 fprintf(stderr, "Could not exec filter: %m\n");
123 void Tarfile::write_object(int id, const char *data, size_t len)
126 sprintf(buf, "%08x", id);
127 string path = segment_name + "/" + buf;
129 internal_write_object(path, data, len);
132 void Tarfile::internal_write_object(const string &path,
133 const char *data, size_t len)
135 memset(&t->th_buf, 0, sizeof(struct tar_header));
137 th_set_type(t, S_IFREG | 0600);
140 th_set_mode(t, 0600);
142 th_set_mtime(t, time(NULL));
143 th_set_path(t, const_cast<char *>(path.c_str()));
146 if (th_write(t) != 0)
147 throw IOException("Error writing tar header");
154 size_t blocks = (len + T_BLOCKSIZE - 1) / T_BLOCKSIZE;
155 size_t padding = blocks * T_BLOCKSIZE - len;
157 for (size_t i = 0; i < blocks - 1; i++) {
158 if (tar_block_write(t, &data[i * T_BLOCKSIZE]) == -1)
159 throw IOException("Error writing tar block");
162 char block[T_BLOCKSIZE];
163 memset(block, 0, sizeof(block));
164 memcpy(block, &data[T_BLOCKSIZE * (blocks - 1)], T_BLOCKSIZE - padding);
165 if (tar_block_write(t, block) == -1)
166 throw IOException("Error writing final tar block");
168 size += blocks * T_BLOCKSIZE;
171 /* Estimate the size based on the size of the actual output file on disk.
172 * However, the filter may not have written all data yet, and in the event that
173 * it is buffering data to a large extent, also use */
174 size_t Tarfile::size_estimate()
178 if (fstat(real_fd, &statbuf) == 0) {
179 size_t disk_size = statbuf.st_size;
181 if (disk_size >= size / 128)
188 static const size_t SEGMENT_SIZE = 4 * 1024 * 1024;
190 ObjectReference TarSegmentStore::write_object(const char *data, size_t len,
191 const std::string &group)
193 struct segment_info *segment;
195 // Find the segment into which the object should be written, looking up by
196 // group. If no segment exists yet, create one.
197 if (segments.find(group) == segments.end()) {
198 segment = new segment_info;
200 segment->name = generate_uuid();
202 string filename = path + "/" + segment->name + ".tar.bz2";
203 segment->file = new Tarfile(filename, segment->name);
207 segments[group] = segment;
209 segment = segments[group];
212 int id = segment->count;
214 sprintf(id_buf, "%08x", id);
216 segment->file->write_object(id, data, len);
219 ObjectReference ref(segment->name, id_buf);
221 // If this segment meets or exceeds the size target, close it so that
222 // future objects will go into a new segment.
223 if (segment->file->size_estimate() >= SEGMENT_SIZE)
224 close_segment(group);
229 void TarSegmentStore::sync()
231 while (!segments.empty())
232 close_segment(segments.begin()->first);
235 void TarSegmentStore::close_segment(const string &group)
237 struct segment_info *segment = segments[group];
239 delete segment->file;
240 segments.erase(segments.find(group));
244 string TarSegmentStore::object_reference_to_segment(const string &object)
249 LbsObject::LbsObject()
250 : group(""), data(NULL), data_len(0), written(false)
254 LbsObject::~LbsObject()
258 void LbsObject::write(TarSegmentStore *store)
260 assert(data != NULL);
263 ref = store->write_object(data, data_len, group);
267 void LbsObject::checksum()
272 hash.process(data, data_len);
273 ref.set_checksum(hash.checksum_str());