Skip to content

Commit 78fff57

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

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

txtar.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func splitFiles(src []byte) (*fileSet, error) {
8989
if numFiles > limitNumFiles {
9090
return nil, fmt.Errorf("too many files in txtar archive (%v exceeds limit of %v)", numFiles, limitNumFiles)
9191
}
92+
dirFileNameMap := map[string]string{} // holds which filename the dirname is part of
9293
for _, f := range a.Files {
9394
if len(f.Name) > 200 { // arbitrary limit
9495
return nil, errors.New("file name too long")
@@ -111,6 +112,16 @@ func splitFiles(src []byte) (*fileSet, error) {
111112
if fs.Contains(f.Name) {
112113
return nil, fmt.Errorf("duplicate file name %q", f.Name)
113114
}
115+
for i := 1; i < len(parts); i++ {
116+
dirname := path.Join(parts[:i]...)
117+
if fs.Contains(dirname) {
118+
return nil, fmt.Errorf("conflict file/dir name %q and %q", dirname, f.Name)
119+
}
120+
dirFileNameMap[dirname] = f.Name
121+
}
122+
if filename, ok := dirFileNameMap[f.Name]; ok {
123+
return nil, fmt.Errorf("conflict dir/file name %q and %q", filename, f.Name)
124+
}
114125
fs.AddFile(f.Name, f.Data)
115126
}
116127
return fs, nil

txtar_test.go

Lines changed: 10 additions & 0 deletions
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 dir/file name "a/b" and "a"`,
129+
},
120130
} {
121131
got, err := splitFiles([]byte(tt.in))
122132
var gotErr string

0 commit comments

Comments
 (0)