mirror of
https://github.com/restic/restic.git
synced 2025-08-12 19:07:40 +00:00
Add --ignore-inode option to backup cmd
revised version of https://github.com/restic/restic/pull/2047
This commit is contained in:
@@ -79,6 +79,7 @@ type Archiver struct {
|
||||
// be saved. Enabling it may result in much metadata, so it's off by
|
||||
// default.
|
||||
WithAtime bool
|
||||
IgnoreInode bool
|
||||
}
|
||||
|
||||
// Options is used to configure the archiver.
|
||||
@@ -133,6 +134,7 @@ func New(repo restic.Repository, fs fs.FS, opts Options) *Archiver {
|
||||
CompleteItem: func(string, *restic.Node, *restic.Node, ItemStats, time.Duration) {},
|
||||
StartFile: func(string) {},
|
||||
CompleteBlob: func(string, uint64) {},
|
||||
IgnoreInode: false,
|
||||
}
|
||||
|
||||
return arch
|
||||
@@ -383,7 +385,7 @@ func (arch *Archiver) Save(ctx context.Context, snPath, target string, previous
|
||||
}
|
||||
|
||||
// use previous node if the file hasn't changed
|
||||
if previous != nil && !fileChanged(fi, previous) {
|
||||
if previous != nil && !fileChanged(fi, previous, arch.IgnoreInode) {
|
||||
debug.Log("%v hasn't changed, returning old node", target)
|
||||
arch.CompleteItem(snPath, previous, previous, ItemStats{}, time.Since(start))
|
||||
arch.CompleteBlob(snPath, previous.Size)
|
||||
@@ -436,7 +438,7 @@ func (arch *Archiver) Save(ctx context.Context, snPath, target string, previous
|
||||
|
||||
// fileChanged returns true if the file's content has changed since the node
|
||||
// was created.
|
||||
func fileChanged(fi os.FileInfo, node *restic.Node) bool {
|
||||
func fileChanged(fi os.FileInfo, node *restic.Node, ignoreInode bool) bool {
|
||||
if node == nil {
|
||||
return true
|
||||
}
|
||||
@@ -458,7 +460,7 @@ func fileChanged(fi os.FileInfo, node *restic.Node) bool {
|
||||
}
|
||||
|
||||
// check inode
|
||||
if node.Inode != extFI.Inode {
|
||||
if !ignoreInode && node.Inode != extFI.Inode {
|
||||
return true
|
||||
}
|
||||
|
||||
|
@@ -555,9 +555,11 @@ func TestFileChanged(t *testing.T) {
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
Name string
|
||||
Content []byte
|
||||
Modify func(t testing.TB, filename string)
|
||||
Name string
|
||||
Content []byte
|
||||
Modify func(t testing.TB, filename string)
|
||||
IgnoreInode bool
|
||||
Check bool
|
||||
}{
|
||||
{
|
||||
Name: "same-content-new-file",
|
||||
@@ -596,6 +598,18 @@ func TestFileChanged(t *testing.T) {
|
||||
save(t, filename, defaultContent)
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "ignore-inode",
|
||||
Modify: func(t testing.TB, filename string) {
|
||||
fi := lstat(t, filename)
|
||||
remove(t, filename)
|
||||
sleep()
|
||||
save(t, filename, defaultContent)
|
||||
setTimestamp(t, filename, fi.ModTime(), fi.ModTime())
|
||||
},
|
||||
IgnoreInode: true,
|
||||
Check: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@@ -613,15 +627,19 @@ func TestFileChanged(t *testing.T) {
|
||||
fiBefore := lstat(t, filename)
|
||||
node := nodeFromFI(t, filename, fiBefore)
|
||||
|
||||
if fileChanged(fiBefore, node) {
|
||||
if fileChanged(fiBefore, node, false) {
|
||||
t.Fatalf("unchanged file detected as changed")
|
||||
}
|
||||
|
||||
test.Modify(t, filename)
|
||||
|
||||
fiAfter := lstat(t, filename)
|
||||
if !fileChanged(fiAfter, node) {
|
||||
t.Fatalf("modified file detected as unchanged")
|
||||
if test.Check == fileChanged(fiAfter, node, test.IgnoreInode) {
|
||||
if test.Check {
|
||||
t.Fatalf("unmodified file detected as changed")
|
||||
} else {
|
||||
t.Fatalf("modified file detected as unchanged")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -637,7 +655,7 @@ func TestFilChangedSpecialCases(t *testing.T) {
|
||||
|
||||
t.Run("nil-node", func(t *testing.T) {
|
||||
fi := lstat(t, filename)
|
||||
if !fileChanged(fi, nil) {
|
||||
if !fileChanged(fi, nil, false) {
|
||||
t.Fatal("nil node detected as unchanged")
|
||||
}
|
||||
})
|
||||
@@ -646,7 +664,7 @@ func TestFilChangedSpecialCases(t *testing.T) {
|
||||
fi := lstat(t, filename)
|
||||
node := nodeFromFI(t, filename, fi)
|
||||
node.Type = "symlink"
|
||||
if !fileChanged(fi, node) {
|
||||
if !fileChanged(fi, node, false) {
|
||||
t.Fatal("node with changed type detected as unchanged")
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user