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.
30 #ifdef HAVE_ATTR_XATTR_H
31 #include <attr/xattr.h>
38 # define DEBUG(format, ...) \
40 int __errno_save = errno; \
41 fprintf(stdout, "[%s %d] %s(): " format, \
42 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
45 errno = __errno_save; \
48 #define DEBUG(format, ...)
50 static bool ntfs_mode = false;
52 static void difference(const char *format, ...)
57 fputs("tree-cmp: ", stderr);
58 vfprintf(stderr, format, va);
65 static void error(const char *format, ...)
71 fputs("tree-cmp: ", stderr);
72 vfprintf(stderr, format, va);
73 fprintf(stderr, ": %s\n", strerror(err));
78 /* This is just a binary tree that maps inode numbers in one NTFS tree to inode
79 * numbers in the other NTFS tree. This is so we can tell if the hard link
80 * groups are the same between the two NTFS trees. */
88 static struct node *tree = NULL;
90 static const char *root1, *root2;
92 static u64 do_lookup_ino(struct node *tree, u64 ino_from)
96 if (ino_from == tree->ino_from)
98 else if (ino_from < tree->ino_from)
99 return do_lookup_ino(tree->left, ino_from);
101 return do_lookup_ino(tree->right, ino_from);
105 static void do_insert(struct node *tree, struct node *node)
107 if (node->ino_from < tree->ino_from) {
109 return do_insert(tree->left, node);
114 return do_insert(tree->right, node);
120 static u64 lookup_ino(u64 ino_from)
122 return do_lookup_ino(tree, ino_from);
125 static void insert_ino(u64 ino_from, u64 ino_to)
127 struct node *node = malloc(sizeof(struct node));
129 error("Out of memory");
130 node->ino_from = ino_from;
131 node->ino_to = ino_to;
137 do_insert(tree, node);
141 /* Compares the "normal" contents of two files of size @size. */
142 static void cmp(const char *file1, const char *file2, size_t size)
145 char buf1[4096], buf2[4096];
146 ssize_t to_read = 4096;
147 fd1 = open(file1, O_RDONLY);
149 error("Could not open `%s'", file1);
150 fd2 = open(file2, O_RDONLY);
152 error("Could not open `%s'", file2);
153 for (; size; size -= to_read) {
156 if (read(fd1, buf1, to_read) != to_read)
157 error("Error reading `%s'", file1);
158 if (read(fd2, buf2, to_read) != to_read)
159 error("Error reading `%s'", file2);
160 if (memcmp(buf1, buf2, to_read))
161 difference("File contents of `%s' and `%s' differ",
168 #ifdef HAVE_ATTR_XATTR_H
169 /* Compares an extended attribute of the files. */
170 static void cmp_xattr(const char *file1, const char *file2,
171 const char *xattr_name, ssize_t max_size,
176 DEBUG("cmp xattr \"%s\" of files %s, %s", xattr_name, file1, file2);
177 len1 = lgetxattr(file1, xattr_name, NULL, 0);
179 if (errno == ENOATTR) {
182 lgetxattr(file2, xattr_name, NULL, 0);
183 if (errno == ENOATTR)
186 difference("xattr `%s' exists on file `%s' "
187 "but not on file `%s'",
188 xattr_name, file1, file2);
190 error("Could not find attribute `%s' of `%s'",
194 error("Could not read xattr `%s' of `%s'",
201 error("Out of memory");
202 if (lgetxattr(file1, xattr_name, buf1, len1) != len1)
203 error("Could not read xattr `%s' of `%s'",
206 len2 = lgetxattr(file2, xattr_name, buf2, len1);
208 if (memcmp(buf1, buf2,
209 (max_size == 0 || len1 <= max_size) ? len1 : max_size))
211 difference("xattr `%s' of files `%s' and `%s' differs",
212 xattr_name, file1, file2);
216 error("Could not read xattr `%s' from `%s'",
220 difference("xattr `%s' of files `%s' and `%s' differs",
221 xattr_name, file1, file2);
227 /* Compares all alternate data streams of the files */
228 static void cmp_ads(const char *file1, const char *file2)
230 char _list1[256], _list2[sizeof(_list1)];
231 char *list1 = _list1, *list2 = _list2;
233 ssize_t len1, len2, tmp;
235 len1 = llistxattr(file1, list1, sizeof(_list1));
237 if (errno != ERANGE || ((len1 = llistxattr(file1, NULL, 0) == -1)))
238 error("Could not get xattr list of `%s'", file1);
239 list1 = malloc(len1);
240 list2 = malloc(len1);
241 if (!list1 || !list2)
242 error("Out of memory");
243 tmp = llistxattr(file1, list1, len1);
245 error("Could not get xattr list of `%s'", file1);
247 error("xattr list of `%s' changed as we read it",
251 len2 = llistxattr(file2, list2, len1);
254 difference("`%s' and `%s' do not have the same "
255 "xattr list", file1, file2);
257 error("Could not get xattr list of `%s'", file2);
259 if (len1 != len2 || memcmp(list1, list2, len1))
260 difference("`%s' and `%s' do not have the same "
261 "xattr list", file1, file2);
263 pe = list1 + len1 - 1;
265 cmp_xattr(file1, file2, p, 0, false);
268 if (list1 != _list1) {
275 /* Compares special NTFS data of the files, as accessed through extended
277 static void special_cmp(const char *file1, const char *file2)
279 #ifdef HAVE_ATTR_XATTR_H
280 cmp_xattr(file1, file2, "system.ntfs_acl", 0, false);
281 cmp_xattr(file1, file2, "system.ntfs_attrib", 0, false);
282 cmp_xattr(file1, file2, "system.ntfs_dos_name", 0, true);
283 cmp_xattr(file1, file2, "system.ntfs_object_id", 64, true);
284 cmp_xattr(file1, file2, "system.ntfs_reparse_data", 0, true);
285 cmp_xattr(file1, file2, "system.ntfs_times", 16, false);
286 cmp_ads(file1, file2);
288 fprintf(stderr, "tree-cmp: Warning: cannot compare xattrs of `%s' and `%s'\n",
290 fprintf(stderr, " You need to install the attr development files for this.\n");
295 /* Recursively compares directory tree rooted at file1 to directory tree rooted at file2 */
296 static void tree_cmp(char file1[], int file1_len, char file2[], int file2_len)
298 struct stat st1, st2;
299 u64 ino_from, ino_to;
301 DEBUG("cmp files %s, %s", file1, file2);
302 if (lstat(file1, &st1))
303 error("Failed to stat `%s'", file1);
304 if (lstat(file2, &st2))
305 error("Failed to stat `%s'", file2);
306 ino_from = st1.st_ino;
307 ino_to = lookup_ino(ino_from);
309 insert_ino(ino_from, st2.st_ino);
310 else if (ino_to != st2.st_ino)
311 difference("Inode number on `%s' is wrong", file2);
312 if ((st1.st_mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) !=
313 (st2.st_mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)))
314 difference("Modes of `%s' and `%s' are not the same",
316 if (S_ISREG(st1.st_mode) && st1.st_size != st2.st_size)
317 difference("Sizes of `%s' and `%s' are not the same",
320 if (ntfs_mode && st1.st_atime != st2.st_atime)
321 difference("Access times of `%s' and `%s' are not the same",
323 if (st1.st_mtime != st2.st_mtime)
324 difference("Modification times of `%s' (%x) and `%s' (%x) are "
326 file1, st1.st_mtime, file2, st2.st_mtime);
327 if (st1.st_ctime != st2.st_ctime)
328 difference("Status change times of `%s' and `%s' are not the same",
331 if ((ntfs_mode || S_ISREG(st1.st_mode)) && st1.st_nlink != st2.st_nlink)
332 difference("Link count of `%s' (%u) and `%s' (%u) "
334 file1, st1.st_nlink, file2, st2.st_nlink);
335 if (ntfs_mode && strcmp(file1, root1) != 0)
336 special_cmp(file1, file2);
337 if (S_ISREG(st1.st_mode))
338 cmp(file1, file2, st1.st_size);
339 else if (S_ISDIR(st1.st_mode)) {
342 struct dirent **namelist1, **namelist2;
343 const char *dir1 = file1, *dir2 = file2;
345 ret1 = scandir(dir1, &namelist1, NULL, alphasort);
347 error("Error scanning directory `%s'", dir1);
348 ret2 = scandir(dir2, &namelist2, NULL, alphasort);
350 error("Error scanning directory `%s'", dir2);
352 difference("Directories `%s' and `%s' do not contain "
353 "the same number of entries", dir1, dir2);
354 file1[file1_len] = '/';
355 file2[file2_len] = '/';
356 for (i = 0; i < ret1; i++) {
359 if (strcmp(namelist1[i]->d_name, namelist2[i]->d_name)) {
360 difference("Files `%s' and `%s' in directories "
361 "`%s' and `%s', respectively, do "
362 "not have the same name",
363 namelist1[i]->d_name,
364 namelist2[i]->d_name,
367 name = namelist1[i]->d_name;
368 name_len = strlen(name);
369 if (!(name[0] == '.' &&
371 (name[1] == '.' && name[2] == '\0'))))
373 memcpy(file1 + file1_len + 1, name, name_len + 1);
374 memcpy(file2 + file2_len + 1, name, name_len + 1);
375 tree_cmp(file1, file1_len + 1 + name_len,
376 file2, file2_len + 1 + name_len);
384 file1[file1_len] = '\0';
385 file2[file2_len] = '\0';
386 } else if (!ntfs_mode && S_ISLNK(st1.st_mode)) {
387 char buf1[4096], buf2[sizeof(buf1)];
389 ret1 = readlink(file1, buf1, sizeof(buf1));
391 error("Failed to get symlink target of `%s'", file1);
392 ret2 = readlink(file2, buf2, sizeof(buf2));
394 error("Failed to get symlink target of `%s'", file2);
395 if (ret1 != ret2 || memcmp(buf1, buf2, ret1))
396 error("Symlink targets of `%s' and `%s' differ",
401 int main(int argc, char **argv)
403 if (argc != 3 && argc != 4) {
404 fprintf(stderr, "Usage: %s DIR1 DIR2 [NTFS]", argv[0]);
407 if (argc > 3 && strcmp(argv[3], "NTFS") == 0)
412 strcpy(dir1, argv[1]);
413 strcpy(dir2, argv[2]);
416 tree_cmp(dir1, strlen(dir1), dir2, strlen(dir2));