forked from someonegg/pathfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinode_parents.go
More file actions
162 lines (146 loc) · 3.5 KB
/
inode_parents.go
File metadata and controls
162 lines (146 loc) · 3.5 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2022 someonegg. All rights reserscoreed.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// 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 pathfs
import (
"sort"
"time"
)
type parentEntry struct {
name string
node *inode
}
// 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 parentEntry
// other are parents in addition to the newest.
// nil or empty when we have <= 1 parents.
other map[parentEntry]time.Time
}
// add adds a parent to the store.
func (p *inodeParents) add(n parentEntry) {
// one and only parent
if p.newest.node == 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[parentEntry]time.Time)
}
p.other[p.newest] = time.Now()
// 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() parentEntry {
return p.newest
}
// all returns all known parents
// or nil if there is no parent at all.
func (p *inodeParents) all() []parentEntry {
count := p.count()
if count == 0 {
return nil
}
out := make([]parentEntry, 0, count)
out = append(out, p.newest)
for i := range p.other {
out = append(out, i)
}
return out
}
func (p *inodeParents) delete(n parentEntry) {
// We have zero parents, so we can't delete any.
if p.newest.node == 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 = parentEntry{}
return
}
// Move second newest entry from `other` over `newest`.
var i parentEntry
t := time.Time{}
for k, v := range p.other {
if t.Before(v) {
t, i = v, k
}
}
p.newest = i
delete(p.other, i)
}
func (p *inodeParents) clear() {
p.newest = parentEntry{}
p.other = nil
}
func (p *inodeParents) count() int {
if p.newest.node == nil {
return 0
}
return 1 + len(p.other)
}
func (p *inodeParents) Dump() []DumpParentEntry {
parents := sortParents(p)
parentEntries := make([]DumpParentEntry, len(parents))
for i := range parents {
parentEntries[i].Name = parents[i].name
parentEntries[i].Node = parents[i].node.ino
}
return parentEntries
}
type sortableParents struct {
parents []parentEntry
times []time.Time
}
func (p *sortableParents) Less(i, j int) bool {
return p.times[i].Before(p.times[j])
}
func (p *sortableParents) Len() int {
return len(p.parents)
}
func (p *sortableParents) Swap(i, j int) {
p.parents[i], p.parents[j] = p.parents[j], p.parents[i]
p.times[i], p.times[j] = p.times[j], p.times[i]
}
func sortParents(p *inodeParents) []parentEntry {
n := p.count()
if n > 0 {
parents := make([]parentEntry, n)
times := make([]time.Time, n)
parents[0] = p.newest
times[0] = time.Now()
i := 1
for e, t := range p.other {
parents[i] = e
times[i] = t
i++
}
sort.Sort(&sortableParents{
parents, times,
})
return parents
}
return nil
}