Assorted minor code cleanups.
[cumulus.git] / metadata.cc
index 86d02aa..a3323a1 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <string>
 #include <iostream>
+#include <map>
 
 #include "metadata.h"
 #include "ref.h"
@@ -16,6 +17,7 @@
 #include "util.h"
 
 using std::list;
+using std::map;
 using std::string;
 using std::ostream;
 using std::ostringstream;
@@ -63,6 +65,27 @@ static int pathcmp(const char *path1, const char *path2)
     return pathcmp(slash1 + 1, slash2 + 1);
 }
 
+/* Encode a dictionary of string key/value pairs into a sequence of lines of
+ * the form "key: value".  If it exists, the key "name" is treated specially
+ * and will be listed first. */
+static string encode_dict(const map<string, string>& dict)
+{
+    string result;
+
+    if (dict.find("name") != dict.end()) {
+        result += "name: " + dict.at("name") + "\n";
+    }
+
+    for (map<string, string>::const_iterator i = dict.begin();
+         i != dict.end(); ++i) {
+        if (i->first == "name")
+            continue;
+        result += i->first + ": " + i->second + "\n";
+    }
+
+    return result;
+}
+
 MetadataWriter::MetadataWriter(TarSegmentStore *store,
                                const char *path,
                                const char *snapshot_name,
@@ -83,7 +106,6 @@ MetadataWriter::MetadataWriter(TarSegmentStore *store,
         throw IOException("Error opening statcache");
     }
 
-    found_match = false;
     old_metadata_eof = false;
 
     this->store = store;
@@ -164,18 +186,14 @@ bool MetadataWriter::find(const string& path)
     while (!old_metadata_eof) {
         string old_path = uri_decode(old_metadata["name"]);
         int cmp = pathcmp(old_path.c_str(), path_str);
-        if (cmp == 0) {
-            found_match = true;
+        if (cmp == 0)
             return true;
-        } else if (cmp > 0) {
-            found_match = false;
+        else if (cmp > 0)
             return false;
-        } else {
+        else
             read_statcache();
-        }
     }
 
-    found_match = false;
     return false;
 }
 
@@ -233,11 +251,9 @@ list<ObjectReference> MetadataWriter::get_blocks()
             s++;
         }
 
-        ObjectReference *r = ObjectReference::parse(ref);
-        if (r != NULL) {
-            blocks.push_back(*r);
-            delete r;
-        }
+        ObjectReference r = ObjectReference::parse(ref);
+        if (!r.is_null())
+            blocks.push_back(r);
     }
 
     return blocks;
@@ -327,11 +343,10 @@ void MetadataWriter::add(dictionary info)
     item.text += encode_dict(info) + "\n";
 
     if (info == old_metadata) {
-        ObjectReference *ref = ObjectReference::parse(old_metadata_loc);
-        if (ref != NULL) {
+        ObjectReference ref = ObjectReference::parse(old_metadata_loc);
+        if (!ref.is_null()) {
             item.reused = true;
-            item.ref = *ref;
-            delete ref;
+            item.ref = ref;
         }
     }