summaryrefslogtreecommitdiff
path: root/internal/inomap/inomap.go
blob: f8909c71aea32219bb588610c912dee0abcd4b25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package inomap

import (
	"sync"
	"syscall"
)

// UINT64_MAX           = 18446744073709551615
const inumTranslateBase = 10000000000000000000

// InoMap ... see New() for description.
type InoMap struct {
	sync.Mutex
	baseDev       uint64
	translate     map[QIno]uint64
	translateNext uint64
}

// New returns a new InoMap.
//
// InoMap translates (device uint64, inode uint64) pairs to unique uint64
// inode numbers.
// Inode numbers on the "baseDev" are passed through unchanged (as long as they
// are not higher than inumTranslateBase).
// Inode numbers on other devices are remapped to the number space above
// 10000000000000000000. The mapping is stored in a simple Go map. Entries
// can only be added and are never removed.
func New(baseDev uint64) *InoMap {
	return &InoMap{
		baseDev:       baseDev,
		translate:     make(map[QIno]uint64),
		translateNext: inumTranslateBase,
	}
}

// Translate maps the passed-in (device, inode) pair to a unique inode number.
func (m *InoMap) Translate(in QIno) (out uint64) {
	if in.Dev == m.baseDev && in.Ino < inumTranslateBase {
		return in.Ino
	}
	m.Lock()
	defer m.Unlock()
	out = m.translate[in]
	if out != 0 {
		return out
	}
	out = m.translateNext
	m.translate[in] = m.translateNext
	m.translateNext++
	return out
}

// TranslateStat translates the inode number contained in "st" if neccessary.
// Convience wrapper around Translate().
func (m *InoMap) TranslateStat(st *syscall.Stat_t) {
	in := QInoFromStat(st)
	st.Ino = m.Translate(in)
}

// Count returns the number of entries in the translation table.
func (m *InoMap) Count() int {
	return len(m.translate)
}