File tree 2 files changed +20
-3
lines changed 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change 5
5
package tar
6
6
7
7
import (
8
+ "errors"
8
9
"fmt"
9
10
"io"
10
11
"io/fs"
@@ -419,6 +420,10 @@ func (tw *Writer) AddFS(fsys fs.FS) error {
419
420
if err != nil {
420
421
return err
421
422
}
423
+ // TODO(#49580): Handle symlinks when fs.ReadLinkFS is available.
424
+ if ! info .Mode ().IsRegular () {
425
+ return errors .New ("tar: cannot add non-regular file" )
426
+ }
422
427
h , err := FileInfoHeader (info , "" )
423
428
if err != nil {
424
429
return err
Original file line number Diff line number Diff line change 9
9
"encoding/hex"
10
10
"errors"
11
11
"io"
12
+ "io/fs"
12
13
"os"
13
14
"path"
14
15
"reflect"
@@ -1335,7 +1336,7 @@ func TestFileWriter(t *testing.T) {
1335
1336
}
1336
1337
}
1337
1338
1338
- func TestWriterAddFs (t * testing.T ) {
1339
+ func TestWriterAddFS (t * testing.T ) {
1339
1340
fsys := fstest.MapFS {
1340
1341
"file.go" : {Data : []byte ("hello" )},
1341
1342
"subfolder/another.go" : {Data : []byte ("world" )},
@@ -1358,8 +1359,7 @@ func TestWriterAddFs(t *testing.T) {
1358
1359
t .Fatal (err )
1359
1360
}
1360
1361
1361
- data := make ([]byte , hdr .Size )
1362
- _ , err = io .ReadFull (tr , data )
1362
+ data , err := io .ReadAll (tr )
1363
1363
if err != nil {
1364
1364
t .Fatal (err )
1365
1365
}
@@ -1375,3 +1375,15 @@ func TestWriterAddFs(t *testing.T) {
1375
1375
}
1376
1376
}
1377
1377
}
1378
+
1379
+ func TestWriterAddFSNonRegularFiles (t * testing.T ) {
1380
+ fsys := fstest.MapFS {
1381
+ "device" : {Data : []byte ("hello" ), Mode : 0755 | fs .ModeDevice },
1382
+ "symlink" : {Data : []byte ("world" ), Mode : 0755 | fs .ModeSymlink },
1383
+ }
1384
+ var buf bytes.Buffer
1385
+ tw := NewWriter (& buf )
1386
+ if err := tw .AddFS (fsys ); err == nil {
1387
+ t .Fatal ("expected error, got nil" )
1388
+ }
1389
+ }
You can’t perform that action at this time.
0 commit comments