-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package getter | ||
|
||
import ( | ||
"fmt" | ||
"github.com/klauspost/compress/zstd" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// TarZstdDecompressor is an implementation of Decompressor that can | ||
// decompress tar.zstd files. | ||
type TarZstdDecompressor struct{} | ||
|
||
func (d *TarZstdDecompressor) Decompress(dst, src string, dir bool, umask os.FileMode) error { | ||
// If we're going into a directory we should make that first | ||
mkdir := dst | ||
if !dir { | ||
mkdir = filepath.Dir(dst) | ||
} | ||
if err := os.MkdirAll(mkdir, mode(0755, umask)); err != nil { | ||
return err | ||
} | ||
|
||
// File first | ||
f, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
// Zstd compression is second | ||
zstdR, err := zstd.NewReader(f) | ||
if err != nil { | ||
return fmt.Errorf("Error opening a zstd reader for %s: %s", src, err) | ||
} | ||
defer zstdR.Close() | ||
|
||
return untar(zstdR, dst, src, dir, umask) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package getter | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestTarZstdDecompressor(t *testing.T) { | ||
|
||
multiplePaths := []string{"dir/", "dir/test2", "test1"} | ||
orderingPaths := []string{"workers/", "workers/mq/", "workers/mq/__init__.py"} | ||
|
||
cases := []TestDecompressCase{ | ||
{ | ||
"empty.tar.zst", | ||
false, | ||
true, | ||
nil, | ||
"", | ||
nil, | ||
}, | ||
|
||
{ | ||
"single.tar.zst", | ||
false, | ||
false, | ||
nil, | ||
"d3b07384d113edec49eaa6238ad5ff00", | ||
nil, | ||
}, | ||
|
||
{ | ||
"single.tar.zst", | ||
true, | ||
false, | ||
[]string{"file"}, | ||
"", | ||
nil, | ||
}, | ||
|
||
{ | ||
"multiple.tar.zst", | ||
true, | ||
false, | ||
[]string{"file1", "file2"}, | ||
"", | ||
nil, | ||
}, | ||
|
||
{ | ||
"multiple.tar.zst", | ||
false, | ||
true, | ||
nil, | ||
"", | ||
nil, | ||
}, | ||
|
||
{ | ||
"multiple_dir.tar.zst", | ||
true, | ||
false, | ||
multiplePaths, | ||
"", | ||
nil, | ||
}, | ||
|
||
// Tests when the file is listed before the parent folder | ||
{ | ||
"ordering.tar.zst", | ||
true, | ||
false, | ||
orderingPaths, | ||
"", | ||
nil, | ||
}, | ||
|
||
// Tests that a tar.zst can't contain references with "..". | ||
// GNU `tar` also disallows this. | ||
{ | ||
"outside_parent.tar.zst", | ||
true, | ||
true, | ||
nil, | ||
"", | ||
nil, | ||
}, | ||
} | ||
|
||
for i, tc := range cases { | ||
cases[i].Input = filepath.Join("./testdata", "decompress-tzst", tc.Input) | ||
} | ||
|
||
TestDecompressor(t, new(TarZstdDecompressor), cases) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package getter | ||
|
||
import ( | ||
"fmt" | ||
"github.com/klauspost/compress/zstd" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// ZstdDecompressor is an implementation of Decompressor that | ||
// can decompress .zst files. | ||
type ZstdDecompressor struct{} | ||
|
||
func (d *ZstdDecompressor) Decompress(dst, src string, dir bool, umask os.FileMode) error { | ||
if dir { | ||
return fmt.Errorf("zstd-compressed files can only unarchive to a single file") | ||
} | ||
|
||
// If we're going into a directory we should make that first | ||
if err := os.MkdirAll(filepath.Dir(dst), mode(0755, umask)); err != nil { | ||
return err | ||
} | ||
|
||
// File first | ||
f, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
// zstd compression is second | ||
zstdR, err := zstd.NewReader(f) | ||
if err != nil { | ||
return err | ||
} | ||
defer zstdR.Close() | ||
|
||
// Copy it out | ||
return copyReader(dst, zstdR, 0622, umask) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package getter | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestZstdDecompressor(t *testing.T) { | ||
cases := []TestDecompressCase{ | ||
{ | ||
"single.zst", | ||
false, | ||
false, | ||
nil, | ||
"d3b07384d113edec49eaa6238ad5ff00", | ||
nil, | ||
}, | ||
|
||
{ | ||
"single.zst", | ||
true, | ||
true, | ||
nil, | ||
"", | ||
nil, | ||
}, | ||
} | ||
|
||
for i, tc := range cases { | ||
cases[i].Input = filepath.Join("./testdata", "decompress-zst", tc.Input) | ||
} | ||
|
||
TestDecompressor(t, new(ZstdDecompressor), cases) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.