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 : segment(""), object("")
36 ObjectReference::ObjectReference(const std::string& segment, int sequence)
40 sprintf(seq_buf, "%08x", sequence);
47 ObjectReference::ObjectReference(const std::string& segment,
48 const std::string& sequence)
49 : segment(segment), object(sequence)
55 string ObjectReference::to_string() const
57 string result = segment + "/" + object;
60 result += "(" + checksum + ")";
64 sprintf(buf, "[%zu+%zu]", range_start, range_length);
71 /* Parse a string object reference and return a pointer to a new
72 * ObjectReference. The caller is responsible for freeing the object. NULL is
73 * returned if there is an error in the syntax. */
74 ObjectReference *ObjectReference::parse(const std::string& str)
76 const char *s = str.c_str();
81 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f') || (*t == '-'))
85 string segment(s, t - s);
87 // Object sequence number
90 while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f'))
92 if (*t != '\0' && *t != '(' && *t != '[')
94 string object(s, t - s);
101 while (*t != ')' && *t != '\0')
105 checksum = string(s, t - s);
110 bool have_range = false;
111 int64_t range1, range2;
115 while (*t >= '0' && *t <= '9')
120 string val(s, t - s);
121 range1 = atoll(val.c_str());
125 while (*t >= '0' && *t <= '9')
130 val = string(s, t - s);
131 range2 = atoll(val.c_str());
136 ObjectReference *ref = new ObjectReference(segment, object);
137 if (checksum.size() > 0)
138 ref->set_checksum(checksum);
141 ref->set_range(range1, range2);