1 /* LBS: An LFS-inspired filesystem backup system
2 * Copyright (C) 2007 Michael Vrable
4 * Backups are structured as a collection of objects, which may refer to other
5 * objects. Object references are used to name other objects or parts of them.
6 * This file defines the class for representing object references and the
7 * textual representation of these references. */
11 #include <uuid/uuid.h>
19 /* Generate a new UUID, and return the text representation of it. This is
20 * suitable for generating the name for a new segment. */
21 string generate_uuid()
27 uuid_unparse_lower(uuid, buf);
31 ObjectReference::ObjectReference()
32 : type(REF_NULL), segment(""), object("")
38 ObjectReference::ObjectReference(RefType t)
39 : type(t), segment(""), object("")
45 ObjectReference::ObjectReference(const std::string& segment, int sequence)
46 : type(REF_NORMAL), segment(segment)
49 sprintf(seq_buf, "%08x", sequence);
56 ObjectReference::ObjectReference(const std::string& segment,
57 const std::string& sequence)
58 : type(REF_NORMAL), segment(segment), object(sequence)
64 string ObjectReference::to_string() const
70 if (type == REF_ZERO) {
72 } else if (type == REF_NORMAL) {
73 result = segment + "/" + object;
76 result += "(" + checksum + ")";
81 sprintf(buf, "[%zu+%zu]", range_start, range_length);
88 /* Parse a string object reference and return a pointer to a new
89 * ObjectReference. The caller is responsible for freeing the object. NULL is
90 * returned if there is an error in the syntax. */
91 ObjectReference ObjectReference::parse(const std::string& str)
93 const char *s = str.c_str();
95 ObjectReference::RefType type = ObjectReference::REF_NORMAL;
97 // Special case: explicit zero objects
98 if (strncmp(s, "zero", 4) == 0) {
99 type = ObjectReference::REF_ZERO;
105 if (type == ObjectReference::REF_NORMAL) {
106 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f')
110 return ObjectReference();
112 string segment(s, t - s);
114 // Object sequence number
115 if (type == ObjectReference::REF_NORMAL) {
118 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f'))
120 if (*t != '\0' && *t != '(' && *t != '[')
121 return ObjectReference();
123 string object(s, t - s);
130 while (*t != ')' && *t != '\0')
133 return ObjectReference();
134 checksum = string(s, t - s);
139 bool have_range = false;
140 int64_t range1 = 0, range2 = 0;
144 while (*t >= '0' && *t <= '9')
147 return ObjectReference();
149 string val(s, t - s);
150 range1 = atoll(val.c_str());
154 while (*t >= '0' && *t <= '9')
157 return ObjectReference();
159 val = string(s, t - s);
160 range2 = atoll(val.c_str());
167 case ObjectReference::REF_ZERO:
168 ref = ObjectReference(ObjectReference::REF_ZERO);
170 case ObjectReference::REF_NORMAL:
171 ref = ObjectReference(segment, object);
174 return ObjectReference();
177 if (checksum.size() > 0)
178 ref.set_checksum(checksum);
181 ref.set_range(range1, range2);
186 /* Attempt to merge a new object reference into the current one. Returns a
187 * boolean indicating success; if successful this reference is modified so that
188 * it refers to the range of bytes originally covered by this reference plus
189 * the reference passed in. Merging only succeeds if both references refer to
190 * the same object and the byte ranges are contiguous. */
191 bool ObjectReference::merge(ObjectReference ref)
193 // Exception: We can always merge into a null object
199 if (segment != ref.segment)
201 if (object != ref.object)
204 // TODO: Allow the case where only one checksum was filled in
205 if (checksum_valid != ref.checksum_valid || checksum != ref.checksum)
208 if (!range_valid || !ref.range_valid)
211 if (range_start + range_length == ref.range_start) {
212 range_length += ref.range_length;