aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/cpio.c
diff options
context:
space:
mode:
authorYi-Yo Chiang via Toybox <toybox@lists.landley.net>2021-04-24 01:52:39 +0800
committerRob Landley <rob@landley.net>2021-04-23 23:53:44 -0500
commitc366525850cfdfd2884ab9a214be21a4f66505bd (patch)
tree86f8d221df1a0746bc46e07b02887147c180ab11 /toys/posix/cpio.c
parentf73d10a50afb2073c3825614d0d6811737cfaf91 (diff)
downloadtoybox-c366525850cfdfd2884ab9a214be21a4f66505bd.tar.gz
cpio: Don't lchown() if -t is specified
When using -t to inspect an archive, cpio would try to set the owner of any symlink in the archive, even though the symlink wasn't created by the command previously. This would lead to two results, either the command fails with a "No such file or directory" message when trying to lchown() the symlink path, or an existing file, with the name of the symlink, is lchown()-ed. Guard the lchown() function call with a "if (!FLAG(t))" block, and add regression test for this.
Diffstat (limited to 'toys/posix/cpio.c')
-rw-r--r--toys/posix/cpio.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/toys/posix/cpio.c b/toys/posix/cpio.c
index 07c294f0..35b74b3b 100644
--- a/toys/posix/cpio.c
+++ b/toys/posix/cpio.c
@@ -166,11 +166,13 @@ void cpio_main(void)
if (!test) err = mkdir(name, mode) && !FLAG(u);
} else if (S_ISLNK(mode)) {
data = strpad(afd, size, 0);
- if (!test) err = symlink(data, name);
+ if (!test) {
+ err = symlink(data, name);
+ // Can't get a filehandle to a symlink, so do special chown
+ if (!err && !geteuid() && !FLAG(no_preserve_owner))
+ err = lchown(name, uid, gid);
+ }
free(data);
- // Can't get a filehandle to a symlink, so do special chown
- if (!err && !geteuid() && !FLAG(no_preserve_owner))
- err = lchown(name, uid, gid);
} else if (S_ISREG(mode)) {
int fd = test ? 0 : open(name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, mode);