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_reparse_data", 0, true);
284 cmp_xattr(file1, file2, "system.ntfs_times", 16, false);
285 cmp_ads(file1, file2);
287 fprintf(stderr, "tree-cmp: Warning: cannot compare xattrs of `%s' and `%s'\n",
289 fprintf(stderr, " You need to install the attr development files for this.\n");
294 /* Recursively compares directory tree rooted at file1 to directory tree rooted at file2 */
295 static void tree_cmp(char file1[], int file1_len, char file2[], int file2_len)
297 struct stat st1, st2;
298 u64 ino_from, ino_to;
300 DEBUG("cmp files %s, %s", file1, file2);
301 if (lstat(file1, &st1))
302 error("Failed to stat `%s'", file1);
303 if (lstat(file2, &st2))
304 error("Failed to stat `%s'", file2);
305 ino_from = st1.st_ino;
306 ino_to = lookup_ino(ino_from);
308 insert_ino(ino_from, st2.st_ino);
309 else if (ino_to != st2.st_ino)
310 difference("Inode number on `%s' is wrong", file2);
311 if ((st1.st_mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) !=
312 (st2.st_mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)))
313 difference("Modes of `%s' and `%s' are not the same",
315 if (S_ISREG(st1.st_mode) && st1.st_size != st2.st_size)
316 difference("Sizes of `%s' and `%s' are not the same",
319 if (ntfs_mode && st1.st_atime != st2.st_atime)
320 difference("Access times of `%s' and `%s' are not the same",
322 if (st1.st_mtime != st2.st_mtime)
323 difference("Modification times of `%s' (%x) and `%s' (%x) are "
325 file1, st1.st_mtime, file2, st2.st_mtime);
326 if (st1.st_ctime != st2.st_ctime)
327 difference("Status change times of `%s' and `%s' are not the same",
330 if ((ntfs_mode || S_ISREG(st1.st_mode)) && st1.st_nlink != st2.st_nlink)
331 difference("Link count of `%s' (%u) and `%s' (%u) "
333 file1, st1.st_nlink, file2, st2.st_nlink);
334 if (ntfs_mode && strcmp(file1, root1) != 0)
335 special_cmp(file1, file2);
336 if (S_ISREG(st1.st_mode))
337 cmp(file1, file2, st1.st_size);
338 else if (S_ISDIR(st1.st_mode)) {
341 struct dirent **namelist1, **namelist2;
342 const char *dir1 = file1, *dir2 = file2;
344 ret1 = scandir(dir1, &namelist1, NULL, alphasort);
346 error("Error scanning directory `%s'", dir1);
347 ret2 = scandir(dir2, &namelist2, NULL, alphasort);
349 error("Error scanning directory `%s'", dir2);
351 difference("Directories `%s' and `%s' do not contain "
352 "the same number of entries", dir1, dir2);
353 file1[file1_len] = '/';
354 file2[file2_len] = '/';
355 for (i = 0; i < ret1; i++) {
358 if (strcmp(namelist1[i]->d_name, namelist2[i]->d_name)) {
359 difference("Files `%s' and `%s' in directories "
360 "`%s' and `%s', respectively, do "
361 "not have the same name",
362 namelist1[i]->d_name,
363 namelist2[i]->d_name,
366 name = namelist1[i]->d_name;
367 name_len = strlen(name);
368 if (!(name[0] == '.' &&
370 (name[1] == '.' && name[2] == '\0'))))
372 memcpy(file1 + file1_len + 1, name, name_len + 1);
373 memcpy(file2 + file2_len + 1, name, name_len + 1);
374 tree_cmp(file1, file1_len + 1 + name_len,
375 file2, file2_len + 1 + name_len);
383 file1[file1_len] = '\0';
384 file2[file2_len] = '\0';
385 } else if (!ntfs_mode && S_ISLNK(st1.st_mode)) {
386 char buf1[4096], buf2[sizeof(buf1)];
388 ret1 = readlink(file1, buf1, sizeof(buf1));
390 error("Failed to get symlink target of `%s'", file1);
391 ret2 = readlink(file2, buf2, sizeof(buf2));
393 error("Failed to get symlink target of `%s'", file2);
394 if (ret1 != ret2 || memcmp(buf1, buf2, ret1))
395 error("Symlink targets of `%s' and `%s' differ",
400 int main(int argc, char **argv)
402 if (argc != 3 && argc != 4) {
403 fprintf(stderr, "Usage: %s DIR1 DIR2 [NTFS]", argv[0]);
406 if (argc > 3 && strcmp(argv[3], "NTFS") == 0)
411 strcpy(dir1, argv[1]);
412 strcpy(dir2, argv[2]);
415 tree_cmp(dir1, strlen(dir1), dir2, strlen(dir2));