1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2025-03-16 00:00:05 +01:00

shortened function names

add file to link index only if the file is successfully restored
changed comments
This commit is contained in:
jgfrm 2017-02-01 17:22:34 +01:00
parent 369f980411
commit 3f53af615b
3 changed files with 28 additions and 33 deletions

View file

@ -4,12 +4,12 @@ import (
"sync"
)
// HardlinkKey is a composed key for finding inodes on a specific device
// 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
// 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
@ -22,8 +22,8 @@ func NewHardlinkIndex() *HardlinkIndex {
}
}
// ExistsLink checks wether the link already exist in the index
func (idx *HardlinkIndex) ExistsLink(inode uint64, device uint64) bool {
// 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}]
@ -31,8 +31,8 @@ func (idx *HardlinkIndex) ExistsLink(inode uint64, device uint64) bool {
return ok
}
// AddLink adds a link to the index
func (idx *HardlinkIndex) AddLink(inode uint64, device uint64, name string) {
// 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}]
@ -42,15 +42,15 @@ func (idx *HardlinkIndex) AddLink(inode uint64, device uint64, name string) {
}
}
// GetLinkName obtains the filename from the index
func (idx *HardlinkIndex) GetLinkName(inode uint64, device uint64) string {
// 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}]
}
// RemoveLink removes a link from the index
func (idx *HardlinkIndex) RemoveLink(inode uint64, device uint64) {
// 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})

View file

@ -7,34 +7,29 @@ import (
. "restic/test"
)
// TestHardLinks contains various tests for the HardlinkIndex
// TestHardLinks contains various tests for the HardlinkIndex.
func TestHardLinks(t *testing.T) {
idx := restic.NewHardlinkIndex()
idx.AddLink(1, 2, "inode1-file1-on-device2")
idx.AddLink(2, 3, "inode2-file2-on-device3")
idx.Add(1, 2, "inode1-file1-on-device2")
idx.Add(2, 3, "inode2-file2-on-device3")
var sresult string
sresult = idx.GetLinkName(1, 2)
Assert(t, sresult == "inode1-file1-on-device2",
"Name doesn't match (%v != %v)", sresult, "inode1-file1-on-device2")
sresult = idx.GetFilename(1, 2)
Equals(t, sresult, "inode1-file1-on-device2")
sresult = idx.GetLinkName(2, 3)
Assert(t, sresult == "inode2-file2-on-device3",
"Name doesn't match (%v != %v)", sresult, "inode2-file2-on-device3")
sresult = idx.GetFilename(2, 3)
Equals(t, sresult, "inode2-file2-on-device3")
var bresult bool
bresult = idx.ExistsLink(1, 2)
Assert(t, bresult == true,
"Existence does not match (%v != %v)", bresult, true)
bresult = idx.Has(1, 2)
Equals(t, bresult, true)
bresult = idx.ExistsLink(1, 3)
Assert(t, bresult == false,
"Existence does not match (%v != %v)", bresult, false)
bresult = idx.Has(1, 3)
Equals(t, bresult, false)
idx.RemoveLink(1, 2)
bresult = idx.ExistsLink(1, 2)
Assert(t, bresult == false,
"Existence does not match (%v != %v)", bresult, false)
idx.Remove(1, 2)
bresult = idx.Has(1, 2)
Equals(t, bresult, false)
}

View file

@ -193,10 +193,8 @@ func (node Node) createDirAt(path string) error {
func (node Node) createFileAt(path string, repo Repository, idx *HardlinkIndex) error {
if node.Links > 1 {
if !idx.ExistsLink(node.Inode, node.Device) {
idx.AddLink(node.Inode, node.Device, path)
} else {
err := fs.Link(idx.GetLinkName(node.Inode, node.Device), path)
if idx.Has(node.Inode, node.Device) {
err := fs.Link(idx.GetFilename(node.Inode, node.Device), path)
if err != nil {
return errors.Wrap(err, "CreateHardlink")
}
@ -235,6 +233,8 @@ func (node Node) createFileAt(path string, repo Repository, idx *HardlinkIndex)
}
}
idx.Add(node.Inode, node.Device, path)
return nil
}