2022-03-28 20:23:47 +00:00
|
|
|
//go:build !windows
|
2019-05-04 08:34:28 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package archiver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2024-03-28 18:11:56 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/feature"
|
|
|
|
"github.com/restic/restic/internal/fs"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2024-03-28 23:24:03 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2019-05-04 08:34:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type wrappedFileInfo struct {
|
|
|
|
os.FileInfo
|
|
|
|
sys interface{}
|
|
|
|
mode os.FileMode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fi wrappedFileInfo) Sys() interface{} {
|
|
|
|
return fi.sys
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fi wrappedFileInfo) Mode() os.FileMode {
|
|
|
|
return fi.mode
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapFileInfo returns a new os.FileInfo with the mode, owner, and group fields changed.
|
2023-05-18 17:21:28 +00:00
|
|
|
func wrapFileInfo(fi os.FileInfo) os.FileInfo {
|
2019-05-04 08:34:28 +00:00
|
|
|
// get the underlying stat_t and modify the values
|
|
|
|
stat := fi.Sys().(*syscall.Stat_t)
|
2019-05-05 12:57:38 +00:00
|
|
|
stat.Mode = mockFileInfoMode
|
|
|
|
stat.Uid = mockFileInfoUID
|
|
|
|
stat.Gid = mockFileInfoGID
|
2019-05-04 08:34:28 +00:00
|
|
|
|
|
|
|
// wrap the os.FileInfo so we can return a modified stat_t
|
|
|
|
res := wrappedFileInfo{
|
|
|
|
FileInfo: fi,
|
|
|
|
sys: stat,
|
2019-05-05 12:57:38 +00:00
|
|
|
mode: mockFileInfoMode,
|
2019-05-04 08:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2024-03-28 18:11:56 +00:00
|
|
|
|
2024-09-14 16:59:59 +00:00
|
|
|
// wrapIrregularFileInfo returns a new os.FileInfo with the mode changed to irregular file
|
|
|
|
func wrapIrregularFileInfo(fi os.FileInfo) os.FileInfo {
|
|
|
|
// wrap the os.FileInfo so we can return a modified stat_t
|
|
|
|
return wrappedFileInfo{
|
|
|
|
FileInfo: fi,
|
|
|
|
sys: fi.Sys().(*syscall.Stat_t),
|
|
|
|
mode: (fi.Mode() &^ os.ModeType) | os.ModeIrregular,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-19 13:11:32 +00:00
|
|
|
func statAndSnapshot(t *testing.T, repo archiverRepo, name string) (*restic.Node, *restic.Node) {
|
2024-03-28 18:11:56 +00:00
|
|
|
fi := lstat(t, name)
|
2024-01-31 19:48:03 +00:00
|
|
|
want, err := restic.NodeFromFileInfo(name, fi, false)
|
2024-03-28 23:24:03 +00:00
|
|
|
rtest.OK(t, err)
|
2024-03-28 18:11:56 +00:00
|
|
|
|
|
|
|
_, node := snapshot(t, repo, fs.Local{}, nil, name)
|
|
|
|
return want, node
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHardlinkMetadata(t *testing.T) {
|
|
|
|
defer feature.TestSetFlag(t, feature.Flag, feature.DeviceIDForHardlinks, true)()
|
|
|
|
|
|
|
|
files := TestDir{
|
|
|
|
"testfile": TestFile{
|
|
|
|
Content: "foo bar test file",
|
|
|
|
},
|
|
|
|
"linktarget": TestFile{
|
|
|
|
Content: "test file",
|
|
|
|
},
|
|
|
|
"testlink": TestHardlink{
|
|
|
|
Target: "./linktarget",
|
|
|
|
},
|
|
|
|
"testdir": TestDir{},
|
|
|
|
}
|
|
|
|
|
|
|
|
tempdir, repo := prepareTempdirRepoSrc(t, files)
|
|
|
|
|
2024-03-28 23:24:03 +00:00
|
|
|
back := rtest.Chdir(t, tempdir)
|
2024-03-28 18:11:56 +00:00
|
|
|
defer back()
|
|
|
|
|
|
|
|
want, node := statAndSnapshot(t, repo, "testlink")
|
2024-03-28 23:24:03 +00:00
|
|
|
rtest.Assert(t, node.DeviceID == want.DeviceID, "device id mismatch expected %v got %v", want.DeviceID, node.DeviceID)
|
|
|
|
rtest.Assert(t, node.Links == want.Links, "link count mismatch expected %v got %v", want.Links, node.Links)
|
|
|
|
rtest.Assert(t, node.Inode == want.Inode, "inode mismatch expected %v got %v", want.Inode, node.Inode)
|
2024-03-28 18:11:56 +00:00
|
|
|
|
|
|
|
_, node = statAndSnapshot(t, repo, "testfile")
|
2024-03-28 23:24:03 +00:00
|
|
|
rtest.Assert(t, node.DeviceID == 0, "device id mismatch for testfile expected %v got %v", 0, node.DeviceID)
|
2024-03-28 18:11:56 +00:00
|
|
|
|
|
|
|
_, node = statAndSnapshot(t, repo, "testdir")
|
2024-03-28 23:24:03 +00:00
|
|
|
rtest.Assert(t, node.DeviceID == 0, "device id mismatch for testdir expected %v got %v", 0, node.DeviceID)
|
2024-03-28 18:11:56 +00:00
|
|
|
}
|