aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
Diffstat (limited to 'toys')
-rw-r--r--toys/posix/cp.c62
1 files changed, 27 insertions, 35 deletions
diff --git a/toys/posix/cp.c b/toys/posix/cp.c
index 8eaecc9c..ed62f04d 100644
--- a/toys/posix/cp.c
+++ b/toys/posix/cp.c
@@ -124,9 +124,10 @@ struct cp_preserve {
static int cp_node(struct dirtree *try)
{
int fdout = -1, cfd = try->parent ? try->parent->extra : AT_FDCWD,
+ save = DIRTREE_SAVE*(CFG_MV && toys.which->name[0] == 'm'), rc = 0,
tfd = dirtree_parentfd(try);
unsigned flags = toys.optflags;
- char *catch = try->parent ? try->name : TT.destname, *err = "%s";
+ char *s = 0, *catch = try->parent ? try->name : TT.destname, *err = "%s";
struct stat cst;
if (!dirtree_notdotdot(try)) return 0;
@@ -135,8 +136,13 @@ static int cp_node(struct dirtree *try)
if (S_ISDIR(try->st.st_mode) && try->again) {
fdout = try->extra;
err = 0;
- } else {
+ // If mv child had a problem, free data and don't try to delete parent dir.
+ if (try->child) {
+ save = 0;
+ llist_traverse(try->child, free);
+ }
+ } else {
// -d is only the same as -r for symlinks, not for directories
if (S_ISLNK(try->st.st_mode) && (flags & FLAG_d)) flags |= FLAG_r;
@@ -150,35 +156,27 @@ static int cp_node(struct dirtree *try)
error_msg("'%s' is '%s'", catch, err = dirtree_path(try, 0));
free(err);
- return 0;
+ return save;
}
// Handle -inuvF
-
if (!faccessat(cfd, catch, F_OK, 0) && !S_ISDIR(cst.st_mode)) {
- char *s;
-
- if (S_ISDIR(try->st.st_mode)) {
+ if (S_ISDIR(try->st.st_mode))
error_msg("dir at '%s'", s = dirtree_path(try, 0));
- free(s);
- return 0;
- } else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0)) {
+ else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0))
error_msg("unlink '%s'", catch);
- return 0;
- } else if (flags & FLAG_n) return 0;
- else if ((flags & FLAG_u) && nanodiff(&try->st.st_mtim, &cst.st_mtim)>0)
- return 0;
else if (flags & FLAG_i) {
fprintf(stderr, "%s: overwrite '%s'", toys.which->name,
s = dirtree_path(try, 0));
- free(s);
- if (!yesno(0)) return 0;
- }
+ if (yesno(0)) rc++;
+ } else if (!((flags&FLAG_u) && nanodiff(&try->st.st_mtim, &cst.st_mtim)>0)
+ && !(flags & FLAG_n)) rc++;
+ free(s);
+ if (!rc) return save;
}
if (flags & FLAG_v) {
- char *s = dirtree_path(try, 0);
- printf("%s '%s'\n", toys.which->name, s);
+ printf("%s '%s'\n", toys.which->name, s = dirtree_path(try, 0));
free(s);
}
@@ -246,13 +244,11 @@ static int cp_node(struct dirtree *try)
} else if (!S_ISREG(try->st.st_mode)
&& (try->parent || (flags & (FLAG_a|FLAG_P|FLAG_r))))
{
- int i;
-
// make symlink, or make block/char/fifo/socket
if (S_ISLNK(try->st.st_mode)
- ? ((i = readlinkat0(tfd, try->name, toybuf, sizeof(toybuf))) &&
- ((!unlinkat(cfd, catch, 0) || ENOENT == errno) &&
- !symlinkat(toybuf, cfd, catch)))
+ ? readlinkat0(tfd, try->name, toybuf, sizeof(toybuf)) &&
+ (!unlinkat(cfd, catch, 0) || ENOENT == errno) &&
+ !symlinkat(toybuf, cfd, catch)
: !mknodat(cfd, catch, try->st.st_mode, try->st.st_rdev))
{
err = 0;
@@ -307,8 +303,6 @@ static int cp_node(struct dirtree *try)
// Did we make a thing?
if (fdout != -1) {
- int rc;
-
// Inability to set --preserve isn't fatal, some require root access.
// ownership
@@ -342,23 +336,21 @@ static int cp_node(struct dirtree *try)
xclose(fdout);
}
- if (CFG_MV && toys.which->name[0] == 'm')
+ if (save)
if (unlinkat(tfd, try->name, S_ISDIR(try->st.st_mode) ? AT_REMOVEDIR :0))
err = "%s";
}
if (err) {
- char *f = 0;
-
if (catch == try->name) {
- f = dirtree_path(try, 0);
+ s = dirtree_path(try, 0);
while (try->parent) try = try->parent;
- catch = xmprintf("%s%s", TT.destname, f+strlen(try->name));
- free(f);
- f = catch;
- }
+ catch = xmprintf("%s%s", TT.destname, s+strlen(try->name));
+ free(s);
+ s = catch;
+ } else s = 0;
perror_msg(err, catch);
- free(f);
+ free(s);
}
return 0;
}