+/* Ensure contents of metadata are flushed to an object. */
+void metadata_flush()
+{
+ string m = metadata.str();
+ if (m.size() == 0)
+ return;
+
+ /* Write current metadata information to a new object. */
+ LbsObject *meta = new LbsObject;
+ meta->set_group("root");
+ meta->set_data(m.data(), m.size());
+ meta->write(tss);
+ meta->checksum();
+
+ /* Write a reference to this block in the root. */
+ ObjectReference ref = meta->get_ref();
+ metadata_root << "@" << ref.to_string() << "\n";
+ segment_list.insert(ref.get_segment());
+
+ delete meta;
+
+ metadata.str("");
+}
+
+/* Read data from a file descriptor and return the amount of data read. A
+ * short read (less than the requested size) will only occur if end-of-file is
+ * hit. */
+size_t file_read(int fd, char *buf, size_t maxlen)
+{
+ size_t bytes_read = 0;
+
+ while (true) {
+ ssize_t res = read(fd, buf, maxlen);
+ if (res < 0) {
+ if (errno == EINTR)
+ continue;
+ throw IOException("file_read: error reading");
+ } else if (res == 0) {
+ break;
+ } else {
+ bytes_read += res;
+ buf += res;
+ maxlen -= res;
+ }
+ }
+
+ return bytes_read;
+}
+
+/* Read the contents of a file (specified by an open file descriptor) and copy
+ * the data to the store. */
+void dumpfile(int fd, dictionary &file_info)
+{
+ struct stat stat_buf;
+ fstat(fd, &stat_buf);
+ int64_t size = 0;
+ list<string> object_list;
+
+ if ((stat_buf.st_mode & S_IFMT) != S_IFREG) {
+ fprintf(stderr, "file is no longer a regular file!\n");
+ return;
+ }
+
+ /* The index data consists of a sequence of pointers to the data blocks
+ * that actually comprise the file data. This level of indirection is used
+ * so that the same data block can be used in multiple files, or multiple
+ * versions of the same file. */
+ SHA1Checksum hash;
+ while (true) {
+ size_t bytes = file_read(fd, block_buf, LBS_BLOCK_SIZE);
+ if (bytes == 0)
+ break;
+
+ hash.process(block_buf, bytes);
+
+ // tarstore processing
+ LbsObject *o = new LbsObject;
+ o->set_group("data");
+ o->set_data(block_buf, bytes);
+ o->write(tss);
+ object_list.push_back(o->get_name());
+ segment_list.insert(o->get_ref().get_segment());
+ delete o;
+
+ size += bytes;
+ }
+
+ file_info["checksum"] = hash.checksum_str();
+
+ /* For files that only need to be broken apart into a few objects, store
+ * the list of objects directly. For larger files, store the data
+ * out-of-line and provide a pointer to the indrect object. */
+ if (object_list.size() < 8) {
+ string blocklist = "";
+ for (list<string>::iterator i = object_list.begin();
+ i != object_list.end(); ++i) {
+ if (i != object_list.begin())
+ blocklist += " ";
+ blocklist += *i;
+ }
+ file_info["data"] = blocklist;
+ } else {
+ string blocklist = "";
+ for (list<string>::iterator i = object_list.begin();
+ i != object_list.end(); ++i) {
+ blocklist += *i + "\n";
+ }
+
+ LbsObject *i = new LbsObject;
+ i->set_group("indirect");
+ i->set_data(blocklist.data(), blocklist.size());
+ i->write(tss);
+ file_info["data"] = "@" + i->get_name();
+ segment_list.insert(i->get_ref().get_segment());
+ delete i;
+ }
+}
+