1 /* Recursively descend the filesystem and visit each file. */
28 static TarSegmentStore *tss = NULL;
30 /* Buffer for holding a single block of data read from a file. */
31 static const int LBS_BLOCK_SIZE = 1024 * 1024;
32 static char *block_buf;
34 void scandir(const string& path, std::ostream& metadata);
36 /* Converts time to microseconds since the epoch. */
37 int64_t encode_time(time_t time)
39 return (int64_t)time * 1000000;
42 /* Read data from a file descriptor and return the amount of data read. A
43 * short read (less than the requested size) will only occur if end-of-file is
45 size_t file_read(int fd, char *buf, size_t maxlen)
47 size_t bytes_read = 0;
50 ssize_t res = read(fd, buf, maxlen);
54 throw IOException("file_read: error reading");
55 } else if (res == 0) {
67 /* Read the contents of a file (specified by an open file descriptor) and copy
68 * the data to the store. */
69 void dumpfile(int fd, dictionary &file_info, ostream &metadata)
74 string segment_list = "";
76 if ((stat_buf.st_mode & S_IFMT) != S_IFREG) {
77 printf("file is no longer a regular file!\n");
81 /* The index data consists of a sequence of pointers to the data blocks
82 * that actually comprise the file data. This level of indirection is used
83 * so that the same data block can be used in multiple files, or multiple
84 * versions of the same file. */
87 size_t bytes = file_read(fd, block_buf, LBS_BLOCK_SIZE);
91 hash.process(block_buf, bytes);
93 // tarstore processing
94 string blockid = tss->write_object(block_buf, bytes, "data");
95 if (segment_list.size() > 0)
97 segment_list += blockid;
102 file_info["checksum"] = hash.checksum_str();
103 file_info["data"] = segment_list;
106 void scanfile(const string& path, ostream &metadata)
110 struct stat stat_buf;
114 // Set to true if the item is a directory and we should recursively scan
115 bool recurse = false;
117 dictionary file_info;
119 lstat(path.c_str(), &stat_buf);
121 printf("%s\n", path.c_str());
123 metadata << "name: " << uri_encode(path) << "\n";
125 file_info["mode"] = encode_int(stat_buf.st_mode & 07777);
126 file_info["atime"] = encode_int(stat_buf.st_atime);
127 file_info["ctime"] = encode_int(stat_buf.st_ctime);
128 file_info["mtime"] = encode_int(stat_buf.st_mtime);
129 file_info["user"] = encode_int(stat_buf.st_uid);
130 file_info["group"] = encode_int(stat_buf.st_gid);
134 switch (stat_buf.st_mode & S_IFMT) {
150 /* Use the reported file size to allocate a buffer large enough to read
151 * the symlink. Allocate slightly more space, so that we ask for more
152 * bytes than we expect and so check for truncation. */
153 buf = new char[stat_buf.st_size + 2];
154 len = readlink(path.c_str(), buf, stat_buf.st_size + 1);
156 printf("error reading symlink: %m\n");
157 } else if (len <= stat_buf.st_size) {
159 printf(" contents=%s\n", buf);
160 } else if (len > stat_buf.st_size) {
161 printf("error reading symlink: name truncated\n");
164 file_info["contents"] = uri_encode(buf);
171 /* Be paranoid when opening the file. We have no guarantee that the
172 * file was not replaced between the stat() call above and the open()
173 * call below, so we might not even be opening a regular file. That
174 * the file descriptor refers to a regular file is checked in
175 * dumpfile(). But we also supply flags to open to to guard against
176 * various conditions before we can perform that verification:
177 * - O_NOFOLLOW: in the event the file was replaced by a symlink
178 * - O_NONBLOCK: prevents open() from blocking if the file was
180 * We also add in O_NOATIME, since this may reduce disk writes (for
182 fd = open(path.c_str(), O_RDONLY|O_NOATIME|O_NOFOLLOW|O_NONBLOCK);
184 /* Drop the use of the O_NONBLOCK flag; we only wanted that for file
186 flags = fcntl(fd, F_GETFL);
187 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
189 file_info["size"] = encode_int(stat_buf.st_size);
190 dumpfile(fd, file_info, metadata);
200 fprintf(stderr, "Unknown inode type: mode=%x\n", stat_buf.st_mode);
204 file_info["type"] = string(1, inode_type);
205 metadata << "type: " << inode_type << "\n";
207 dict_output(metadata, file_info);
210 // If we hit a directory, now that we've written the directory itself,
211 // recursively scan the directory.
213 scandir(path, metadata);
216 void scandir(const string& path, ostream &metadata)
218 DIR *dir = opendir(path.c_str());
221 printf("Error: %m\n");
226 vector<string> contents;
227 while ((ent = readdir(dir)) != NULL) {
228 string filename(ent->d_name);
229 if (filename == "." || filename == "..")
231 contents.push_back(filename);
234 sort(contents.begin(), contents.end());
236 for (vector<string>::iterator i = contents.begin();
237 i != contents.end(); ++i) {
238 const string& filename = *i;
239 scanfile(path + "/" + filename, metadata);
245 int main(int argc, char *argv[])
247 block_buf = new char[LBS_BLOCK_SIZE];
249 tss = new TarSegmentStore(".");
251 std::ostringstream metadata;
254 scanfile(".", metadata);
255 } catch (IOException e) {
256 fprintf(stderr, "IOException: %s\n", e.getError().c_str());
259 const string md = metadata.str();
260 string root = tss->write_object(md.data(), md.size(), "root");