Skip to content

Commit

Permalink
tests: fix problems in the m_rootgnutar test
Browse files Browse the repository at this point in the history
The mkgnutar.pl file only works if the developer had a specific
username and uid.  In addition, if it is used, the round-trip from tar
to an ext4 file system and back to tar isn't properly tested.  So only
use mkgnutar.pl if the system doesn't have GNU TAR.

In addition, make sure all of the temp files created by the test are
deleted when the test is completed.

Signed-off-by: Theodore Ts'o <[email protected]>
  • Loading branch information
tytso committed Apr 23, 2024
1 parent f639371 commit ea562cb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
12 changes: 7 additions & 5 deletions tests/m_rootgnutar/mkgnutar.pl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ sub recurse_dir {
my $content = "";
my $type;
my $linkname = "";
my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
if (S_ISLNK($mode)) {
$type = 2;
$linkname = readlink $fname;
Expand All @@ -74,17 +75,18 @@ sub recurse_dir {
'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12',
$fname,
sprintf('%07o', $mode & 07777),
sprintf('%07o', 1000), # uid
sprintf('%07o', 1000), # gid
sprintf('%07o', $<), # uid
sprintf('%07o', $(), # gid
sprintf('%011o', length $content), # size
sprintf('%011o', $mtime), # mtime
sprintf('%011o', $mtime),
# mtime
'', # checksum
$type,
$linkname, # linkname
"ustar ", # magic
" ", # version
"josch", # username
"josch", # groupname
"$username", # username
"$username", # groupname
'', # dev major
'', # dev minor
'', # prefix
Expand Down
43 changes: 34 additions & 9 deletions tests/m_rootgnutar/script
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# vim: filetype=sh

use_mkgnutar=

test_description="create fs image from GNU tarball"
if ! test -x "$DEBUGFS_EXE"; then
echo "$test_name: $test_description: skipped (no debugfs)"
Expand All @@ -10,6 +12,21 @@ if [ "$(grep -c 'define HAVE_ARCHIVE_H' ../lib/config.h)" -eq 0 ]; then
exit 0
fi

if test -z "$use_mkgnutar" ; then
if type ztar > /dev/null 2>&1 && \
tar --version 2>&1 | head -1 | grep -q "GNU tar" ; then
TAR=tar
elif type gtar > /dev/null 2>&1 && \
gtar --version 2>&1 | head -1 | grep -q "GNU tar" ; then
TAR=gtar
else
# if GNU tar is not available, fall back to using mkgnutar.pl
use_mkgnutar=yes
# echo "$test_name: $test_description: skipped (no GNU tar)"
# exit 0
fi
fi

MKFS_TAR="$TMPFILE.tar"
MKFS_DIR="$TMPFILE.dir"
OUT="$test_name.log"
Expand All @@ -28,21 +45,26 @@ mkdir "$MKFS_DIR/test/emptydir"
mkdir "$MKFS_DIR/test/dir"
echo "will be overwritten" > "$MKFS_DIR/test/dir/file"

if false; then
if test -z "$use_mkgnutar"; then
# debugfs rdump does not preserve the timestamps when it extracts the
# files so we ignore them by using tar --clamp-mtime
# first write a partial tar
tar --sort=name -C "$MKFS_DIR" --mtime="$DEBUGFS_EXE" --clamp-mtime --format=gnu -cf "$MKFS_TAR.dupl" test
$TAR --sort=name -C "$MKFS_DIR" --mtime="$DEBUGFS_EXE" --clamp-mtime \
--format=gnu -cf "$MKFS_TAR.dupl" test
# now overwrite the contents of a file
echo "Test me" > "$MKFS_DIR/test/dir/file"
# and update the tar so that it contains two entries for the same file
# we need this to test the code path that first unlinks and then overwrites an
# existing file
tar -C "$MKFS_DIR" --mtime="$DEBUGFS_EXE" --clamp-mtime --format=gnu -rf "$MKFS_TAR.dupl" test/dir/file
$TAR -C "$MKFS_DIR" --mtime="$DEBUGFS_EXE" --clamp-mtime \
--format=gnu -rf "$MKFS_TAR.dupl" test/dir/file
# also add a duplicate directory entry because those must not be unlinked
echo test | tar -C "$MKFS_DIR" --mtime="$DEBUGFS_EXE" --clamp-mtime --format=gnu -rf "$MKFS_TAR.dupl" --no-recursion --verbatim-files-from --files-from=-
echo test | $TAR -C "$MKFS_DIR" --mtime="$DEBUGFS_EXE" --clamp-mtime \
--format=gnu -rf "$MKFS_TAR.dupl" --no-recursion \
--verbatim-files-from --files-from=-
# also create a tarball of the directory with only one entry per file
tar --sort=name -C "$MKFS_DIR" --mtime="$DEBUGFS_EXE" --clamp-mtime --format=gnu -cf "$MKFS_TAR.uniq" test
$TAR --sort=name -C "$MKFS_DIR" --mtime="$DEBUGFS_EXE" --clamp-mtime \
--format=gnu -cf "$MKFS_TAR.uniq" test
else
# same as above but without using GNU tar
perl $test_dir/mkgnutar.pl --nopadding --directory="$MKFS_DIR" --mtime "$DEBUGFS_EXE_MTIME" test > "$MKFS_TAR.dupl"
Expand Down Expand Up @@ -102,9 +124,11 @@ for ext in uniq dupl; do
$DEBUGFS -R "rdump /test $MKFS_DIR" "$TMPFILE.$ext" 2>&1;
echo Exit status is $?;
# debugfs rdump does not preserve the timestamps when it extracts the
# files so we ignore them by using tar --clamp-mtime
if false; then
tar --sort=name -C "$MKFS_DIR" --mtime="$DEBUGFS_EXE" --clamp-mtime --format=gnu -cvf "$TMPFILE.new.tar" test 2>&1;
if test -z "$use_mkgnutar"; then
# files so we ignore them by using tar --clamp-mtime
$TAR --sort=name -C "$MKFS_DIR" \
--mtime="$DEBUGFS_EXE" --clamp-mtime --format=gnu \
-cvf "$TMPFILE.new.tar" test 2>&1;
else
perl $test_dir/mkgnutar.pl --verbose --directory="$MKFS_DIR" --mtime "$DEBUGFS_EXE_MTIME" test 2>&1 > "$TMPFILE.new.tar";
fi;
Expand Down Expand Up @@ -140,5 +164,6 @@ else
diff $DIFF_OPTS "$EXP" "$OUT" > "$test_name.failed"
fi

rm -rf "$MKFS_TAR.dupl" "$MKFS_TAR.uniq" "$TMPFILE.cmd1" "$TMPFILE.cmd2" "$TMPFILE.log"
rm -rf "$MKFS_TAR.dupl" "$MKFS_TAR.uniq" "$TMPFILE.cmd1" "$TMPFILE.cmd2" \
"$TMPFILE.log" "$TMPFILE.dupl" "$TMPFILE.uniq" "$TMPFILE.testme"
unset MKFS_TAR MKFS_DIR OUT EXP

0 comments on commit ea562cb

Please sign in to comment.