1 /* LBS: An LFS-inspired filesystem backup system
2 * Copyright (C) 2007 Michael Vrable
4 * Handling of metadata written to backup snapshots. This manages the writing
5 * of file metadata into new backup snapshots, including breaking the metadata
6 * log apart across separate objects. Eventually this should include unified
7 * handling of the statcache, and re-use of metadata between snapshots.
24 using std::ostringstream;
26 static const size_t LBS_METADATA_BLOCK_SIZE = 65536;
28 // If true, forces a full write of metadata: will not include pointers to
29 // metadata in old snapshots.
30 bool flag_full_metadata = false;
32 /* TODO: Move to header file */
34 void add_segment(const string& segment);
36 /* Like strcmp, but sorts in the order that files will be visited in the
37 * filesystem. That is, we break paths apart at slashes, and compare path
38 * components separately. */
39 static int pathcmp(const char *path1, const char *path2)
41 /* Find the first component in each path. */
42 const char *slash1 = strchr(path1, '/');
43 const char *slash2 = strchr(path2, '/');
50 comp1 = string(path1, slash1 - path1);
55 comp2 = string(path2, slash2 - path2);
57 /* Directly compare the two components first. */
64 if (slash1 == NULL && slash2 == NULL)
71 return pathcmp(slash1 + 1, slash2 + 1);
74 /* Encode a dictionary of string key/value pairs into a sequence of lines of
75 * the form "key: value". If it exists, the key "name" is treated specially
76 * and will be listed first. */
77 static string encode_dict(const map<string, string>& dict)
81 if (dict.find("name") != dict.end()) {
82 result += "name: " + dict.at("name") + "\n";
85 for (map<string, string>::const_iterator i = dict.begin();
86 i != dict.end(); ++i) {
87 if (i->first == "name")
89 result += i->first + ": " + i->second + "\n";
95 MetadataWriter::MetadataWriter(TarSegmentStore *store,
97 const char *snapshot_name,
98 const char *snapshot_scheme)
100 statcache_path = path;
101 statcache_path += "/statcache2";
102 if (snapshot_scheme != NULL)
103 statcache_path = statcache_path + "-" + snapshot_scheme;
104 statcache_tmp_path = statcache_path + "." + snapshot_name;
106 statcache_in = fopen(statcache_path.c_str(), "r");
108 statcache_out = fopen(statcache_tmp_path.c_str(), "w");
109 if (statcache_out == NULL) {
110 fprintf(stderr, "Error opening statcache %s: %m\n",
111 statcache_tmp_path.c_str());
112 throw IOException("Error opening statcache");
115 old_metadata_eof = false;
121 /* Read the next entry from the old statcache file, loading it into
123 void MetadataWriter::read_statcache()
125 if (statcache_in == NULL) {
126 old_metadata_eof = true;
130 old_metadata.clear();
134 string field = ""; // Last field to be read in
136 /* Look for a first line starting with "@@", which tells where the metadata
137 * can be found in the metadata log of an old snapshot. */
138 if (getline(&buf, &n, statcache_in) < 0
139 || buf == NULL || buf[0] != '@' || buf[1] != '@') {
140 old_metadata_eof = true;
144 if (strchr(buf, '\n') != NULL)
145 *strchr(buf, '\n') = '\0';
146 old_metadata_loc = buf + 2;
148 /* After the initial line follows the metadata, as key-value pairs. */
149 while (!feof(statcache_in)) {
150 if (getline(&buf, &n, statcache_in) < 0)
153 char *eol = strchr(buf, '\n');
157 /* Is the line blank? If so, we have reached the end of this entry. */
161 /* Is this a continuation line? (Does it start with whitespace?) */
162 if (isspace(buf[0]) && field != "") {
163 old_metadata[field] += string("\n") + buf;
167 /* For lines of the form "Key: Value" look for ':' and split the line
169 char *value = strchr(buf, ':');
176 while (isspace(*value))
179 old_metadata[field] = value;
182 if (feof(statcache_in) && old_metadata.size() == 0) {
183 old_metadata_eof = true;
189 bool MetadataWriter::find(const string& path)
191 const char *path_str = path.c_str();
192 while (!old_metadata_eof) {
193 string old_path = uri_decode(old_metadata["name"]);
194 int cmp = pathcmp(old_path.c_str(), path_str);
206 /* Does a file appear to be unchanged from the previous time it was backed up,
207 * based on stat information? */
208 bool MetadataWriter::is_unchanged(const struct stat *stat_buf)
210 if (old_metadata.find("volatile") != old_metadata.end()
211 && parse_int(old_metadata["volatile"]) != 0)
214 if (old_metadata.find("ctime") == old_metadata.end())
216 if (stat_buf->st_ctime != parse_int(old_metadata["ctime"]))
219 if (old_metadata.find("mtime") == old_metadata.end())
221 if (stat_buf->st_mtime != parse_int(old_metadata["mtime"]))
224 if (old_metadata.find("size") == old_metadata.end())
226 if (stat_buf->st_size != parse_int(old_metadata["size"]))
229 if (old_metadata.find("inode") == old_metadata.end())
231 string inode = encode_int(major(stat_buf->st_dev))
232 + "/" + encode_int(minor(stat_buf->st_dev))
233 + "/" + encode_int(stat_buf->st_ino);
234 if (inode != old_metadata["inode"])
240 list<ObjectReference> MetadataWriter::get_blocks()
242 list<ObjectReference> blocks;
244 /* Parse the list of blocks. */
245 const char *s = old_metadata["data"].c_str();
253 while (*s != '\0' && !isspace(*s)) {
261 ObjectReference r = ObjectReference::parse(ref);
269 /* Ensure contents of metadata are flushed to an object. */
270 void MetadataWriter::metadata_flush()
274 ostringstream metadata;
275 ObjectReference indirect;
276 for (list<MetadataItem>::iterator i = items.begin();
277 i != items.end(); ++i) {
278 // If indirectly referencing any other metadata logs, be sure those
279 // segments are properly referenced.
281 add_segment(i->ref.get_segment());
282 db->UseSegment(i->ref.get_segment(), 1.0);
285 // Write out an indirect reference to any previous objects which could
287 if (!i->reused || !indirect.merge(i->ref)) {
288 if (!indirect.is_null()) {
289 string refstr = indirect.to_string();
290 metadata << "@" << refstr << "\n";
291 offset += refstr.size() + 2;
300 indirect = ObjectReference();
306 offset += i->text.size();
309 if (!indirect.is_null()) {
310 string refstr = indirect.to_string();
311 metadata << "@" << refstr << "\n";
312 offset += refstr.size() + 2;
313 indirect = ObjectReference();
316 string m = metadata.str();
320 /* Write current metadata information to a new object. */
321 LbsObject *meta = new LbsObject;
322 meta->set_group("metadata");
323 meta->set_data(m.data(), m.size());
327 /* Write a reference to this block in the root. */
328 ObjectReference ref = meta->get_ref();
329 metadata_root << "@" << ref.to_string() << "\n";
330 add_segment(ref.get_segment());
331 db->UseSegment(ref.get_segment(), 1.0);
335 /* Write these files out to the statcache, and include a reference to where
336 * the metadata lives (so we can re-use it if it has not changed). */
337 for (list<MetadataItem>::const_iterator i = items.begin();
338 i != items.end(); ++i) {
339 ObjectReference r = ref;
340 r.set_range(i->offset, i->text.size());
345 string refstr = r.to_string();
346 fprintf(statcache_out, "@@%s\n%s", refstr.c_str(), i->text.c_str());
353 void MetadataWriter::add(dictionary info)
358 item.text += encode_dict(info) + "\n";
360 if (info == old_metadata && !flag_full_metadata) {
361 ObjectReference ref = ObjectReference::parse(old_metadata_loc);
362 if (!ref.is_null()) {
368 items.push_back(item);
369 chunk_size += item.text.size();
371 if (chunk_size > LBS_METADATA_BLOCK_SIZE)
375 ObjectReference MetadataWriter::close()
378 const string root_data = metadata_root.str();
380 LbsObject *root = new LbsObject;
381 root->set_group("metadata");
382 root->set_data(root_data.data(), root_data.size());
385 add_segment(root->get_ref().get_segment());
386 db->UseSegment(root->get_ref().get_segment(), 1.0);
388 ObjectReference ref = root->get_ref();
391 fclose(statcache_out);
392 if (rename(statcache_tmp_path.c_str(), statcache_path.c_str()) < 0) {
393 fprintf(stderr, "Error renaming statcache from %s to %s: %m\n",
394 statcache_tmp_path.c_str(), statcache_path.c_str());