1 /* Cumulus: Efficient Filesystem Backup to the Cloud
2 * Copyright (C) 2007-2008 The Cumulus Developers
3 * See the AUTHORS file for a list of contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /* Backups are structured as a collection of objects, which may refer to other
21 * objects. Object references are used to name other objects or parts of them.
22 * This file defines the class for representing object references and the
23 * textual representation of these references. */
30 #include <uuid/uuid.h>
38 /* Generate a new UUID, and return the text representation of it. This is
39 * suitable for generating the name for a new segment. */
40 string generate_uuid()
46 uuid_unparse_lower(uuid, buf);
50 ObjectReference::ObjectReference()
51 : type(REF_NULL), segment(""), object("")
57 ObjectReference::ObjectReference(RefType t)
58 : type(t), segment(""), object("")
64 ObjectReference::ObjectReference(const std::string& segment, int sequence)
65 : type(REF_NORMAL), segment(segment)
68 sprintf(seq_buf, "%08x", sequence);
75 ObjectReference::ObjectReference(const std::string& segment,
76 const std::string& sequence)
77 : type(REF_NORMAL), segment(segment), object(sequence)
83 string ObjectReference::to_string() const
89 if (type == REF_ZERO) {
91 } else if (type == REF_NORMAL) {
92 result = segment + "/" + object;
95 result += "(" + checksum + ")";
100 if (range_exact || type == REF_ZERO) {
101 sprintf(buf, "[%zu]", range_length);
103 sprintf(buf, "[%zu+%zu]", range_start, range_length);
111 /* Parse a string object reference and return a pointer to a new
112 * ObjectReference. The caller is responsible for freeing the object. NULL is
113 * returned if there is an error in the syntax. */
114 ObjectReference ObjectReference::parse(const std::string& str)
116 const char *s = str.c_str();
118 ObjectReference::RefType type = ObjectReference::REF_NORMAL;
120 // Special case: explicit zero objects
121 if (strncmp(s, "zero", 4) == 0) {
122 type = ObjectReference::REF_ZERO;
128 if (type == ObjectReference::REF_NORMAL) {
129 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f')
133 return ObjectReference();
135 string segment(s, t - s);
137 // Object sequence number
138 if (type == ObjectReference::REF_NORMAL) {
141 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f'))
143 if (*t != '\0' && *t != '(' && *t != '[')
144 return ObjectReference();
146 string object(s, t - s);
153 while (*t != ')' && *t != '\0')
156 return ObjectReference();
157 checksum = string(s, t - s);
162 bool have_range = false, range_exact = false;
163 int64_t range1 = 0, range2 = 0;
167 // An equal sign was once used for a length assertion but is now
168 // deprecated. Skip it if present, and mark that we are expecting a
169 // length-only reference.
176 while (*t >= '0' && *t <= '9')
179 // Abbreviated-length only range?
181 string val(s, t - s);
182 range2 = atoll(val.c_str());
186 return ObjectReference();
188 return ObjectReference();
190 string val(s, t - s);
191 range1 = atoll(val.c_str());
195 while (*t >= '0' && *t <= '9')
198 return ObjectReference();
200 val = string(s, t - s);
201 range2 = atoll(val.c_str());
209 case ObjectReference::REF_ZERO:
210 ref = ObjectReference(ObjectReference::REF_ZERO);
212 case ObjectReference::REF_NORMAL:
213 ref = ObjectReference(segment, object);
216 return ObjectReference();
219 if (checksum.size() > 0)
220 ref.set_checksum(checksum);
223 ref.set_range(range1, range2, range_exact);
228 /* Attempt to merge a new object reference into the current one. Returns a
229 * boolean indicating success; if successful this reference is modified so that
230 * it refers to the range of bytes originally covered by this reference plus
231 * the reference passed in. Merging only succeeds if both references refer to
232 * the same object and the byte ranges are contiguous. */
233 bool ObjectReference::merge(ObjectReference ref)
235 // Exception: We can always merge into a null object
241 if (segment != ref.segment)
243 if (object != ref.object)
246 // TODO: Allow the case where only one checksum was filled in
247 if (checksum_valid != ref.checksum_valid || checksum != ref.checksum)
250 if (!range_valid || !ref.range_valid)
253 if (range_exact || ref.range_exact)
256 if (range_start + range_length == ref.range_start) {
257 range_length += ref.range_length;