copy_file.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) Index: libbb/copy_file.c =================================================================== --- libbb/copy_file.c (revision 19704) +++ libbb/copy_file.c (working copy) @@ -115,6 +115,7 @@ int copy_file(const char *source, const if (S_ISDIR(source_stat.st_mode)) { DIR *dp; struct dirent *d; + char *existing_name; mode_t saved_umask = 0; if (!(flags & FILEUTILS_RECUR)) { @@ -143,9 +144,27 @@ int copy_file(const char *source, const return -1; } umask(saved_umask); + + if (lstat(dest, &dest_stat) < 0) + { + bb_perror_msg("cannot stat '%s'", dest); + return -1; + } + } + + if ((existing_name = is_in_ino_dev_hashtable(&source_stat)) != NULL) + { + /* source can't be a parent of dest. */ + bb_error_msg("cannot copy a directory, `%s', into itself, `%s'", + existing_name, dest); + return -1; } /* Recursively copy files in SOURCE */ + + printf("adding to hashtable: %s\n", source); + add_to_ino_dev_hashtable(&dest_stat, source); + dp = opendir(source); if (dp == NULL) { retval = -1; @@ -158,6 +177,7 @@ int copy_file(const char *source, const new_source = concat_subpath_file(source, d->d_name); if (new_source == NULL) continue; + new_dest = concat_path_file(dest, d->d_name); if (copy_file(new_source, new_dest, flags) < 0) retval = -1;