Skip to content

Commit

Permalink
playground: check file/dir name conflicts in txtar
Browse files Browse the repository at this point in the history
  • Loading branch information
yaegashi committed Mar 1, 2021
1 parent 4d02eef commit bd7d5e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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

0 comments on commit bd7d5e9

Please sign in to comment.