Continue work on reference decoder.
[cumulus.git] / scandir.cc
index 702fce2..30b8d45 100644 (file)
@@ -15,6 +15,7 @@
 #include <list>
 #include <vector>
 #include <iostream>
+#include <fstream>
 #include <sstream>
 #include <set>
 
@@ -30,9 +31,11 @@ using std::ostream;
 static TarSegmentStore *tss = NULL;
 
 /* Buffer for holding a single block of data read from a file. */
-static const int LBS_BLOCK_SIZE = 1024 * 1024;
+static const size_t LBS_BLOCK_SIZE = 1024 * 1024;
 static char *block_buf;
 
+static const size_t LBS_METADATA_BLOCK_SIZE = 65536;
+
 /* Contents of the root object.  This will contain a set of indirect links to
  * the metadata objects. */
 std::ostringstream metadata_root;
@@ -104,7 +107,7 @@ void dumpfile(int fd, dictionary &file_info)
     list<string> object_list;
 
     if ((stat_buf.st_mode & S_IFMT) != S_IFREG) {
-        printf("file is no longer a regular file!\n");
+        fprintf(stderr, "file is no longer a regular file!\n");
         return;
     }
 
@@ -214,16 +217,14 @@ void scanfile(const string& path)
         buf = new char[stat_buf.st_size + 2];
         len = readlink(path.c_str(), buf, stat_buf.st_size + 1);
         if (len < 0) {
-            printf("error reading symlink: %m\n");
+            fprintf(stderr, "error reading symlink: %m\n");
         } else if (len <= stat_buf.st_size) {
             buf[len] = '\0';
-            printf("    contents=%s\n", buf);
+            file_info["contents"] = uri_encode(buf);
         } else if (len > stat_buf.st_size) {
-            printf("error reading symlink: name truncated\n");
+            fprintf(stderr, "error reading symlink: name truncated\n");
         }
 
-        file_info["contents"] = uri_encode(buf);
-
         delete[] buf;
         break;
     case S_IFREG:
@@ -263,13 +264,12 @@ void scanfile(const string& path)
     }
 
     file_info["type"] = string(1, inode_type);
-    metadata << "type: " << inode_type << "\n";
 
     dict_output(metadata, file_info);
     metadata << "\n";
 
     // Break apart metadata listing if it becomes too large.
-    if (metadata.str().size() > 4096)
+    if (metadata.str().size() > LBS_METADATA_BLOCK_SIZE)
         metadata_flush();
 
     // If we hit a directory, now that we've written the directory itself,
@@ -283,7 +283,7 @@ void scandir(const string& path)
     DIR *dir = opendir(path.c_str());
 
     if (dir == NULL) {
-        printf("Error: %m\n");
+        fprintf(stderr, "Error: %m\n");
         return;
     }
 
@@ -311,11 +311,15 @@ int main(int argc, char *argv[])
 {
     block_buf = new char[LBS_BLOCK_SIZE];
 
-    if (argc > 1) {
-        tss = new TarSegmentStore(argv[1]);
-    } else {
-        tss = new TarSegmentStore(".");
-    }
+    string backup_dest = ".";
+
+    if (argc > 1)
+        backup_dest = argv[1];
+
+    tss = new TarSegmentStore(backup_dest);
+
+    string desc_filename = backup_dest + "/snapshot.lbs";
+    std::ofstream descriptor(desc_filename.c_str());
 
     try {
         scanfile(".");
@@ -333,14 +337,14 @@ int main(int argc, char *argv[])
     root->checksum();
 
     segment_list.insert(root->get_ref().get_segment());
-    string r = root->get_ref().to_string();
-    printf("\nroot: %s\n\n", r.c_str());
+    descriptor << "root: " << root->get_ref().to_string() << "\n\n";
+
     delete root;
 
-    printf("segments:\n");
+    descriptor << "segments:\n";
     for (std::set<string>::iterator i = segment_list.begin();
          i != segment_list.end(); ++i) {
-        printf("    %s\n", i->c_str());
+        descriptor << "    " << *i << "\n";
     }
 
     tss->sync();