1 /* Recursively descend the filesystem and visit each file. */
23 static OutputStream *info_dump = NULL;
25 void scandir(const string& path);
27 /* Converts time to microseconds since the epoch. */
28 int64_t encode_time(time_t time)
30 return (int64_t)time * 1000000;
33 void dumpfile(int fd, dictionary &file_info)
41 if ((stat_buf.st_mode & S_IFMT) != S_IFREG) {
42 printf("file is no longer a regular file!\n");
48 ssize_t res = read(fd, buf, sizeof(buf));
52 printf("Error while reading: %m\n");
54 } else if (res == 0) {
57 hash.process(buf, res);
62 file_info["sha1"] = string((const char *)hash.checksum(),
63 hash.checksum_size());
66 void scanfile(const string& path)
74 // Set to true if the item is a directory and we should recursively scan
79 lstat(path.c_str(), &stat_buf);
81 printf("%s\n", path.c_str());
83 file_info["mode"] = encode_u16(stat_buf.st_mode & 07777);
84 file_info["atime"] = encode_u64(encode_time(stat_buf.st_atime));
85 file_info["ctime"] = encode_u64(encode_time(stat_buf.st_ctime));
86 file_info["mtime"] = encode_u64(encode_time(stat_buf.st_mtime));
87 file_info["user"] = encode_u32(stat_buf.st_uid);
88 file_info["group"] = encode_u32(stat_buf.st_gid);
92 switch (stat_buf.st_mode & S_IFMT) {
108 /* Use the reported file size to allocate a buffer large enough to read
109 * the symlink. Allocate slightly more space, so that we ask for more
110 * bytes than we expect and so check for truncation. */
111 buf = new char[stat_buf.st_size + 2];
112 len = readlink(path.c_str(), buf, stat_buf.st_size + 1);
114 printf("error reading symlink: %m\n");
115 } else if (len <= stat_buf.st_size) {
117 printf(" contents=%s\n", buf);
118 } else if (len > stat_buf.st_size) {
119 printf("error reading symlink: name truncated\n");
122 file_info["contents"] = buf;
129 /* Be paranoid when opening the file. We have no guarantee that the
130 * file was not replaced between the stat() call above and the open()
131 * call below, so we might not even be opening a regular file. That
132 * the file descriptor refers to a regular file is checked in
133 * dumpfile(). But we also supply flags to open to to guard against
134 * various conditions before we can perform that verification:
135 * - O_NOFOLLOW: in the event the file was replaced by a symlink
136 * - O_NONBLOCK: prevents open() from blocking if the file was
138 * We also add in O_NOATIME, since this may reduce disk writes (for
140 fd = open(path.c_str(), O_RDONLY|O_NOATIME|O_NOFOLLOW|O_NONBLOCK);
142 /* Drop the use of the O_NONBLOCK flag; we only wanted that for file
144 flags = fcntl(fd, F_GETFL);
145 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
147 file_info["size"] = encode_u64(stat_buf.st_size);
148 dumpfile(fd, file_info);
158 fprintf(stderr, "Unknown inode type: mode=%x\n", stat_buf.st_mode);
162 file_info["type"] = string(1, inode_type);
164 info_dump->write_string(path);
165 info_dump->write_dictionary(file_info);
167 // If we hit a directory, now that we've written the directory itself,
168 // recursively scan the directory.
173 void scandir(const string& path)
175 DIR *dir = opendir(path.c_str());
178 printf("Error: %m\n");
183 vector<string> contents;
184 while ((ent = readdir(dir)) != NULL) {
185 string filename(ent->d_name);
186 if (filename == "." || filename == "..")
188 contents.push_back(filename);
191 sort(contents.begin(), contents.end());
193 for (vector<string>::iterator i = contents.begin();
194 i != contents.end(); ++i) {
195 const string& filename = *i;
196 scanfile(path + "/" + filename);
202 int main(int argc, char *argv[])
204 struct uuid id = SegmentWriter::generate_uuid();
205 string filename = SegmentWriter::format_uuid(id);
207 printf("Backup UUID: %s\n", filename.c_str());
208 FILE *dump = fopen(filename.c_str(), "w");
210 fprintf(stderr, "Cannot open file %s: %m\n", filename.c_str());
214 FileOutputStream os(dump);
215 SegmentWriter sw(os, id);
216 info_dump = sw.new_object();
220 } catch (IOException e) {
221 fprintf(stderr, "IOException: %s\n", e.getError().c_str());