1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-30 19:57:19 +00:00
restic/internal/restorer/hardlinks_index.go

58 lines
1.4 KiB
Go
Raw Normal View History

package restorer
2017-01-30 23:14:20 +00:00
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[T any] struct {
2017-01-30 23:14:20 +00:00
m sync.Mutex
Index map[HardlinkKey]T
2017-01-30 23:14:20 +00:00
}
// NewHardlinkIndex create a new index for hard links
func NewHardlinkIndex[T any]() *HardlinkIndex[T] {
return &HardlinkIndex[T]{
Index: make(map[HardlinkKey]T),
2017-01-30 23:14:20 +00:00
}
}
2023-12-06 12:11:55 +00:00
// Has checks whether the link already exist in the index.
func (idx *HardlinkIndex[T]) Has(inode uint64, device uint64) bool {
2017-01-30 23:14:20 +00:00
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[T]) Add(inode uint64, device uint64, value T) {
2017-01-30 23:14:20 +00:00
idx.m.Lock()
defer idx.m.Unlock()
_, ok := idx.Index[HardlinkKey{inode, device}]
if !ok {
idx.Index[HardlinkKey{inode, device}] = value
2017-01-30 23:14:20 +00:00
}
}
// Value obtains the filename from the index.
func (idx *HardlinkIndex[T]) Value(inode uint64, device uint64) T {
2017-01-30 23:14:20 +00:00
idx.m.Lock()
defer idx.m.Unlock()
return idx.Index[HardlinkKey{inode, device}]
}
// Remove removes a link from the index.
func (idx *HardlinkIndex[T]) Remove(inode uint64, device uint64) {
2017-01-30 23:14:20 +00:00
idx.m.Lock()
defer idx.m.Unlock()
delete(idx.Index, HardlinkKey{inode, device})
}