Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct invalid amTimes #105

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions stat/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package stat
import (
"io/fs"
"os"
"syscall"
"time"

"github.com/inconshreveable/log15"
Expand Down Expand Up @@ -61,6 +62,7 @@ type StatterWithTimeout struct {
failureCount int
lstat LstatFunc
logger log15.Logger
defTime int64
}

// WithTimeout returns a Statter with the given timeout, maxAttempts and
Expand All @@ -75,6 +77,7 @@ func WithTimeout(timeout time.Duration, maxAttempts, maxFailureCount int, logger
logger: logger,
maxFailureCount: maxFailureCount,
lstat: os.Lstat,
defTime: time.Now().Unix(),
}
}

Expand Down Expand Up @@ -135,6 +138,47 @@ func (s *StatterWithTimeout) doLstat(path string, infoCh chan fs.FileInfo, errCh
}

info, err := s.lstat(path)
if err == nil {
stat, ok := info.Sys().(*syscall.Stat_t)
if ok {
s.correctFutureTimes(stat)
s.correctZeroAtimes(stat)
}
}

infoCh <- info
errCh <- err
}

func (s *StatterWithTimeout) correctFutureTimes(stat *syscall.Stat_t) {
if stat.Atim.Sec > s.defTime {
stat.Atim.Sec = s.defTime
}

if stat.Mtim.Sec > s.defTime {
stat.Mtim.Sec = s.defTime
}
}

func (s *StatterWithTimeout) correctZeroAtimes(stat *syscall.Stat_t) {
rk1274 marked this conversation as resolved.
Show resolved Hide resolved
if stat.Atim.Sec <= 0 {
stat.Atim.Sec = s.correctZeroTimes(stat)
}

if stat.Mtim.Sec <= 0 {
stat.Mtim.Sec = s.correctZeroTimes(stat)
}
}

func (s *StatterWithTimeout) correctZeroTimes(stat *syscall.Stat_t) int64 {
switch {
case stat.Mtim.Sec > 0:
return stat.Mtim.Sec
case stat.Atim.Sec > 0:
return stat.Atim.Sec
case stat.Ctim.Sec > 0:
return stat.Ctim.Sec
default:
return s.defTime
}
}
72 changes: 72 additions & 0 deletions stat/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"bytes"
"os"
"path/filepath"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -128,6 +129,77 @@ func TestLstat(t *testing.T) {
So(s.failureCount, ShouldEqual, 2)
})
})

Convey("which will correct invalid times", func() {
s.defTime = time.Now().Add(-24 * time.Hour).Unix()

err = os.Chtimes(pathContent1, time.Unix(0, 0), time.Unix(0, 0))
So(err, ShouldBeNil)

info, err = s.Lstat(pathContent1)
So(err, ShouldBeNil)
So(info, ShouldNotBeNil)

stat, ok := info.Sys().(*syscall.Stat_t)
So(ok, ShouldBeTrue)

So(stat.Atim.Sec, ShouldEqual, stat.Ctim.Sec)
So(stat.Mtim.Sec, ShouldEqual, stat.Ctim.Sec)

err = os.Chtimes(pathContent1, time.Now().Add(time.Hour), time.Now().Add(time.Hour*2))
So(err, ShouldBeNil)

info, err = s.Lstat(pathContent1)
So(err, ShouldBeNil)
So(info, ShouldNotBeNil)

stat, ok = info.Sys().(*syscall.Stat_t)
So(ok, ShouldBeTrue)

So(stat.Atim.Sec, ShouldEqual, s.defTime)
So(stat.Mtim.Sec, ShouldEqual, s.defTime)

validTime := time.Now().Add(-48 * time.Hour)

err = os.Chtimes(pathContent1, validTime, validTime)
So(err, ShouldBeNil)

info, err = s.Lstat(pathContent1)
So(err, ShouldBeNil)
So(info, ShouldNotBeNil)

stat, ok = info.Sys().(*syscall.Stat_t)
So(ok, ShouldBeTrue)

So(stat.Atim.Sec, ShouldEqual, validTime.Unix())
So(stat.Mtim.Sec, ShouldEqual, validTime.Unix())

err = os.Chtimes(pathContent1, validTime, time.Unix(0, 0))
So(err, ShouldBeNil)

info, err = s.Lstat(pathContent1)
So(err, ShouldBeNil)
So(info, ShouldNotBeNil)

stat, ok = info.Sys().(*syscall.Stat_t)
So(ok, ShouldBeTrue)

So(stat.Atim.Sec, ShouldEqual, validTime.Unix())
So(stat.Mtim.Sec, ShouldEqual, validTime.Unix())

err = os.Chtimes(pathContent1, time.Unix(0, 0), validTime)
So(err, ShouldBeNil)

info, err = s.Lstat(pathContent1)
So(err, ShouldBeNil)
So(info, ShouldNotBeNil)

stat, ok = info.Sys().(*syscall.Stat_t)
So(ok, ShouldBeTrue)

So(stat.Atim.Sec, ShouldEqual, validTime.Unix())
So(stat.Mtim.Sec, ShouldEqual, validTime.Unix())
})
})
})
}
Expand Down
Loading