Skip to content

Commit 94dd193

Browse files
Remove UntarBundleWithRequiredFilePermission (#62)
1 parent a88c068 commit 94dd193

File tree

2 files changed

+0
-82
lines changed

2 files changed

+0
-82
lines changed

fsutil/filesystem.go

-45
Original file line numberDiff line numberDiff line change
@@ -134,51 +134,6 @@ func UntarBundle(destination string, source string) error {
134134
return nil
135135
}
136136

137-
// UntarBundleWithRequiredFilePermission performs the same operation as UntarBundle,
138-
// but enforces `requiredFilePerm` for all files in the bundle.
139-
func UntarBundleWithRequiredFilePermission(destination string, source string, requiredFilePerm fs.FileMode) error {
140-
f, err := os.Open(source)
141-
if err != nil {
142-
return fmt.Errorf("opening source: %w", err)
143-
}
144-
defer f.Close()
145-
146-
gzr, err := gzip.NewReader(f)
147-
if err != nil {
148-
return fmt.Errorf("creating gzip reader from %s: %w", source, err)
149-
}
150-
defer gzr.Close()
151-
152-
tr := tar.NewReader(gzr)
153-
for {
154-
header, err := tr.Next()
155-
if err == io.EOF {
156-
break
157-
}
158-
if err != nil {
159-
return fmt.Errorf("reading tar file: %w", err)
160-
}
161-
162-
if err := sanitizeExtractPath(filepath.Dir(destination), header.Name); err != nil {
163-
return fmt.Errorf("checking filename: %w", err)
164-
}
165-
166-
destPath := filepath.Join(filepath.Dir(destination), header.Name)
167-
info := header.FileInfo()
168-
if info.IsDir() {
169-
if err = os.MkdirAll(destPath, info.Mode()); err != nil {
170-
return fmt.Errorf("creating directory %s for tar file: %w", destPath, err)
171-
}
172-
continue
173-
}
174-
175-
if err := writeBundleFile(destPath, requiredFilePerm, tr); err != nil {
176-
return fmt.Errorf("writing file: %w", err)
177-
}
178-
}
179-
return nil
180-
}
181-
182137
func writeBundleFile(destPath string, perm fs.FileMode, srcReader io.Reader) error {
183138
file, err := os.OpenFile(destPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perm)
184139
if err != nil {

fsutil/filesystem_test.go

-37
Original file line numberDiff line numberDiff line change
@@ -52,43 +52,6 @@ func TestUntarBundle(t *testing.T) {
5252
require.Equal(t, nestedFileMode, nestedFileInfo.Mode())
5353
}
5454

55-
func TestUntarBundleWithRequiredFilePermission(t *testing.T) {
56-
t.Parallel()
57-
58-
// Create tarball contents
59-
originalDir := t.TempDir()
60-
topLevelFile := filepath.Join(originalDir, "testfile.txt")
61-
require.NoError(t, os.WriteFile(topLevelFile, []byte("test1"), 0655))
62-
internalDir := filepath.Join(originalDir, "some", "path", "to")
63-
require.NoError(t, os.MkdirAll(internalDir, 0744))
64-
nestedFile := filepath.Join(internalDir, "anotherfile.txt")
65-
require.NoError(t, os.WriteFile(nestedFile, []byte("test2"), 0744))
66-
67-
// Create test tarball
68-
tarballDir := t.TempDir()
69-
tarballFile := filepath.Join(tarballDir, "test.gz")
70-
createTar(t, tarballFile, originalDir)
71-
72-
// Confirm we can untar the tarball successfully
73-
newDir := t.TempDir()
74-
var requiredFileMode fs.FileMode = 0755
75-
require.NoError(t, UntarBundleWithRequiredFilePermission(filepath.Join(newDir, "anything"), tarballFile, requiredFileMode))
76-
77-
// Confirm the tarball has the contents we expect
78-
newTopLevelFile := filepath.Join(newDir, filepath.Base(topLevelFile))
79-
require.FileExists(t, newTopLevelFile)
80-
newNestedFile := filepath.Join(newDir, "some", "path", "to", filepath.Base(nestedFile))
81-
require.FileExists(t, newNestedFile)
82-
83-
// Require that both files have the required permission 0755
84-
topLevelFileInfo, err := os.Stat(newTopLevelFile)
85-
require.NoError(t, err)
86-
require.Equal(t, requiredFileMode, topLevelFileInfo.Mode())
87-
nestedFileInfo, err := os.Stat(newNestedFile)
88-
require.NoError(t, err)
89-
require.Equal(t, requiredFileMode, nestedFileInfo.Mode())
90-
}
91-
9255
// createTar is a helper to create a test tar
9356
func createTar(t *testing.T, createLocation string, sourceDir string) {
9457
tarballFile, err := os.Create(createLocation)

0 commit comments

Comments
 (0)