Skip to content

Commit e560152

Browse files
committed
playground: check file/dir name conflicts in txtar
1 parent 4d02eef commit e560152

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

txtar.go

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func (fs *fileSet) Data(filename string) []byte { return fs.m[filename] }
2929
// Num returns the number of files in the set.
3030
func (fs *fileSet) Num() int { return len(fs.m) }
3131

32+
// Files returns filenames in user-provided order
33+
func (fs *fileSet) Files() []string { return fs.files }
34+
3235
// Contains reports whether fs contains the given filename.
3336
func (fs *fileSet) Contains(filename string) bool {
3437
_, ok := fs.m[filename]
@@ -113,6 +116,15 @@ func splitFiles(src []byte) (*fileSet, error) {
113116
}
114117
fs.AddFile(f.Name, f.Data)
115118
}
119+
for _, filename := range fs.Files() {
120+
parts := strings.Split(filename, "/")
121+
for i := 1; i < len(parts); i++ {
122+
dirname := path.Join(parts[:i]...)
123+
if fs.Contains(dirname) {
124+
return nil, fmt.Errorf("conflict file/dir name %q and %q", dirname, filename)
125+
}
126+
}
127+
}
116128
return fs, nil
117129
}
118130

txtar_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ func TestSplitFiles(t *testing.T) {
117117
in: strings.Repeat("-- x.go --\n", 50),
118118
wantErr: `too many files in txtar archive (50 exceeds limit of 20)`,
119119
},
120+
{
121+
name: "reject file overwritten by dir",
122+
in: "-- a --\n-- a/b --\n",
123+
wantErr: `conflict file/dir name "a" and "a/b"`,
124+
},
125+
{
126+
name: "reject dir overwritten by file",
127+
in: "-- a/b --\n-- a --\n",
128+
wantErr: `conflict file/dir name "a" and "a/b"`,
129+
},
120130
} {
121131
got, err := splitFiles([]byte(tt.in))
122132
var gotErr string

0 commit comments

Comments
 (0)