2 * Compare directory trees (Windows version)
17 #define REPARSE_POINT_MAX_SIZE (16 * 1024)
20 win32_error_string(DWORD err_code)
22 static wchar_t buf[1024];
24 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err_code, 0,
29 static void __attribute__((noreturn))
30 error(const wchar_t *format, ...)
34 vfwprintf(stderr, format, va);
40 static void __attribute__((noreturn))
41 win32_error(const wchar_t *format, ...)
44 DWORD err = GetLastError();
47 vfwprintf(stderr, format, va);
48 fwprintf(stderr, L": %ls\n", win32_error_string(err));
60 static struct node *tree = NULL;
62 static u64 do_lookup_ino(struct node *tree, u64 ino_from)
66 if (ino_from == tree->ino_from)
68 else if (ino_from < tree->ino_from)
69 return do_lookup_ino(tree->left, ino_from);
71 return do_lookup_ino(tree->right, ino_from);
74 static void do_insert(struct node *tree, struct node *node)
76 if (node->ino_from < tree->ino_from) {
78 return do_insert(tree->left, node);
83 return do_insert(tree->right, node);
89 static u64 lookup_ino(u64 ino_from)
91 return do_lookup_ino(tree, ino_from);
94 static void insert_ino(u64 ino_from, u64 ino_to)
96 struct node *node = malloc(sizeof(struct node));
98 error(L"Out of memory");
99 node->ino_from = ino_from;
100 node->ino_to = ino_to;
106 do_insert(tree, node);
110 win32_open_file_readonly(const wchar_t *path)
112 HANDLE hFile = CreateFile(path,
113 GENERIC_READ | ACCESS_SYSTEM_SECURITY,
114 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
117 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
119 if (hFile == INVALID_HANDLE_VALUE)
120 win32_error(L"Failed to open file %ls read-only", path);
125 get_reparse_data(HANDLE hFile, const wchar_t *path,
128 DWORD bytesReturned = 0;
129 if (!DeviceIoControl(hFile,
130 FSCTL_GET_REPARSE_POINT,
131 NULL, /* "Not used with this operation; set to NULL" */
132 0, /* "Not used with this operation; set to 0" */
133 rpdata, /* "A pointer to a buffer that
134 receives the reparse point data */
135 REPARSE_POINT_MAX_SIZE, /* "The size of the output
139 win32_error(L"Can't get reparse data from %ls", path);
140 return bytesReturned;
144 cmp_reparse_data(HANDLE hFile_1, const wchar_t *path_1,
145 HANDLE hFile_2, const wchar_t *path_2)
147 char rpdata_1[REPARSE_POINT_MAX_SIZE];
148 char rpdata_2[REPARSE_POINT_MAX_SIZE];
152 len_1 = get_reparse_data(hFile_1, path_1, rpdata_1);
153 len_2 = get_reparse_data(hFile_2, path_2, rpdata_2);
154 if (len_1 != len_2 || memcmp(rpdata_1, rpdata_2, len_1)) {
155 error(L"Reparse point data for %ls and %ls differs",
160 struct win32_stream_wrapper {
161 struct win32_stream_wrapper *next;
162 WIN32_FIND_STREAM_DATA dat;
166 qsort_cmp_streams_by_name(const void *p1, const void *p2)
168 const WIN32_FIND_STREAM_DATA *s1 = p1, *s2 = p2;
169 return wcscmp(s1->cStreamName, s2->cStreamName);
172 static WIN32_FIND_STREAM_DATA *
173 get_stream_array(const wchar_t *path, size_t *nstreams_ret)
175 WIN32_FIND_STREAM_DATA dat;
176 WIN32_FIND_STREAM_DATA *array = NULL;
177 WIN32_FIND_STREAM_DATA *p;
179 struct win32_stream_wrapper *stream_list = NULL;
182 hFind = FindFirstStreamW(path, FindStreamInfoStandard, &dat, 0);
183 if (hFind != INVALID_HANDLE_VALUE) {
185 struct win32_stream_wrapper *wrapper;
187 wrapper = malloc(sizeof(*wrapper));
189 error(L"out of memory");
190 memcpy(&wrapper->dat, &dat, sizeof(dat));
191 wrapper->next = stream_list;
192 stream_list = wrapper;
194 } while (FindNextStreamW(hFind, &dat));
196 if (GetLastError() != ERROR_HANDLE_EOF)
197 win32_error(L"Can't lookup streams from %ls", path);
198 if (hFind != INVALID_HANDLE_VALUE)
200 array = malloc(nstreams * sizeof(array[0]));
202 while (stream_list) {
203 struct win32_stream_wrapper *next;
205 memcpy(p, &stream_list->dat, sizeof(*p));
206 next = stream_list->next;
211 assert(p - array == nstreams);
212 qsort(array, nstreams, sizeof(array[0]), qsort_cmp_streams_by_name);
213 *nstreams_ret = nstreams;
217 static const wchar_t *
218 fix_stream_name(wchar_t *stream_name)
222 /* The stream name should be returned as :NAME:TYPE */
223 if (stream_name[0] != L':')
225 colon = wcschr(stream_name + 1, L':');
228 if (wcscmp(colon + 1, L"$DATA"))
231 if (stream_name == colon - 1)
236 #define BUFSIZE 32768
239 cmp_data(HANDLE hFile_1, const wchar_t *path_1,
240 HANDLE hFile_2, const wchar_t *path_2, u64 size)
244 u64 bytes_remaining = size;
248 while (bytes_remaining) {
249 bytesToRead = BUFSIZE;
250 if (bytesToRead > bytes_remaining)
251 bytesToRead = bytes_remaining;
252 if (!ReadFile(hFile_1, buf_1, bytesToRead, &bytesRead, NULL) ||
253 bytesRead != bytesToRead)
255 win32_error(L"Error reading from %ls", path_1);
257 if (!ReadFile(hFile_2, buf_2, bytesToRead, &bytesRead, NULL) ||
258 bytesRead != bytesToRead)
260 win32_error(L"Error reading from %ls", path_2);
262 if (memcmp(buf_1, buf_2, bytesToRead))
263 error(L"Data of %ls and %ls differs", path_1, path_2);
264 bytes_remaining -= bytesToRead;
269 cmp_stream(wchar_t *path_1, size_t path_1_len, WIN32_FIND_STREAM_DATA *dat_1,
270 wchar_t *path_2, size_t path_2_len, WIN32_FIND_STREAM_DATA *dat_2)
272 const wchar_t *stream_name;
274 if (wcscmp(dat_1->cStreamName, dat_2->cStreamName)) {
275 error(L"%ls%ls and %ls%ls are not named the same",
276 path_1, dat_1->cStreamName,
277 path_2, dat_2->cStreamName);
279 if (dat_1->StreamSize.QuadPart != dat_2->StreamSize.QuadPart) {
280 error(L"%ls%ls (%"PRIu64" bytes) and %ls%ls "
281 "(%"PRIu64" bytes) are not the same size",
282 path_1, dat_1->cStreamName, dat_1->StreamSize.QuadPart,
283 path_2, dat_2->cStreamName, dat_2->StreamSize.QuadPart);
286 stream_name = fix_stream_name(dat_1->cStreamName);
291 wcscpy(&path_1[path_1_len], stream_name);
292 wcscpy(&path_2[path_2_len], stream_name);
294 HANDLE hFile_1 = win32_open_file_readonly(path_1);
295 HANDLE hFile_2 = win32_open_file_readonly(path_2);
297 cmp_data(hFile_1, path_1, hFile_2, path_2,
298 dat_1->StreamSize.QuadPart);
300 CloseHandle(hFile_1);
301 CloseHandle(hFile_2);
302 path_1[path_1_len] = L'\0';
303 path_2[path_2_len] = L'\0';
307 cmp_streams(wchar_t *path_1, size_t path_1_len,
308 wchar_t *path_2, size_t path_2_len)
310 WIN32_FIND_STREAM_DATA *streams_1, *streams_2;
311 size_t nstreams_1, nstreams_2;
314 streams_1 = get_stream_array(path_1, &nstreams_1);
315 streams_2 = get_stream_array(path_2, &nstreams_2);
317 if (nstreams_1 != nstreams_2) {
318 error(L"%ls and %ls do not have the same number of streams",
322 for (i = 0; i < nstreams_1; i++)
323 cmp_stream(path_1, path_1_len, &streams_1[i],
324 path_2, path_2_len, &streams_2[i]);
329 struct win32_dentry_wrapper {
330 struct win32_dentry_wrapper *next;
335 qsort_cmp_dentries_by_name(const void *p1, const void *p2)
337 const WIN32_FIND_DATA *d1 = p1, *d2 = p2;
338 return wcscmp(d1->cFileName, d2->cFileName);
341 static WIN32_FIND_DATA *
342 get_dentry_array(wchar_t *path, size_t path_len, size_t *ndentries_ret)
345 WIN32_FIND_DATA *array = NULL;
347 size_t ndentries = 0;
348 struct win32_dentry_wrapper *dentry_list = NULL;
352 path[path_len] = L'\\';
353 path[path_len + 1] = L'*';
354 path[path_len + 2] = L'\0';
355 hFind = FindFirstFile(path, &dat);
356 path[path_len] = L'\0';
357 if (hFind != INVALID_HANDLE_VALUE) {
359 struct win32_dentry_wrapper *wrapper;
361 wrapper = malloc(sizeof(*wrapper));
363 error(L"out of memory");
364 memcpy(&wrapper->dat, &dat, sizeof(dat));
365 wrapper->next = dentry_list;
366 dentry_list = wrapper;
368 } while (FindNextFile(hFind, &dat));
370 err = GetLastError();
371 if (err != ERROR_NO_MORE_FILES && err != ERROR_FILE_NOT_FOUND)
372 win32_error(L"Can't lookup dentries from %ls", path);
373 if (hFind != INVALID_HANDLE_VALUE)
375 array = malloc(ndentries * sizeof(array[0]));
377 while (dentry_list) {
378 struct win32_dentry_wrapper *next;
380 memcpy(p, &dentry_list->dat, sizeof(*p));
381 next = dentry_list->next;
386 assert(p - array == ndentries);
387 qsort(array, ndentries, sizeof(array[0]), qsort_cmp_dentries_by_name);
388 *ndentries_ret = ndentries;
393 tree_cmp(wchar_t *path_1, size_t path_1_len, wchar_t *path_2, size_t path_2_len);
396 recurse_directory(wchar_t *path_1, size_t path_1_len,
397 wchar_t *path_2, size_t path_2_len)
399 WIN32_FIND_DATA *dentries_1, *dentries_2;
400 size_t ndentries_1, ndentries_2;
403 dentries_1 = get_dentry_array(path_1, path_1_len, &ndentries_1);
404 dentries_2 = get_dentry_array(path_2, path_2_len, &ndentries_2);
406 if (ndentries_1 != ndentries_2) {
407 error(L"%ls and %ls do not have the same number of dentries",
411 path_1[path_1_len] = L'\\';
412 path_2[path_2_len] = L'\\';
413 for (i = 0; i < ndentries_1; i++) {
414 size_t name_1_len, name_2_len;
416 name_1_len = wcslen(dentries_1[i].cFileName);
417 name_2_len = wcslen(dentries_2[i].cFileName);
418 wmemcpy(&path_1[path_1_len + 1], dentries_1[i].cFileName, name_1_len + 1);
419 wmemcpy(&path_2[path_2_len + 1], dentries_2[i].cFileName, name_2_len + 1);
421 if (wcscmp(dentries_1[i].cFileName,
422 dentries_2[i].cFileName))
423 error(L"%ls and %ls do not have the same name",
426 if (wcscmp(dentries_1[i].cAlternateFileName,
427 dentries_2[i].cAlternateFileName))
428 error(L"%ls and %ls do not have the same short name",
431 if (!wcscmp(dentries_1[i].cFileName, L".") ||
432 !wcscmp(dentries_2[i].cFileName, L".."))
434 tree_cmp(path_1, path_1_len + 1 + name_1_len,
435 path_2, path_2_len + 1 + name_2_len);
437 path_1[path_1_len] = L'\0';
438 path_2[path_2_len] = L'\0';
444 file_times_equal(const FILETIME *t1, const FILETIME *t2)
446 return t1->dwLowDateTime == t2->dwLowDateTime &&
447 t1->dwHighDateTime == t2->dwHighDateTime;
451 get_security(const wchar_t *path, size_t *len_ret)
454 DWORD requestedInformation = DACL_SECURITY_INFORMATION |
455 SACL_SECURITY_INFORMATION |
456 OWNER_SECURITY_INFORMATION |
457 GROUP_SECURITY_INFORMATION;
462 bret = GetFileSecurity(path, requestedInformation,
463 NULL, 0, &lenNeeded);
465 if (bret || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
467 descr = malloc(lenNeeded);
469 error(L"out of memory");
470 if (!GetFileSecurity(path, requestedInformation, descr, lenNeeded,
473 *len_ret = lenNeeded;
476 win32_error(L"Can't read security descriptor of %ls", path);
480 cmp_security(const wchar_t *path_1, const wchar_t *path_2)
482 void *descr_1, *descr_2;
485 descr_1 = get_security(path_1, &len_1);
486 descr_2 = get_security(path_2, &len_2);
488 if (len_1 != len_2 || memcmp(descr_1, descr_2, len_1))
489 error(L"%ls and %ls do not have the same security descriptor",
496 tree_cmp(wchar_t *path_1, size_t path_1_len, wchar_t *path_2, size_t path_2_len)
498 HANDLE hFile_1, hFile_2;
499 BY_HANDLE_FILE_INFORMATION file_info_1, file_info_2;
505 hFile_1 = win32_open_file_readonly(path_1);
506 hFile_2 = win32_open_file_readonly(path_2);
507 if (!GetFileInformationByHandle(hFile_1, &file_info_1))
508 win32_error(L"Failed to get file information for %ls", path_1);
509 if (!GetFileInformationByHandle(hFile_2, &file_info_2))
510 win32_error(L"Failed to get file information for %ls", path_2);
512 if (file_info_1.dwFileAttributes != file_info_2.dwFileAttributes) {
513 error(L"Attributes for %ls (%#x) differ from attributes for %ls (%#x)",
514 path_1, (unsigned)file_info_1.dwFileAttributes,
515 path_2, (unsigned)file_info_2.dwFileAttributes);
518 attribs = file_info_1.dwFileAttributes;
520 if (!(attribs & FILE_ATTRIBUTE_DIRECTORY)) {
521 size_1 = ((u64)file_info_1.nFileSizeHigh << 32) |
522 file_info_1.nFileSizeLow;
523 size_2 = ((u64)file_info_2.nFileSizeHigh << 32) |
524 file_info_2.nFileSizeLow;
525 if (size_1 != size_2) {
526 error(L"Size for %ls (%"PRIu64") differs from size for %ls (%"PRIu64")",
527 path_1, size_1, path_2, size_2);
530 if (file_info_1.nNumberOfLinks != file_info_2.nNumberOfLinks) {
531 error(L"Number of links for %ls (%u) differs from number "
532 "of links for %ls (%u)",
533 path_1, (unsigned)file_info_1.nNumberOfLinks,
534 path_2, (unsigned)file_info_2.nNumberOfLinks);
536 ino_1 = ((u64)file_info_1.nFileIndexHigh << 32) |
537 file_info_1.nFileIndexLow;
538 ino_2 = ((u64)file_info_2.nFileIndexHigh << 32) |
539 file_info_2.nFileIndexLow;
540 ino_to = lookup_ino(ino_1);
542 insert_ino(ino_1, ino_2);
543 else if (ino_to != ino_2)
544 error(L"Inode number on %ls is wrong", path_2);
546 if (!file_times_equal(&file_info_1.ftCreationTime, &file_info_2.ftCreationTime))
547 error(L"Creation times on %ls and %ls differ",
550 if (!file_times_equal(&file_info_1.ftLastWriteTime, &file_info_2.ftLastWriteTime))
551 error(L"Last write times on %ls and %ls differ",
554 cmp_security(path_1, path_2);
555 cmp_streams(path_1, path_1_len, path_2, path_2_len);
556 if (attribs & FILE_ATTRIBUTE_REPARSE_POINT)
557 cmp_reparse_data(hFile_1, path_1, hFile_2, path_2);
558 else if (attribs & FILE_ATTRIBUTE_DIRECTORY)
559 recurse_directory(path_1, path_1_len, path_2, path_2_len);
560 CloseHandle(hFile_1);
561 CloseHandle(hFile_2);
565 enable_privilege(const wchar_t *privilege)
569 TOKEN_PRIVILEGES newState;
571 if (!OpenProcessToken(GetCurrentProcess(),
572 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
573 win32_error(L"Failed to open process token");
575 if (!LookupPrivilegeValueW(NULL, privilege, &luid))
576 win32_error(L"Failed to look up privileges %ls", privilege);
578 newState.PrivilegeCount = 1;
579 newState.Privileges[0].Luid = luid;
580 newState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
581 if (!AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL))
582 win32_error(L"Failed to acquire privilege %ls", privilege);
586 int wmain(int argc, wchar_t **argv, wchar_t **envp)
588 wchar_t dir_1[32769], dir_2[32769];
592 fwprintf(stderr, L"Usage: win32-tree-cmp DIR1 DIR2\n");
596 enable_privilege(SE_BACKUP_NAME);
597 enable_privilege(SE_SECURITY_NAME);
599 len_1 = wcslen(argv[1]);
600 len_2 = wcslen(argv[2]);
601 wmemcpy(dir_1, argv[1], len_1 + 1);
602 wmemcpy(dir_2, argv[2], len_2 + 1);
603 tree_cmp(dir_1, len_1, dir_2, len_2);