threads / patch / 7726

patchFix overwriting of the files to apply the patch to in git-apply

Subject: [PATCH] Fix overwriting of the files to apply the patch to in git-apply

## tl;dr

2 messages between Apr 18, 2007 and Apr 18, 2007. Diffs are folded; open one to read it.

replies: 1people: 1as markdown or json

Alex Riesen· Apr 18, 2007, 16:29 UTC · lore

The check for existence of a file was wrong: it assumed it was a directory and reset the errno (twice: directly and by calling lstat). So if an entry existed and was _not_ a directory no attempt was made to rename into it, because the errno (expected by renaming code) was already reset to 0.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---

The decision to check for directory first looks suboptimal. Why not try to _rename_ first and if that fails check if we've got a directory? An existing file seems to be more of a common case.

P.S. gmail: will resend unless applied
 builtin-apply.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
From 23d8d5677f8c34c8c17bef1c7678cca08ba856b6 Mon Sep 17 00:00:00 2001
From: Alex Riesen <raa.lkml@gmail.com>
Date: Wed, 18 Apr 2007 18:24:53 +0200
Subject: [PATCH] Fix overwriting of the files to apply the patch to in git-apply

The check for existence of a file was wrong: it assumed it was a directory and reset the errno (twice: directly and by calling lstat). So if an entry existed and was _not_ a directory no attempt was made to rename into it, because the errno (expected by renaming code) was already reset to 0.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
 builtin-apply.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to builtin-apply.c +1 −1
diff --git a/builtin-apply.c b/builtin-apply.c
index fd92ef7..240cc08 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -2417,7 +2417,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
 		 */
 		struct stat st;
 		errno = 0;
-		if (!lstat(path, &st) && S_ISDIR(st.st_mode) && !rmdir(path))
+		if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
 			errno = EEXIST;
 	}
 
-- 
1.5.1.1.195.gdcb3f
Alex Riesen· Apr 18, 2007, 16:33 UTC · re: Alex Riesen · lore

Re: [PATCH] Fix overwriting of the files to apply the patch to in git-apply

On 4/18/07, Alex Riesen <raa.lkml@gmail.com> wrote:
> The decision to check for directory first looks suboptimal.
> Why not try to _rename_ first and if that fails check if we've got a directory?
> An existing file seems to be more of a common case.
Simplier to code and read, for one.

← back to recent threads