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

[PATCH 08/11] Close files opened by lock_file() before unlinking.

From
Johannes Sixt <johannes.sixt@telecom.at>
Date
Nov 13, 2007, 20:05 UTC
Message-ID
<1194984306-3181-9-git-send-email-johannes.sixt@telecom.at>
In-Reply-To
<1194984306-3181-8-git-send-email-johannes.sixt@telecom.at>
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
This is needed on Windows since open files cannot be unlinked.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
	This was authored by Dscho, but carries only my sign-off.
	Is this ok?
 cache.h    |    1 +
 lockfile.c |   17 ++++++++++-------
 2 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/cache.h b/cache.h
index f0a25c7..28870c5 100644
--- a/cache.h
+++ b/cache.h
@@ -284,6 +284,7 @@ extern int refresh_index(struct index_state *, unsigned int flags, const char **
 
 struct lock_file {
 	struct lock_file *next;
+	int fd;
 	pid_t owner;
 	char on_list;
 	char filename[PATH_MAX];
diff --git a/lockfile.c b/lockfile.c
index 9a1f64d..258fb3f 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -12,8 +12,10 @@ static void remove_lock_file(void)
 
 	while (lock_file_list) {
 		if (lock_file_list->owner == me &&
-		    lock_file_list->filename[0])
+		    lock_file_list->filename[0]) {
+			close(lock_file_list->fd);
 			unlink(lock_file_list->filename);
+		}
 		lock_file_list = lock_file_list->next;
 	}
 }
@@ -120,8 +122,6 @@ static char *resolve_symlink(char *p, size_t s)
 
 static int lock_file(struct lock_file *lk, const char *path)
 {
-	int fd;
-
 	if (strlen(path) >= sizeof(lk->filename)) return -1;
 	strcpy(lk->filename, path);
 	/*
@@ -130,8 +130,8 @@ static int lock_file(struct lock_file *lk, const char *path)
 	 */
 	resolve_symlink(lk->filename, sizeof(lk->filename)-5);
 	strcat(lk->filename, ".lock");
-	fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
-	if (0 <= fd) {
+	lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
+	if (0 <= lk->fd) {
 		if (!lock_file_list) {
 			signal(SIGINT, remove_lock_file_on_signal);
 			atexit(remove_lock_file);
@@ -148,7 +148,7 @@ static int lock_file(struct lock_file *lk, const char *path)
 	}
 	else
 		lk->filename[0] = 0;
-	return fd;
+	return lk->fd;
 }
 
 int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error)
@@ -163,6 +163,7 @@ int commit_lock_file(struct lock_file *lk)
 {
 	char result_file[PATH_MAX];
 	int i;
+	close(lk->fd);
 	strcpy(result_file, lk->filename);
 	i = strlen(result_file) - 5; /* .lock */
 	result_file[i] = 0;
@@ -194,7 +195,9 @@ int commit_locked_index(struct lock_file *lk)
 
 void rollback_lock_file(struct lock_file *lk)
 {
-	if (lk->filename[0])
+	if (lk->filename[0]) {
+		close(lk->fd);
 		unlink(lk->filename);
+	}
 	lk->filename[0] = 0;
 }
-- 
1.5.3.5.1592.g0d6db
Previous: Johannes SixtNext: Johannes Sixt
Message 9 of 35 in “Miscellaneous MinGW port fallout”
  1. 0/11 Miscellaneous MinGW port falloutJohannes Sixt, Nov 13, 2007
  2. 01/11 t5300-pack-object.sh: Split the big verify-pack test into smaller parts.Johannes Sixt, Nov 13, 2007
  3. 02/11 t7501-commit.sh: Not all seds understand option -iJohannes Sixt, Nov 13, 2007
  4. 03/11 t5302-pack-index: Skip tests of 64-bit offsets if necessary.Johannes Sixt, Nov 13, 2007
  5. 04/11 Skip t3902-quoted.sh if the file system does not support funny names.Johannes Sixt, Nov 13, 2007
  6. 05/11 Use is_absolute_path() in sha1_file.c.Johannes Sixt, Nov 13, 2007
  7. 06/11 Move #include <sys/select.h> and <sys/ioctl.h> to git-compat-util.h.Johannes Sixt, Nov 13, 2007
  8. 07/11 builtin run_command: do not exit with -1.Johannes Sixt, Nov 13, 2007
  9. 08/11 Close files opened by lock_file() before unlinking.Johannes Sixt, Nov 13, 2007
  10. 09/11 Allow a relative builtin template directory.Johannes Sixt, Nov 13, 2007
  11. 10/11 Introduce git_etc_gitconfig() that encapsulates access of ETC_GITCONFIG.Johannes Sixt, Nov 13, 2007
  12. 11/11 Allow ETC_GITCONFIG to be a relative path.Johannes Sixt, Nov 13, 2007
  13. Steffen ProhaskaNov 15, 2007
  14. Johannes SixtNov 15, 2007
  15. Steffen ProhaskaNov 15, 2007
  16. Johannes SixtNov 15, 2007
  17. Steffen ProhaskaNov 15, 2007
  18. Johannes SchindelinNov 13, 2007
  19. Johannes SixtNov 13, 2007
  20. Johannes SchindelinNov 13, 2007
  21. Johannes SchindelinNov 13, 2007
  22. Johannes SchindelinNov 13, 2007
  23. Johannes SixtNov 13, 2007
  24. Johannes SchindelinNov 13, 2007
  25. Robin RosenbergNov 14, 2007
  26. Johannes SixtNov 13, 2007
  27. Junio C HamanoNov 13, 2007
  28. Johannes SixtNov 13, 2007
  29. Johannes SchindelinNov 13, 2007
  30. Johannes SixtNov 13, 2007
  31. Junio C HamanoNov 13, 2007
  32. Junio C HamanoNov 14, 2007
  33. Johannes SchindelinNov 14, 2007
  34. Junio C HamanoNov 14, 2007
  35. Johannes SchindelinNov 14, 2007

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.