Skip to content

Commit a173807

Browse files
committed
rename some vars to prevent shadowing "path" import
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 71f1d11 commit a173807

4 files changed

Lines changed: 64 additions & 64 deletions

File tree

archive.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ const (
9898

9999
// IsArchivePath checks if the (possibly compressed) file at the given path
100100
// starts with a tar file header.
101-
func IsArchivePath(path string) bool {
102-
file, err := os.Open(path)
101+
func IsArchivePath(filePath string) bool {
102+
file, err := os.Open(filePath)
103103
if err != nil {
104104
return false
105105
}
@@ -229,15 +229,15 @@ const paxSchilyXattr = "SCHILY.xattr."
229229

230230
// ReadSecurityXattrToTarHeader reads security.capability xattr from filesystem
231231
// to a tar header
232-
func ReadSecurityXattrToTarHeader(path string, hdr *tar.Header) error {
232+
func ReadSecurityXattrToTarHeader(filePath string, hdr *tar.Header) error {
233233
const (
234234
// Values based on linux/include/uapi/linux/capability.h
235235
xattrCapsSz2 = 20
236236
versionOffset = 3
237237
vfsCapRevision2 = 2
238238
vfsCapRevision3 = 3
239239
)
240-
capability, _ := lgetxattr(path, "security.capability")
240+
capability, _ := lgetxattr(filePath, "security.capability")
241241
if capability != nil {
242242
if capability[versionOffset] == vfsCapRevision3 {
243243
// Convert VFS_CAP_REVISION_3 to VFS_CAP_REVISION_2 as root UID makes no
@@ -294,17 +294,17 @@ func canonicalTarName(name string, isDir bool) string {
294294
return name
295295
}
296296

297-
// addTarFile adds to the tar archive a file from `path` as `name`
298-
func (ta *tarAppender) addTarFile(path, name string) error {
299-
fi, err := os.Lstat(path)
297+
// addTarFile adds to the tar archive a file from `srcPath` as `name`
298+
func (ta *tarAppender) addTarFile(srcPath, name string) error {
299+
fi, err := os.Lstat(srcPath)
300300
if err != nil {
301301
return err
302302
}
303303

304304
var link string
305305
if fi.Mode()&os.ModeSymlink != 0 {
306306
var err error
307-
link, err = os.Readlink(path)
307+
link, err = os.Readlink(srcPath)
308308
if err != nil {
309309
return err
310310
}
@@ -314,7 +314,7 @@ func (ta *tarAppender) addTarFile(path, name string) error {
314314
if err != nil {
315315
return err
316316
}
317-
if err := ReadSecurityXattrToTarHeader(path, hdr); err != nil {
317+
if err := ReadSecurityXattrToTarHeader(srcPath, hdr); err != nil {
318318
return err
319319
}
320320

@@ -361,7 +361,7 @@ func (ta *tarAppender) addTarFile(path, name string) error {
361361
}
362362

363363
if ta.WhiteoutConverter != nil {
364-
wo, err := ta.WhiteoutConverter.ConvertWrite(hdr, path, fi)
364+
wo, err := ta.WhiteoutConverter.ConvertWrite(hdr, srcPath, fi)
365365
if err != nil {
366366
return err
367367
}
@@ -389,7 +389,7 @@ func (ta *tarAppender) addTarFile(path, name string) error {
389389
if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
390390
// We use sequential file access to avoid depleting the standby list on
391391
// Windows. On Linux, this equates to a regular os.Open.
392-
file, err := sequential.Open(path)
392+
file, err := sequential.Open(srcPath)
393393
if err != nil {
394394
return err
395395
}
@@ -404,7 +404,7 @@ func (ta *tarAppender) addTarFile(path, name string) error {
404404
return nil
405405
}
406406

407-
func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, opts *TarOptions) error {
407+
func createTarFile(dstPath, extractDir string, hdr *tar.Header, reader io.Reader, opts *TarOptions) error {
408408
var (
409409
Lchown = true
410410
inUserns, bestEffortXattrs bool
@@ -428,16 +428,16 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, o
428428
case tar.TypeDir:
429429
// Create directory unless it exists as a directory already.
430430
// In that case we just want to merge the two
431-
if fi, err := os.Lstat(path); err != nil || !fi.IsDir() {
432-
if err := os.Mkdir(path, hdrInfo.Mode()); err != nil {
431+
if fi, err := os.Lstat(dstPath); err != nil || !fi.IsDir() {
432+
if err := os.Mkdir(dstPath, hdrInfo.Mode()); err != nil {
433433
return err
434434
}
435435
}
436436

437437
case tar.TypeReg:
438438
// Source is regular file. We use sequential file access to avoid depleting
439439
// the standby list on Windows. On Linux, this equates to a regular os.OpenFile.
440-
file, err := sequential.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
440+
file, err := sequential.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
441441
if err != nil {
442442
return err
443443
}
@@ -449,20 +449,20 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, o
449449

450450
case tar.TypeBlock, tar.TypeChar:
451451
if inUserns { // cannot create devices in a userns
452-
log.G(context.TODO()).WithFields(log.Fields{"path": path, "type": hdr.Typeflag}).Debug("skipping device nodes in a userns")
452+
log.G(context.TODO()).WithFields(log.Fields{"path": dstPath, "type": hdr.Typeflag}).Debug("skipping device nodes in a userns")
453453
return nil
454454
}
455455
// Handle this is an OS-specific way
456-
if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
456+
if err := handleTarTypeBlockCharFifo(hdr, dstPath); err != nil {
457457
return err
458458
}
459459

460460
case tar.TypeFifo:
461461
// Handle this is an OS-specific way
462-
if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
462+
if err := handleTarTypeBlockCharFifo(hdr, dstPath); err != nil {
463463
if inUserns && errors.Is(err, syscall.EPERM) {
464464
// In most cases, cannot create a fifo if running in user namespace
465-
log.G(context.TODO()).WithFields(log.Fields{"error": err, "path": path, "type": hdr.Typeflag}).Debug("creating fifo node in a userns")
465+
log.G(context.TODO()).WithFields(log.Fields{"error": err, "path": dstPath, "type": hdr.Typeflag}).Debug("creating fifo node in a userns")
466466
return nil
467467
}
468468
return err
@@ -475,21 +475,21 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, o
475475
if !strings.HasPrefix(targetPath, extractDir) {
476476
return breakoutError(fmt.Errorf("invalid hardlink %q -> %q", targetPath, hdr.Linkname))
477477
}
478-
if err := os.Link(targetPath, path); err != nil {
478+
if err := os.Link(targetPath, dstPath); err != nil {
479479
return err
480480
}
481481

482482
case tar.TypeSymlink:
483483
// path -> hdr.Linkname = targetPath
484484
// e.g. /extractDir/path/to/symlink -> ../2/file = /extractDir/path/2/file
485-
targetPath := filepath.Join(filepath.Dir(path), hdr.Linkname) // #nosec G305 -- The target path is checked for path traversal.
485+
targetPath := filepath.Join(filepath.Dir(dstPath), hdr.Linkname) // #nosec G305 -- The target path is checked for path traversal.
486486

487487
// the reason we don't need to check symlinks in the path (with FollowSymlinkInScope) is because
488488
// that symlink would first have to be created, which would be caught earlier, at this very check:
489489
if !strings.HasPrefix(targetPath, extractDir) {
490-
return breakoutError(fmt.Errorf("invalid symlink %q -> %q", path, hdr.Linkname))
490+
return breakoutError(fmt.Errorf("invalid symlink %q -> %q", dstPath, hdr.Linkname))
491491
}
492-
if err := os.Symlink(hdr.Linkname, path); err != nil {
492+
if err := os.Symlink(hdr.Linkname, dstPath); err != nil {
493493
return err
494494
}
495495

@@ -506,12 +506,12 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, o
506506
if chownOpts == nil {
507507
chownOpts = &ChownOpts{UID: hdr.Uid, GID: hdr.Gid}
508508
}
509-
if err := os.Lchown(path, chownOpts.UID, chownOpts.GID); err != nil {
509+
if err := os.Lchown(dstPath, chownOpts.UID, chownOpts.GID); err != nil {
510510
var msg string
511511
if inUserns && errors.Is(err, syscall.EINVAL) {
512512
msg = " (try increasing the number of subordinate IDs in /etc/subuid and /etc/subgid)"
513513
}
514-
return fmt.Errorf("failed to Lchown %q for UID %d, GID %d%s: %w", path, hdr.Uid, hdr.Gid, msg, err)
514+
return fmt.Errorf("failed to Lchown %q for UID %d, GID %d%s: %w", dstPath, hdr.Uid, hdr.Gid, msg, err)
515515
}
516516
}
517517

@@ -521,7 +521,7 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, o
521521
if !ok {
522522
continue
523523
}
524-
if err := lsetxattr(path, xattr, []byte(value), 0); err != nil {
524+
if err := lsetxattr(dstPath, xattr, []byte(value), 0); err != nil {
525525
if bestEffortXattrs && errors.Is(err, syscall.ENOTSUP) || errors.Is(err, syscall.EPERM) {
526526
// EPERM occurs if modifying xattrs is not allowed. This can
527527
// happen when running in userns with restrictions (ChromeOS).
@@ -540,7 +540,7 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, o
540540

541541
// There is no LChmod, so ignore mode for symlink. Also, this
542542
// must happen after chown, as that can modify the file mode
543-
if err := handleLChmod(hdr, path, hdrInfo); err != nil {
543+
if err := handleLChmod(hdr, dstPath, hdrInfo); err != nil {
544544
return err
545545
}
546546

@@ -550,29 +550,29 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, o
550550
// chtimes doesn't support a NOFOLLOW flag atm
551551
if hdr.Typeflag == tar.TypeLink {
552552
if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
553-
if err := chtimes(path, aTime, mTime); err != nil {
553+
if err := chtimes(dstPath, aTime, mTime); err != nil {
554554
return err
555555
}
556556
}
557557
} else if hdr.Typeflag != tar.TypeSymlink {
558-
if err := chtimes(path, aTime, mTime); err != nil {
558+
if err := chtimes(dstPath, aTime, mTime); err != nil {
559559
return err
560560
}
561561
} else {
562-
if err := lchtimes(path, aTime, mTime); err != nil {
562+
if err := lchtimes(dstPath, aTime, mTime); err != nil {
563563
return err
564564
}
565565
}
566566
return nil
567567
}
568568

569-
// Tar creates an archive from the directory at `path`, and returns it as a
569+
// Tar creates an archive from the directory at `srcPath`, and returns it as a
570570
// stream of bytes.
571-
func Tar(path string, comp compression.Compression) (io.ReadCloser, error) {
572-
return TarWithOptions(path, &TarOptions{Compression: comp})
571+
func Tar(srcPath string, comp compression.Compression) (io.ReadCloser, error) {
572+
return TarWithOptions(srcPath, &TarOptions{Compression: comp})
573573
}
574574

575-
// TarWithOptions creates an archive from the directory at `path`, only including files whose relative
575+
// TarWithOptions creates an archive from the directory at `srcPath`, only including files whose relative
576576
// paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`.
577577
func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) {
578578
tb, err := NewTarballer(srcPath, options)
@@ -848,8 +848,8 @@ loop:
848848
}
849849

850850
// #nosec G305 -- The joined path is checked for path traversal.
851-
path := filepath.Join(dest, hdr.Name)
852-
rel, err := filepath.Rel(dest, path)
851+
dstPath := filepath.Join(dest, hdr.Name)
852+
rel, err := filepath.Rel(dest, dstPath)
853853
if err != nil {
854854
return err
855855
}
@@ -861,25 +861,25 @@ loop:
861861
// The only exception is when it is a directory *and* the file from
862862
// the layer is also a directory. Then we want to merge them (i.e.
863863
// just apply the metadata from the layer).
864-
if fi, err := os.Lstat(path); err == nil {
864+
if fi, err := os.Lstat(dstPath); err == nil {
865865
if options.NoOverwriteDirNonDir && fi.IsDir() && hdr.Typeflag != tar.TypeDir {
866866
// If NoOverwriteDirNonDir is true then we cannot replace
867867
// an existing directory with a non-directory from the archive.
868-
return fmt.Errorf("cannot overwrite directory %q with non-directory %q", path, dest)
868+
return fmt.Errorf("cannot overwrite directory %q with non-directory %q", dstPath, dest)
869869
}
870870

871871
if options.NoOverwriteDirNonDir && !fi.IsDir() && hdr.Typeflag == tar.TypeDir {
872872
// If NoOverwriteDirNonDir is true then we cannot replace
873873
// an existing non-directory with a directory from the archive.
874-
return fmt.Errorf("cannot overwrite non-directory %q with directory %q", path, dest)
874+
return fmt.Errorf("cannot overwrite non-directory %q with directory %q", dstPath, dest)
875875
}
876876

877877
if fi.IsDir() && hdr.Name == "." {
878878
continue
879879
}
880880

881881
if !fi.IsDir() || hdr.Typeflag != tar.TypeDir {
882-
if err := os.RemoveAll(path); err != nil {
882+
if err := os.RemoveAll(dstPath); err != nil {
883883
return err
884884
}
885885
}
@@ -890,7 +890,7 @@ loop:
890890
}
891891

892892
if whiteoutConverter != nil {
893-
writeFile, err := whiteoutConverter.ConvertRead(hdr, path)
893+
writeFile, err := whiteoutConverter.ConvertRead(hdr, dstPath)
894894
if err != nil {
895895
return err
896896
}
@@ -899,7 +899,7 @@ loop:
899899
}
900900
}
901901

902-
if err := createTarFile(path, dest, hdr, tr, options); err != nil {
902+
if err := createTarFile(dstPath, dest, hdr, tr, options); err != nil {
903903
return err
904904
}
905905

@@ -912,9 +912,9 @@ loop:
912912

913913
for _, hdr := range dirs {
914914
// #nosec G305 -- The header was checked for path traversal before it was appended to the dirs slice.
915-
path := filepath.Join(dest, hdr.Name)
915+
dstPath := filepath.Join(dest, hdr.Name)
916916

917-
if err := chtimes(path, boundTime(latestTime(hdr.AccessTime, hdr.ModTime)), boundTime(hdr.ModTime)); err != nil {
917+
if err := chtimes(dstPath, boundTime(latestTime(hdr.AccessTime, hdr.ModTime)), boundTime(hdr.ModTime)); err != nil {
918918
return err
919919
}
920920
}

archive_linux.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func getWhiteoutConverter(format WhiteoutFormat) tarWhiteoutConverter {
2020

2121
type overlayWhiteoutConverter struct{}
2222

23-
func (overlayWhiteoutConverter) ConvertWrite(hdr *tar.Header, path string, fi os.FileInfo) (wo *tar.Header, _ error) {
23+
func (overlayWhiteoutConverter) ConvertWrite(hdr *tar.Header, filePath string, fi os.FileInfo) (wo *tar.Header, _ error) {
2424
// convert whiteouts to AUFS format
2525
if fi.Mode()&os.ModeCharDevice != 0 && hdr.Devmajor == 0 && hdr.Devminor == 0 {
2626
// we just rename the file and make it normal
@@ -42,7 +42,7 @@ func (overlayWhiteoutConverter) ConvertWrite(hdr *tar.Header, path string, fi os
4242
}
4343

4444
// convert opaque dirs to AUFS format by writing an empty file with the prefix
45-
opaque, err := lgetxattr(path, opaqueXattrName)
45+
opaque, err := lgetxattr(filePath, opaqueXattrName)
4646
if err != nil {
4747
return nil, err
4848
}
@@ -68,9 +68,9 @@ func (overlayWhiteoutConverter) ConvertWrite(hdr *tar.Header, path string, fi os
6868
}, nil
6969
}
7070

71-
func (c overlayWhiteoutConverter) ConvertRead(hdr *tar.Header, path string) (bool, error) {
72-
base := filepath.Base(path)
73-
dir := filepath.Dir(path)
71+
func (c overlayWhiteoutConverter) ConvertRead(hdr *tar.Header, filePath string) (bool, error) {
72+
base := filepath.Base(filePath)
73+
dir := filepath.Dir(filePath)
7474

7575
// if a directory is marked as opaque by the AUFS special file, we need to translate that to overlay
7676
if base == WhiteoutOpaqueDir {

archive_unix.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func getFileUIDGID(stat any) (int, int, error) {
5656
//
5757
// Creating device nodes is not supported when running in a user namespace,
5858
// produces a [syscall.EPERM] in most cases.
59-
func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
59+
func handleTarTypeBlockCharFifo(hdr *tar.Header, dstPath string) error {
6060
mode := uint32(hdr.Mode & 0o7777)
6161
switch hdr.Typeflag {
6262
case tar.TypeBlock:
@@ -67,18 +67,18 @@ func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
6767
mode |= unix.S_IFIFO
6868
}
6969

70-
return mknod(path, mode, unix.Mkdev(uint32(hdr.Devmajor), uint32(hdr.Devminor)))
70+
return mknod(dstPath, mode, unix.Mkdev(uint32(hdr.Devmajor), uint32(hdr.Devminor)))
7171
}
7272

73-
func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
73+
func handleLChmod(hdr *tar.Header, dstPath string, hdrInfo os.FileInfo) error {
7474
if hdr.Typeflag == tar.TypeLink {
7575
if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
76-
if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
76+
if err := os.Chmod(dstPath, hdrInfo.Mode()); err != nil {
7777
return err
7878
}
7979
}
8080
} else if hdr.Typeflag != tar.TypeSymlink {
81-
if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
81+
if err := os.Chmod(dstPath, hdrInfo.Mode()); err != nil {
8282
return err
8383
}
8484
}

0 commit comments

Comments
 (0)