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

2 messages from 2007-04-18 to 2007-04-18. Participants: Alex Riesen.
Thread: https://gitlist.dev/t/7726

## Alex Riesen, 2007-04-18 16:29

Subject: [PATCH] Fix overwriting of the files to apply the patch to in git-apply
Message-ID: <81b0412b0704180929h7960bedeje87050fa5ca543b0@mail.gmail.com>
URL: https://gitlist.dev/e/81b0412b0704180929h7960bedeje87050fa5ca543b0%40mail.gmail.com

```
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(-)

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, 2007-04-18 16:33

Subject: Re: [PATCH] Fix overwriting of the files to apply the patch to in git-apply
Message-ID: <81b0412b0704180933t7f161bcah57bce6dbea43a710@mail.gmail.com>
URL: https://gitlist.dev/e/81b0412b0704180933t7f161bcah57bce6dbea43a710%40mail.gmail.com
In-Reply-To: <81b0412b0704180929h7960bedeje87050fa5ca543b0@mail.gmail.com>

```
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.

```
