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

playground: check file/dir name conflicts in txtar #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions txtar.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (fs *fileSet) Data(filename string) []byte { return fs.m[filename] }
// Num returns the number of files in the set.
func (fs *fileSet) Num() int { return len(fs.m) }

// Files returns the filenames in user-provided order.
func (fs *fileSet) Files() []string { return fs.files }

// Contains reports whether fs contains the given filename.
func (fs *fileSet) Contains(filename string) bool {
_, ok := fs.m[filename]
Expand Down Expand Up @@ -113,6 +116,15 @@ func splitFiles(src []byte) (*fileSet, error) {
}
fs.AddFile(f.Name, f.Data)
}
for _, filename := range fs.Files() {
parts := strings.Split(filename, "/")
for i := 1; i < len(parts); i++ {
dirname := path.Join(parts[:i]...)
if fs.Contains(dirname) {
return nil, fmt.Errorf("conflict file/dir name %q and %q", dirname, filename)
}
}
}
return fs, nil
}

Expand Down
10 changes: 10 additions & 0 deletions txtar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ func TestSplitFiles(t *testing.T) {
in: strings.Repeat("-- x.go --\n", 50),
wantErr: `too many files in txtar archive (50 exceeds limit of 20)`,
},
{
name: "reject file overwritten by dir",
in: "-- a --\n-- a/b --\n",
wantErr: `conflict file/dir name "a" and "a/b"`,
},
{
name: "reject dir overwritten by file",
in: "-- a/b --\n-- a --\n",
wantErr: `conflict file/dir name "a" and "a/b"`,
},
} {
got, err := splitFiles([]byte(tt.in))
var gotErr string
Expand Down