Rewrite object reference parser.
[cumulus.git] / ref.cc
1 /* LBS: An LFS-inspired filesystem backup system
2  * Copyright (C) 2007  Michael Vrable
3  *
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. */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <uuid/uuid.h>
12
13 #include <string>
14
15 #include "ref.h"
16
17 using std::string;
18
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()
22 {
23     uint8_t uuid[16];
24     char buf[40];
25
26     uuid_generate(uuid);
27     uuid_unparse_lower(uuid, buf);
28     return string(buf);
29 }
30
31 ObjectReference::ObjectReference()
32     : segment(""), object("")
33 {
34 }
35
36 ObjectReference::ObjectReference(const std::string& segment, int sequence)
37     : segment(segment)
38 {
39     char seq_buf[64];
40     sprintf(seq_buf, "%08x", sequence);
41     object = seq_buf;
42
43     clear_checksum();
44     clear_range();
45 }
46
47 ObjectReference::ObjectReference(const std::string& segment,
48                                  const std::string& sequence)
49     : segment(segment), object(sequence)
50 {
51     clear_checksum();
52     clear_range();
53 }
54
55 string ObjectReference::to_string() const
56 {
57     string result = segment + "/" + object;
58
59     if (checksum_valid)
60         result += "(" + checksum + ")";
61
62     if (range_valid) {
63         char buf[64];
64         sprintf(buf, "[%zu+%zu]", range_start, range_length);
65         result += buf;
66     }
67
68     return result;
69 }
70
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)
75 {
76     const char *s = str.c_str();
77     const char *t;
78
79     // Segment
80     t = s;
81     while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f') || (*t == '-'))
82         t++;
83     if (*t != '/')
84         return NULL;
85     string segment(s, t - s);
86
87     // Object sequence number
88     t++;
89     s = t;
90     while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f'))
91         t++;
92     if (*t != '\0' && *t != '(' && *t != '[')
93         return NULL;
94     string object(s, t - s);
95
96     // Checksum
97     string checksum;
98     if (*t == '(') {
99         t++;
100         s = t;
101         while (*t != ')' && *t != '\0')
102             t++;
103         if (*t != ')')
104             return NULL;
105         checksum = string(s, t - s);
106         t++;
107     }
108
109     // Range
110     bool have_range = false;
111     int64_t range1, range2;
112     if (*t == '[') {
113         t++;
114         s = t;
115         while (*t >= '0' && *t <= '9')
116             t++;
117         if (*t != '+')
118             return NULL;
119
120         string val(s, t - s);
121         range1 = atoll(val.c_str());
122
123         t++;
124         s = t;
125         while (*t >= '0' && *t <= '9')
126             t++;
127         if (*t != ']')
128             return NULL;
129
130         val = string(s, t - s);
131         range2 = atoll(val.c_str());
132
133         have_range = true;
134     }
135
136     ObjectReference *ref = new ObjectReference(segment, object);
137     if (checksum.size() > 0)
138         ref->set_checksum(checksum);
139
140     if (have_range)
141         ref->set_range(range1, range2);
142
143     return ref;
144 }