github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/internal/restic/hardlinks_index.go (about)

     1  package restic
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // HardlinkKey is a composed key for finding inodes on a specific device.
     8  type HardlinkKey struct {
     9  	Inode, Device uint64
    10  }
    11  
    12  // HardlinkIndex contains a list of inodes, devices these inodes are one, and associated file names.
    13  type HardlinkIndex struct {
    14  	m     sync.Mutex
    15  	Index map[HardlinkKey]string
    16  }
    17  
    18  // NewHardlinkIndex create a new index for hard links
    19  func NewHardlinkIndex() *HardlinkIndex {
    20  	return &HardlinkIndex{
    21  		Index: make(map[HardlinkKey]string),
    22  	}
    23  }
    24  
    25  // Has checks wether the link already exist in the index.
    26  func (idx *HardlinkIndex) Has(inode uint64, device uint64) bool {
    27  	idx.m.Lock()
    28  	defer idx.m.Unlock()
    29  	_, ok := idx.Index[HardlinkKey{inode, device}]
    30  
    31  	return ok
    32  }
    33  
    34  // Add adds a link to the index.
    35  func (idx *HardlinkIndex) Add(inode uint64, device uint64, name string) {
    36  	idx.m.Lock()
    37  	defer idx.m.Unlock()
    38  	_, ok := idx.Index[HardlinkKey{inode, device}]
    39  
    40  	if !ok {
    41  		idx.Index[HardlinkKey{inode, device}] = name
    42  	}
    43  }
    44  
    45  // GetFilename obtains the filename from the index.
    46  func (idx *HardlinkIndex) GetFilename(inode uint64, device uint64) string {
    47  	idx.m.Lock()
    48  	defer idx.m.Unlock()
    49  	return idx.Index[HardlinkKey{inode, device}]
    50  }
    51  
    52  // Remove removes a link from the index.
    53  func (idx *HardlinkIndex) Remove(inode uint64, device uint64) {
    54  	idx.m.Lock()
    55  	defer idx.m.Unlock()
    56  	delete(idx.Index, HardlinkKey{inode, device})
    57  }