diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cp.c | 45 | ||||
-rw-r--r-- | src/ls.c | 2 | ||||
-rw-r--r-- | src/mv.c | 17 | ||||
-rw-r--r-- | src/rm.c | 14 |
4 files changed, 56 insertions, 22 deletions
@@ -133,6 +133,7 @@ static struct option const long_opts[] = {"link", no_argument, NULL, 'l'}, {"no-dereference", no_argument, &flag_dereference, 0}, {"one-file-system", no_argument, &flag_one_file_system, 1}, + {"parents", no_argument, &flag_path, 1}, {"path", no_argument, &flag_path, 1}, {"preserve", no_argument, &flag_preserve, 1}, {"recursive", no_argument, NULL, 'R'}, @@ -358,7 +359,7 @@ do_copy (argc, argv) dst_path = xmalloc (strlen (dest) + strlen (arg) + 2); stpcpy (stpcpy (stpcpy (dst_path, dest), "/"), arg); - /* For --path, we have to make sure that the directory + /* For --parents, we have to make sure that the directory dirname (dst_path) exists. We may have to create a few leading directories. */ parent_exists = !make_path (dst_path, @@ -407,9 +408,32 @@ do_copy (argc, argv) } else if (argc - optind == 2) { + char *dst_path; + char *source; + struct stat source_stats; + if (flag_path) usage ("when preserving paths, last argument must be a directory"); - return copy (argv[optind], dest, new_dst, 0, (struct dir_list *) 0); + + source = argv[optind]; + + if (dest[strlen (dest) - 1] == '/' + && lstat (source, &source_stats) == 0 + && !S_ISDIR (source_stats.st_mode)) + { + char *base; + + strip_trailing_slashes (source); + base = basename (source); + dst_path = (char *) alloca (strlen (dest) + 1 + strlen (base) + 1); + stpcpy (stpcpy (stpcpy (dst_path, dest), "/"), base); + } + else + { + dst_path = dest; + } + + return copy (argv[optind], dst_path, new_dst, 0, (struct dir_list *) 0); } else usage ("when copying multiple files, last argument must be a directory"); @@ -711,17 +735,12 @@ copy (src_path, dst_path, new_dst, device, ancestors) } else #ifdef S_ISLNK -#ifdef _AIX -#define LINK_BUF PATH_MAX -#else -#define LINK_BUF src_sb.st_size -#endif if (S_ISLNK (src_type)) { - char *link_val = (char *) alloca (LINK_BUF + 1); + char link_val[PATH_MAX + 1]; int link_size; - link_size = readlink (src_path, link_val, LINK_BUF); + link_size = readlink (src_path, link_val, sizeof (link_val) - 1); if (link_size < 0) { error (0, errno, "cannot read symbolic link `%s'", src_path); @@ -801,7 +820,7 @@ un_backup: } /* Ensure that the parent directory of CONST_DIRPATH exists, for - the --path option. + the --parents option. SRC_OFFSET is the index in CONST_DIRPATH (which is a destination path) of the beginning of the source directory name. @@ -870,7 +889,7 @@ make_path (const_dirpath, src_offset, mode, verbose_fmt_string, { /* This element of the path does not exist. We must set *new_dst and new->is_new_dir inside this loop because, - for example, in the command `cp --path ../a/../b/c e_dir', + for example, in the command `cp --parents ../a/../b/c e_dir', make_path creates only e_dir/../a if ./b already exists. */ *new_dst = 1; new->is_new_dir = 1; @@ -924,7 +943,7 @@ make_path (const_dirpath, src_offset, mode, verbose_fmt_string, } /* Ensure that the parent directories of CONST_DST_PATH have the - correct protections, for the --path option. This is done + correct protections, for the --parents option. This is done after all copying has been completed, to allow permissions that don't include user write/execute. @@ -934,7 +953,7 @@ make_path (const_dirpath, src_offset, mode, verbose_fmt_string, ATTR_LIST is a null-terminated linked list of structures that indicates the end of the filename of each intermediate directory in CONST_DST_PATH that may need to have its attributes changed. - The command `cp --path --preserve a/b/c d/e_dir' changes the + The command `cp --parents --preserve a/b/c d/e_dir' changes the attributes of the directories d/e_dir/a and d/e_dir/a/b to match the corresponding source directories regardless of whether they existed before the `cp' command was given. @@ -1081,7 +1081,7 @@ get_link_name (filename, f) /* Some automounters give incorrect st_size for mount points. I can't think of a good workaround for it, though. */ - linksize = readlink (filename, linkbuf, sizeof (linkbuf)); + linksize = readlink (filename, linkbuf, sizeof (linkbuf) - 1); if (linksize < 0) { error (0, errno, "%s", filename); @@ -58,6 +58,7 @@ int yesno (); void error (); void strip_trailing_slashes (); int eaccess_stat (); +char *stpcpy (); static int copy_reg (); static int do_move (); @@ -188,6 +189,17 @@ main (argc, argv) exit (errors); } +/* If PATH is an existing directory, return nonzero, else 0. */ + +static int +is_real_dir (path) + char *path; +{ + struct stat stats; + + return lstat (path, &stats) == 0 && S_ISDIR (stats.st_mode); +} + /* Move file SOURCE onto DEST. Handles the case when DEST is a directory. Return 0 if successful, 1 if an error occurred. */ @@ -198,7 +210,8 @@ movefile (source, dest) { strip_trailing_slashes (source); - if (isdir (dest)) + if ((dest[strlen (dest) - 1] == '/' && !is_real_dir (source)) + || isdir (dest)) { /* Target is a directory; build full target filename. */ char *base; @@ -206,7 +219,7 @@ movefile (source, dest) base = basename (source); new_dest = (char *) alloca (strlen (dest) + 1 + strlen (base) + 1); - sprintf (new_dest, "%s/%s", dest, base); + stpcpy (stpcpy (stpcpy (new_dest, dest), "/"), base); return do_move (source, new_dest); } else @@ -346,7 +346,8 @@ clear_directory (statp) int pathname_length; /* Length of `pathname'. */ ino_t *inode_space; /* Copy of directory's inodes. */ ino_t *inodep; /* Current entry in `inode_space'. */ - unsigned inode_size; /* Bytes allocated for `inode_space'. */ + unsigned n_inodes_allocated; /* There is space for this many inodes + in `inode_space'. */ int err = 0; /* Return status. */ struct pathstack pathframe; /* New top of stack. */ struct pathstack *pp; /* Temporary. */ @@ -354,8 +355,8 @@ clear_directory (statp) name_size = statp->st_size; name_space = (char *) xmalloc (name_size); - inode_size = statp->st_size; - inode_space = (ino_t *) xmalloc (inode_size); + n_inodes_allocated = (statp->st_size + sizeof (ino_t) - 1) / sizeof (ino_t); + inode_space = (ino_t *) xmalloc (n_inodes_allocated * sizeof (ino_t)); do { @@ -398,12 +399,13 @@ clear_directory (statp) } namep = stpcpy (namep, dp->d_name) + 1; - if (inodep == inode_space + inode_size) + if (inodep == inode_space + n_inodes_allocated) { ino_t *new_inode_space; - inode_size += 1024; - new_inode_space = (ino_t *) xrealloc (inode_space, inode_size); + n_inodes_allocated += 1024; + new_inode_space = (ino_t *) xrealloc (inode_space, + n_inodes_allocated * sizeof (ino_t)); inodep += new_inode_space - inode_space; inode_space = new_inode_space; } |