Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for -r on empty directory #3027

Merged
merged 6 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion programs/zstdcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ int main(int argCount, const char* argv[])
size_t streamSrcSize = 0;
size_t targetCBlockSize = 0;
size_t srcSizeHint = 0;
size_t nbInputFileNames = 0;
int dictCLevel = g_defaultDictCLevel;
unsigned dictSelect = g_defaultSelectivityLevel;
#ifndef ZSTD_NODICT
Expand Down Expand Up @@ -1256,6 +1257,8 @@ int main(int argCount, const char* argv[])
}
}

nbInputFileNames = filenames->tableSize; /* saving number of input files */

if (recursive) { /* at this stage, filenameTable is a list of paths, which can contain both files and directories */
UTIL_expandFNT(&filenames, followLinks);
}
Expand Down Expand Up @@ -1358,7 +1361,17 @@ int main(int argCount, const char* argv[])
#endif

/* No input filename ==> use stdin and stdout */
if (filenames->tableSize == 0) UTIL_refFilename(filenames, stdinmark);
if (filenames->tableSize == 0) {
/* It is possible that the input
was a number of empty directories. In this case
stdin and stdout should not be used */
if (nbInputFileNames > 0 ){
DISPLAYLEVEL(1, "please provide correct input file(s) or non-empty directories -- ignored \n");
CLEAN_RETURN(0);
}
UTIL_refFilename(filenames, stdinmark);
}

if (!strcmp(filenames->fileNames[0], stdinmark) && !outFileName)
outFileName = stdoutmark; /* when input is stdin, default output is stdout */

Expand Down
12 changes: 12 additions & 0 deletions tests/playTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,18 @@ println "\n===> recursive mode test "
# combination of -r with empty list of input file
zstd -c -r < tmp > tmp.zst

# combination of -r with empty folder
mkdir -p tmpEmptyDir
zstd -r tmpEmptyDir 2>tmplog2
if [ grep "aborting" tmplog2 ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems this construction is still problematic for sh compatibility.

I'm no sh expert, and this specific construction is not used in our test script,
but I can recommend an existing idiom that has worked well so far :

zstd -r tmpEmptydir 2>&1 | grep "aborting" && die "Should not abort on empty directory"

This would be equivalent, but you could go for something even simpler :

zstd -r tmpEmptydir 

this second test only relies on return code being successful, which, I believe, is the goal of the test ?

Copy link
Contributor Author

@brailovich brailovich Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! my initial thought was to go with just zstd -r tmpEmptydir but I thought that the test should be more verbose. if this is acceptable, that's the best solution, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, "if then" sh syntax allows cleaning tmp dirs/files before exiting.

println "Should not abort on empty directory"
rm -rf tmplog2
rm -rf tmpEmptyDir
die
fi
rm -rf tmplog2
rm -rf tmpEmptyDir


println "\n===> file removal"
zstd -f --rm tmp
Expand Down