fs: replace statT with ExtendedFileInfo

This commit is contained in:
Michael Eischer 2024-08-24 23:43:45 +02:00
parent 6d3a5260d3
commit f0329bb4e6
14 changed files with 50 additions and 176 deletions

View file

@ -4,12 +4,12 @@
package fs
import (
"io/fs"
"os"
"path/filepath"
"runtime"
"syscall"
"testing"
"time"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
@ -28,8 +28,11 @@ func stat(t testing.TB, filename string) (fi os.FileInfo, ok bool) {
return fi, true
}
func checkFile(t testing.TB, stat *syscall.Stat_t, node *restic.Node) {
func checkFile(t testing.TB, fi fs.FileInfo, node *restic.Node) {
t.Helper()
stat := fi.Sys().(*syscall.Stat_t)
if uint32(node.Mode.Perm()) != uint32(stat.Mode&0777) {
t.Errorf("Mode does not match, want %v, got %v", stat.Mode&0777, node.Mode)
}
@ -59,29 +62,20 @@ func checkFile(t testing.TB, stat *syscall.Stat_t, node *restic.Node) {
}
// use the os dependent function to compare the timestamps
s, ok := toStatT(stat)
if !ok {
return
s := ExtendedStat(fi)
if node.ModTime != s.ModTime {
t.Errorf("ModTime does not match, want %v, got %v", s.ModTime, node.ModTime)
}
mtime := s.mtim()
if node.ModTime != time.Unix(mtime.Unix()) {
t.Errorf("ModTime does not match, want %v, got %v", time.Unix(mtime.Unix()), node.ModTime)
if node.ChangeTime != s.ChangeTime {
t.Errorf("ChangeTime does not match, want %v, got %v", s.ChangeTime, node.ChangeTime)
}
ctime := s.ctim()
if node.ChangeTime != time.Unix(ctime.Unix()) {
t.Errorf("ChangeTime does not match, want %v, got %v", time.Unix(ctime.Unix()), node.ChangeTime)
if node.AccessTime != s.AccessTime {
t.Errorf("AccessTime does not match, want %v, got %v", s.AccessTime, node.AccessTime)
}
atime := s.atim()
if node.AccessTime != time.Unix(atime.Unix()) {
t.Errorf("AccessTime does not match, want %v, got %v", time.Unix(atime.Unix()), node.AccessTime)
}
}
func checkDevice(t testing.TB, stat *syscall.Stat_t, node *restic.Node) {
func checkDevice(t testing.TB, fi fs.FileInfo, node *restic.Node) {
stat := fi.Sys().(*syscall.Stat_t)
if node.Device != uint64(stat.Rdev) {
t.Errorf("Rdev does not match, want %v, got %v", stat.Rdev, node.Device)
}
@ -123,12 +117,6 @@ func TestNodeFromFileInfo(t *testing.T) {
return
}
s, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
t.Skipf("fi type is %T, not stat_t", fi.Sys())
return
}
node, err := NodeFromFileInfo(test.filename, fi, false)
if err != nil {
t.Fatal(err)
@ -136,10 +124,10 @@ func TestNodeFromFileInfo(t *testing.T) {
switch node.Type {
case restic.NodeTypeFile, restic.NodeTypeSymlink:
checkFile(t, s, node)
checkFile(t, fi, node)
case restic.NodeTypeDev, restic.NodeTypeCharDev:
checkFile(t, s, node)
checkDevice(t, s, node)
checkFile(t, fi, node)
checkDevice(t, fi, node)
default:
t.Fatalf("invalid node type %q", node.Type)
}