Skip to content

Commit

Permalink
Compare timestamps with 100ns resolution instead of 1ns
Browse files Browse the repository at this point in the history
SMB only supports 100ns, so we may have missed corruption in
the following case:

1) Files are stored on ext4, cshatag runs and tags all files
2) Files are moved to SMB
3) Some file content gets corrupted during the move
4) cshatag considers these as "outdated" instead of "corrupt"

100ns should be good enough for everything, and avoids this
problem.

#21
  • Loading branch information
rfjakob committed Apr 16, 2023
1 parent 2685330 commit 3e1f62b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion check.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ func (ts *fileTimestamp) prettyPrint() string {
return fmt.Sprintf("%010d.%09d", ts.s, ts.ns)
}

// equal100ns compares ts and ts2 with 100ns resolution.
// Why 100ns? That's what SMB supports.
func (ts *fileTimestamp) equal100ns(ts2 *fileTimestamp) bool {
if ts.s != ts2.s {
return false
}
if ts.ns/100 != ts2.ns/100 {
return false
}
return true
}

type fileAttr struct {
ts fileTimestamp
sha256 []byte
Expand Down Expand Up @@ -144,7 +156,7 @@ func checkFile(fn string) {
return
}

if stored.ts == actual.ts {
if stored.ts.equal100ns(&actual.ts) {
if bytes.Equal(stored.sha256, actual.sha256) {
if !args.q {
fmt.Printf("<ok> %s\n", fn)
Expand Down
17 changes: 17 additions & 0 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,21 @@ diff -u 11.expected 11.out
../cshatag -remove foo.txt > 11.out2
diff -u 11.expected 11.out2

echo '*** Testing 100ns resolution ***'
# https://github.com/rfjakob/cshatag/issues/21
rm -rf foo.txt
# Datestring generated using "date --rfc-3339=ns"
touch --date="2023-04-16 20:56:16.585798497+02:00" foo.txt
../cshatag foo.txt > /dev/null
echo asd > foo.txt
touch --date="2023-04-16 20:56:16.585798400+02:00" foo.txt
set +e
../cshatag foo.txt &> /dev/null
RES=$?
set -e
if [[ $RES -ne 5 ]]; then
echo "returned $RES, should have returned error code 5"
exit 1
fi

echo "*** ALL TESTS PASSED ***"

0 comments on commit 3e1f62b

Please sign in to comment.