Ensure that segments with reused metadata are listed in root descriptor.
[cumulus.git] / scandir.cc
index 7c940f5..cf66ea6 100644 (file)
@@ -114,7 +114,7 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path,
      * re-reading the entire contents. */
     bool cached = false;
 
-    if (metawriter->matched() && metawriter->is_unchanged(&stat_buf)) {
+    if (metawriter->find(path) && metawriter->is_unchanged(&stat_buf)) {
         cached = true;
         list<ObjectReference> blocks = metawriter->get_blocks();
 
@@ -137,7 +137,8 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path,
                  i != blocks.end(); ++i) {
                 const ObjectReference &ref = *i;
                 object_list.push_back(ref.to_string());
-                segment_list.insert(ref.get_segment());
+                if (ref.is_normal())
+                    add_segment(ref.get_segment());
                 db->UseObject(ref);
             }
             size = stat_buf.st_size;
@@ -160,16 +161,34 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path,
 
             hash.process(block_buf, bytes);
 
+            // Sparse file processing: if we read a block of all zeroes, encode
+            // that explicitly.
+            bool all_zero = true;
+            for (int i = 0; i < bytes; i++) {
+                if (block_buf[i] != 0) {
+                    all_zero = false;
+                    break;
+                }
+            }
+
             // Either find a copy of this block in an already-existing segment,
             // or index it so it can be re-used in the future
             double block_age = 0.0;
+            ObjectReference ref;
+
             SHA1Checksum block_hash;
             block_hash.process(block_buf, bytes);
             string block_csum = block_hash.checksum_str();
-            ObjectReference ref = db->FindObject(block_csum, bytes);
+
+            if (all_zero) {
+                ref = ObjectReference(ObjectReference::REF_ZERO);
+                ref.set_range(0, bytes);
+            } else {
+                ObjectReference ref = db->FindObject(block_csum, bytes);
+            }
 
             // Store a copy of the object if one does not yet exist
-            if (ref.get_segment().size() == 0) {
+            if (ref.is_null()) {
                 LbsObject *o = new LbsObject;
                 int object_group;
 
@@ -208,7 +227,8 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path,
             }
 
             object_list.push_back(ref.to_string());
-            segment_list.insert(ref.get_segment());
+            if (ref.is_normal())
+                add_segment(ref.get_segment());
             db->UseObject(ref);
             size += bytes;
 
@@ -248,7 +268,6 @@ void dump_inode(const string& path,         // Path within snapshot
     ssize_t len;
 
     printf("%s\n", path.c_str());
-
     metawriter->find(path);
 
     file_info["name"] = uri_encode(path);
@@ -258,6 +277,11 @@ void dump_inode(const string& path,         // Path within snapshot
     file_info["user"] = encode_int(stat_buf.st_uid);
     file_info["group"] = encode_int(stat_buf.st_gid);
 
+    time_t now = time(NULL);
+    if (now - stat_buf.st_ctime < 30 || now - stat_buf.st_mtime < 30)
+        if ((stat_buf.st_mode & S_IFMT) != S_IFDIR)
+            file_info["volatile"] = "1";
+
     struct passwd *pwd = getpwuid(stat_buf.st_uid);
     if (pwd != NULL) {
         file_info["user"] += " (" + uri_encode(pwd->pw_name) + ")";
@@ -322,6 +346,7 @@ void dump_inode(const string& path,         // Path within snapshot
         if (file_size != stat_buf.st_size) {
             fprintf(stderr, "Warning: Size of %s changed during reading\n",
                     path.c_str());
+            file_info["volatile"] = "1";
         }
 
         break;