Skip to content

Commit 852f098

Browse files
j6tgitster
authored andcommitted
Windows: fix utime() for read-only files
Starting with 5256b00 (Use git_mkstemp_mode instead of plain mkstemp to create object files, 2010-02-22) utime() is invoked on read-only files. This is not allowed on Windows and results in many warnings of the form failed utime() on .git/objects/23/tmp_obj_VlgHlc: Permission denied during a repack. Fix it by making the file temporarily writable. Signed-off-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent da1fbed commit 852f098

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

compat/mingw.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,17 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
259259
int fh, rc;
260260

261261
/* must have write permission */
262-
if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0)
263-
return -1;
262+
DWORD attrs = GetFileAttributes(file_name);
263+
if (attrs != INVALID_FILE_ATTRIBUTES &&
264+
(attrs & FILE_ATTRIBUTE_READONLY)) {
265+
/* ignore errors here; open() will report them */
266+
SetFileAttributes(file_name, attrs & ~FILE_ATTRIBUTE_READONLY);
267+
}
268+
269+
if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0) {
270+
rc = -1;
271+
goto revert_attrs;
272+
}
264273

265274
time_t_to_filetime(times->modtime, &mft);
266275
time_t_to_filetime(times->actime, &aft);
@@ -270,6 +279,13 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
270279
} else
271280
rc = 0;
272281
close(fh);
282+
283+
revert_attrs:
284+
if (attrs != INVALID_FILE_ATTRIBUTES &&
285+
(attrs & FILE_ATTRIBUTE_READONLY)) {
286+
/* ignore errors again */
287+
SetFileAttributes(file_name, attrs);
288+
}
273289
return rc;
274290
}
275291

0 commit comments

Comments
 (0)