Properly honor FILEUTILS_INTERACTIVE and FILEUTILS_FORCE for
authorEric Andersen <andersen@codepoet.org>
Mon, 16 Sep 2002 09:23:22 +0000 (09:23 -0000)
committerEric Andersen <andersen@codepoet.org>
Mon, 16 Sep 2002 09:23:22 +0000 (09:23 -0000)
file all file types (not just regular files and dirs).  Unlink
destination files when needed.
 -Erik

libbb/copy_file.c

index 3d174ddb3464db5fb4d2a69450b253efab9280c2..1a584017f157ba02f4cee0d9e670c5b453844716 100644 (file)
@@ -203,17 +203,81 @@ int copy_file(const char *source, const char *dest, int flags)
                }
        } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
                        S_ISSOCK(source_stat.st_mode)) {
+
+               if (dest_exists) {
+                       if (flags & FILEUTILS_INTERACTIVE) {
+                               fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
+                               if (!ask_confirmation())
+                                       return 0;
+                       }
+
+                       if (!(flags & FILEUTILS_FORCE)) {
+                               perror_msg("unable to remove `%s'", dest);
+                               return -1;
+                       }
+
+                       if (unlink(dest) < 0) {
+                               perror_msg("unable to remove `%s'", dest);
+                               return -1;
+                       }
+
+                       dest_exists = 0;
+               }
+
                if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
                        perror_msg("unable to create `%s'", dest);
                        return -1;
                }
        } else if (S_ISFIFO(source_stat.st_mode)) {
+
+               if (dest_exists) {
+                       if (flags & FILEUTILS_INTERACTIVE) {
+                               fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
+                               if (!ask_confirmation())
+                                       return 0;
+                       }
+
+                       if (!(flags & FILEUTILS_FORCE)) {
+                               perror_msg("unable to remove `%s'", dest);
+                               return -1;
+                       }
+
+                       if (unlink(dest) < 0) {
+                               perror_msg("unable to remove `%s'", dest);
+                               return -1;
+                       }
+
+                       dest_exists = 0;
+               }
+
                if (mkfifo(dest, source_stat.st_mode) < 0) {
                        perror_msg("cannot create fifo `%s'", dest);
                        return -1;
                }
        } else if (S_ISLNK(source_stat.st_mode)) {
-               char *lpath = xreadlink(source);
+               char *lpath;
+
+               if (dest_exists) {
+                       if (flags & FILEUTILS_INTERACTIVE) {
+                               fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
+                               if (!ask_confirmation())
+                                       return 0;
+                       }
+
+                       if (!(flags & FILEUTILS_FORCE)) {
+                               perror_msg("unable to remove `%s'", dest);
+                               return -1;
+                       }
+
+                       if (unlink(dest) < 0) {
+                               perror_msg("unable to remove `%s'", dest);
+                               return -1;
+                       }
+
+                       dest_exists = 0;
+               }
+
+               lpath = xreadlink(source);
                if (symlink(lpath, dest) < 0) {
                        perror_msg("cannot create symlink `%s'", dest);
                        return -1;