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. */
31 #include <uuid/uuid.h>
39 /* Generate a new UUID, and return the text representation of it. This is
40 * suitable for generating the name for a new segment. */
41 string generate_uuid()
47 uuid_unparse_lower(uuid, buf);
51 ObjectReference::ObjectReference()
52 : type(REF_NULL), segment(""), object("")
58 ObjectReference::ObjectReference(RefType t)
59 : type(t), segment(""), object("")
65 ObjectReference::ObjectReference(const std::string& segment, int sequence)
66 : type(REF_NORMAL), segment(segment)
69 sprintf(seq_buf, "%08x", sequence);
76 ObjectReference::ObjectReference(const std::string& segment,
77 const std::string& sequence)
78 : type(REF_NORMAL), segment(segment), object(sequence)
84 string ObjectReference::to_string() const
90 if (type == REF_ZERO) {
92 } else if (type == REF_NORMAL) {
93 result = segment + "/" + object;
96 result += "(" + checksum + ")";
102 sprintf(buf, "[=%zu]", range_length);
103 } else if (type == REF_ZERO) {
104 sprintf(buf, "[%zu]", range_length);
106 sprintf(buf, "[%zu+%zu]", range_start, range_length);
114 /* Parse a string object reference and return a pointer to a new
115 * ObjectReference. The caller is responsible for freeing the object. NULL is
116 * returned if there is an error in the syntax. */
117 ObjectReference ObjectReference::parse(const std::string& str)
119 const char *s = str.c_str();
121 ObjectReference::RefType type = ObjectReference::REF_NORMAL;
123 // Special case: explicit zero objects
124 if (strncmp(s, "zero", 4) == 0) {
125 type = ObjectReference::REF_ZERO;
131 if (type == ObjectReference::REF_NORMAL) {
132 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f')
136 return ObjectReference();
138 string segment(s, t - s);
140 // Object sequence number
141 if (type == ObjectReference::REF_NORMAL) {
144 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f'))
146 if (*t != '\0' && *t != '(' && *t != '[')
147 return ObjectReference();
149 string object(s, t - s);
156 while (*t != ')' && *t != '\0')
159 return ObjectReference();
160 checksum = string(s, t - s);
165 bool have_range = false, range_exact = false;
166 int64_t range1 = 0, range2 = 0;
176 while (*t >= '0' && *t <= '9')
179 // Abbreviated-length only range?
181 string val(s, t - s);
182 range2 = atoll(val.c_str());
185 return ObjectReference();
187 return ObjectReference();
189 string val(s, t - s);
190 range1 = atoll(val.c_str());
194 while (*t >= '0' && *t <= '9')
197 return ObjectReference();
199 val = string(s, t - s);
200 range2 = atoll(val.c_str());
208 case ObjectReference::REF_ZERO:
209 ref = ObjectReference(ObjectReference::REF_ZERO);
211 case ObjectReference::REF_NORMAL:
212 ref = ObjectReference(segment, object);
215 return ObjectReference();
218 if (checksum.size() > 0)
219 ref.set_checksum(checksum);
222 ref.set_range(range1, range2, range_exact);
227 /* Attempt to merge a new object reference into the current one. Returns a
228 * boolean indicating success; if successful this reference is modified so that
229 * it refers to the range of bytes originally covered by this reference plus
230 * the reference passed in. Merging only succeeds if both references refer to
231 * the same object and the byte ranges are contiguous. */
232 bool ObjectReference::merge(ObjectReference ref)
234 // Exception: We can always merge into a null object
240 if (segment != ref.segment)
242 if (object != ref.object)
245 // TODO: Allow the case where only one checksum was filled in
246 if (checksum_valid != ref.checksum_valid || checksum != ref.checksum)
249 if (!range_valid || !ref.range_valid)
252 if (range_exact || ref.range_exact)
255 if (range_start + range_length == ref.range_start) {
256 range_length += ref.range_length;