Skip to content

Commit

Permalink
fixup! Restore original timestamps when unpacking
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonc committed Oct 12, 2023
1 parent e4f8153 commit 665639c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions internal/unpackinfo/lchtimes_linux_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// This capability is only available on Linux and Darwin as of now.
func (i UnpackInfo) Lchtimes() error {
return unix.Lutimes(i.Path, []unix.Timeval{
{Sec: i.AccessTime.Unix()},
{Sec: i.ModTime.Unix()}},
{Sec: i.OriginalAccessTime.Unix()},
{Sec: i.OriginalModTime.Unix()}},
)
}
28 changes: 14 additions & 14 deletions internal/unpackinfo/unpackinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
// The information can be used later to restore the original permissions
// and timestamps based on the type of entry the info represents.
type UnpackInfo struct {
Path string
AccessTime time.Time
ModTime time.Time
Mode fs.FileMode
Typeflag byte
Path string
OriginalAccessTime time.Time
OriginalModTime time.Time
OriginalMode fs.FileMode
Typeflag byte
}

// NewUnpackInfo returns an UnpackInfo based on a destination root and a tar header.
Expand Down Expand Up @@ -74,11 +74,11 @@ func NewUnpackInfo(dst string, header *tar.Header) (UnpackInfo, error) {
}

result := UnpackInfo{
Path: path,
AccessTime: header.AccessTime,
ModTime: header.ModTime,
Mode: header.FileInfo().Mode(),
Typeflag: header.Typeflag,
Path: path,
OriginalAccessTime: header.AccessTime,
OriginalModTime: header.ModTime,
OriginalMode: header.FileInfo().Mode(),
Typeflag: header.Typeflag,
}

if !result.IsDirectory() && !result.IsSymlink() && !result.IsRegular() && !result.IsTypeX() {
Expand Down Expand Up @@ -136,11 +136,11 @@ func (i UnpackInfo) RestoreInfo() error {
}

func (i UnpackInfo) restoreDirectory() error {
if err := os.Chtimes(i.Path, i.AccessTime, i.ModTime); err != nil && !os.IsNotExist(err) {
if err := os.Chtimes(i.Path, i.OriginalAccessTime, i.OriginalModTime); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed setting times on directory %q: %w", i.Path, err)
}

if err := os.Chmod(i.Path, i.Mode); err != nil && !os.IsNotExist(err) {
if err := os.Chmod(i.Path, i.OriginalMode); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed setting permissions on directory %q: %w", i.Path, err)
}
return nil
Expand All @@ -154,11 +154,11 @@ func (i UnpackInfo) restoreSymlink() error {
}

func (i UnpackInfo) restoreNormal() error {
if err := os.Chmod(i.Path, i.Mode); err != nil {
if err := os.Chmod(i.Path, i.OriginalMode); err != nil {
return fmt.Errorf("failed setting permissions on %q: %w", i.Path, err)
}

if err := os.Chtimes(i.Path, i.AccessTime, i.ModTime); err != nil {
if err := os.Chtimes(i.Path, i.OriginalAccessTime, i.OriginalModTime); err != nil {
return fmt.Errorf("failed setting times on %q: %w", i.Path, err)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/unpackinfo/unpackinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ func TestUnpackInfo_RestoreInfo(t *testing.T) {
}

if !info.IsSymlink() {
if stat.Mode() != info.Mode {
t.Errorf("%q mode %q did not match expected header mode %q", info.Path, stat.Mode(), info.Mode)
if stat.Mode() != info.OriginalMode {
t.Errorf("%q mode %q did not match expected header mode %q", info.Path, stat.Mode(), info.OriginalMode)
}
} else if CanMaintainSymlinkTimestamps() {
if !stat.ModTime().Truncate(time.Second).Equal(exampleModTime) {
Expand Down

0 comments on commit 665639c

Please sign in to comment.