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 if (range_start == 0) {
82 sprintf(buf, "[%zu]", range_length);
84 sprintf(buf, "[%zu+%zu]", range_start, range_length);
92 /* Parse a string object reference and return a pointer to a new
93 * ObjectReference. The caller is responsible for freeing the object. NULL is
94 * returned if there is an error in the syntax. */
95 ObjectReference ObjectReference::parse(const std::string& str)
97 const char *s = str.c_str();
99 ObjectReference::RefType type = ObjectReference::REF_NORMAL;
101 // Special case: explicit zero objects
102 if (strncmp(s, "zero", 4) == 0) {
103 type = ObjectReference::REF_ZERO;
109 if (type == ObjectReference::REF_NORMAL) {
110 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f')
114 return ObjectReference();
116 string segment(s, t - s);
118 // Object sequence number
119 if (type == ObjectReference::REF_NORMAL) {
122 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f'))
124 if (*t != '\0' && *t != '(' && *t != '[')
125 return ObjectReference();
127 string object(s, t - s);
134 while (*t != ')' && *t != '\0')
137 return ObjectReference();
138 checksum = string(s, t - s);
143 bool have_range = false;
144 int64_t range1 = 0, range2 = 0;
148 while (*t >= '0' && *t <= '9')
151 // Abbreviated-length only range?
153 string val(s, t - s);
154 range2 = atoll(val.c_str());
157 return ObjectReference();
159 string val(s, t - s);
160 range1 = atoll(val.c_str());
164 while (*t >= '0' && *t <= '9')
167 return ObjectReference();
169 val = string(s, t - s);
170 range2 = atoll(val.c_str());
178 case ObjectReference::REF_ZERO:
179 ref = ObjectReference(ObjectReference::REF_ZERO);
181 case ObjectReference::REF_NORMAL:
182 ref = ObjectReference(segment, object);
185 return ObjectReference();
188 if (checksum.size() > 0)
189 ref.set_checksum(checksum);
192 ref.set_range(range1, range2);
197 /* Attempt to merge a new object reference into the current one. Returns a
198 * boolean indicating success; if successful this reference is modified so that
199 * it refers to the range of bytes originally covered by this reference plus
200 * the reference passed in. Merging only succeeds if both references refer to
201 * the same object and the byte ranges are contiguous. */
202 bool ObjectReference::merge(ObjectReference ref)
204 // Exception: We can always merge into a null object
210 if (segment != ref.segment)
212 if (object != ref.object)
215 // TODO: Allow the case where only one checksum was filled in
216 if (checksum_valid != ref.checksum_valid || checksum != ref.checksum)
219 if (!range_valid || !ref.range_valid)
222 if (range_start + range_length == ref.range_start) {
223 range_length += ref.range_length;