{"thread":{"id":"23249","subject":"[PATCH] Windows: fix utime() for read-only files","startedAt":"2010-03-30T07:46:23Z","lastAt":"2010-03-30T07:46:23Z","messageCount":1,"participants":["Johannes Sixt"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"138165","messageId":"4BB1AC4F.4020906@viscovery.net","threadId":"23249","inReplyTo":null,"subject":"[PATCH] Windows: fix utime() for read-only files","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2010-03-30T07:46:23Z","receivedAt":"2010-03-30T07:46:23Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"From: Johannes Sixt <j6t@kdbg.org>\n\nStarting with 5256b00 (Use git_mkstemp_mode instead of plain mkstemp to\ncreate object files, 2010-02-22) utime() is invoked on read-only files.\nThis is not allowed on Windows and results in many warnings of the form\n\nfailed utime() on .git/objects/23/tmp_obj_VlgHlc: Permission denied\n\nduring a repack.  Fix it by making the file temporarily writable.\n\nSigned-off-by: Johannes Sixt <j6t@kdbg.org>\n---\n This should go on top of 5256b00 in maint.\n\n -- Hannes\n\n PS: First patch submission with a new MUA; I hope it gets through\n undamaged.\n\n compat/mingw.c |   20 ++++++++++++++++++--\n 1 files changed, 18 insertions(+), 2 deletions(-)\n\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex ab65f77..59b18dc 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -259,8 +259,17 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)\n \tint fh, rc;\n \n \t/* must have write permission */\n-\tif ((fh = open(file_name, O_RDWR | O_BINARY)) < 0)\n-\t\treturn -1;\n+\tDWORD attrs = GetFileAttributes(file_name);\n+\tif (attrs != INVALID_FILE_ATTRIBUTES &&\n+\t    (attrs & FILE_ATTRIBUTE_READONLY)) {\n+\t\t/* ignore errors here; open() will report them */\n+\t\tSetFileAttributes(file_name, attrs & ~FILE_ATTRIBUTE_READONLY);\n+\t}\n+\n+\tif ((fh = open(file_name, O_RDWR | O_BINARY)) < 0) {\n+\t\trc = -1;\n+\t\tgoto revert_attrs;\n+\t}\n \n \ttime_t_to_filetime(times->modtime, &mft);\n \ttime_t_to_filetime(times->actime, &aft);\n@@ -270,6 +279,13 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)\n \t} else\n \t\trc = 0;\n \tclose(fh);\n+\n+revert_attrs:\n+\tif (attrs != INVALID_FILE_ATTRIBUTES &&\n+\t    (attrs & FILE_ATTRIBUTE_READONLY)) {\n+\t\t/* ignore errors again */\n+\t\tSetFileAttributes(file_name, attrs);\n+\t}\n \treturn rc;\n }\n \n-- \n1.7.0.7.gcaca7\n"}]}