3e004fdfc29aee5af00282504fc94dcdc38f04aa
[cumulus.git] / scandir.cc
1 /* Recursively descend the filesystem and visit each file. */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <dirent.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12
13 #include <algorithm>
14 #include <string>
15 #include <vector>
16
17 #include "store.h"
18
19 using std::string;
20 using std::vector;
21
22 static OutputStream *info_dump = NULL;
23
24 void scandir(const string& path);
25
26 /* Converts time to microseconds since the epoch. */
27 int64_t encode_time(time_t time)
28 {
29     return (int64_t)time * 1000000;
30 }
31
32 void dumpfile(int fd)
33 {
34     struct stat stat_buf;
35     fstat(fd, &stat_buf);
36     int64_t size = 0;
37
38     char buf[4096];
39
40     if ((stat_buf.st_mode & S_IFMT) != S_IFREG) {
41         printf("file is no longer a regular file!\n");
42         return;
43     }
44
45     while (true) {
46         ssize_t res = read(fd, buf, sizeof(buf));
47         if (res < 0) {
48             if (errno == EINTR)
49                 continue;
50             printf("Error while reading: %m\n");
51             return;
52         } else if (res == 0) {
53             break;
54         } else {
55             size += res;
56         }
57     }
58
59     printf("    bytes=%Ld\n", size);
60 }
61
62 void scanfile(const string& path)
63 {
64     int fd;
65     long flags;
66     struct stat stat_buf;
67     char *buf;
68     ssize_t len;
69
70     dictionary file_info;
71
72     lstat(path.c_str(), &stat_buf);
73
74     printf("%s\n", path.c_str());
75
76     file_info["mode"] = encode_u16(stat_buf.st_mode & 07777);
77     file_info["atime"] = encode_u64(encode_time(stat_buf.st_atime));
78     file_info["ctime"] = encode_u64(encode_time(stat_buf.st_ctime));
79     file_info["mtime"] = encode_u64(encode_time(stat_buf.st_mtime));
80     file_info["user"] = encode_u32(stat_buf.st_uid);
81     file_info["group"] = encode_u32(stat_buf.st_gid);
82
83     char inode_type;
84
85     switch (stat_buf.st_mode & S_IFMT) {
86     case S_IFIFO:
87         inode_type = 'p';
88         break;
89     case S_IFSOCK:
90         inode_type = 's';
91         break;
92     case S_IFCHR:
93         inode_type = 'c';
94         break;
95     case S_IFBLK:
96         inode_type = 'b';
97         break;
98     case S_IFLNK:
99         inode_type = 'l';
100
101         /* Use the reported file size to allocate a buffer large enough to read
102          * the symlink.  Allocate slightly more space, so that we ask for more
103          * bytes than we expect and so check for truncation. */
104         buf = new char[stat_buf.st_size + 2];
105         len = readlink(path.c_str(), buf, stat_buf.st_size + 1);
106         if (len < 0) {
107             printf("error reading symlink: %m\n");
108         } else if (len <= stat_buf.st_size) {
109             buf[len] = '\0';
110             printf("    contents=%s\n", buf);
111         } else if (len > stat_buf.st_size) {
112             printf("error reading symlink: name truncated\n");
113         }
114
115         file_info["contents"] = buf;
116
117         delete[] buf;
118         break;
119     case S_IFREG:
120         inode_type = '-';
121
122         /* Be paranoid when opening the file.  We have no guarantee that the
123          * file was not replaced between the stat() call above and the open()
124          * call below, so we might not even be opening a regular file.  That
125          * the file descriptor refers to a regular file is checked in
126          * dumpfile().  But we also supply flags to open to to guard against
127          * various conditions before we can perform that verification:
128          *   - O_NOFOLLOW: in the event the file was replaced by a symlink
129          *   - O_NONBLOCK: prevents open() from blocking if the file was
130          *     replaced by a fifo
131          * We also add in O_NOATIME, since this may reduce disk writes (for
132          * inode updates). */
133         fd = open(path.c_str(), O_RDONLY|O_NOATIME|O_NOFOLLOW|O_NONBLOCK);
134
135         /* Drop the use of the O_NONBLOCK flag; we only wanted that for file
136          * open. */
137         flags = fcntl(fd, F_GETFL);
138         fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
139
140         //dumpfile(fd);
141         file_info["size"] = encode_u64(stat_buf.st_size);
142         close(fd);
143
144         break;
145     case S_IFDIR:
146         inode_type = 'd';
147         scandir(path);
148         break;
149
150     default:
151         fprintf(stderr, "Unknown inode type: mode=%x\n", stat_buf.st_mode);
152         return;
153     }
154
155     file_info["type"] = string(1, inode_type);
156
157     info_dump->write_string(path);
158     info_dump->write_dictionary(file_info);
159 }
160
161 void scandir(const string& path)
162 {
163     DIR *dir = opendir(path.c_str());
164
165     if (dir == NULL) {
166         printf("Error: %m\n");
167         return;
168     }
169
170     struct dirent *ent;
171     vector<string> contents;
172     while ((ent = readdir(dir)) != NULL) {
173         string filename(ent->d_name);
174         if (filename == "." || filename == "..")
175             continue;
176         contents.push_back(filename);
177     }
178
179     sort(contents.begin(), contents.end());
180
181     for (vector<string>::iterator i = contents.begin();
182          i != contents.end(); ++i) {
183         const string& filename = *i;
184         scanfile(path + "/" + filename);
185     }
186
187     closedir(dir);
188 }
189
190 int main(int argc, char *argv[])
191 {
192     FILE *dump = fopen("fileinfo", "w");
193     if (dump == NULL) {
194         fprintf(stderr, "Cannot open fileinfo: %m\n");
195         return 1;
196     }
197
198     FileOutputStream os(dump);
199     info_dump = &os;
200
201     try {
202         scanfile(".");
203     } catch (IOException e) {
204         fprintf(stderr, "IOException: %s\n", e.getError().c_str());
205     }
206
207     return 0;
208 }