1 /* Recursively descend the filesystem and visit each file. */
27 static SegmentStore *segment_store;
28 static OutputStream *info_dump = NULL;
30 static TarSegmentStore *tss = NULL;
32 static SegmentPartitioner *index_segment, *data_segment;
34 /* Buffer for holding a single block of data read from a file. */
35 static const int LBS_BLOCK_SIZE = 1024 * 1024;
36 static char *block_buf;
38 void scandir(const string& path, std::ostream& metadata);
40 /* Converts time to microseconds since the epoch. */
41 int64_t encode_time(time_t time)
43 return (int64_t)time * 1000000;
46 /* Read data from a file descriptor and return the amount of data read. A
47 * short read (less than the requested size) will only occur if end-of-file is
49 size_t file_read(int fd, char *buf, size_t maxlen)
51 size_t bytes_read = 0;
54 ssize_t res = read(fd, buf, maxlen);
58 throw IOException("file_read: error reading");
59 } else if (res == 0) {
71 /* Read the contents of a file (specified by an open file descriptor) and copy
72 * the data to the store. */
73 void dumpfile(int fd, dictionary &file_info, ostream &metadata)
78 string segment_list = "data:";
80 if ((stat_buf.st_mode & S_IFMT) != S_IFREG) {
81 printf("file is no longer a regular file!\n");
85 /* The index data consists of a sequence of pointers to the data blocks
86 * that actually comprise the file data. This level of indirection is used
87 * so that the same data block can be used in multiple files, or multiple
88 * versions of the same file. */
89 struct uuid segment_uuid;
91 OutputStream *index_data = index_segment->new_object(&segment_uuid,
97 struct uuid block_segment_uuid;
100 size_t bytes = file_read(fd, block_buf, LBS_BLOCK_SIZE);
104 hash.process(block_buf, bytes);
105 OutputStream *block = data_segment->new_object(&block_segment_uuid,
108 block->write(block_buf, bytes);
109 index_data->write_uuid(block_segment_uuid);
110 index_data->write_u32(block_object_id);
112 // tarstore processing
113 string blockid = tss->write_object(block_buf, bytes, "data");
114 segment_list += " " + blockid;
119 file_info["sha1"] = string((const char *)hash.checksum(),
120 hash.checksum_size());
121 file_info["data"] = encode_objref(segment_uuid, object_id);
123 metadata << segment_list << "\n";
126 void scanfile(const string& path, ostream &metadata)
130 struct stat stat_buf;
134 // Set to true if the item is a directory and we should recursively scan
135 bool recurse = false;
137 dictionary file_info;
139 lstat(path.c_str(), &stat_buf);
141 printf("%s\n", path.c_str());
143 metadata << "name: " << path << "\n";
144 metadata << "mode: " << (stat_buf.st_mode & 07777) << "\n";
145 metadata << "atime: " << stat_buf.st_atime << "\n";
146 metadata << "ctime: " << stat_buf.st_ctime << "\n";
147 metadata << "mtime: " << stat_buf.st_mtime << "\n";
148 metadata << "user: " << stat_buf.st_uid << "\n";
149 metadata << "group: " << stat_buf.st_gid << "\n";
151 file_info["mode"] = encode_u16(stat_buf.st_mode & 07777);
152 file_info["atime"] = encode_u64(encode_time(stat_buf.st_atime));
153 file_info["ctime"] = encode_u64(encode_time(stat_buf.st_ctime));
154 file_info["mtime"] = encode_u64(encode_time(stat_buf.st_mtime));
155 file_info["user"] = encode_u32(stat_buf.st_uid);
156 file_info["group"] = encode_u32(stat_buf.st_gid);
160 switch (stat_buf.st_mode & S_IFMT) {
176 /* Use the reported file size to allocate a buffer large enough to read
177 * the symlink. Allocate slightly more space, so that we ask for more
178 * bytes than we expect and so check for truncation. */
179 buf = new char[stat_buf.st_size + 2];
180 len = readlink(path.c_str(), buf, stat_buf.st_size + 1);
182 printf("error reading symlink: %m\n");
183 } else if (len <= stat_buf.st_size) {
185 printf(" contents=%s\n", buf);
186 } else if (len > stat_buf.st_size) {
187 printf("error reading symlink: name truncated\n");
190 file_info["contents"] = buf;
197 /* Be paranoid when opening the file. We have no guarantee that the
198 * file was not replaced between the stat() call above and the open()
199 * call below, so we might not even be opening a regular file. That
200 * the file descriptor refers to a regular file is checked in
201 * dumpfile(). But we also supply flags to open to to guard against
202 * various conditions before we can perform that verification:
203 * - O_NOFOLLOW: in the event the file was replaced by a symlink
204 * - O_NONBLOCK: prevents open() from blocking if the file was
206 * We also add in O_NOATIME, since this may reduce disk writes (for
208 fd = open(path.c_str(), O_RDONLY|O_NOATIME|O_NOFOLLOW|O_NONBLOCK);
210 /* Drop the use of the O_NONBLOCK flag; we only wanted that for file
212 flags = fcntl(fd, F_GETFL);
213 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
215 file_info["size"] = encode_u64(stat_buf.st_size);
216 dumpfile(fd, file_info, metadata);
226 fprintf(stderr, "Unknown inode type: mode=%x\n", stat_buf.st_mode);
230 file_info["type"] = string(1, inode_type);
231 metadata << "type: " << inode_type << "\n";
233 info_dump->write_string(path);
234 info_dump->write_dictionary(file_info);
238 // If we hit a directory, now that we've written the directory itself,
239 // recursively scan the directory.
241 scandir(path, metadata);
244 void scandir(const string& path, ostream &metadata)
246 DIR *dir = opendir(path.c_str());
249 printf("Error: %m\n");
254 vector<string> contents;
255 while ((ent = readdir(dir)) != NULL) {
256 string filename(ent->d_name);
257 if (filename == "." || filename == "..")
259 contents.push_back(filename);
262 sort(contents.begin(), contents.end());
264 for (vector<string>::iterator i = contents.begin();
265 i != contents.end(); ++i) {
266 const string& filename = *i;
267 scanfile(path + "/" + filename, metadata);
273 int main(int argc, char *argv[])
275 block_buf = new char[LBS_BLOCK_SIZE];
277 tss = new TarSegmentStore(".");
278 segment_store = new SegmentStore(".");
279 SegmentWriter *sw = segment_store->new_segment();
280 info_dump = sw->new_object(NULL, "ROOT");
282 index_segment = new SegmentPartitioner(segment_store);
283 data_segment = new SegmentPartitioner(segment_store);
285 string uuid = SegmentWriter::format_uuid(sw->get_uuid());
286 printf("Backup UUID: %s\n", uuid.c_str());
288 std::ostringstream metadata;
291 scanfile(".", metadata);
292 } catch (IOException e) {
293 fprintf(stderr, "IOException: %s\n", e.getError().c_str());
296 const char testdata[] = "Test string.";
297 tss->write_object(testdata, strlen(testdata));
298 tss->write_object(testdata, strlen(testdata));
299 tss->write_object(testdata, strlen(testdata));
301 const string md = metadata.str();
302 string root = tss->write_object(md.data(), md.size(), "root");
304 fprintf(stderr, "Metadata root is at %s\n", root.c_str());
309 delete index_segment;