[BusyBox] [PATCH] don't cp a directory into itself
Chris Larson
kergoth at handhelds.org
Thu Feb 19 01:25:46 UTC 2004
* Erik Andersen (andersen at codepoet.org) wrote:
> On Wed Feb 18, 2004 at 06:15:56PM -0600, Chris Larson wrote:
> > 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.
>
> Applied,
As Manuel points out, this is a flawed fix, and doesnt fix the
following:
mkdir -p cpa cpb cpc
cp -a cpa cpa/cpb/cpc
Attached what appears to be a more sane fix. Apply on top of previous.
Please confirm sanity.
--
Chris Larson - kergoth at handhelds dot org
Embedded Linux Developer - clarson at ti dot com
OpenZaurus Project Maintainer - http://openzaurus.org/
-------------- next part --------------
#
# Patch managed by http://www.mn-logistik.de/unsupported/pxa250/patcher
#
--- busybox/libbb/copy_file.c~cp_intoself_r2 2004-02-18 19:33:33.000000000 -0500
+++ busybox/libbb/copy_file.c 2004-02-18 20:12:39.000000000 -0500
@@ -65,28 +65,12 @@
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)) {
@@ -111,6 +95,8 @@
umask(saved_umask);
}
+ add_to_ino_dev_hashtable(&dest_stat, source);
+
/* Recursively copy files in SOURCE. */
if ((dp = opendir(source)) == NULL) {
bb_perror_msg("unable to open directory `%s'", source);
@@ -124,6 +110,10 @@
new_source = concat_subpath_file(source, d->d_name);
if(new_source == NULL)
continue;
+ if (is_in_ino_dev_hashtable(&dest_stat, &new_source)) {
+ bb_error_msg("cannot copy a directory, `%s', into itself, `%s'", new_source, dest);
+ continue;
+ }
new_dest = concat_path_file(dest, d->d_name);
if (copy_file(new_source, new_dest, flags) < 0)
status = -1;
More information about the busybox
mailing list