aboutsummaryrefslogtreecommitdiff
path: root/libbb/copy_file.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2004-02-19 00:44:08 +0000
committerEric Andersen <andersen@codepoet.org>2004-02-19 00:44:08 +0000
commite0cbe4863704c78ea703c06c0dd2d76ebac90f75 (patch)
treebbdb6a67c5ef096f90bdf6fc91821f94d0f3e50a /libbb/copy_file.c
parent03a0643fbc5ae3e1ab2e7e29bb64617a4b94a5c9 (diff)
downloadbusybox-e0cbe4863704c78ea703c06c0dd2d76ebac90f75.tar.gz
Chris Larson (kergoth) writes:
I was adding -s/--symbolic-link support to busybox cp when I noticed a bug with -r/-a. Test case: mkdir -p test/out cd test busybox cp -a * out/ Will never return until we run out of open files or similar. Coreutils cp on the other hand will error with "cannot copy a directory, `out', into itself, `out'". Patch attached.
Diffstat (limited to 'libbb/copy_file.c')
-rw-r--r--libbb/copy_file.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index 7ddb9a23f..637b8179b 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -65,12 +65,28 @@ int copy_file(const char *source, const char *dest, int flags)
DIR *dp;
struct dirent *d;
mode_t saved_umask = 0;
+ char *dstparent;
+ struct stat dstparent_stat;
if (!(flags & FILEUTILS_RECUR)) {
bb_error_msg("%s: omitting directory", source);
return -1;
}
+ dstparent = dirname(bb_xstrdup(dest));
+ if (lstat(dstparent, &dstparent_stat) < 0) {
+ bb_perror_msg("unable to stat `%s'", dstparent);
+ free(dstparent);
+ return -1;
+ }
+ free(dstparent);
+
+ if (source_stat.st_dev == dstparent_stat.st_dev &&
+ source_stat.st_ino == dstparent_stat.st_ino) {
+ bb_error_msg("cannot copy a directory, `%s', into itself, `%s'", source, dest);
+ return -1;
+ }
+
/* Create DEST. */
if (dest_exists) {
if (!S_ISDIR(dest_stat.st_mode)) {