diff options
author | Rob Landley <rob@landley.net> | 2022-03-31 15:29:53 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2022-03-31 15:29:53 -0500 |
commit | 690526a84ffcab500ccaebc70cea4d0ca3989a3a (patch) | |
tree | 446bbd779d8c2514ac861e97c8b508adc897d9d4 | |
parent | 0f77c9981882d94f06c633c8ed834611f160a666 (diff) | |
download | toybox-690526a84ffcab500ccaebc70cea4d0ca3989a3a.tar.gz |
Make tar tests more portable, and add --owner :UID and --group :GID
-rw-r--r--[-rwxr-xr-x] | tests/tar.test | 9 | ||||
-rw-r--r-- | toys/posix/tar.c | 32 |
2 files changed, 27 insertions, 14 deletions
diff --git a/tests/tar.test b/tests/tar.test index 740e0abf..034a3bfb 100755..100644 --- a/tests/tar.test +++ b/tests/tar.test @@ -41,12 +41,12 @@ testing "pass file" "$TAR file | LST" \ # The kernel has two hardwired meaningful UIDs: 0 (root) and 65534 (nobody). # (Technically changeable via /proc/sys/*/overflowuid but nobody ever does) skipnot id nobody >/dev/null -testing "pass user" "tar -c --owner nobody --group root --mtime @0 file | LST" \ +testing "pass user" "tar -c --owner nobody:65534 --group root --mtime @0 file | LST" \ "-rw-rw-r-- nobody/root 0 1970-01-01 00:00 file\n" "" "" # (We assume that if we have the nobody user, we also have the group, in the # absence of a good portable way to test for the existence of a named group.) skipnot id nobody >/dev/null -testing "pass group" "tar c --owner root --group nobody --mtime @0 file | LST" \ +testing "pass group" "tar c --owner root --group nobody:65534 --mtime @0 file | LST" \ "-rw-rw-r-- root/nobody 0 1970-01-01 00:00 file\n" "" "" # Historically we output a "base 256" format that _we_ could decode but that @@ -210,11 +210,12 @@ toyonly testing "cat tbz | extract dir/file (autodetect)" \ "dir/\ndir/file\ndrwxr-x--- 1494637555 dd/dir\n-rw-r----- 1494637555 dd/dir/file\n" \ "" "" -yes | (dd bs=$((1<<16)) count=1 status=none; dd bs=8192 seek=14 count=1 status=none; dd bs=4096 seek=64 count=5 status=none) > fweep +yes | head -n $((1<<18)) > bang +{ dd bs=$((1<<16)) count=1 status=none; dd bs=8192 seek=14 count=1 status=none; dd bs=4096 seek=64 count=5 status=none; } < bang > fweep testing "sparse without overflow" "$TAR --sparse fweep | SUM 3" \ "e1560110293247934493626d564c8f03c357cec5\n" "" "" +rm bang fweep -rm fweep for i in 1 3 5 7 9 14 27 36 128 256 300 304 do dd if=/dev/zero of=fweep bs=65536 seek=$i count=1 2>/dev/null diff --git a/toys/posix/tar.c b/toys/posix/tar.c index 0566bb65..f6330ac3 100644 --- a/toys/posix/tar.c +++ b/toys/posix/tar.c @@ -34,10 +34,10 @@ config TAR J xz compression j bzip2 compression z gzip compression O Extract to stdout X exclude names in FILE T include names in FILE - --exclude FILENAME to exclude --full-time Show seconds with -tv - --mode MODE Adjust modes --mtime TIME Override timestamps - --owner NAME Set file owner to NAME --group NAME Set file group to NAME - --sparse Record sparse files --selinux Record/restore labels + --exclude FILENAME to exclude --full-time Show seconds with -tv + --mode MODE Adjust permissions --owner NAME[:UID] Set file ownership + --mtime TIME Override timestamps --group NAME[:GID] Set file group + --sparse Record sparse files --selinux Save/restore labels --restrict All archive contents must extract under one subdirectory --numeric-owner Save/use/display uid and gid, not user/group name --no-recursion Don't store directory contents @@ -336,10 +336,10 @@ static int add_to_tar(struct dirtree *node) if (!FLAG(numeric_owner)) { if ((TT.owner || (pw = bufgetpwuid(st->st_uid))) && ascii_fits(st->st_uid, sizeof(hdr.uid))) - strncpy(hdr.uname, TT.owner ? TT.owner : pw->pw_name, sizeof(hdr.uname)); + strncpy(hdr.uname, TT.owner ? : pw->pw_name, sizeof(hdr.uname)); if ((TT.group || (gr = bufgetgrgid(st->st_gid))) && ascii_fits(st->st_gid, sizeof(hdr.gid))) - strncpy(hdr.gname, TT.group ? TT.group : gr->gr_name, sizeof(hdr.gname)); + strncpy(hdr.gname, TT.group ? : gr->gr_name, sizeof(hdr.gname)); } TT.sparselen = 0; @@ -716,8 +716,8 @@ static void unpack_tar(char *first) maj = OTOI(tar.major); min = OTOI(tar.minor); TT.hdr.device = dev_makedev(maj, min); - TT.hdr.uname = xstrndup(TT.owner ? TT.owner : tar.uname, sizeof(tar.uname)); - TT.hdr.gname = xstrndup(TT.group ? TT.group : tar.gname, sizeof(tar.gname)); + TT.hdr.uname = xstrndup(TT.owner ? : tar.uname, sizeof(tar.uname)); + TT.hdr.gname = xstrndup(TT.group ? : tar.gname, sizeof(tar.gname)); if (TT.owner) TT.hdr.uid = TT.ouid; else if (!FLAG(numeric_owner)) { @@ -861,8 +861,20 @@ void tar_main(void) // Get possible early errors out of the way if (!geteuid()) toys.optflags |= FLAG_p; - if (TT.owner) TT.ouid = xgetuid(TT.owner); - if (TT.group) TT.ggid = xgetgid(TT.group); + if (TT.owner) { + if (!(s = strchr(TT.owner, ':'))) TT.ouid = xgetuid(TT.owner); + else { + TT.owner = xstrndup(TT.owner, s++-TT.owner); + TT.ouid = atolx_range(s, 0, INT_MAX); + } + } + if (TT.group) { + if (!(s = strchr(TT.group, ':'))) TT.ggid = xgetgid(TT.group); + else { + TT.group = xstrndup(TT.group, s++-TT.group); + TT.ggid = atolx_range(s, 0, INT_MAX); + } + } if (TT.mtime) xparsedate(TT.mtime, &TT.mtt, (void *)&s, 1); // Collect file list. |