@@ -26,6 +26,7 @@ import (
26
26
27
27
"github.com/containerd/log"
28
28
"github.com/klauspost/compress/zstd"
29
+ "github.com/moby/go-archive/tarheader"
29
30
"github.com/moby/patternmatcher"
30
31
"github.com/moby/sys/sequential"
31
32
"github.com/moby/sys/user"
@@ -475,71 +476,11 @@ func (compression *Compression) Extension() string {
475
476
return ""
476
477
}
477
478
478
- // assert that we implement [tar.FileInfoNames].
479
- var _ tar.FileInfoNames = (* nosysFileInfo )(nil )
480
-
481
- // nosysFileInfo hides the system-dependent info of the wrapped FileInfo to
482
- // prevent tar.FileInfoHeader from introspecting it and potentially calling into
483
- // glibc.
484
- //
485
- // It implements [tar.FileInfoNames] to further prevent [tar.FileInfoHeader]
486
- // from performing any lookups on go1.23 and up. see https://go.dev/issue/50102
487
- type nosysFileInfo struct {
488
- os.FileInfo
489
- }
490
-
491
- // Uname stubs out looking up username. It implements [tar.FileInfoNames]
492
- // to prevent [tar.FileInfoHeader] from loading libraries to perform
493
- // username lookups.
494
- func (fi nosysFileInfo ) Uname () (string , error ) {
495
- return "" , nil
496
- }
497
-
498
- // Gname stubs out looking up group-name. It implements [tar.FileInfoNames]
499
- // to prevent [tar.FileInfoHeader] from loading libraries to perform
500
- // username lookups.
501
- func (fi nosysFileInfo ) Gname () (string , error ) {
502
- return "" , nil
503
- }
504
-
505
- func (fi nosysFileInfo ) Sys () interface {} {
506
- // A Sys value of type *tar.Header is safe as it is system-independent.
507
- // The tar.FileInfoHeader function copies the fields into the returned
508
- // header without performing any OS lookups.
509
- if sys , ok := fi .FileInfo .Sys ().(* tar.Header ); ok {
510
- return sys
511
- }
512
- return nil
513
- }
514
-
515
- // sysStat, if non-nil, populates hdr from system-dependent fields of fi.
516
- var sysStat func (fi os.FileInfo , hdr * tar.Header ) error
517
-
518
479
// FileInfoHeaderNoLookups creates a partially-populated tar.Header from fi.
519
480
//
520
- // Compared to the archive/tar.FileInfoHeader function, this function is safe to
521
- // call from a chrooted process as it does not populate fields which would
522
- // require operating system lookups. It behaves identically to
523
- // tar.FileInfoHeader when fi is a FileInfo value returned from
524
- // tar.Header.FileInfo().
525
- //
526
- // When fi is a FileInfo for a native file, such as returned from os.Stat() and
527
- // os.Lstat(), the returned Header value differs from one returned from
528
- // tar.FileInfoHeader in the following ways. The Uname and Gname fields are not
529
- // set as OS lookups would be required to populate them. The AccessTime and
530
- // ChangeTime fields are not currently set (not yet implemented) although that
531
- // is subject to change. Callers which require the AccessTime or ChangeTime
532
- // fields to be zeroed should explicitly zero them out in the returned Header
533
- // value to avoid any compatibility issues in the future.
481
+ // Deprecated: use [tarheader.FileInfoHeaderNoLookups].
534
482
func FileInfoHeaderNoLookups (fi os.FileInfo , link string ) (* tar.Header , error ) {
535
- hdr , err := tar .FileInfoHeader (nosysFileInfo {fi }, link )
536
- if err != nil {
537
- return nil , err
538
- }
539
- if sysStat != nil {
540
- return hdr , sysStat (fi , hdr )
541
- }
542
- return hdr , nil
483
+ return tarheader .FileInfoHeaderNoLookups (fi , link )
543
484
}
544
485
545
486
// FileInfoHeader creates a populated Header from fi.
@@ -550,7 +491,7 @@ func FileInfoHeaderNoLookups(fi os.FileInfo, link string) (*tar.Header, error) {
550
491
// precision, and the Uname and Gname fields are only set when fi is a FileInfo
551
492
// value returned from tar.Header.FileInfo().
552
493
func FileInfoHeader (name string , fi os.FileInfo , link string ) (* tar.Header , error ) {
553
- hdr , err := FileInfoHeaderNoLookups (fi , link )
494
+ hdr , err := tarheader . FileInfoHeaderNoLookups (fi , link )
554
495
if err != nil {
555
496
return nil , err
556
497
}
@@ -1420,7 +1361,7 @@ func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
1420
1361
}
1421
1362
defer srcF .Close ()
1422
1363
1423
- hdr , err := FileInfoHeaderNoLookups (srcSt , "" )
1364
+ hdr , err := tarheader . FileInfoHeaderNoLookups (srcSt , "" )
1424
1365
if err != nil {
1425
1366
return err
1426
1367
}
0 commit comments