File: inode_parents.go

package info (click to toggle)
golang-github-hanwen-go-fuse 2.1.0%2Bgit20220822.58a7e14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,292 kB
  • sloc: cpp: 78; sh: 43; makefile: 16
file content (101 lines) | stat: -rw-r--r-- 2,270 bytes parent folder | download | duplicates (3)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2021 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fs

// inodeParents stores zero or more parents of an Inode,
// remembering which one is the most recent.
//
// No internal locking: the caller is responsible for preventing
// concurrent access.
type inodeParents struct {
	// newest is the most-recently add()'ed parent.
	// nil when we don't have any parents.
	newest *parentData
	// other are parents in addition to the newest.
	// nil or empty when we have <= 1 parents.
	other map[parentData]struct{}
}

// add adds a parent to the store.
func (p *inodeParents) add(n parentData) {
	// one and only parent
	if p.newest == nil {
		p.newest = &n
	}
	// already known as `newest`
	if *p.newest == n {
		return
	}
	// old `newest` gets displaced into `other`
	if p.other == nil {
		p.other = make(map[parentData]struct{})
	}
	p.other[*p.newest] = struct{}{}
	// new parent becomes `newest` (possibly moving up from `other`)
	delete(p.other, n)
	p.newest = &n
}

// get returns the most recent parent
// or nil if there is no parent at all.
func (p *inodeParents) get() *parentData {
	return p.newest
}

// all returns all known parents
// or nil if there is no parent at all.
func (p *inodeParents) all() []parentData {
	count := p.count()
	if count == 0 {
		return nil
	}
	out := make([]parentData, 0, count)
	out = append(out, *p.newest)
	for i := range p.other {
		out = append(out, i)
	}
	return out
}

func (p *inodeParents) delete(n parentData) {
	// We have zero parents, so we can't delete any.
	if p.newest == nil {
		return
	}
	// If it's not the `newest` it must be in `other` (or nowhere).
	if *p.newest != n {
		delete(p.other, n)
		return
	}
	// We want to delete `newest`, but there is no other to replace it.
	if len(p.other) == 0 {
		p.newest = nil
		return
	}
	// Move random entry from `other` over `newest`.
	var i parentData
	for i = range p.other {
		p.newest = &i
		break
	}
	delete(p.other, i)
}

func (p *inodeParents) clear() {
	p.newest = nil
	p.other = nil
}

func (p *inodeParents) count() int {
	if p.newest == nil {
		return 0
	}
	return 1 + len(p.other)
}

type parentData struct {
	name   string
	parent *Inode
}