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 /* Default filter program is bzip2 */
34 const char *filter_program = "bzip2 -c";
35 const char *filter_extension = ".bz2";
37 static void cloexec(int fd)
39 long flags = fcntl(fd, F_GETFD);
44 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
47 Tarfile::Tarfile(const string &path, const string &segment)
51 real_fd = open(path.c_str(), O_WRONLY | O_CREAT, 0600);
53 throw IOException("Error opening output file");
55 filter_fd = spawn_filter(real_fd);
57 if (tar_fdopen(&t, filter_fd, (char *)path.c_str(), NULL,
58 O_WRONLY | O_CREAT, 0600, TAR_VERBOSE | TAR_GNU) == -1)
59 throw IOException("Error opening Tarfile");
64 /* Close the tar file... */
67 if (tar_close(t) != 0)
68 throw IOException("Error closing Tarfile");
70 /* ...and wait for filter process to finish. */
72 waitpid(filter_pid, &status, 0);
74 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
75 throw IOException("Filter process error");
81 /* Launch a child process which can act as a filter (compress, encrypt, etc.)
82 * on the TAR output. The file descriptor to which output should be written
83 * must be specified; the return value is the file descriptor which will be
84 * attached to the standard input of the filter program. */
85 int Tarfile::spawn_filter(int fd_out)
89 /* Create a pipe for communicating with the filter process. */
91 throw IOException("Unable to create pipe for filter");
94 /* Create a child process which can exec() the filter program. */
97 throw IOException("Unable to fork filter process");
104 /* Child process. Rearrange file descriptors. stdin is fds[0], stdout
105 * is fd_out, stderr is unchanged. */
108 if (dup2(fds[0], 0) < 0)
112 if (dup2(fd_out, 1) < 0)
116 /* Exec the filter program. */
117 execlp("/bin/sh", "/bin/sh", "-c", filter_program, NULL);
119 /* Should not reach here except for error cases. */
120 fprintf(stderr, "Could not exec filter: %m\n");
127 void Tarfile::write_object(int id, const char *data, size_t len)
130 sprintf(buf, "%08x", id);
131 string path = segment_name + "/" + buf;
133 internal_write_object(path, data, len);
136 void Tarfile::internal_write_object(const string &path,
137 const char *data, size_t len)
139 memset(&t->th_buf, 0, sizeof(struct tar_header));
141 th_set_type(t, S_IFREG | 0600);
144 th_set_mode(t, 0600);
146 th_set_mtime(t, time(NULL));
147 th_set_path(t, const_cast<char *>(path.c_str()));
150 if (th_write(t) != 0)
151 throw IOException("Error writing tar header");
158 size_t blocks = (len + T_BLOCKSIZE - 1) / T_BLOCKSIZE;
159 size_t padding = blocks * T_BLOCKSIZE - len;
161 for (size_t i = 0; i < blocks - 1; i++) {
162 if (tar_block_write(t, &data[i * T_BLOCKSIZE]) == -1)
163 throw IOException("Error writing tar block");
166 char block[T_BLOCKSIZE];
167 memset(block, 0, sizeof(block));
168 memcpy(block, &data[T_BLOCKSIZE * (blocks - 1)], T_BLOCKSIZE - padding);
169 if (tar_block_write(t, block) == -1)
170 throw IOException("Error writing final tar block");
172 size += blocks * T_BLOCKSIZE;
175 /* Estimate the size based on the size of the actual output file on disk.
176 * However, it might be the case that the filter program is buffering all its
177 * data, and might potentially not write a single byte until we have closed
178 * our end of the pipe. If we don't do so until we see data written, we have
179 * a problem. So, arbitrarily pick an upper bound on the compression ratio
180 * that the filter will achieve (128:1), and return a size estimate which is
181 * the larger of a) bytes actually seen written to disk, and b) input
183 size_t Tarfile::size_estimate()
187 if (fstat(real_fd, &statbuf) == 0)
188 return max((int64_t)statbuf.st_size, (int64_t)(size / 128));
190 /* Couldn't stat the file on disk, so just return the actual number of
191 * bytes, before compression. */
195 static const size_t SEGMENT_SIZE = 4 * 1024 * 1024;
197 ObjectReference TarSegmentStore::write_object(const char *data, size_t len,
198 const std::string &group)
200 struct segment_info *segment;
202 // Find the segment into which the object should be written, looking up by
203 // group. If no segment exists yet, create one.
204 if (segments.find(group) == segments.end()) {
205 segment = new segment_info;
207 segment->name = generate_uuid();
209 string filename = path + "/" + segment->name + ".tar";
210 filename += filter_extension;
211 segment->file = new Tarfile(filename, segment->name);
215 segments[group] = segment;
217 segment = segments[group];
220 int id = segment->count;
222 sprintf(id_buf, "%08x", id);
224 segment->file->write_object(id, data, len);
227 ObjectReference ref(segment->name, id_buf);
229 // If this segment meets or exceeds the size target, close it so that
230 // future objects will go into a new segment.
231 if (segment->file->size_estimate() >= SEGMENT_SIZE)
232 close_segment(group);
237 void TarSegmentStore::sync()
239 while (!segments.empty())
240 close_segment(segments.begin()->first);
243 void TarSegmentStore::close_segment(const string &group)
245 struct segment_info *segment = segments[group];
247 delete segment->file;
248 segments.erase(segments.find(group));
252 string TarSegmentStore::object_reference_to_segment(const string &object)
257 LbsObject::LbsObject()
258 : group(""), data(NULL), data_len(0), written(false)
262 LbsObject::~LbsObject()
266 void LbsObject::write(TarSegmentStore *store)
268 assert(data != NULL);
271 ref = store->write_object(data, data_len, group);
275 void LbsObject::checksum()
280 hash.process(data, data_len);
281 ref.set_checksum(hash.checksum_str());