2 * A program to compare directory trees
5 * - Normal mode for any filesystems. We compare file names, contents,
6 * sizes, modes, access times, and hard links.
7 * - NTFS mode for NTFS-3g mounted volumes. In this mode we need to
8 * compare various NTFS-specific attributes such as named data streams
11 * Both modes compare hard link groups between the two directory trees. If two
12 * files are hard linked together in one directory tree, exactly the same two
13 * files are expected to be hard linked together in the other directory tree.
27 #include <attr/xattr.h>
33 # define DEBUG(format, ...) \
35 int __errno_save = errno; \
36 fprintf(stdout, "[%s %d] %s(): " format, \
37 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
40 errno = __errno_save; \
43 #define DEBUG(format, ...)
45 static bool ntfs_mode = false;
47 static void difference(const char *format, ...)
52 fputs("tree-cmp: ", stderr);
53 vfprintf(stderr, format, va);
60 static void error(const char *format, ...)
66 fputs("tree-cmp: ", stderr);
67 vfprintf(stderr, format, va);
68 fprintf(stderr, ": %s\n", strerror(err));
73 /* This is just a binary tree that maps inode numbers in one NTFS tree to inode
74 * numbers in the other NTFS tree. This is so we can tell if the hard link
75 * groups are the same between the two NTFS trees. */
83 static struct node *tree = NULL;
85 static const char *root1, *root2;
87 static u64 do_lookup_ino(struct node *tree, u64 ino_from)
91 if (ino_from == tree->ino_from)
93 else if (ino_from < tree->ino_from)
94 return do_lookup_ino(tree->left, ino_from);
96 return do_lookup_ino(tree->right, ino_from);
100 static void do_insert(struct node *tree, struct node *node)
102 if (node->ino_from < tree->ino_from) {
104 return do_insert(tree->left, node);
109 return do_insert(tree->right, node);
115 static u64 lookup_ino(u64 ino_from)
117 return do_lookup_ino(tree, ino_from);
120 static void insert_ino(u64 ino_from, u64 ino_to)
122 struct node *node = malloc(sizeof(struct node));
124 error("Out of memory");
125 node->ino_from = ino_from;
126 node->ino_to = ino_to;
132 do_insert(tree, node);
136 /* Compares the "normal" contents of two files of size @size. */
137 static void cmp(const char *file1, const char *file2, size_t size)
140 char buf1[4096], buf2[4096];
141 ssize_t to_read = 4096;
142 fd1 = open(file1, O_RDONLY);
144 error("Could not open `%s'", file1);
145 fd2 = open(file2, O_RDONLY);
147 error("Could not open `%s'", file2);
148 for (; size; size -= to_read) {
151 if (read(fd1, buf1, to_read) != to_read)
152 error("Error reading `%s'", file1);
153 if (read(fd2, buf2, to_read) != to_read)
154 error("Error reading `%s'", file2);
155 if (memcmp(buf1, buf2, to_read))
156 difference("File contents of `%s' and `%s' differ",
163 /* Compares an extended attribute of the files. */
164 static void cmp_xattr(const char *file1, const char *file2,
165 const char *xattr_name, ssize_t max_size,
170 DEBUG("cmp xattr \"%s\" of files %s, %s", xattr_name, file1, file2);
171 len1 = lgetxattr(file1, xattr_name, NULL, 0);
173 if (errno == ENOATTR) {
176 lgetxattr(file2, xattr_name, NULL, 0);
177 if (errno == ENOATTR)
180 difference("xattr `%s' exists on file `%s' "
181 "but not on file `%s'",
182 xattr_name, file1, file2);
184 error("Could not find attribute `%s' of `%s'",
188 error("Could not read xattr `%s' of `%s'",
195 error("Out of memory");
196 if (lgetxattr(file1, xattr_name, buf1, len1) != len1)
197 error("Could not read xattr `%s' of `%s'",
200 len2 = lgetxattr(file2, xattr_name, buf2, len1);
202 if (memcmp(buf1, buf2,
203 (max_size == 0 || len1 <= max_size) ? len1 : max_size))
205 difference("xattr `%s' of files `%s' and `%s' differs",
206 xattr_name, file1, file2);
210 error("Could not read xattr `%s' from `%s'",
214 difference("xattr `%s' of files `%s' and `%s' differs",
215 xattr_name, file1, file2);
221 /* Compares all alternate data streams of the files */
222 static void cmp_ads(const char *file1, const char *file2)
224 char _list1[256], _list2[sizeof(_list1)];
225 char *list1 = _list1, *list2 = _list2;
227 ssize_t len1, len2, tmp;
229 len1 = llistxattr(file1, list1, sizeof(_list1));
231 if (errno != ERANGE || ((len1 = llistxattr(file1, NULL, 0) == -1)))
232 error("Could not get xattr list of `%s'", file1);
233 list1 = malloc(len1);
234 list2 = malloc(len1);
235 if (!list1 || !list2)
236 error("Out of memory");
237 tmp = llistxattr(file1, list1, len1);
239 error("Could not get xattr list of `%s'", file1);
241 error("xattr list of `%s' changed as we read it",
245 len2 = llistxattr(file2, list2, len1);
248 difference("`%s' and `%s' do not have the same "
249 "xattr list", file1, file2);
251 error("Could not get xattr list of `%s'", file2);
253 if (len1 != len2 || memcmp(list1, list2, len1))
254 difference("`%s' and `%s' do not have the same "
255 "xattr list", file1, file2);
257 pe = list1 + len1 - 1;
259 cmp_xattr(file1, file2, p, 0, false);
262 if (list1 != _list1) {
268 /* Compares special NTFS data of the files, as accessed through extended
270 static void special_cmp(const char *file1, const char *file2)
272 cmp_xattr(file1, file2, "system.ntfs_acl", 0, false);
273 cmp_xattr(file1, file2, "system.ntfs_attrib", 0, false);
274 cmp_xattr(file1, file2, "system.ntfs_dos_name", 0, true);
275 cmp_xattr(file1, file2, "system.ntfs_reparse_data", 0, true);
276 cmp_xattr(file1, file2, "system.ntfs_times", 16, false);
277 cmp_ads(file1, file2);
281 /* Recursively compares directory tree rooted at file1 to directory tree rooted at file2 */
282 static void tree_cmp(char file1[], int file1_len, char file2[], int file2_len)
284 struct stat st1, st2;
285 u64 ino_from, ino_to;
287 DEBUG("cmp files %s, %s", file1, file2);
288 if (lstat(file1, &st1))
289 error("Failed to stat `%s'", file1);
290 if (lstat(file2, &st2))
291 error("Failed to stat `%s'", file2);
292 ino_from = st1.st_ino;
293 ino_to = lookup_ino(ino_from);
295 insert_ino(ino_from, st2.st_ino);
296 else if (ino_to != st2.st_ino)
297 difference("Inode number on `%s' is wrong", file2);
298 if ((st1.st_mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) !=
299 (st2.st_mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)))
300 difference("Modes of `%s' and `%s' are not the same",
302 if (S_ISREG(st1.st_mode) && st1.st_size != st2.st_size)
303 difference("Sizes of `%s' and `%s' are not the same",
305 if (ntfs_mode && st1.st_atime != st2.st_atime)
306 difference("Access times of `%s' and `%s' are not the same",
308 if (st1.st_mtime != st2.st_mtime)
309 difference("Modification times of `%s' (%x) and `%s' (%x) are "
311 file1, st1.st_mtime, file2, st2.st_mtime);
313 if (st1.st_ctime != st2.st_ctime)
314 difference("Status change times of `%s' and `%s' are not the same",
317 if ((ntfs_mode || S_ISREG(st1.st_mode)) && st1.st_nlink != st2.st_nlink)
318 difference("Link count of `%s' (%u) and `%s' (%u) "
320 file1, st1.st_nlink, file2, st2.st_nlink);
321 if (ntfs_mode && strcmp(file1, root1) != 0)
322 special_cmp(file1, file2);
323 if (S_ISREG(st1.st_mode))
324 cmp(file1, file2, st1.st_size);
325 else if (S_ISDIR(st1.st_mode)) {
328 struct dirent **namelist1, **namelist2;
329 const char *dir1 = file1, *dir2 = file2;
331 ret1 = scandir(dir1, &namelist1, NULL, alphasort);
333 error("Error scanning directory `%s'", dir1);
334 ret2 = scandir(dir2, &namelist2, NULL, alphasort);
336 error("Error scanning directory `%s'", dir2);
338 difference("Directories `%s' and `%s' do not contain "
339 "the same number of entries", dir1, dir2);
340 file1[file1_len] = '/';
341 file2[file2_len] = '/';
342 for (i = 0; i < ret1; i++) {
345 if (strcmp(namelist1[i]->d_name, namelist2[i]->d_name)) {
346 difference("Files `%s' and `%s' in directories "
347 "`%s' and `%s', respectively, do "
348 "not have the same name",
349 namelist1[i]->d_name,
350 namelist2[i]->d_name,
353 name = namelist1[i]->d_name;
354 name_len = strlen(name);
355 if (!(name[0] == '.' &&
357 (name[1] == '.' && name[2] == '\0'))))
359 memcpy(file1 + file1_len + 1, name, name_len + 1);
360 memcpy(file2 + file2_len + 1, name, name_len + 1);
361 tree_cmp(file1, file1_len + 1 + name_len,
362 file2, file2_len + 1 + name_len);
370 file1[file1_len] = '\0';
371 file2[file2_len] = '\0';
372 } else if (!ntfs_mode && S_ISLNK(st1.st_mode)) {
373 char buf1[4096], buf2[sizeof(buf1)];
375 ret1 = readlink(file1, buf1, sizeof(buf1));
377 error("Failed to get symlink target of `%s'", file1);
378 ret2 = readlink(file2, buf2, sizeof(buf2));
380 error("Failed to get symlink target of `%s'", file2);
381 if (ret1 != ret2 || memcmp(buf1, buf2, ret1))
382 error("Symlink targets of `%s' and `%s' differ",
387 int main(int argc, char **argv)
389 if (argc != 3 && argc != 4) {
390 fprintf(stderr, "Usage: %s DIR1 DIR2 [NTFS]", argv[0]);
393 if (argc > 3 && strcmp(argv[3], "NTFS") == 0)
398 strcpy(dir1, argv[1]);
399 strcpy(dir2, argv[2]);
402 tree_cmp(dir1, strlen(dir1), dir2, strlen(dir2));