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

storage: include directories in artifact tarball #543

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions controllers/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ func SourceIgnoreFilter(ps []gitignore.Pattern, domain []string) ArchiveFileFilt
matcher = sourceignore.NewMatcher(ps)
}
return func(p string, fi os.FileInfo) bool {
// The directory is always false as the archiver does already skip
// directories.
return matcher.Match(strings.Split(p, string(filepath.Separator)), false)
return matcher.Match(strings.Split(p, string(filepath.Separator)), fi.IsDir())
}
}

Expand Down Expand Up @@ -191,8 +189,8 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
return err
}

// Ignore anything that is not a file (directories, symlinks)
if !fi.Mode().IsRegular() {
// Ignore anything that is not a file or directories e.g. symlinks
if m := fi.Mode(); !(m.IsRegular() || m.IsDir()) {
return nil
}

Expand Down Expand Up @@ -231,6 +229,9 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
return err
}

if !fi.Mode().IsRegular() {
return nil
}
f, err := os.Open(p)
if err != nil {
f.Close()
Expand Down
59 changes: 49 additions & 10 deletions controllers/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestStorageConstructor(t *testing.T) {

// walks a tar.gz and looks for paths with the basename. It does not match
// symlinks properly at this time because that's painful.
func walkTar(tarFile string, match string) (int64, bool, error) {
func walkTar(tarFile string, match string, dir bool) (int64, bool, error) {
f, err := os.Open(tarFile)
if err != nil {
return 0, false, fmt.Errorf("could not open file: %w", err)
Expand All @@ -93,7 +93,11 @@ func walkTar(tarFile string, match string) (int64, bool, error) {
}

switch header.Typeflag {
case tar.TypeDir, tar.TypeReg:
case tar.TypeDir:
if header.Name == match && dir {
return 0, true, nil
}
case tar.TypeReg:
if header.Name == match {
return header.Size, true, nil
}
Expand Down Expand Up @@ -145,13 +149,14 @@ func TestStorage_Archive(t *testing.T) {
return
}

matchFiles := func(t *testing.T, storage *Storage, artifact sourcev1.Artifact, files map[string][]byte) {
matchFiles := func(t *testing.T, storage *Storage, artifact sourcev1.Artifact, files map[string][]byte, dirs []string) {
t.Helper()
for name, b := range files {
mustExist := !(name[0:1] == "!")
if !mustExist {
name = name[1:]
}
s, exist, err := walkTar(storage.LocalPath(artifact), name)
s, exist, err := walkTar(storage.LocalPath(artifact), name, false)
if err != nil {
t.Fatalf("failed reading tarball: %v", err)
}
Expand All @@ -166,14 +171,32 @@ func TestStorage_Archive(t *testing.T) {
}
}
}
for _, name := range dirs {
mustExist := !(name[0:1] == "!")
if !mustExist {
name = name[1:]
}
_, exist, err := walkTar(storage.LocalPath(artifact), name, true)
if err != nil {
t.Fatalf("failed reading tarball: %v", err)
}
if exist != mustExist {
if mustExist {
t.Errorf("could not find dir %q in tarball", name)
} else {
t.Errorf("tarball contained excluded file %q", name)
}
}
}
}

tests := []struct {
name string
files map[string][]byte
filter ArchiveFileFilter
want map[string][]byte
wantErr bool
name string
files map[string][]byte
filter ArchiveFileFilter
want map[string][]byte
wantDirs []string
wantErr bool
}{
{
name: "no filter",
Expand All @@ -195,6 +218,9 @@ func TestStorage_Archive(t *testing.T) {
".git/config": nil,
"manifest.yaml": nil,
},
wantDirs: []string{
"!.git",
},
filter: SourceIgnoreFilter(nil, nil),
want: map[string][]byte{
"!.git/config": nil,
Expand All @@ -218,6 +244,19 @@ func TestStorage_Archive(t *testing.T) {
},
wantErr: false,
},
{
hiddeco marked this conversation as resolved.
Show resolved Hide resolved
name: "including directories",
files: map[string][]byte{
"test/.gitkeep": nil,
},
filter: SourceIgnoreFilter([]gitignore.Pattern{
gitignore.ParsePattern("custom", nil),
}, nil),
wantDirs: []string{
"test",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -236,7 +275,7 @@ func TestStorage_Archive(t *testing.T) {
if err := storage.Archive(&artifact, dir, tt.filter); (err != nil) != tt.wantErr {
t.Errorf("Archive() error = %v, wantErr %v", err, tt.wantErr)
}
matchFiles(t, storage, artifact, tt.want)
matchFiles(t, storage, artifact, tt.want, tt.wantDirs)
})
}
}
Expand Down