1 /* Compares two mounted NTFS filesystems. */
14 #include <attr/xattr.h>
20 # define DEBUG(format, ...) \
22 int __errno_save = errno; \
23 fprintf(stdout, "[%s %d] %s(): " format, \
24 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
27 errno = __errno_save; \
30 #define DEBUG(format, ...)
33 static void difference(const char *format, ...)
38 fputs("ntfs-cmp: ", stderr);
39 vfprintf(stderr, format, va);
46 static void error(const char *format, ...)
52 fputs("ntfs-cmp: ", stderr);
53 vfprintf(stderr, format, va);
54 fprintf(stderr, ": %s\n", strerror(err));
66 static struct node *tree = NULL;
68 static const char *root1, *root2;
70 static u64 do_lookup_ino(struct node *tree, u64 ino_from)
74 if (ino_from == tree->ino_from)
76 else if (ino_from < tree->ino_from)
77 return do_lookup_ino(tree->left, ino_from);
79 return do_lookup_ino(tree->right, ino_from);
83 static void do_insert(struct node *tree, struct node *node)
85 if (node->ino_from < tree->ino_from) {
87 return do_insert(tree->left, node);
92 return do_insert(tree->right, node);
98 static u64 lookup_ino(u64 ino_from)
100 return do_lookup_ino(tree, ino_from);
103 static void insert_ino(u64 ino_from, u64 ino_to)
105 struct node *node = malloc(sizeof(struct node));
107 error("Out of memory");
108 node->ino_from = ino_from;
109 node->ino_to = ino_to;
115 do_insert(tree, node);
119 static void cmp(const char *file1, const char *file2, size_t size)
122 char buf1[4096], buf2[4096];
123 ssize_t to_read = 4096;
124 fd1 = open(file1, O_RDONLY);
126 error("Could not open `%s'", file1);
127 fd2 = open(file2, O_RDONLY);
129 error("Could not open `%s'", file2);
130 for (; size; size -= to_read) {
133 if (read(fd1, buf1, to_read) != to_read)
134 error("Error reading `%s'", file1);
135 if (read(fd2, buf2, to_read) != to_read)
136 error("Error reading `%s'", file2);
137 if (memcmp(buf1, buf2, to_read))
138 difference("File contents of `%s' and `%s' differ",
145 static void cmp_xattr(const char *file1, const char *file2,
146 const char *xattr_name, ssize_t max_size,
151 DEBUG("cmp xattr \"%s\" of files %s, %s", xattr_name, file1, file2);
152 len1 = lgetxattr(file1, xattr_name, NULL, 0);
154 if (errno == ENOATTR) {
157 lgetxattr(file2, xattr_name, NULL, 0);
158 if (errno == ENOATTR)
161 error("xattr `%s' exists on file `%s' "
162 "but not on file `%s'",
163 xattr_name, file1, file2);
165 error("Could not find attribute `%s' of `%s'",
169 error("Could not read xattr `%s' of `%s'",
176 error("Out of memory");
177 if (lgetxattr(file1, xattr_name, buf1, len1) != len1)
178 error("Could not read xattr `%s' of `%s'",
181 len2 = lgetxattr(file2, xattr_name, buf2, len1);
183 if (memcmp(buf1, buf2,
184 (max_size == 0 || len1 <= max_size) ? len1 : max_size))
186 difference("xattr `%s' of files `%s' and `%s' differs",
187 xattr_name, file1, file2);
191 error("Could not read xattr `%s' from `%s'",
195 difference("xattr `%s' of files `%s' and `%s' differs",
196 xattr_name, file1, file2);
202 static void cmp_ads(const char *file1, const char *file2)
204 char _list1[256], _list2[sizeof(_list1)];
205 char *list1 = _list1, *list2 = _list2;
207 ssize_t len1, len2, tmp;
209 len1 = llistxattr(file1, list1, sizeof(_list1));
211 if (errno != ERANGE || ((len1 = llistxattr(file1, NULL, 0) == -1)))
212 error("Could not get xattr list of `%s'", file1);
213 list1 = malloc(len1);
214 list2 = malloc(len1);
215 if (!list1 || !list2)
216 error("Out of memory");
217 tmp = llistxattr(file1, list1, len1);
219 error("Could not get xattr list of `%s'", file1);
221 error("xattr list of `%s' changed as we read it",
225 len2 = llistxattr(file2, list2, len1);
228 difference("`%s' and `%s' do not have the same "
229 "xattr list", file1, file2);
231 error("Could not get xattr list of `%s'", file2);
233 if (len1 != len2 || memcmp(list1, list2, len1))
234 difference("`%s' and `%s' do not have the same "
235 "xattr list", file1, file2);
237 pe = list1 + len1 - 1;
239 cmp_xattr(file1, file2, p, 0, false);
242 if (list1 != _list1) {
248 static void special_cmp(const char *file1, const char *file2)
250 cmp_xattr(file1, file2, "system.ntfs_acl", 0, false);
251 cmp_xattr(file1, file2, "system.ntfs_attrib", 0, false);
252 cmp_xattr(file1, file2, "system.ntfs_dos_name", 0, true);
253 cmp_xattr(file1, file2, "system.ntfs_reparse_data", 0, true);
254 cmp_xattr(file1, file2, "system.ntfs_times", 16, false);
255 cmp_ads(file1, file2);
259 static void ntfs_cmp(char file1[], int file1_len, char file2[], int file2_len)
261 struct stat st1, st2;
262 u64 ino_from, ino_to;
264 DEBUG("cmp files %s, %s", file1, file2);
265 if (lstat(file1, &st1))
266 error("Failed to stat `%s'", file1);
267 if (lstat(file2, &st2))
268 error("Failed to stat `%s'", file2);
269 ino_from = st1.st_ino;
270 ino_to = lookup_ino(ino_from);
272 insert_ino(ino_from, st2.st_ino);
273 else if (ino_to != st2.st_ino)
274 difference("Inode number on `%s' is wrong", file2);
275 if (st1.st_size != st2.st_size)
276 difference("Sizes of `%s' and `%s' are not the same",
278 if (st1.st_mode != st2.st_mode)
279 difference("Modes of `%s' and `%s' are not the same",
281 if (st1.st_atime != st2.st_atime)
282 difference("Access times of `%s' and `%s' are not the same",
284 if (st1.st_mtime != st2.st_mtime)
285 difference("Modification times times of `%s' and `%s' are not the same",
288 if (st1.st_ctime != st2.st_ctime)
289 difference("Status change times of `%s' and `%s' are not the same",
292 if (st1.st_nlink != st2.st_nlink)
293 difference("Link count of `%s' and `%s' are not the same",
295 if (strcmp(file1, root1))
296 special_cmp(file1, file2);
297 if (S_ISREG(st1.st_mode))
298 cmp(file1, file2, st1.st_size);
299 else if (S_ISDIR(st1.st_mode)) {
302 struct dirent **namelist1, **namelist2;
303 const char *dir1 = file1, *dir2 = file2;
305 ret1 = scandir(dir1, &namelist1, NULL, alphasort);
307 error("Error scanning directory `%s'", dir1);
308 ret2 = scandir(dir2, &namelist2, NULL, alphasort);
310 error("Error scanning directory `%s'", dir2);
312 difference("Directories `%s' and `%s' do not contain "
313 "the same number of entries", dir1, dir2);
314 file1[file1_len] = '/';
315 file2[file2_len] = '/';
316 for (i = 0; i < ret1; i++) {
319 if (strcmp(namelist1[i]->d_name, namelist2[i]->d_name)) {
320 difference("Files `%s' and `%s' in directories "
321 "`%s' and `%s', respectively, do "
322 "not have the same name",
323 namelist1[i]->d_name,
324 namelist2[i]->d_name,
327 name = namelist1[i]->d_name;
328 name_len = strlen(name);
329 if (!(name[0] == '.' &&
331 (name[1] == '.' && name[2] == '\0'))))
333 memcpy(file1 + file1_len + 1, name, name_len + 1);
334 memcpy(file2 + file2_len + 1, name, name_len + 1);
335 ntfs_cmp(file1, file1_len + 1 + name_len,
336 file2, file2_len + 1 + name_len);
344 file1[file1_len] = '\0';
345 file2[file2_len] = '\0';
349 int main(int argc, char **argv)
352 fprintf(stderr, "Usage: %s DIR1 DIR2", argv[0]);
357 strcpy(dir1, argv[1]);
358 strcpy(dir2, argv[2]);
361 ntfs_cmp(dir1, strlen(dir1), dir2, strlen(dir2));