Compare commits
1
Commits
8e814bf0ba
..
next
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5900feb515 |
@@ -427,8 +427,8 @@ operands:
|
|||||||
- A file whose mtime is newer than recorded, or whose size differs, is processed
|
- A file whose mtime is newer than recorded, or whose size differs, is processed
|
||||||
as if new: re-hashed, or recorded without hashes, per the shared-size rule.
|
as if new: re-hashed, or recorded without hashes, per the shared-size rule.
|
||||||
Change detection compares the mtime to the nanosecond, as finely as the
|
Change detection compares the mtime to the nanosecond, as finely as the
|
||||||
filesystem records it, so a same-size rewrite later in the same second as the
|
filesystem records it, so a same-size rewrite counts as a change whenever the
|
||||||
recorded mtime still counts as a change.
|
filesystem gives it a later mtime than recorded, even within the same second.
|
||||||
- A database record whose path lies under one of the scanned operands but was
|
- A database record whose path lies under one of the scanned operands but was
|
||||||
not successfully processed this run is deleted. This removes records for
|
not successfully processed this run is deleted. This removes records for
|
||||||
deleted files. It also removes records for paths that failed to stat or hash
|
deleted files. It also removes records for paths that failed to stat or hash
|
||||||
|
|||||||
@@ -416,7 +416,7 @@ func (s *scanState) walkPhase(
|
|||||||
old, ok := s.existing[ev.rec.path]
|
old, ok := s.existing[ev.rec.path]
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case !ok || old.size != ev.rec.size || ev.rec.mtime.After(old.mtime):
|
case !ok || old.size != ev.rec.size || mtimeAfter(ev.rec.mtime, old.mtime):
|
||||||
changed = append(changed, ev.rec)
|
changed = append(changed, ev.rec)
|
||||||
case old.hashed:
|
case old.hashed:
|
||||||
delete(s.existing, ev.rec.path)
|
delete(s.existing, ev.rec.path)
|
||||||
@@ -808,7 +808,7 @@ func unchangedFile(r scanRec) (fileRec, bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !fi.Mode().IsRegular() || fi.Size() != r.size ||
|
if !fi.Mode().IsRegular() || fi.Size() != r.size ||
|
||||||
fi.ModTime().After(r.mtime) {
|
mtimeAfter(fi.ModTime(), r.mtime) {
|
||||||
return fileRec{}, false, nil
|
return fileRec{}, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -819,6 +819,16 @@ func unchangedFile(r scanRec) (fileRec, bool, error) {
|
|||||||
}, true, nil
|
}, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mtimeAfter reports whether mtime a is later than mtime b.
|
||||||
|
// Not a.After(b): time.Time wraps an mtime past year 292 billion; Unix() undoes it.
|
||||||
|
func mtimeAfter(a, b time.Time) bool {
|
||||||
|
if a.Unix() != b.Unix() {
|
||||||
|
return a.Unix() > b.Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
return a.Nanosecond() > b.Nanosecond()
|
||||||
|
}
|
||||||
|
|
||||||
// underAnyRoot reports whether path is any of the roots or lies under
|
// underAnyRoot reports whether path is any of the roots or lies under
|
||||||
// one of them.
|
// one of them.
|
||||||
func underAnyRoot(path string, roots []string) bool {
|
func underAnyRoot(path string, roots []string) bool {
|
||||||
|
|||||||
+92
-10
@@ -20,6 +20,8 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
// writeFile creates a file with the given content and returns its path.
|
// writeFile creates a file with the given content and returns its path.
|
||||||
@@ -581,6 +583,65 @@ func TestScanContentHashedStalePartners(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestScanContentSameSecondRewrite is TestScanContentStalePartners for
|
||||||
|
// a stored file rewritten in place at the same size with an mtime later
|
||||||
|
// in the same second than recorded: the file counts as changed, so
|
||||||
|
// neither it nor its match inside the operand is read.
|
||||||
|
func TestScanContentSameSecondRewrite(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
db := openTestDB(t)
|
||||||
|
dirA := t.TempDir()
|
||||||
|
changed := sparseFileWithoutMatch(t, dirA, "changed", headTailMin)
|
||||||
|
|
||||||
|
first := time.Date(2026, 1, 2, 3, 4, 5, 100_000_000, time.UTC)
|
||||||
|
|
||||||
|
err := os.Chtimes(changed, first, first)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
syncTree(t, db, dirA)
|
||||||
|
|
||||||
|
before := dbRecords(t, db)
|
||||||
|
|
||||||
|
// Rewrite one byte in place, keeping the size.
|
||||||
|
pokeAt(t, changed, headTailMin/2, []byte{1})
|
||||||
|
|
||||||
|
later := first.Add(500 * time.Millisecond)
|
||||||
|
|
||||||
|
err = os.Chtimes(changed, later, later)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dirB := t.TempDir()
|
||||||
|
sparseFile(t, dirB, "changed-copy", headTailMin)
|
||||||
|
|
||||||
|
st := syncTree(t, db, dirB)
|
||||||
|
if st != (scanStats{walked: 1, added: 1}) {
|
||||||
|
t.Errorf("stats = %+v, want 1 added and nothing skipped", st)
|
||||||
|
}
|
||||||
|
|
||||||
|
recs := dbRecords(t, db)
|
||||||
|
for _, r := range recs {
|
||||||
|
if r.content != "" {
|
||||||
|
t.Errorf("%s: content = %q, want none: its only match is stale",
|
||||||
|
r.path, r.content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, old := range before {
|
||||||
|
if r := recordByPath(t, recs, old.path); r != old {
|
||||||
|
t.Errorf("record = %+v, want it left as %+v", r, old)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if groups := dupeGroups(t, db); len(groups) != 0 {
|
||||||
|
t.Errorf("groups = %+v, want none", groups)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestScanContentReadFailure checks that a failed content read is
|
// TestScanContentReadFailure checks that a failed content read is
|
||||||
// counted as skipped and leaves the record without a content hash, and
|
// counted as skipped and leaves the record without a content hash, and
|
||||||
// that a later scan tries the read again.
|
// that a later scan tries the read again.
|
||||||
@@ -1385,12 +1446,30 @@ func TestSyncScanOperandSameSecondRewrite(t *testing.T) {
|
|||||||
assertWholeFileHashed(t, db, a, rewritten)
|
assertWholeFileHashed(t, db, a, rewritten)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSyncScanRewriteAfter2262 rewrites a file in place at the same
|
// TestSyncScanRewriteAfter2262 runs assertLateRewriteRehashed with an
|
||||||
// size and sets its mtime after 2262, a time too late to count in
|
// mtime after 2262, a time too late to count in nanoseconds in an int64.
|
||||||
// nanoseconds in an int64: the next scan must still re-hash it.
|
|
||||||
func TestSyncScanRewriteAfter2262(t *testing.T) {
|
func TestSyncScanRewriteAfter2262(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
assertLateRewriteRehashed(t, time.Date(2300, 1, 2, 3, 4, 5, 0, time.UTC))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSyncScanRewritePastTimeLimit runs assertLateRewriteRehashed with an
|
||||||
|
// mtime one second past the latest a time.Time holds without wrapping it
|
||||||
|
// to a time far in the past.
|
||||||
|
func TestSyncScanRewritePastTimeLimit(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assertLateRewriteRehashed(t, time.Unix(9223371974719179008, 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
// assertLateRewriteRehashed scans a directory, rewrites a file in it in
|
||||||
|
// place at the same size, sets its mtime to late, and fails unless the
|
||||||
|
// next scan re-hashes the file. It skips where late does not fit the
|
||||||
|
// platform's timespec or the filesystem does not store it.
|
||||||
|
func assertLateRewriteRehashed(t *testing.T, late time.Time) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
db := openTestDB(t)
|
db := openTestDB(t)
|
||||||
a := writeFile(t, dir, "a.bin", pattern(1, 500))
|
a := writeFile(t, dir, "a.bin", pattern(1, 500))
|
||||||
@@ -1403,11 +1482,14 @@ func TestSyncScanRewriteAfter2262(t *testing.T) {
|
|||||||
rewritten := pattern(3, 500)
|
rewritten := pattern(3, 500)
|
||||||
writeFile(t, dir, "a.bin", rewritten)
|
writeFile(t, dir, "a.bin", rewritten)
|
||||||
|
|
||||||
// os.Chtimes cannot set this time: it converts through UnixNano.
|
// os.Chtimes cannot set such a time: it converts through UnixNano.
|
||||||
late := time.Date(2300, 1, 2, 3, 4, 5, 0, time.UTC)
|
ts, err := unix.TimeToTimespec(late)
|
||||||
ts := syscall.Timespec{Sec: late.Unix()}
|
if err != nil {
|
||||||
|
t.Skipf("an mtime %d seconds after 1970 does not fit this platform's "+
|
||||||
|
"timespec: %v", late.Unix(), err)
|
||||||
|
}
|
||||||
|
|
||||||
err := syscall.UtimesNano(a, []syscall.Timespec{ts, ts})
|
err = unix.UtimesNano(a, []unix.Timespec{ts, ts})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -1417,9 +1499,9 @@ func TestSyncScanRewriteAfter2262(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !fi.ModTime().Equal(late) {
|
if fi.ModTime().Unix() != late.Unix() {
|
||||||
t.Skipf("the filesystem stored mtime %v instead of %v",
|
t.Skipf("the filesystem stored the mtime as %d seconds after 1970, "+
|
||||||
fi.ModTime().UTC(), late)
|
"not %d", fi.ModTime().Unix(), late.Unix())
|
||||||
}
|
}
|
||||||
|
|
||||||
st := syncTree(t, db, dir)
|
st := syncTree(t, db, dir)
|
||||||
|
|||||||
Reference in New Issue
Block a user