aboutsummaryrefslogtreecommitdiff
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
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
-rw-r--r--coreutils/du.c74
-rw-r--r--include/libbb.h4
-rw-r--r--libbb/Makefile2
-rw-r--r--libbb/copy_file.c24
-rw-r--r--libbb/inode_hash.c7
-rw-r--r--testsuite/cp/cp-preserves-hard-links6
6 files changed, 42 insertions, 75 deletions
diff --git a/coreutils/du.c b/coreutils/du.c
index c378837d0..e34a7f46f 100644
--- a/coreutils/du.c
+++ b/coreutils/du.c
@@ -59,78 +59,6 @@ static void print_summary(long size, char *filename)
59 } 59 }
60} 60}
61 61
62#define HASH_SIZE 311 /* Should be prime */
63#define hash_inode(i) ((i) % HASH_SIZE)
64
65typedef struct ino_dev_hash_bucket_struct {
66 struct ino_dev_hash_bucket_struct *next;
67 ino_t ino;
68 dev_t dev;
69 char name[1];
70} ino_dev_hashtable_bucket_t;
71
72static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
73
74/*
75 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
76 * `ino_dev_hashtable', else return 0
77 *
78 * If NAME is a non-NULL pointer to a character pointer, and there is
79 * a match, then set *NAME to the value of the name slot in that
80 * bucket.
81 */
82static int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
83{
84 ino_dev_hashtable_bucket_t *bucket;
85
86 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
87 while (bucket != NULL) {
88 if ((bucket->ino == statbuf->st_ino) &&
89 (bucket->dev == statbuf->st_dev))
90 {
91 if (name) *name = bucket->name;
92 return 1;
93 }
94 bucket = bucket->next;
95 }
96 return 0;
97}
98
99/* Add statbuf to statbuf hash table */
100static void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
101{
102 int i;
103 size_t s;
104 ino_dev_hashtable_bucket_t *bucket;
105
106 i = hash_inode(statbuf->st_ino);
107 s = name ? strlen(name) : 0;
108 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
109 bucket->ino = statbuf->st_ino;
110 bucket->dev = statbuf->st_dev;
111 if (name)
112 strcpy(bucket->name, name);
113 else
114 bucket->name[0] = '\0';
115 bucket->next = ino_dev_hashtable[i];
116 ino_dev_hashtable[i] = bucket;
117}
118
119/* Clear statbuf hash table */
120static void reset_ino_dev_hashtable(void)
121{
122 int i;
123 ino_dev_hashtable_bucket_t *bucket;
124
125 for (i = 0; i < HASH_SIZE; i++) {
126 while (ino_dev_hashtable[i] != NULL) {
127 bucket = ino_dev_hashtable[i]->next;
128 free(ino_dev_hashtable[i]);
129 ino_dev_hashtable[i] = bucket;
130 }
131 }
132}
133
134/* tiny recursive du */ 62/* tiny recursive du */
135static long du(char *filename) 63static long du(char *filename)
136{ 64{
@@ -246,7 +174,7 @@ int du_main(int argc, char **argv)
246 return status; 174 return status;
247} 175}
248 176
249/* $Id: du.c,v 1.51 2001/10/24 04:59:27 andersen Exp $ */ 177/* $Id: du.c,v 1.52 2001/12/17 15:26:25 kraai Exp $ */
250/* 178/*
251Local Variables: 179Local Variables:
252c-file-style: "linux" 180c-file-style: "linux"
diff --git a/include/libbb.h b/include/libbb.h
index a953b3cb0..fccdf5fdf 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -286,4 +286,8 @@ extern const char * const can_not_create_raw_socket;
286#define CURRENT_TTY "/dev/tty" 286#define CURRENT_TTY "/dev/tty"
287#define CONSOLE_DEV "/dev/console" 287#define CONSOLE_DEV "/dev/console"
288 288
289int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
290void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
291void reset_ino_dev_hashtable(void);
292
289#endif /* __LIBCONFIG_H__ */ 293#endif /* __LIBCONFIG_H__ */
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/*
diff --git a/testsuite/cp/cp-preserves-hard-links b/testsuite/cp/cp-preserves-hard-links
new file mode 100644
index 000000000..c17f42635
--- /dev/null
+++ b/testsuite/cp/cp-preserves-hard-links
@@ -0,0 +1,6 @@
1# UNSUPPORTED: CONFIG_FEATURE_PRESERVE_HARDLINKS
2touch foo
3ln foo bar
4mkdir baz
5busybox cp -d foo bar baz
6test baz/foo -ef baz/bar