Apply vodz' last_patch52
[oweals/busybox.git] / libbb / copy_file.c
index 29778f2a454703b24a125a8f4d81f6abf27c0718..3d174ddb3464db5fb4d2a69450b253efab9280c2 100644 (file)
@@ -30,7 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "libbb.h"
+#include "busybox.h"
 
 int copy_file(const char *source, const char *dest, int flags)
 {
@@ -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;
@@ -131,6 +131,19 @@ int copy_file(const char *source, const char *dest, int flags)
                }
        } else if (S_ISREG(source_stat.st_mode)) {
                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);
@@ -212,12 +225,21 @@ int copy_file(const char *source, const char *dest, int flags)
                        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) {