X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=store.cc;h=a156a570f791c3539ec9f457533fd5b04287dbb7;hb=79c9c740d9b78e357dd66cb9a4b77d03a5076f3b;hp=9d4d1bd8b0ec59c701021f98f2a9d2c7d5dc8dbc;hpb=4c7c830e38130011d1f5cc68934a31619e3b8a85;p=cumulus.git diff --git a/store.cc b/store.cc index 9d4d1bd..a156a57 100644 --- a/store.cc +++ b/store.cc @@ -56,7 +56,7 @@ Tarfile::Tarfile(const string &path, const string &segment) if (real_fd < 0) throw IOException("Error opening output file"); - filter_fd = spawn_filter(real_fd); + filter_fd = spawn_filter(real_fd, filter_program, &filter_pid); } Tarfile::~Tarfile() @@ -86,9 +86,10 @@ Tarfile::~Tarfile() * on the TAR output. The file descriptor to which output should be written * must be specified; the return value is the file descriptor which will be * attached to the standard input of the filter program. */ -int Tarfile::spawn_filter(int fd_out) +int spawn_filter(int fd_out, const char *program, pid_t *filter_pid) { int fds[2]; + pid_t pid; /* Create a pipe for communicating with the filter process. */ if (pipe(fds) < 0) { @@ -96,14 +97,16 @@ int Tarfile::spawn_filter(int fd_out) } /* Create a child process which can exec() the filter program. */ - filter_pid = fork(); - if (filter_pid < 0) + pid = fork(); + if (pid < 0) throw IOException("Unable to fork filter process"); - if (filter_pid > 0) { + if (pid > 0) { /* Parent process */ close(fds[0]); cloexec(fds[1]); + if (filter_pid != NULL) + *filter_pid = pid; } else { /* Child process. Rearrange file descriptors. stdin is fds[0], stdout * is fd_out, stderr is unchanged. */ @@ -118,7 +121,7 @@ int Tarfile::spawn_filter(int fd_out) close(fd_out); /* Exec the filter program. */ - execlp("/bin/sh", "/bin/sh", "-c", filter_program, NULL); + execlp("/bin/sh", "/bin/sh", "-c", program, NULL); /* Should not reach here except for error cases. */ fprintf(stderr, "Could not exec filter: %m\n"); @@ -149,19 +152,13 @@ void Tarfile::tar_write(const char *data, size_t len) void Tarfile::write_object(int id, const char *data, size_t len) { + struct tar_header header; + memset(&header, 0, sizeof(header)); + char buf[64]; sprintf(buf, "%08x", id); string path = segment_name + "/" + buf; - internal_write_object(path, data, len); -} - -void Tarfile::internal_write_object(const string &path, - const char *data, size_t len) -{ - struct tar_header header; - memset(&header, 0, sizeof(header)); - assert(path.size() < 100); memcpy(header.name, path.data(), path.size()); sprintf(header.mode, "%07o", 0600); @@ -230,11 +227,10 @@ ObjectReference TarSegmentStore::write_object(const char *data, size_t len, segment = new segment_info; segment->name = generate_uuid(); - - string filename = path + "/" + segment->name + ".tar"; - filename += filter_extension; - segment->file = new Tarfile(filename, segment->name); - + segment->basename = segment->name + ".tar"; + segment->basename += filter_extension; + segment->fullname = path + "/" + segment->basename; + segment->file = new Tarfile(segment->fullname, segment->name); segment->count = 0; segments[group] = segment; @@ -281,6 +277,15 @@ void TarSegmentStore::close_segment(const string &group) struct segment_info *segment = segments[group]; delete segment->file; + + if (db != NULL) { + SHA1Checksum segment_checksum; + if (segment_checksum.process_file(segment->fullname.c_str())) { + string checksum = segment_checksum.checksum_str(); + db->SetSegmentChecksum(segment->name, segment->basename, checksum); + } + } + segments.erase(segments.find(group)); delete segment; }