From ace02dc9cd3ca0c95db5b5ebe87b9d6cd6ca1733 Mon Sep 17 00:00:00 2001 From: Matt Kraai Date: Mon, 17 Dec 2001 15:26:36 +0000 Subject: Make cp and mv optionally preserve hard links. --- libbb/copy_file.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'libbb/copy_file.c') diff --git a/libbb/copy_file.c b/libbb/copy_file.c index 29778f2a4..ea05c9b8e 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -30,7 +30,7 @@ #include #include -#include "libbb.h" +#include "busybox.h" int copy_file(const char *source, const char *dest, int flags) { @@ -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) { -- cgit v1.2.3