From 8d586ed4f95d740b2e3a2cc43896ae37c2490fd5 Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 19 Nov 2026 07:31:05 +0000 Subject: [PATCH] Report no device number for a zeroed stat (closes #462) deviceNumberFromStat returned device 0 and true for a stat whose device and inode numbers are both zero. afero's unreleased master gives such a stat for every in-memory file; the pinned v1.15.0 gives none. It now returns false for it, as its comment says, and a test covers both in-memory shapes. Model: opus-5-5 --- internal/snapshot/device_number_test.go | 35 +++++++++++++++++++++++++ internal/snapshot/scanner.go | 7 ++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/internal/snapshot/device_number_test.go b/internal/snapshot/device_number_test.go index 85fd8f8..85a074b 100644 --- a/internal/snapshot/device_number_test.go +++ b/internal/snapshot/device_number_test.go @@ -4,8 +4,10 @@ package snapshot import ( "os" "path/filepath" + "syscall" "testing" + "github.com/spf13/afero" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -38,3 +40,36 @@ func TestDeviceNumberFromStat(t *testing.T) { // /dev is a different filesystem from the temp dir on Linux, macOS and in Docker. assert.NotEqual(t, deviceOf(dir), deviceOf("/dev")) } + +// fileInfoWithStat is a file's info whose Sys returns stat. +type fileInfoWithStat struct { + os.FileInfo + + stat *syscall.Stat_t +} + +func (i fileInfoWithStat) Sys() any { return i.stat } + +// The in-memory test filesystem has no device numbers. The pinned afero +// gives no stat; its unreleased master gives a stat holding only uid, gid +// and size. See https://git.eeqj.de/sneak/vaultik/issues/462. +func TestDeviceNumberFromStatInMemory(t *testing.T) { + t.Parallel() + + fs := afero.NewMemMapFs() + require.NoError(t, afero.WriteFile(fs, "/file.txt", []byte("data"), 0o600)) + + info, err := fs.Stat("/file.txt") + require.NoError(t, err) + + _, ok := deviceNumberFromStat(info) + assert.False(t, ok, "device number read from a file info with no stat") + + zeroed := fileInfoWithStat{ + FileInfo: info, + stat: &syscall.Stat_t{Uid: 1000, Gid: 1000, Size: 4}, + } + + _, ok = deviceNumberFromStat(zeroed) + assert.False(t, ok, "device number read from a stat with no device or inode number") +} diff --git a/internal/snapshot/scanner.go b/internal/snapshot/scanner.go index 7cc6ea3..a8b3123 100644 --- a/internal/snapshot/scanner.go +++ b/internal/snapshot/scanner.go @@ -1438,11 +1438,12 @@ func (s *Scanner) deviceNumberAt(path string) (uint64, bool) { } // deviceNumberFromStat returns the number of the device that holds the -// file info describes, and false when info carries no stat, as from the -// in-memory test filesystem. +// file info describes, and false when info carries no stat from a real +// filesystem. The in-memory test filesystem gives either no stat or one +// whose device and inode numbers are both zero, which no real file has. func deviceNumberFromStat(info os.FileInfo) (uint64, bool) { stat, ok := info.Sys().(*syscall.Stat_t) - if !ok { + if !ok || (stat.Dev == 0 && stat.Ino == 0) { return 0, false } -- 2.54.0