@matheusbsbs / matheus-bagliano / commits / 852f098c06
Fork 0

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 <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Sixt committed Mar 30, 2010 at 09:46 UTC 852f098c0667abc634f5aba0b91e0317f714fa67
1 file changed +18 -2
compat/mingw.c
+18 -2
@@ -259,8 +259,17 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
259 int fh, rc;
260
261 /* 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 + }
273
274 time_t_to_filetime(times->modtime, &mft);
275 time_t_to_filetime(times->actime, &aft);
@@ -270,6 +279,13 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
279 } else
280 rc = 0;
281 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 + }
289 return rc;
290 }
291