Apply vodz' last_patch52
[oweals/busybox.git] / libbb / copy_file.c
index 4ee3efdbc59270140663d5ac0b44356d9c48ce03..3d174ddb3464db5fb4d2a69450b253efab9280c2 100644 (file)
@@ -2,7 +2,6 @@
 /*
  * Mini copy_file implementation for busybox
  *
- *
  * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -29,8 +28,9 @@
 #include <errno.h>
 #include <dirent.h>
 #include <stdlib.h>
+#include <string.h>
 
-#include "libbb.h"
+#include "busybox.h"
 
 int copy_file(const char *source, const char *dest, int flags)
 {
@@ -39,9 +39,9 @@ int copy_file(const char *source, const char *dest, int flags)
        int dest_exists = 1;
        int status = 0;
 
-       if (((flags & FILEUTILS_PRESERVE_SYMLINKS) &&
+       if ((!(flags & FILEUTILS_DEREFERENCE) &&
                        lstat(source, &source_stat) < 0) ||
-                       (!(flags & FILEUTILS_PRESERVE_SYMLINKS) &&
+                       ((flags & FILEUTILS_DEREFERENCE) &&
                         stat(source, &source_stat) < 0)) {
                perror_msg("%s", source);
                return -1;
@@ -55,7 +55,7 @@ int copy_file(const char *source, const char *dest, int flags)
                dest_exists = 0;
        }
 
-       if (dest_exists && source_stat.st_rdev == dest_stat.st_rdev &&
+       if (dest_exists && source_stat.st_dev == dest_stat.st_dev &&
                        source_stat.st_ino == dest_stat.st_ino) {
                error_msg("`%s' and `%s' are the same file", source, dest);
                return -1;
@@ -64,6 +64,7 @@ int copy_file(const char *source, const char *dest, int flags)
        if (S_ISDIR(source_stat.st_mode)) {
                DIR *dp;
                struct dirent *d;
+               mode_t saved_umask = 0;
 
                if (!(flags & FILEUTILS_RECUR)) {
                        error_msg("%s: omitting directory", source);
@@ -77,7 +78,7 @@ int copy_file(const char *source, const char *dest, int flags)
                                return -1;
                        }
                } else {
-                       mode_t mode, saved_umask;
+                       mode_t mode;
                        saved_umask = umask(0);
 
                        mode = source_stat.st_mode;
@@ -93,7 +94,7 @@ int copy_file(const char *source, const char *dest, int flags)
 
                        umask(saved_umask);
                }
-               
+
                /* Recursively copy files in SOURCE.  */
                if ((dp = opendir(source)) == NULL) {
                        perror_msg("unable to open directory `%s'", source);
@@ -115,31 +116,59 @@ int copy_file(const char *source, const char *dest, int flags)
                        free(new_source);
                        free(new_dest);
                }
-               
+
                /* ??? What if an error occurs in readdir?  */
 
                if (closedir(dp) < 0) {
                        perror_msg("unable to close directory `%s'", source);
                        status = -1;
                }
+
+               if (!dest_exists &&
+                               chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
+                       perror_msg("unable to change permissions of `%s'", dest);
+                       status = -1;
+               }
        } else if (S_ISREG(source_stat.st_mode)) {
-               FILE *sfp, *dfp;
+               FILE *sfp, *dfp=NULL;
+#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
+               char *link_name;
+
+               if (!(flags & FILEUTILS_DEREFERENCE) &&
+                               is_in_ino_dev_hashtable(&source_stat, &link_name)) {
+                       if (link(link_name, dest) < 0) {
+                               perror_msg("unable to link `%s'", dest);
+                               return -1;
+                       }
+
+                       return 0;
+               }
+#endif
+
+               if ((sfp = fopen(source, "r")) == NULL) {
+                       perror_msg("unable to open `%s'", source);
+                       return -1;
+               }
 
                if (dest_exists) {
                        if (flags & FILEUTILS_INTERACTIVE) {
                                fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
-                               if (!ask_confirmation())
+                               if (!ask_confirmation()) {
+                                       fclose (sfp);
                                        return 0;
+                               }
                        }
 
                        if ((dfp = fopen(dest, "w")) == NULL) {
                                if (!(flags & FILEUTILS_FORCE)) {
                                        perror_msg("unable to open `%s'", dest);
+                                       fclose (sfp);
                                        return -1;
                                }
 
                                if (unlink(dest) < 0) {
                                        perror_msg("unable to remove `%s'", dest);
+                                       fclose (sfp);
                                        return -1;
                                }
 
@@ -155,18 +184,13 @@ int copy_file(const char *source, const char *dest, int flags)
                                if (fd >= 0)
                                        close(fd);
                                perror_msg("unable to open `%s'", dest);
+                               fclose (sfp);
                                return -1;
                        }
                }
 
-               if ((sfp = fopen(source, "r")) == NULL) {
-                       fclose(dfp);
-                       perror_msg("unable to open `%s'", source);
+               if (copy_file_chunk(sfp, dfp, -1) < 0)
                        status = -1;
-                       goto end;
-               }
-
-               copy_file_chunk(sfp, dfp, source_stat.st_size);
 
                if (fclose(dfp) < 0) {
                        perror_msg("unable to close `%s'", dest);
@@ -184,46 +208,38 @@ int copy_file(const char *source, const char *dest, int flags)
                        return -1;
                }
        } else if (S_ISFIFO(source_stat.st_mode)) {
-               mode_t mode, saved_umask;
-               saved_umask = umask(0);
-
-               mode = source_stat.st_mode;
-               if (!(flags & FILEUTILS_PRESERVE_STATUS))
-                       mode = source_stat.st_mode & ~saved_umask;
-               mode |= S_IRWXU;
-
-               if (mkfifo(dest, mode) < 0) {
-                       umask(saved_umask);
+               if (mkfifo(dest, source_stat.st_mode) < 0) {
                        perror_msg("cannot create fifo `%s'", dest);
                        return -1;
                }
-
-               umask(saved_umask);
        } else if (S_ISLNK(source_stat.st_mode)) {
-               char buf[BUFSIZ + 1];
-
-               if (readlink(source, buf, BUFSIZ) < 0) {
-                       perror_msg("cannot read `%s'", source);
-                       return -1;
-               }
-               buf[BUFSIZ] = '\0';
-
-               if (symlink(buf, dest) < 0) {
+               char *lpath = xreadlink(source);
+               if (symlink(lpath, dest) < 0) {
                        perror_msg("cannot create symlink `%s'", dest);
                        return -1;
                }
+               free(lpath);
 
 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
                if (flags & FILEUTILS_PRESERVE_STATUS)
                        if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
                                perror_msg("unable to preserve ownership of `%s'", dest);
 #endif
+
+#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
+               add_to_ino_dev_hashtable(&source_stat, dest);
+#endif
+
                return 0;
        } else {
                error_msg("internal error: unrecognized file type");
                return -1;
        }
 
+#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
+       add_to_ino_dev_hashtable(&source_stat, dest);
+#endif
+
 end:
 
        if (flags & FILEUTILS_PRESERVE_STATUS) {
@@ -241,5 +257,5 @@ end:
                        perror_msg("unable to preserve permissions of `%s'", dest);
        }
 
-       return 0;
+       return status;
 }