runtime: Invalid UnixFileMode value when using the TarFile API
Description
I’m trying to exact a tar file into a folder, and the tar file may contains folders and files, when I try to use the TarFile.ExtractToDirectory API, I got the error like follows, maybe I’m wrong on using the API
Exception when invoke command System.ArgumentException: Invalid UnixFileMode value. (Parameter 'unixCreateMode')
at System.IO.Directory.CreateDirectoryCore(String path, UnixFileMode unixCreateMode)
at System.Formats.Tar.TarHelpers.CreateDirectory(String fullPath, Nullable`1 mode, SortedDictionary`2 pendingModes)
at System.Formats.Tar.TarEntry.ExtractRelativeToDirectoryAsync(String destinationDirectoryPath, Boolean overwrite, SortedDictionary`2 pendingModes, CancellationToken cancellationToken)
at System.Formats.Tar.TarFile.ExtractToDirectoryInternalAsync(Stream source, String destinationDirectoryPath, Boolean overwriteFiles, Boolean leaveOpen, CancellationToken cancellationToken)
at System.Formats.Tar.TarFile.ExtractToDirectoryInternalAsync(Stream source, String destinationDirectoryPath, Boolean overwriteFiles, Boolean leaveOpen, CancellationToken cancellationToken)
Reproduction Steps
var tmpDirPath = Path.Combine(TmpPath, Guid.NewGuid().ToString("N"));
await var fs = File.OpenRead(@"xx.tar.gz");
await using var decompressStream = new GZipStream(fs, CompressionMode.Decompress);
await TarFile.ExtractToDirectoryAsync(decompressStream, tmpDirPath, true);
Expected behavior
Work as normal
Actual behavior
Exception as before
Regression?
No response
Known Workarounds
No response
Configuration
.NET 7 RC2 CentOS 7
Other information
Works well on Windows but not on CentOS
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (19 by maintainers)
The issue will be fixed in the next version of Docker: https://github.com/moby/moby/pull/44083.
The patch for
UnixFileModeproperty looks good to me.Probably the spec says this should store
st_modewhich isUnixFileMode+ file type (similar tozipExternalAttributes). We should update theTarWriterso it matches the spec.My bad. I had a modification in Directory.Unix.cs, which was skipping the exception. Yes it is there on the main without GZipStream.
Thanks. Perhaps it is archive/tar golang package which generates such a tarball? I was able to reproduce it with a 20K image:
I’ll open a PR in runtime-assets repo first so we can add test with the fix in this repo.
Here, the directory mode is 16877, but we should just discard the top four bits.
This patch fixes the issue (and passes all the existing tests):
No problem, with the repro file they can easily do it.