git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Windows: fix utime() for read-only files

From
Johannes Sixt <j.sixt@viscovery.net>
Date
Mar 30, 2010, 07:46 UTC
Message-ID
<4BB1AC4F.4020906@viscovery.net>
From: Johannes Sixt <j6t@kdbg.org>

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>
---
 This should go on top of 5256b00 in maint.
 -- Hannes
 PS: First patch submission with a new MUA; I hope it gets through
 undamaged.
 compat/mingw.c |   20 ++++++++++++++++++--
 1 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index ab65f77..59b18dc 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -259,8 +259,17 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
 	int fh, rc;
 
 	/* must have write permission */
-	if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0)
-		return -1;
+	DWORD attrs = GetFileAttributes(file_name);
+	if (attrs != INVALID_FILE_ATTRIBUTES &&
+	    (attrs & FILE_ATTRIBUTE_READONLY)) {
+		/* ignore errors here; open() will report them */
+		SetFileAttributes(file_name, attrs & ~FILE_ATTRIBUTE_READONLY);
+	}
+
+	if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0) {
+		rc = -1;
+		goto revert_attrs;
+	}
 
 	time_t_to_filetime(times->modtime, &mft);
 	time_t_to_filetime(times->actime, &aft);
@@ -270,6 +279,13 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
 	} else
 		rc = 0;
 	close(fh);
+
+revert_attrs:
+	if (attrs != INVALID_FILE_ATTRIBUTES &&
+	    (attrs & FILE_ATTRIBUTE_READONLY)) {
+		/* ignore errors again */
+		SetFileAttributes(file_name, attrs);
+	}
 	return rc;
 }
 
-- 
1.7.0.7.gcaca7
Message 1 of 1 in “Windows: fix utime() for read-only files”
  1. Windows: fix utime() for read-only filesJohannes Sixt, Mar 30, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.