From ca9a4006c4ffc86a24d653d1b1b68bec4a6fbfdf Mon Sep 17 00:00:00 2001 From: Michael Vrable Date: Thu, 29 Nov 2007 13:04:11 -0800 Subject: [PATCH] Assorted minor code cleanups. --- metadata.cc | 26 +++++++++----------------- metadata.h | 4 +--- ref.cc | 21 ++++++++++++--------- ref.h | 4 ++-- scandir.cc | 4 +--- 5 files changed, 25 insertions(+), 34 deletions(-) diff --git a/metadata.cc b/metadata.cc index dac3d20..a3323a1 100644 --- a/metadata.cc +++ b/metadata.cc @@ -106,7 +106,6 @@ MetadataWriter::MetadataWriter(TarSegmentStore *store, throw IOException("Error opening statcache"); } - found_match = false; old_metadata_eof = false; this->store = store; @@ -187,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; } @@ -256,11 +251,9 @@ list 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; @@ -350,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; } } diff --git a/metadata.h b/metadata.h index 2b4b2e1..6347080 100644 --- a/metadata.h +++ b/metadata.h @@ -38,11 +38,10 @@ public: ObjectReference close(); bool find(const std::string& path); - ObjectReference *old_ref() const { + ObjectReference old_ref() const { return ObjectReference::parse(old_metadata_loc); } - bool matched() const { return found_match; } bool is_unchanged(const struct stat *stat_buf); dictionary get_old_metadata() const { return old_metadata; } @@ -66,7 +65,6 @@ private: std::ostringstream metadata_root; // Statcache information read back in from a previous run - bool found_match; // Result of last call to find bool old_metadata_eof; dictionary old_metadata; std::string old_metadata_loc; // Reference to where the metadata is found diff --git a/ref.cc b/ref.cc index 158fec3..3019c20 100644 --- a/ref.cc +++ b/ref.cc @@ -54,6 +54,9 @@ ObjectReference::ObjectReference(const std::string& segment, string ObjectReference::to_string() const { + if (is_null()) + return "/"; + string result = segment + "/" + object; if (checksum_valid) @@ -71,7 +74,7 @@ string ObjectReference::to_string() const /* Parse a string object reference and return a pointer to a new * ObjectReference. The caller is responsible for freeing the object. NULL is * returned if there is an error in the syntax. */ -ObjectReference *ObjectReference::parse(const std::string& str) +ObjectReference ObjectReference::parse(const std::string& str) { const char *s = str.c_str(); const char *t; @@ -81,7 +84,7 @@ ObjectReference *ObjectReference::parse(const std::string& str) while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f') || (*t == '-')) t++; if (*t != '/') - return NULL; + return ObjectReference(); string segment(s, t - s); // Object sequence number @@ -90,7 +93,7 @@ ObjectReference *ObjectReference::parse(const std::string& str) while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f')) t++; if (*t != '\0' && *t != '(' && *t != '[') - return NULL; + return ObjectReference(); string object(s, t - s); // Checksum @@ -101,7 +104,7 @@ ObjectReference *ObjectReference::parse(const std::string& str) while (*t != ')' && *t != '\0') t++; if (*t != ')') - return NULL; + return ObjectReference(); checksum = string(s, t - s); t++; } @@ -115,7 +118,7 @@ ObjectReference *ObjectReference::parse(const std::string& str) while (*t >= '0' && *t <= '9') t++; if (*t != '+') - return NULL; + return ObjectReference(); string val(s, t - s); range1 = atoll(val.c_str()); @@ -125,7 +128,7 @@ ObjectReference *ObjectReference::parse(const std::string& str) while (*t >= '0' && *t <= '9') t++; if (*t != ']') - return NULL; + return ObjectReference(); val = string(s, t - s); range2 = atoll(val.c_str()); @@ -133,12 +136,12 @@ ObjectReference *ObjectReference::parse(const std::string& str) have_range = true; } - ObjectReference *ref = new ObjectReference(segment, object); + ObjectReference ref(segment, object); if (checksum.size() > 0) - ref->set_checksum(checksum); + ref.set_checksum(checksum); if (have_range) - ref->set_range(range1, range2); + ref.set_range(range1, range2); return ref; } diff --git a/ref.h b/ref.h index 7ed7ff0..e7d41e1 100644 --- a/ref.h +++ b/ref.h @@ -68,9 +68,9 @@ public: ObjectReference(const std::string& segment, int sequence); ObjectReference(const std::string& segment, const std::string& sequence); - bool is_null() { return segment.size() == 0; } + bool is_null() const { return segment.size() == 0; } std::string to_string() const; - static ObjectReference *parse(const std::string& s); + static ObjectReference parse(const std::string& s); std::string get_segment() const { return segment; } std::string get_sequence() const { return object; } diff --git a/scandir.cc b/scandir.cc index 7c940f5..818a460 100644 --- a/scandir.cc +++ b/scandir.cc @@ -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 blocks = metawriter->get_blocks(); @@ -249,8 +249,6 @@ void dump_inode(const string& path, // Path within snapshot 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); -- 2.20.1