1 /* Cumulus: Smart Filesystem Backup to Dumb Servers
3 * Copyright (C) 2007 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /* Backups are structured as a collection of objects, which may refer to other
22 * objects. Object references are used to name other objects or parts of them.
23 * This file defines the class for representing object references and the
24 * textual representation of these references. */
28 #include <uuid/uuid.h>
36 /* Generate a new UUID, and return the text representation of it. This is
37 * suitable for generating the name for a new segment. */
38 string generate_uuid()
44 uuid_unparse_lower(uuid, buf);
48 ObjectReference::ObjectReference()
49 : type(REF_NULL), segment(""), object("")
55 ObjectReference::ObjectReference(RefType t)
56 : type(t), segment(""), object("")
62 ObjectReference::ObjectReference(const std::string& segment, int sequence)
63 : type(REF_NORMAL), segment(segment)
66 sprintf(seq_buf, "%08x", sequence);
73 ObjectReference::ObjectReference(const std::string& segment,
74 const std::string& sequence)
75 : type(REF_NORMAL), segment(segment), object(sequence)
81 string ObjectReference::to_string() const
87 if (type == REF_ZERO) {
89 } else if (type == REF_NORMAL) {
90 result = segment + "/" + object;
93 result += "(" + checksum + ")";
98 if (range_start == 0) {
99 sprintf(buf, "[%zu]", range_length);
101 sprintf(buf, "[%zu+%zu]", range_start, range_length);
109 /* Parse a string object reference and return a pointer to a new
110 * ObjectReference. The caller is responsible for freeing the object. NULL is
111 * returned if there is an error in the syntax. */
112 ObjectReference ObjectReference::parse(const std::string& str)
114 const char *s = str.c_str();
116 ObjectReference::RefType type = ObjectReference::REF_NORMAL;
118 // Special case: explicit zero objects
119 if (strncmp(s, "zero", 4) == 0) {
120 type = ObjectReference::REF_ZERO;
126 if (type == ObjectReference::REF_NORMAL) {
127 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f')
131 return ObjectReference();
133 string segment(s, t - s);
135 // Object sequence number
136 if (type == ObjectReference::REF_NORMAL) {
139 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f'))
141 if (*t != '\0' && *t != '(' && *t != '[')
142 return ObjectReference();
144 string object(s, t - s);
151 while (*t != ')' && *t != '\0')
154 return ObjectReference();
155 checksum = string(s, t - s);
160 bool have_range = false;
161 int64_t range1 = 0, range2 = 0;
165 while (*t >= '0' && *t <= '9')
168 // Abbreviated-length only range?
170 string val(s, t - s);
171 range2 = atoll(val.c_str());
174 return ObjectReference();
176 string val(s, t - s);
177 range1 = atoll(val.c_str());
181 while (*t >= '0' && *t <= '9')
184 return ObjectReference();
186 val = string(s, t - s);
187 range2 = atoll(val.c_str());
195 case ObjectReference::REF_ZERO:
196 ref = ObjectReference(ObjectReference::REF_ZERO);
198 case ObjectReference::REF_NORMAL:
199 ref = ObjectReference(segment, object);
202 return ObjectReference();
205 if (checksum.size() > 0)
206 ref.set_checksum(checksum);
209 ref.set_range(range1, range2);
214 /* Attempt to merge a new object reference into the current one. Returns a
215 * boolean indicating success; if successful this reference is modified so that
216 * it refers to the range of bytes originally covered by this reference plus
217 * the reference passed in. Merging only succeeds if both references refer to
218 * the same object and the byte ranges are contiguous. */
219 bool ObjectReference::merge(ObjectReference ref)
221 // Exception: We can always merge into a null object
227 if (segment != ref.segment)
229 if (object != ref.object)
232 // TODO: Allow the case where only one checksum was filled in
233 if (checksum_valid != ref.checksum_valid || checksum != ref.checksum)
236 if (!range_valid || !ref.range_valid)
239 if (range_start + range_length == ref.range_start) {
240 range_length += ref.range_length;