Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #763 from jgfrm/issue25
Support hard links
- Loading branch information
Showing
12 changed files
with
281 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package restic | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// HardlinkKey is a composed key for finding inodes on a specific device. | ||
type HardlinkKey struct { | ||
Inode, Device uint64 | ||
} | ||
|
||
// HardlinkIndex contains a list of inodes, devices these inodes are one, and associated file names. | ||
type HardlinkIndex struct { | ||
m sync.Mutex | ||
Index map[HardlinkKey]string | ||
} | ||
|
||
// NewHardlinkIndex create a new index for hard links | ||
func NewHardlinkIndex() *HardlinkIndex { | ||
return &HardlinkIndex{ | ||
Index: make(map[HardlinkKey]string), | ||
} | ||
} | ||
|
||
// Has checks wether the link already exist in the index. | ||
func (idx *HardlinkIndex) Has(inode uint64, device uint64) bool { | ||
idx.m.Lock() | ||
defer idx.m.Unlock() | ||
_, ok := idx.Index[HardlinkKey{inode, device}] | ||
|
||
return ok | ||
} | ||
|
||
// Add adds a link to the index. | ||
func (idx *HardlinkIndex) Add(inode uint64, device uint64, name string) { | ||
idx.m.Lock() | ||
defer idx.m.Unlock() | ||
_, ok := idx.Index[HardlinkKey{inode, device}] | ||
|
||
if !ok { | ||
idx.Index[HardlinkKey{inode, device}] = name | ||
} | ||
} | ||
|
||
// GetFilename obtains the filename from the index. | ||
func (idx *HardlinkIndex) GetFilename(inode uint64, device uint64) string { | ||
idx.m.Lock() | ||
defer idx.m.Unlock() | ||
return idx.Index[HardlinkKey{inode, device}] | ||
} | ||
|
||
// Remove removes a link from the index. | ||
func (idx *HardlinkIndex) Remove(inode uint64, device uint64) { | ||
idx.m.Lock() | ||
defer idx.m.Unlock() | ||
delete(idx.Index, HardlinkKey{inode, device}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package restic_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"restic" | ||
. "restic/test" | ||
) | ||
|
||
// TestHardLinks contains various tests for HardlinkIndex. | ||
func TestHardLinks(t *testing.T) { | ||
|
||
idx := restic.NewHardlinkIndex() | ||
|
||
idx.Add(1, 2, "inode1-file1-on-device2") | ||
idx.Add(2, 3, "inode2-file2-on-device3") | ||
|
||
var sresult string | ||
sresult = idx.GetFilename(1, 2) | ||
Equals(t, sresult, "inode1-file1-on-device2") | ||
|
||
sresult = idx.GetFilename(2, 3) | ||
Equals(t, sresult, "inode2-file2-on-device3") | ||
|
||
var bresult bool | ||
bresult = idx.Has(1, 2) | ||
Equals(t, bresult, true) | ||
|
||
bresult = idx.Has(1, 3) | ||
Equals(t, bresult, false) | ||
|
||
idx.Remove(1, 2) | ||
bresult = idx.Has(1, 2) | ||
Equals(t, bresult, false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.