- add a few basic tests for pidof(8)
[oweals/busybox.git] / libbb / copy_file.c
index 81c547479981ce8f97f960af1ac31830770da96a..cd6d3802294baafa11ad37dd4d3072265fd889c6 100644 (file)
@@ -54,10 +54,11 @@ int copy_file(const char *source, const char *dest, int flags)
                }
        } else {
                if (source_stat.st_dev == dest_stat.st_dev &&
-                       source_stat.st_ino == dest_stat.st_ino) {
-               bb_error_msg("`%s' and `%s' are the same file", source, dest);
-               return -1;
-       }
+                       source_stat.st_ino == dest_stat.st_ino)
+               {
+                       bb_error_msg("`%s' and `%s' are the same file", source, dest);
+                       return -1;
+               }
                dest_exists = 1;
        }
 
@@ -105,11 +106,9 @@ int copy_file(const char *source, const char *dest, int flags)
                while ((d = readdir(dp)) != NULL) {
                        char *new_source, *new_dest;
 
-                       if (strcmp(d->d_name, ".") == 0 ||
-                                       strcmp(d->d_name, "..") == 0)
+                       new_source = concat_subpath_file(source, d->d_name);
+                       if(new_source == NULL)
                                continue;
-
-                       new_source = concat_path_file(source, d->d_name);
                        new_dest = concat_path_file(dest, d->d_name);
                        if (copy_file(new_source, new_dest, flags) < 0)
                                status = -1;
@@ -125,7 +124,8 @@ int copy_file(const char *source, const char *dest, int flags)
                        status = -1;
                }
        } else if (S_ISREG(source_stat.st_mode)) {
-               FILE *sfp, *dfp=NULL;
+               int src_fd;
+               int dst_fd;
 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
                char *link_name;
 
@@ -139,30 +139,32 @@ int copy_file(const char *source, const char *dest, int flags)
                        return 0;
                }
 #endif
-
-               if ((sfp = bb_wfopen(source, "r")) == NULL) {
-                       return -1;
+               src_fd = open(source, O_RDONLY);
+               if (src_fd == -1) {
+                       bb_perror_msg("unable to open `%s'", source);
+                       return(-1);
                }
 
                if (dest_exists) {
                        if (flags & FILEUTILS_INTERACTIVE) {
                                fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
                                if (!bb_ask_confirmation()) {
-                                       fclose (sfp);
+                                       close (src_fd);
                                        return 0;
                                }
                        }
 
-                       if ((dfp = fopen(dest, "w")) == NULL) {
+                       dst_fd = open(dest, O_WRONLY|O_TRUNC);
+                       if (dst_fd == -1) {
                                if (!(flags & FILEUTILS_FORCE)) {
                                        bb_perror_msg("unable to open `%s'", dest);
-                                       fclose (sfp);
+                                       close(src_fd);
                                        return -1;
                                }
 
                                if (unlink(dest) < 0) {
                                        bb_perror_msg("unable to remove `%s'", dest);
-                                       fclose (sfp);
+                                       close(src_fd);
                                        return -1;
                                }
 
@@ -171,27 +173,23 @@ int copy_file(const char *source, const char *dest, int flags)
                }
 
                if (!dest_exists) {
-                       int fd;
-
-                       if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 ||
-                                       (dfp = fdopen(fd, "w")) == NULL) {
-                               if (fd >= 0)
-                                       close(fd);
+                       dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
+                       if (dst_fd == -1) {
                                bb_perror_msg("unable to open `%s'", dest);
-                               fclose (sfp);
-                               return -1;
+                               close(src_fd);
+                               return(-1);
                        }
                }
 
-               if (bb_copyfd(fileno(sfp), fileno(dfp), 0) == -1)
+               if (bb_copyfd_eof(src_fd, dst_fd) == -1)
                        status = -1;
 
-               if (fclose(dfp) < 0) {
+               if (close(dst_fd) < 0) {
                        bb_perror_msg("unable to close `%s'", dest);
                        status = -1;
                }
 
-               if (fclose(sfp) < 0) {
+               if (close(src_fd) < 0) {
                        bb_perror_msg("unable to close `%s'", source);
                        status = -1;
                }
@@ -200,12 +198,16 @@ int copy_file(const char *source, const char *dest, int flags)
            S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
            S_ISLNK(source_stat.st_mode)) {
 
-               if (dest_exists &&
-                      ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) {
+               if (dest_exists) {
+                       if((flags & FILEUTILS_FORCE) == 0) {
+                               fprintf(stderr, "`%s' exists\n", dest);
+                               return -1;
+                       }
+                       if(unlink(dest) < 0) {
                                bb_perror_msg("unable to remove `%s'", dest);
                                return -1;
-
                        }
+               }
        } else {
                bb_error_msg("internal error: unrecognized file type");
                return -1;
@@ -245,7 +247,9 @@ int copy_file(const char *source, const char *dest, int flags)
        }
 
 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
-       add_to_ino_dev_hashtable(&source_stat, dest);
+       if (! S_ISDIR(source_stat.st_mode)) {
+               add_to_ino_dev_hashtable(&source_stat, dest);
+       }
 #endif
 
 end: