Upgrades to utility code for new formats, and a few more database tweaks.
[cumulus.git] / scandir.cc
index 49d8c32..03d8414 100644 (file)
@@ -28,7 +28,6 @@
 #include "metadata.h"
 #include "store.h"
 #include "sha1.h"
-#include "statcache.h"
 #include "util.h"
 
 using std::list;
@@ -55,10 +54,6 @@ static char *block_buf;
  * invocations to help in creating incremental snapshots. */
 LocalDb *db;
 
-/* Stat cache, which stored data locally to speed the backup process by quickly
- * skipping files which have not changed. */
-StatCache *statcache;
-
 /* Keep track of all segments which are needed to reconstruct the snapshot. */
 std::set<string> segment_list;
 
@@ -119,9 +114,9 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path,
      * re-reading the entire contents. */
     bool cached = false;
 
-    if (statcache->Find(path, &stat_buf)) {
+    if (metawriter->find(path) && metawriter->is_unchanged(&stat_buf)) {
         cached = true;
-        const list<ObjectReference> &blocks = statcache->get_blocks();
+        list<ObjectReference> blocks = metawriter->get_blocks();
 
         /* If any of the blocks in the object have been expired, then we should
          * fall back to fully reading in the file. */
@@ -137,7 +132,7 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path,
 
         /* If everything looks okay, use the cached information */
         if (cached) {
-            file_info["checksum"] = statcache->get_checksum();
+            file_info["checksum"] = metawriter->get_checksum();
             for (list<ObjectReference>::const_iterator i = blocks.begin();
                  i != blocks.end(); ++i) {
                 const ObjectReference &ref = *i;
@@ -227,35 +222,14 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path,
     if (status != NULL)
         printf("    [%s]\n", status);
 
-    statcache->Save(path, &stat_buf, file_info["checksum"], object_list);
-
-    /* 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("metadata");
-        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;
+    string blocklist = "";
+    for (list<string>::iterator i = object_list.begin();
+         i != object_list.end(); ++i) {
+        if (i != object_list.begin())
+            blocklist += "\n    ";
+        blocklist += *i;
     }
+    file_info["data"] = blocklist;
 
     return size;
 }
@@ -274,12 +248,20 @@ 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);
     file_info["mode"] = encode_int(stat_buf.st_mode & 07777, 8);
+    file_info["ctime"] = encode_int(stat_buf.st_ctime);
     file_info["mtime"] = encode_int(stat_buf.st_mtime);
     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) + ")";
@@ -292,11 +274,12 @@ void dump_inode(const string& path,         // Path within snapshot
 
     if (stat_buf.st_nlink > 1 && (stat_buf.st_mode & S_IFMT) != S_IFDIR) {
         file_info["links"] = encode_int(stat_buf.st_nlink);
-        file_info["inode"] = encode_int(major(stat_buf.st_dev))
-            + "/" + encode_int(minor(stat_buf.st_dev))
-            + "/" + encode_int(stat_buf.st_ino);
     }
 
+    file_info["inode"] = encode_int(major(stat_buf.st_dev))
+        + "/" + encode_int(minor(stat_buf.st_dev))
+        + "/" + encode_int(stat_buf.st_ino);
+
     char inode_type;
 
     switch (stat_buf.st_mode & S_IFMT) {
@@ -343,6 +326,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;
@@ -357,7 +341,7 @@ void dump_inode(const string& path,         // Path within snapshot
 
     file_info["type"] = string(1, inode_type);
 
-    metawriter->add(path, file_info);
+    metawriter->add(file_info);
 }
 
 void scanfile(const string& path, bool include)
@@ -683,12 +667,11 @@ int main(int argc, char *argv[])
 
     tss = new TarSegmentStore(backup_dest, db);
 
-    metawriter = new MetadataWriter(tss);
-
     /* Initialize the stat cache, for skipping over unchanged files. */
-    statcache = new StatCache;
-    statcache->Open(localdb_dir.c_str(), desc_buf,
-                    backup_scheme.size() ? backup_scheme.c_str() : NULL);
+    metawriter = new MetadataWriter(tss, localdb_dir.c_str(), desc_buf,
+                                    backup_scheme.size()
+                                        ? backup_scheme.c_str()
+                                        : NULL);
 
     scanfile(".", false);
 
@@ -696,9 +679,6 @@ int main(int argc, char *argv[])
     add_segment(root_ref.get_segment());
     string backup_root = root_ref.to_string();
 
-    statcache->Close();
-    delete statcache;
-
     delete metawriter;
 
     tss->sync();