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.
21 using std::ostringstream;
23 static const size_t LBS_METADATA_BLOCK_SIZE = 65536;
25 /* TODO: Move to header file */
26 void add_segment(const string& segment);
28 /* Like strcmp, but sorts in the order that files will be visited in the
29 * filesystem. That is, we break paths apart at slashes, and compare path
30 * components separately. */
31 static int pathcmp(const char *path1, const char *path2)
33 /* Find the first component in each path. */
34 const char *slash1 = strchr(path1, '/');
35 const char *slash2 = strchr(path2, '/');
42 comp1 = string(path1, slash1 - path1);
47 comp2 = string(path2, slash2 - path2);
49 /* Directly compare the two components first. */
56 if (slash1 == NULL && slash2 == NULL)
63 return pathcmp(slash1 + 1, slash2 + 1);
66 MetadataWriter::MetadataWriter(TarSegmentStore *store,
68 const char *snapshot_name,
69 const char *snapshot_scheme)
71 statcache_path = path;
72 statcache_path += "/statcache2";
73 if (snapshot_scheme != NULL)
74 statcache_path = statcache_path + "-" + snapshot_scheme;
75 statcache_tmp_path = statcache_path + "." + snapshot_name;
77 statcache_in = fopen(statcache_path.c_str(), "r");
79 statcache_out = fopen(statcache_tmp_path.c_str(), "w");
80 if (statcache_out == NULL) {
81 fprintf(stderr, "Error opening statcache %s: %m\n",
82 statcache_tmp_path.c_str());
83 throw IOException("Error opening statcache");
87 old_metadata_eof = false;
93 /* Read the next entry from the old statcache file, loading it into
95 void MetadataWriter::read_statcache()
97 if (statcache_in == NULL) {
98 old_metadata_eof = true;
102 old_metadata.clear();
106 string field = ""; // Last field to be read in
108 /* Look for a first line starting with "@@", which tells where the metadata
109 * can be found in the metadata log of an old snapshot. */
110 if (getline(&buf, &n, statcache_in) < 0
111 || buf == NULL || buf[0] != '@' || buf[1] != '@') {
112 old_metadata_eof = true;
116 if (strchr(buf, '\n') != NULL)
117 *strchr(buf, '\n') = '\0';
118 old_metadata_loc = buf + 2;
120 /* After the initial line follows the metadata, as key-value pairs. */
121 while (!feof(statcache_in)) {
122 if (getline(&buf, &n, statcache_in) < 0)
125 char *eol = strchr(buf, '\n');
129 /* Is the line blank? If so, we have reached the end of this entry. */
133 /* Is this a continuation line? (Does it start with whitespace?) */
134 if (isspace(buf[0]) && field != "") {
135 old_metadata[field] += string("\n") + buf;
139 /* For lines of the form "Key: Value" look for ':' and split the line
141 char *value = strchr(buf, ':');
148 while (isspace(*value))
151 old_metadata[field] = value;
154 if (feof(statcache_in) && old_metadata.size() == 0) {
155 old_metadata_eof = true;
161 bool MetadataWriter::find(const string& path)
163 const char *path_str = path.c_str();
164 while (!old_metadata_eof) {
165 string old_path = uri_decode(old_metadata["name"]);
166 int cmp = pathcmp(old_path.c_str(), path_str);
170 } else if (cmp > 0) {
182 /* Does a file appear to be unchanged from the previous time it was backed up,
183 * based on stat information?
185 * TODO: Notice files that were modified as they were being backed up the last
187 bool MetadataWriter::is_unchanged(const struct stat *stat_buf)
189 if (old_metadata.find("ctime") == old_metadata.end())
191 if (stat_buf->st_ctime != parse_int(old_metadata["ctime"]))
194 if (old_metadata.find("mtime") == old_metadata.end())
196 if (stat_buf->st_mtime != parse_int(old_metadata["mtime"]))
199 if (old_metadata.find("size") == old_metadata.end())
201 if (stat_buf->st_size != parse_int(old_metadata["size"]))
204 if (old_metadata.find("inode") == old_metadata.end())
206 string inode = encode_int(major(stat_buf->st_dev))
207 + "/" + encode_int(minor(stat_buf->st_dev))
208 + "/" + encode_int(stat_buf->st_ino);
209 if (inode != old_metadata["inode"])
215 list<ObjectReference> MetadataWriter::get_blocks()
217 list<ObjectReference> blocks;
219 /* Parse the list of blocks. */
220 const char *s = old_metadata["data"].c_str();
228 while (*s != '\0' && !isspace(*s)) {
236 ObjectReference *r = ObjectReference::parse(ref);
238 blocks.push_back(*r);
246 /* Ensure contents of metadata are flushed to an object. */
247 void MetadataWriter::metadata_flush()
251 ostringstream metadata;
252 ObjectReference indirect;
253 for (list<MetadataItem>::iterator i = items.begin();
254 i != items.end(); ++i) {
255 // Write out an indirect reference to any previous objects which could
257 if (!i->reused || !indirect.merge(i->ref)) {
258 if (!indirect.is_null()) {
259 string refstr = indirect.to_string();
260 metadata << "@" << refstr << "\n";
261 offset += refstr.size() + 2;
270 indirect = ObjectReference();
276 offset += i->text.size();
279 if (!indirect.is_null()) {
280 string refstr = indirect.to_string();
281 metadata << "@" << refstr << "\n";
282 offset += refstr.size() + 2;
283 indirect = ObjectReference();
286 string m = metadata.str();
290 /* Write current metadata information to a new object. */
291 LbsObject *meta = new LbsObject;
292 meta->set_group("metadata");
293 meta->set_data(m.data(), m.size());
297 /* Write a reference to this block in the root. */
298 ObjectReference ref = meta->get_ref();
299 metadata_root << "@" << ref.to_string() << "\n";
300 add_segment(ref.get_segment());
304 /* Write these files out to the statcache, and include a reference to where
305 * the metadata lives (so we can re-use it if it has not changed). */
306 for (list<MetadataItem>::const_iterator i = items.begin();
307 i != items.end(); ++i) {
308 ObjectReference r = ref;
309 r.set_range(i->offset, i->text.size());
314 string refstr = r.to_string();
315 fprintf(statcache_out, "@@%s\n%s", refstr.c_str(), i->text.c_str());
322 void MetadataWriter::add(dictionary info)
327 item.text += encode_dict(info) + "\n";
329 if (info == old_metadata) {
330 ObjectReference *ref = ObjectReference::parse(old_metadata_loc);
338 items.push_back(item);
339 chunk_size += item.text.size();
341 if (chunk_size > LBS_METADATA_BLOCK_SIZE)
345 ObjectReference MetadataWriter::close()
348 const string root_data = metadata_root.str();
350 LbsObject *root = new LbsObject;
351 root->set_group("metadata");
352 root->set_data(root_data.data(), root_data.size());
355 add_segment(root->get_ref().get_segment());
357 ObjectReference ref = root->get_ref();
360 fclose(statcache_out);
361 if (rename(statcache_tmp_path.c_str(), statcache_path.c_str()) < 0) {
362 fprintf(stderr, "Error renaming statcache from %s to %s: %m\n",
363 statcache_tmp_path.c_str(), statcache_path.c_str());