Fix working directory errno handling when unlinking a directory
Alex Riesen noticed that the case where a file replaced a directory entry in the working tree was broken on cygwin. It turns out that the code made some Linux-specific assumptions, and also ignored errors entirely for the case where the entry was a symlink rather than a file. This cleans it up by separating out the common case into a function of its own, so that both regular files and symlinks can share it, and by making the error handling more obvious (and not depend on any Linux-specific behaviour). Acked-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Apr 19, 2007 at 22:48 UTC
851c603e9ca9d0954d89be1532d924a28ccb79fa
1 file changed
+29
-25
merge-recursive.c
+29
-25
@@ -574,6 +574,31 @@ static void flush_buffer(int fd, const char *buf, unsigned long size)
574
}
575
}
576
577
+static int make_room_for_path(const char *path)
578
+{
579
+ int status;
580
+ const char *msg = "failed to create path '%s'%s";
581
+
582
+ status = mkdir_p(path, 0777);
583
+ if (status) {
584
+ if (status == -3) {
585
+ /* something else exists */
586
+ error(msg, path, ": perhaps a D/F conflict?");
587
+ return -1;
588
+ }
589
+ die(msg, path, "");
590
+ }
591
+
592
+ /* Successful unlink is good.. */
593
+ if (!unlink(path))
594
+ return 0;
595
+ /* .. and so is no existing file */
596
+ if (errno == ENOENT)
597
+ return 0;
598
+ /* .. but not some other error (who really cares what?) */
599
+ return error(msg, path, ": perhaps a D/F conflict?");
600
+}
601
+
602
static void update_file_flags(const unsigned char *sha,
603
unsigned mode,
604
const char *path,
@@ -594,33 +619,12 @@ static void update_file_flags(const unsigned char *sha,
619
if (type != OBJ_BLOB)
620
die("blob expected for %s '%s'", sha1_to_hex(sha), path);
621
622
+ if (make_room_for_path(path) < 0) {
623
+ update_wd = 0;
624
+ goto update_index;
625
+ }
626
if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
627
int fd;
599
- int status;
600
- const char *msg = "failed to create path '%s'%s";
601
-
602
- status = mkdir_p(path, 0777);
603
- if (status) {
604
- if (status == -3) {
605
- /* something else exists */
606
- error(msg, path, ": perhaps a D/F conflict?");
607
- update_wd = 0;
608
- goto update_index;
609
- }
610
- die(msg, path, "");
611
- }
612
- if (unlink(path)) {
613
- if (errno == EISDIR) {
614
- /* something else exists */
615
- error(msg, path, ": perhaps a D/F conflict?");
616
- update_wd = 0;
617
- goto update_index;
618
- }
619
- if (errno != ENOENT)
620
- die("failed to unlink %s "
621
- "in preparation to update: %s",
622
- path, strerror(errno));
623
- }
628
if (mode & 0100)
629
mode = 0777;
630
else