1 /* Recursively descend the filesystem and visit each file. */
28 static SegmentStore *segment_store;
29 static OutputStream *info_dump = NULL;
31 static TarSegmentStore *tss = NULL;
33 static SegmentPartitioner *index_segment, *data_segment;
35 /* Buffer for holding a single block of data read from a file. */
36 static const int LBS_BLOCK_SIZE = 1024 * 1024;
37 static char *block_buf;
39 void scandir(const string& path, std::ostream& metadata);
41 /* Converts time to microseconds since the epoch. */
42 int64_t encode_time(time_t time)
44 return (int64_t)time * 1000000;
47 /* Read data from a file descriptor and return the amount of data read. A
48 * short read (less than the requested size) will only occur if end-of-file is
50 size_t file_read(int fd, char *buf, size_t maxlen)
52 size_t bytes_read = 0;
55 ssize_t res = read(fd, buf, maxlen);
59 throw IOException("file_read: error reading");
60 } else if (res == 0) {
72 /* Read the contents of a file (specified by an open file descriptor) and copy
73 * the data to the store. */
74 void dumpfile(int fd, dictionary &file_info, ostream &metadata)
79 string segment_list = "data:";
81 if ((stat_buf.st_mode & S_IFMT) != S_IFREG) {
82 printf("file is no longer a regular file!\n");
86 /* The index data consists of a sequence of pointers to the data blocks
87 * that actually comprise the file data. This level of indirection is used
88 * so that the same data block can be used in multiple files, or multiple
89 * versions of the same file. */
90 struct uuid segment_uuid;
92 OutputStream *index_data = index_segment->new_object(&segment_uuid,
98 struct uuid block_segment_uuid;
101 size_t bytes = file_read(fd, block_buf, LBS_BLOCK_SIZE);
105 hash.process(block_buf, bytes);
106 OutputStream *block = data_segment->new_object(&block_segment_uuid,
109 block->write(block_buf, bytes);
110 index_data->write_uuid(block_segment_uuid);
111 index_data->write_u32(block_object_id);
113 // tarstore processing
114 string blockid = tss->write_object(block_buf, bytes, "data");
115 segment_list += " " + blockid;
120 file_info["sha1"] = string((const char *)hash.checksum(),
121 hash.checksum_size());
122 file_info["data"] = encode_objref(segment_uuid, object_id);
124 metadata << segment_list << "\n";
127 void scanfile(const string& path, ostream &metadata)
131 struct stat stat_buf;
135 // Set to true if the item is a directory and we should recursively scan
136 bool recurse = false;
138 dictionary file_info;
140 lstat(path.c_str(), &stat_buf);
142 printf("%s\n", path.c_str());
144 metadata << "name: " << uri_encode(path) << "\n";
145 metadata << "mode: " << (stat_buf.st_mode & 07777) << "\n";
146 metadata << "atime: " << stat_buf.st_atime << "\n";
147 metadata << "ctime: " << stat_buf.st_ctime << "\n";
148 metadata << "mtime: " << stat_buf.st_mtime << "\n";
149 metadata << "user: " << stat_buf.st_uid << "\n";
150 metadata << "group: " << stat_buf.st_gid << "\n";
152 file_info["mode"] = encode_u16(stat_buf.st_mode & 07777);
153 file_info["atime"] = encode_u64(encode_time(stat_buf.st_atime));
154 file_info["ctime"] = encode_u64(encode_time(stat_buf.st_ctime));
155 file_info["mtime"] = encode_u64(encode_time(stat_buf.st_mtime));
156 file_info["user"] = encode_u32(stat_buf.st_uid);
157 file_info["group"] = encode_u32(stat_buf.st_gid);
161 switch (stat_buf.st_mode & S_IFMT) {
177 /* Use the reported file size to allocate a buffer large enough to read
178 * the symlink. Allocate slightly more space, so that we ask for more
179 * bytes than we expect and so check for truncation. */
180 buf = new char[stat_buf.st_size + 2];
181 len = readlink(path.c_str(), buf, stat_buf.st_size + 1);
183 printf("error reading symlink: %m\n");
184 } else if (len <= stat_buf.st_size) {
186 printf(" contents=%s\n", buf);
187 } else if (len > stat_buf.st_size) {
188 printf("error reading symlink: name truncated\n");
191 file_info["contents"] = buf;
198 /* Be paranoid when opening the file. We have no guarantee that the
199 * file was not replaced between the stat() call above and the open()
200 * call below, so we might not even be opening a regular file. That
201 * the file descriptor refers to a regular file is checked in
202 * dumpfile(). But we also supply flags to open to to guard against
203 * various conditions before we can perform that verification:
204 * - O_NOFOLLOW: in the event the file was replaced by a symlink
205 * - O_NONBLOCK: prevents open() from blocking if the file was
207 * We also add in O_NOATIME, since this may reduce disk writes (for
209 fd = open(path.c_str(), O_RDONLY|O_NOATIME|O_NOFOLLOW|O_NONBLOCK);
211 /* Drop the use of the O_NONBLOCK flag; we only wanted that for file
213 flags = fcntl(fd, F_GETFL);
214 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
216 file_info["size"] = encode_u64(stat_buf.st_size);
217 dumpfile(fd, file_info, metadata);
227 fprintf(stderr, "Unknown inode type: mode=%x\n", stat_buf.st_mode);
231 file_info["type"] = string(1, inode_type);
232 metadata << "type: " << inode_type << "\n";
234 info_dump->write_string(path);
235 info_dump->write_dictionary(file_info);
239 // If we hit a directory, now that we've written the directory itself,
240 // recursively scan the directory.
242 scandir(path, metadata);
245 void scandir(const string& path, ostream &metadata)
247 DIR *dir = opendir(path.c_str());
250 printf("Error: %m\n");
255 vector<string> contents;
256 while ((ent = readdir(dir)) != NULL) {
257 string filename(ent->d_name);
258 if (filename == "." || filename == "..")
260 contents.push_back(filename);
263 sort(contents.begin(), contents.end());
265 for (vector<string>::iterator i = contents.begin();
266 i != contents.end(); ++i) {
267 const string& filename = *i;
268 scanfile(path + "/" + filename, metadata);
274 int main(int argc, char *argv[])
276 block_buf = new char[LBS_BLOCK_SIZE];
278 tss = new TarSegmentStore(".");
279 segment_store = new SegmentStore(".");
280 SegmentWriter *sw = segment_store->new_segment();
281 info_dump = sw->new_object(NULL, "ROOT");
283 index_segment = new SegmentPartitioner(segment_store);
284 data_segment = new SegmentPartitioner(segment_store);
286 string uuid = SegmentWriter::format_uuid(sw->get_uuid());
287 printf("Backup UUID: %s\n", uuid.c_str());
289 std::ostringstream metadata;
292 scanfile(".", metadata);
293 } catch (IOException e) {
294 fprintf(stderr, "IOException: %s\n", e.getError().c_str());
297 const string md = metadata.str();
298 string root = tss->write_object(md.data(), md.size(), "root");
300 fprintf(stderr, "Metadata root is at %s\n", root.c_str());
305 delete index_segment;