aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-12-17 15:26:36 +0000
committerkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-12-17 15:26:36 +0000
commita71ab54376eed7d51c028ca7986fa7667417d3c7 (patch)
tree1dec04901febc0fd63ac5b92bc2cca4333689477 /libbb
parentb38aba2140fc308cb4040e2241bdff352f0f93e8 (diff)
downloadbusybox-w32-a71ab54376eed7d51c028ca7986fa7667417d3c7.tar.gz
busybox-w32-a71ab54376eed7d51c028ca7986fa7667417d3c7.tar.bz2
busybox-w32-a71ab54376eed7d51c028ca7986fa7667417d3c7.zip
Make cp and mv optionally preserve hard links.
git-svn-id: svn://busybox.net/trunk/busybox@3894 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Makefile2
-rw-r--r--libbb/copy_file.c24
-rw-r--r--libbb/inode_hash.c7
3 files changed, 31 insertions, 2 deletions
diff --git a/libbb/Makefile b/libbb/Makefile
index ef8fef4b7..879be2452 100644
--- a/libbb/Makefile
+++ b/libbb/Makefile
@@ -45,7 +45,7 @@ obj-y += ask_confirmation.o chomp.o concat_path_file.o copy_file.o \
45 xgetcwd.o xreadlink.o xregcomp.o interface.o remove_file.o last_char_is.o \ 45 xgetcwd.o xreadlink.o xregcomp.o interface.o remove_file.o last_char_is.o \
46 copyfd.o vherror_msg.o herror_msg.o herror_msg_and_die.o xgethostbyname.o \ 46 copyfd.o vherror_msg.o herror_msg.o herror_msg_and_die.o xgethostbyname.o \
47 dirname.o make_directory.o create_icmp_socket.o u_signal_names.o arith.o \ 47 dirname.o make_directory.o create_icmp_socket.o u_signal_names.o arith.o \
48 simplify_path.o inet_common.o $(LIBBB_MOBJS) $(LIBBB_AROBJS) 48 simplify_path.o inet_common.o inode_hash.o $(LIBBB_MOBJS) $(LIBBB_AROBJS)
49 49
50 50
51# Hand off to toplevel Rules.mak 51# Hand off to toplevel Rules.mak
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 @@
30#include <stdlib.h> 30#include <stdlib.h>
31#include <string.h> 31#include <string.h>
32 32
33#include "libbb.h" 33#include "busybox.h"
34 34
35int copy_file(const char *source, const char *dest, int flags) 35int copy_file(const char *source, const char *dest, int flags)
36{ 36{
@@ -131,6 +131,19 @@ int copy_file(const char *source, const char *dest, int flags)
131 } 131 }
132 } else if (S_ISREG(source_stat.st_mode)) { 132 } else if (S_ISREG(source_stat.st_mode)) {
133 FILE *sfp, *dfp=NULL; 133 FILE *sfp, *dfp=NULL;
134#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
135 char *link_name;
136
137 if (!(flags & FILEUTILS_DEREFERENCE) &&
138 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
139 if (link(link_name, dest) < 0) {
140 perror_msg("unable to link `%s'", dest);
141 return -1;
142 }
143
144 return 0;
145 }
146#endif
134 147
135 if ((sfp = fopen(source, "r")) == NULL) { 148 if ((sfp = fopen(source, "r")) == NULL) {
136 perror_msg("unable to open `%s'", source); 149 perror_msg("unable to open `%s'", source);
@@ -212,12 +225,21 @@ int copy_file(const char *source, const char *dest, int flags)
212 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) 225 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
213 perror_msg("unable to preserve ownership of `%s'", dest); 226 perror_msg("unable to preserve ownership of `%s'", dest);
214#endif 227#endif
228
229#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
230 add_to_ino_dev_hashtable(&source_stat, dest);
231#endif
232
215 return 0; 233 return 0;
216 } else { 234 } else {
217 error_msg("internal error: unrecognized file type"); 235 error_msg("internal error: unrecognized file type");
218 return -1; 236 return -1;
219 } 237 }
220 238
239#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
240 add_to_ino_dev_hashtable(&source_stat, dest);
241#endif
242
221end: 243end:
222 244
223 if (flags & FILEUTILS_PRESERVE_STATUS) { 245 if (flags & FILEUTILS_PRESERVE_STATUS) {
diff --git a/libbb/inode_hash.c b/libbb/inode_hash.c
index 52c54cdc1..36484e6ae 100644
--- a/libbb/inode_hash.c
+++ b/libbb/inode_hash.c
@@ -29,6 +29,13 @@
29#define HASH_SIZE 311 /* Should be prime */ 29#define HASH_SIZE 311 /* Should be prime */
30#define hash_inode(i) ((i) % HASH_SIZE) 30#define hash_inode(i) ((i) % HASH_SIZE)
31 31
32typedef struct ino_dev_hash_bucket_struct {
33 struct ino_dev_hash_bucket_struct *next;
34 ino_t ino;
35 dev_t dev;
36 char name[1];
37} ino_dev_hashtable_bucket_t;
38
32static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE]; 39static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
33 40
34/* 41/*