-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.cc
410 lines (340 loc) · 11.5 KB
/
metadata.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* Cumulus: Efficient Filesystem Backup to the Cloud
* Copyright (C) 2007-2008 The Cumulus Developers
* See the AUTHORS file for a list of contributors.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Handling of metadata written to backup snapshots. This manages the writing
* of file metadata into new backup snapshots, including breaking the metadata
* log apart across separate objects. Eventually this should include unified
* handling of the statcache, and re-use of metadata between snapshots.
*/
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <map>
#include "metadata.h"
#include "localdb.h"
#include "ref.h"
#include "store.h"
#include "util.h"
using std::list;
using std::map;
using std::string;
using std::ostream;
using std::ostringstream;
static const size_t LBS_METADATA_BLOCK_SIZE = 65536;
// If true, forces a full write of metadata: will not include pointers to
// metadata in old snapshots.
bool flag_full_metadata = false;
/* TODO: Move to header file */
extern LocalDb *db;
/* Like strcmp, but sorts in the order that files will be visited in the
* filesystem. That is, we break paths apart at slashes, and compare path
* components separately. */
static int pathcmp(const char *path1, const char *path2)
{
/* Find the first component in each path. */
const char *slash1 = strchr(path1, '/');
const char *slash2 = strchr(path2, '/');
{
string comp1, comp2;
if (slash1 == NULL)
comp1 = path1;
else
comp1 = string(path1, slash1 - path1);
if (slash2 == NULL)
comp2 = path2;
else
comp2 = string(path2, slash2 - path2);
/* Directly compare the two components first. */
if (comp1 < comp2)
return -1;
if (comp1 > comp2)
return 1;
}
if (slash1 == NULL && slash2 == NULL)
return 0;
if (slash1 == NULL)
return -1;
if (slash2 == NULL)
return 1;
return pathcmp(slash1 + 1, slash2 + 1);
}
/* Encode a dictionary of string key/value pairs into a sequence of lines of
* the form "key: value". If it exists, the key "name" is treated specially
* and will be listed first. */
static string encode_dict(const map<string, string>& dict)
{
string result;
if (dict.find("name") != dict.end()) {
result += "name: " + dict.find("name")->second + "\n";
}
for (map<string, string>::const_iterator i = dict.begin();
i != dict.end(); ++i) {
if (i->first == "name")
continue;
result += i->first + ": " + i->second + "\n";
}
return result;
}
MetadataWriter::MetadataWriter(TarSegmentStore *store,
const char *path,
const char *snapshot_name,
const char *snapshot_scheme)
{
statcache_path = path;
statcache_path += "/statcache2";
if (snapshot_scheme != NULL && strlen(snapshot_scheme) > 0)
statcache_path = statcache_path + "-" + snapshot_scheme;
statcache_tmp_path = statcache_path + "." + snapshot_name;
statcache_in = fopen(statcache_path.c_str(), "r");
statcache_out = fopen(statcache_tmp_path.c_str(), "w");
if (statcache_out == NULL) {
fprintf(stderr, "Error opening statcache %s: %m\n",
statcache_tmp_path.c_str());
fatal("Error opening statcache");
}
old_metadata_eof = false;
this->store = store;
chunk_size = 0;
}
/* Read the next entry from the old statcache file, loading it into
* old_metadata. */
void MetadataWriter::read_statcache()
{
if (statcache_in == NULL) {
old_metadata_eof = true;
return;
}
old_metadata.clear();
char *buf = NULL;
size_t n = 0;
string field = ""; // Last field to be read in
/* Look for a first line starting with "@@", which tells where the metadata
* can be found in the metadata log of an old snapshot. */
if (getline(&buf, &n, statcache_in) < 0
|| buf == NULL || buf[0] != '@' || buf[1] != '@') {
old_metadata_eof = true;
return;
}
if (strchr(buf, '\n') != NULL)
*strchr(buf, '\n') = '\0';
old_metadata_loc = buf + 2;
/* After the initial line follows the metadata, as key-value pairs. */
while (!feof(statcache_in)) {
if (getline(&buf, &n, statcache_in) < 0)
break;
char *eol = strchr(buf, '\n');
if (eol != NULL)
*eol = '\0';
/* Is the line blank? If so, we have reached the end of this entry. */
if (buf[0] == '\0')
break;
/* Is this a continuation line? (Does it start with whitespace?) */
if (isspace(buf[0]) && field != "") {
old_metadata[field] += string("\n") + buf;
continue;
}
/* For lines of the form "Key: Value" look for ':' and split the line
* apart. */
char *value = strchr(buf, ':');
if (value == NULL)
continue;
*value = '\0';
field = buf;
value++;
while (isspace(*value))
value++;
old_metadata[field] = value;
}
if (feof(statcache_in) && old_metadata.size() == 0) {
old_metadata_eof = true;
}
free(buf);
}
bool MetadataWriter::find(const string& path)
{
const char *path_str = path.c_str();
while (!old_metadata_eof) {
string old_path = uri_decode(old_metadata["name"]);
int cmp = pathcmp(old_path.c_str(), path_str);
if (cmp == 0)
return true;
else if (cmp > 0)
return false;
else
read_statcache();
}
return false;
}
/* Does a file appear to be unchanged from the previous time it was backed up,
* based on stat information? */
bool MetadataWriter::is_unchanged(const struct stat *stat_buf)
{
if (old_metadata.find("volatile") != old_metadata.end()
&& parse_int(old_metadata["volatile"]) != 0)
return false;
if (old_metadata.find("ctime") == old_metadata.end())
return false;
if (stat_buf->st_ctime != parse_int(old_metadata["ctime"]))
return false;
if (old_metadata.find("mtime") == old_metadata.end())
return false;
if (stat_buf->st_mtime != parse_int(old_metadata["mtime"]))
return false;
if (old_metadata.find("size") == old_metadata.end())
return false;
if (stat_buf->st_size != parse_int(old_metadata["size"]))
return false;
if (old_metadata.find("inode") == old_metadata.end())
return false;
string inode = encode_int(major(stat_buf->st_dev))
+ "/" + encode_int(minor(stat_buf->st_dev))
+ "/" + encode_int(stat_buf->st_ino);
if (inode != old_metadata["inode"])
return false;
return true;
}
list<ObjectReference> MetadataWriter::get_blocks()
{
list<ObjectReference> blocks;
/* Parse the list of blocks. */
const char *s = old_metadata["data"].c_str();
while (*s != '\0') {
if (isspace(*s)) {
s++;
continue;
}
string ref = "";
while (*s != '\0' && !isspace(*s)) {
char buf[2];
buf[0] = *s;
buf[1] = '\0';
ref += buf;
s++;
}
ObjectReference r = ObjectReference::parse(ref);
if (!r.is_null())
blocks.push_back(r);
}
return blocks;
}
/* Ensure contents of metadata are flushed to an object. */
void MetadataWriter::metadata_flush()
{
int offset = 0;
ostringstream metadata;
ObjectReference indirect;
for (list<MetadataItem>::iterator i = items.begin();
i != items.end(); ++i) {
// If indirectly referencing any other metadata logs, be sure those
// segments are properly referenced.
if (i->reused) {
db->UseObject(i->ref);
}
// Write out an indirect reference to any previous objects which could
// be reused
if (!i->reused || !indirect.merge(i->ref)) {
if (!indirect.is_null()) {
string refstr = indirect.to_string();
metadata << "@" << refstr << "\n";
offset += refstr.size() + 2;
if (!i->reused) {
metadata << "\n";
offset += 1;
}
}
if (i->reused)
indirect = i->ref;
else
indirect = ObjectReference();
}
if (!i->reused) {
metadata << i->text;
i->offset = offset;
offset += i->text.size();
}
}
if (!indirect.is_null()) {
string refstr = indirect.to_string();
metadata << "@" << refstr << "\n";
offset += refstr.size() + 2;
indirect = ObjectReference();
}
string m = metadata.str();
if (m.size() == 0)
return;
/* Write current metadata information to a new object. */
LbsObject *meta = new LbsObject;
meta->set_group("metadata");
meta->set_data(m.data(), m.size(), NULL);
meta->write(store);
/* Write a reference to this block in the root. */
ObjectReference ref = meta->get_ref();
metadata_root << "@" << ref.to_string() << "\n";
db->UseObject(ref);
delete meta;
/* Write these files out to the statcache, and include a reference to where
* the metadata lives (so we can re-use it if it has not changed). */
for (list<MetadataItem>::const_iterator i = items.begin();
i != items.end(); ++i) {
ObjectReference r = ref;
r.set_range(i->offset, i->text.size());
if (i->reused)
r = i->ref;
string refstr = r.to_string();
fprintf(statcache_out, "@@%s\n%s", refstr.c_str(), i->text.c_str());
}
chunk_size = 0;
items.clear();
}
void MetadataWriter::add(dictionary info)
{
MetadataItem item;
item.offset = 0;
item.reused = false;
item.text += encode_dict(info) + "\n";
if (info == old_metadata && !flag_full_metadata) {
ObjectReference ref = ObjectReference::parse(old_metadata_loc);
if (!ref.is_null() && db->IsAvailable(ref)) {
item.reused = true;
item.ref = ref;
}
}
items.push_back(item);
chunk_size += item.text.size();
if (chunk_size > LBS_METADATA_BLOCK_SIZE)
metadata_flush();
}
ObjectReference MetadataWriter::close()
{
metadata_flush();
const string root_data = metadata_root.str();
LbsObject *root = new LbsObject;
root->set_group("metadata");
root->set_data(root_data.data(), root_data.size(), NULL);
root->write(store);
db->UseObject(root->get_ref());
ObjectReference ref = root->get_ref();
delete root;
fclose(statcache_out);
if (rename(statcache_tmp_path.c_str(), statcache_path.c_str()) < 0) {
fprintf(stderr, "Error renaming statcache from %s to %s: %m\n",
statcache_tmp_path.c_str(), statcache_path.c_str());
}
return ref;
}