1 /* Recursively descend the filesystem and visit each file. */
23 static SegmentStore *segment_store;
24 static OutputStream *info_dump = NULL;
26 static SegmentPartitioner *index_segment, *data_segment;
28 /* Buffer for holding a single block of data read from a file. */
29 static const int LBS_BLOCK_SIZE = 1024 * 1024;
30 static char *block_buf;
32 void scandir(const string& path);
34 /* Converts time to microseconds since the epoch. */
35 int64_t encode_time(time_t time)
37 return (int64_t)time * 1000000;
40 /* Read data from a file descriptor and return the amount of data read. A
41 * short read (less than the requested size) will only occur if end-of-file is
43 size_t file_read(int fd, char *buf, size_t maxlen)
45 size_t bytes_read = 0;
48 ssize_t res = read(fd, buf, maxlen);
52 throw IOException("file_read: error reading");
53 } else if (res == 0) {
65 /* Read the contents of a file (specified by an open file descriptor) and copy
66 * the data to the store. */
67 void dumpfile(int fd, dictionary &file_info)
73 if ((stat_buf.st_mode & S_IFMT) != S_IFREG) {
74 printf("file is no longer a regular file!\n");
78 /* The index data consists of a sequence of pointers to the data blocks
79 * that actually comprise the file data. This level of indirection is used
80 * so that the same data block can be used in multiple files, or multiple
81 * versions of the same file. */
82 struct uuid segment_uuid;
84 OutputStream *index_data = index_segment->new_object(&segment_uuid,
89 struct uuid block_segment_uuid;
92 size_t bytes = file_read(fd, block_buf, LBS_BLOCK_SIZE);
96 hash.process(block_buf, bytes);
97 OutputStream *block = data_segment->new_object(&block_segment_uuid,
99 block->write(block_buf, bytes);
100 index_data->write_uuid(block_segment_uuid);
101 index_data->write_u32(block_object_id);
106 file_info["sha1"] = string((const char *)hash.checksum(),
107 hash.checksum_size());
108 file_info["data"] = encode_objref(segment_uuid, object_id);
111 void scanfile(const string& path)
115 struct stat stat_buf;
119 // Set to true if the item is a directory and we should recursively scan
120 bool recurse = false;
122 dictionary file_info;
124 lstat(path.c_str(), &stat_buf);
126 printf("%s\n", path.c_str());
128 file_info["mode"] = encode_u16(stat_buf.st_mode & 07777);
129 file_info["atime"] = encode_u64(encode_time(stat_buf.st_atime));
130 file_info["ctime"] = encode_u64(encode_time(stat_buf.st_ctime));
131 file_info["mtime"] = encode_u64(encode_time(stat_buf.st_mtime));
132 file_info["user"] = encode_u32(stat_buf.st_uid);
133 file_info["group"] = encode_u32(stat_buf.st_gid);
137 switch (stat_buf.st_mode & S_IFMT) {
153 /* Use the reported file size to allocate a buffer large enough to read
154 * the symlink. Allocate slightly more space, so that we ask for more
155 * bytes than we expect and so check for truncation. */
156 buf = new char[stat_buf.st_size + 2];
157 len = readlink(path.c_str(), buf, stat_buf.st_size + 1);
159 printf("error reading symlink: %m\n");
160 } else if (len <= stat_buf.st_size) {
162 printf(" contents=%s\n", buf);
163 } else if (len > stat_buf.st_size) {
164 printf("error reading symlink: name truncated\n");
167 file_info["contents"] = buf;
174 /* Be paranoid when opening the file. We have no guarantee that the
175 * file was not replaced between the stat() call above and the open()
176 * call below, so we might not even be opening a regular file. That
177 * the file descriptor refers to a regular file is checked in
178 * dumpfile(). But we also supply flags to open to to guard against
179 * various conditions before we can perform that verification:
180 * - O_NOFOLLOW: in the event the file was replaced by a symlink
181 * - O_NONBLOCK: prevents open() from blocking if the file was
183 * We also add in O_NOATIME, since this may reduce disk writes (for
185 fd = open(path.c_str(), O_RDONLY|O_NOATIME|O_NOFOLLOW|O_NONBLOCK);
187 /* Drop the use of the O_NONBLOCK flag; we only wanted that for file
189 flags = fcntl(fd, F_GETFL);
190 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
192 file_info["size"] = encode_u64(stat_buf.st_size);
193 dumpfile(fd, file_info);
203 fprintf(stderr, "Unknown inode type: mode=%x\n", stat_buf.st_mode);
207 file_info["type"] = string(1, inode_type);
209 info_dump->write_string(path);
210 info_dump->write_dictionary(file_info);
212 // If we hit a directory, now that we've written the directory itself,
213 // recursively scan the directory.
218 void scandir(const string& path)
220 DIR *dir = opendir(path.c_str());
223 printf("Error: %m\n");
228 vector<string> contents;
229 while ((ent = readdir(dir)) != NULL) {
230 string filename(ent->d_name);
231 if (filename == "." || filename == "..")
233 contents.push_back(filename);
236 sort(contents.begin(), contents.end());
238 for (vector<string>::iterator i = contents.begin();
239 i != contents.end(); ++i) {
240 const string& filename = *i;
241 scanfile(path + "/" + filename);
247 int main(int argc, char *argv[])
249 block_buf = new char[LBS_BLOCK_SIZE];
251 segment_store = new SegmentStore(".");
252 SegmentWriter *sw = segment_store->new_segment();
253 info_dump = sw->new_object(NULL);
255 index_segment = new SegmentPartitioner(segment_store);
256 data_segment = new SegmentPartitioner(segment_store);
258 string uuid = SegmentWriter::format_uuid(sw->get_uuid());
259 printf("Backup UUID: %s\n", uuid.c_str());
263 } catch (IOException e) {
264 fprintf(stderr, "IOException: %s\n", e.getError().c_str());
267 delete index_segment;