Skip to content

Commit 090a308

Browse files
TaoKgitster
authored andcommitted
t/helper/test-chmtime: update mingw to support chmtime on directories
The mingw_utime implementation in mingw.c does not support directories. This means that "test-tool chmtime" fails on Windows when targeting directories. This has previously been noted and sidestepped temporarily by Jeff Hostetler, in "t/helper/test-chmtime: skip directories on Windows" in the "Builtin FSMonitor Part 2" work, but not yet fixed. It would make sense to backdate file and folder changes in untracked cache tests, to avoid needing to insert explicit delays/pauses in the tests. Add support for directory date manipulation in mingw_utime by replacing the file-oriented _wopen() call with the directory-supporting CreateFileW() windows API explicitly. Signed-off-by: Tao Klerks <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 715d08a commit 090a308

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

compat/mingw.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -961,9 +961,11 @@ static inline void time_t_to_filetime(time_t t, FILETIME *ft)
961961
int mingw_utime (const char *file_name, const struct utimbuf *times)
962962
{
963963
FILETIME mft, aft;
964-
int fh, rc;
964+
int rc;
965965
DWORD attrs;
966966
wchar_t wfilename[MAX_PATH];
967+
HANDLE osfilehandle;
968+
967969
if (xutftowcs_path(wfilename, file_name) < 0)
968970
return -1;
969971

@@ -975,7 +977,17 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
975977
SetFileAttributesW(wfilename, attrs & ~FILE_ATTRIBUTE_READONLY);
976978
}
977979

978-
if ((fh = _wopen(wfilename, O_RDWR | O_BINARY)) < 0) {
980+
osfilehandle = CreateFileW(wfilename,
981+
FILE_WRITE_ATTRIBUTES,
982+
0 /*FileShare.None*/,
983+
NULL,
984+
OPEN_EXISTING,
985+
(attrs != INVALID_FILE_ATTRIBUTES &&
986+
(attrs & FILE_ATTRIBUTE_DIRECTORY)) ?
987+
FILE_FLAG_BACKUP_SEMANTICS : 0,
988+
NULL);
989+
if (osfilehandle == INVALID_HANDLE_VALUE) {
990+
errno = err_win_to_posix(GetLastError());
979991
rc = -1;
980992
goto revert_attrs;
981993
}
@@ -987,12 +999,15 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
987999
GetSystemTimeAsFileTime(&mft);
9881000
aft = mft;
9891001
}
990-
if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) {
1002+
1003+
if (!SetFileTime(osfilehandle, NULL, &aft, &mft)) {
9911004
errno = EINVAL;
9921005
rc = -1;
9931006
} else
9941007
rc = 0;
995-
close(fh);
1008+
1009+
if (osfilehandle != INVALID_HANDLE_VALUE)
1010+
CloseHandle(osfilehandle);
9961011

9971012
revert_attrs:
9981013
if (attrs != INVALID_FILE_ATTRIBUTES &&

0 commit comments

Comments
 (0)