apply: check D/F conflicts more carefully.
When creating a new file where a directory used to be (or the user had an empty directory) the code did not check the result from lstat() closely enough, and mistakenly thought the path already existed in the working tree. This does not fix the problem where you have a patch that creates a file at "foo" and removes a file at "foo/bar" (which presumably is the last file in "foo/" directory in the original). For that, we would need to restructure write_out_results() loop. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jul 16, 2006 at 23:28 UTC
c28c571c143a5145665f4bf334671ac3a7d0980c
1 file changed
+18
-3
builtin-apply.c
+18
-3
@@ -1732,9 +1732,14 @@ static int check_patch(struct patch *patch)
1732
if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
1733
return error("%s: already exists in index", new_name);
1734
if (!cached) {
1735
- if (!lstat(new_name, &st))
1736
- return error("%s: already exists in working directory", new_name);
1737
- if (errno != ENOENT)
1735
+ struct stat nst;
1736
+ if (!lstat(new_name, &nst)) {
1737
+ if (S_ISDIR(nst.st_mode))
1738
+ ; /* ok */
1739
+ else
1740
+ return error("%s: already exists in working directory", new_name);
1741
+ }
1742
+ else if ((errno != ENOENT) && (errno != ENOTDIR))
1743
return error("%s: %s", new_name, strerror(errno));
1744
}
1745
if (!patch->new_mode) {
@@ -2010,6 +2015,16 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
2015
return;
2016
}
2017
2018
+ if (errno == EEXIST) {
2019
+ /* We may be trying to create a file where a directory
2020
+ * used to be.
2021
+ */
2022
+ struct stat st;
2023
+ errno = 0;
2024
+ if (!lstat(path, &st) && S_ISDIR(st.st_mode) && !rmdir(path))
2025
+ errno = EEXIST;
2026
+ }
2027
+
2028
if (errno == EEXIST) {
2029
unsigned int nr = getpid();
2030