Skip to content

Commit

Permalink
Provide a platform-dependent implementation of CanMaintainSymlinkTime…
Browse files Browse the repository at this point in the history
…stamps
  • Loading branch information
brandonc committed Oct 24, 2023
1 parent cc4cde7 commit 23d1530
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
15 changes: 14 additions & 1 deletion internal/unpackinfo/lchtimes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@ import (
)

// Lchtimes modifies the access and modified timestamps on a target path
// This capability is only available on Linux and Darwin as of now.
// This capability is only available on Linux and Darwin as of now. The
// timestamps within UnpackInfo would have already been rounded by
// archive/tar so there is no need for subsecond precision.
func (i UnpackInfo) Lchtimes() error {
return unix.Lutimes(i.Path, []unix.Timeval{
{Sec: i.OriginalAccessTime.Unix(), Usec: int32(i.OriginalAccessTime.UnixMicro() % 1000)},

Check failure on line 16 in internal/unpackinfo/lchtimes.go

View workflow job for this annotation

GitHub Actions / unit-test

cannot use int32(i.OriginalAccessTime.UnixMicro() % 1000) (value of type int32) as int64 value in struct literal
{Sec: i.OriginalModTime.Unix(), Usec: int32(i.OriginalModTime.UnixMicro() % 1000)}},

Check failure on line 17 in internal/unpackinfo/lchtimes.go

View workflow job for this annotation

GitHub Actions / unit-test

cannot use int32(i.OriginalModTime.UnixMicro() % 1000) (value of type int32) as int64 value in struct literal
)
}

// CanMaintainSymlinkTimestamps determines whether is is possible to change
// timestamps on symlinks for the the current platform. For regular files
// and directories, attempts are made to restore permissions and timestamps
// after extraction. But for symbolic links, go's cross-platform
// packages (Chmod and Chtimes) are not capable of changing symlink info
// because those methods follow the symlinks. However, a platform-dependent option
// is provided for linux and darwin (see Lchtimes)
func CanMaintainSymlinkTimestamps() bool {
return true
}
11 changes: 11 additions & 0 deletions internal/unpackinfo/lchtimes_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ import (
func (i UnpackInfo) Lchtimes() error {
return errors.New("Lchtimes is not supported on this platform")
}

// CanMaintainSymlinkTimestamps determines whether is is possible to change
// timestamps on symlinks for the the current platform. For regular files
// and directories, attempts are made to restore permissions and timestamps
// after extraction. But for symbolic links, go's cross-platform
// packages (Chmod and Chtimes) are not capable of changing symlink info
// because those methods follow the symlinks. However, a platform-dependent option
// is provided for linux and darwin (see Lchtimes)
func CanMaintainSymlinkTimestamps() bool {
return false
}
12 changes: 0 additions & 12 deletions internal/unpackinfo/unpackinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
Expand Down Expand Up @@ -109,17 +108,6 @@ func (i UnpackInfo) IsRegular() bool {
return i.Typeflag == tar.TypeReg || i.Typeflag == tar.TypeRegA
}

// CanMaintainSymlinkTimestamps determines whether is is possible to change
// timestamps on symlinks for the the current platform. For regular files
// and directories, attempts are made to restore permissions and timestamps
// after extraction. But for symbolic links, go's cross-platform
// packages (Chmod and Chtimes) are not capable of changing symlink info
// because those methods follow the symlinks. However, a platform-dependent option
// is provided for linux and darwin (see Lchtimes)
func CanMaintainSymlinkTimestamps() bool {
return runtime.GOOS == "linux" || runtime.GOOS == "darwin"
}

// RestoreInfo changes the file mode and timestamps for the given UnpackInfo data
func (i UnpackInfo) RestoreInfo() error {
switch {
Expand Down

0 comments on commit 23d1530

Please sign in to comment.