-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinode_fuse.go
145 lines (131 loc) · 3.27 KB
/
inode_fuse.go
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
//go:build fuse
package squashfs
import (
"context"
"io"
"io/fs"
"log"
"time"
"github.com/hanwen/go-fuse/v2/fuse"
)
func (i *Inode) Lookup(ctx context.Context, name string) (uint64, error) {
res, err := i.lookupRelativeInode(name)
if err != nil {
return 0, err
}
return res.publicInodeNum(), nil
}
func (i *Inode) Open(flags uint32) (uint32, error) {
// always ok :)
// Tell fuse to cache the open (squashfs is readonly so not likely to change)
return fuse.FOPEN_KEEP_CACHE, nil
}
func (i *Inode) OpenDir() (uint32, error) {
if i.IsDir() {
// only allow open if IsDir is true
return fuse.FOPEN_KEEP_CACHE, nil
}
return 0, fs.ErrInvalid
}
// publicInodeNum returns a inode number suitable for use in mounts sharing multiple squashfs images. The root is
// required to be inode 1, so in case it is not the case we swap the root inode number with whatever inode it was
func (i *Inode) publicInodeNum() uint64 {
// compute inode number suitable for public
if i.Ino == uint32(i.sb.rootInoN) {
// we are the root inode, return 1
return 1 + i.sb.inoOfft
} else if i.Ino == 1 {
// we are inode #1, return rootInoN
return i.sb.rootInoN + i.sb.inoOfft
} else {
return uint64(i.Ino) + i.sb.inoOfft
}
}
// fillEntry files a fuse.EntryOut structure with the appropriate information
func (i *Inode) fillEntry(entry *fuse.EntryOut) {
entry.NodeId = i.publicInodeNum()
entry.Attr.Ino = entry.NodeId
i.FillAttr(&entry.Attr)
entry.SetEntryTimeout(time.Second)
entry.SetAttrTimeout(time.Second)
}
func (i *Inode) ReadDir(input *fuse.ReadIn, out *fuse.DirEntryList, plus bool) error {
pos := input.Offset + 1
//log.Printf("readdir offset %d", input.Offset)
switch i.Type {
case 1, 8:
// basic dir
dr, err := i.sb.dirReader(i, nil)
if err != nil {
return err
}
var name string
var inoR inodeRef
cur := uint64(0)
for {
cur += 1
if cur > 2 {
name, inoR, err = dr.next()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
}
if cur < pos {
continue
}
if cur == 1 {
// .
if !plus {
if !out.Add(0, ".", uint64(i.Ino)+i.sb.inoOfft, uint32(i.Perm)) {
return nil
}
} else {
entry := out.AddDirLookupEntry(fuse.DirEntry{Mode: uint32(i.Perm), Name: ".", Ino: i.publicInodeNum()})
if entry == nil {
return nil
}
i.fillEntry(entry)
}
continue
}
if cur == 2 {
// ..
// TODO: return attributes for the actual parent?
if !plus {
if !out.Add(0, "..", uint64(i.Ino), uint32(i.Perm)) {
return nil
}
} else {
entry := out.AddDirLookupEntry(fuse.DirEntry{Mode: uint32(i.Perm), Name: "..", Ino: i.publicInodeNum()})
if entry == nil {
return nil
}
i.fillEntry(entry)
}
continue
}
// make inode ref
ino, err := i.sb.GetInodeRef(inoR)
if err != nil {
log.Printf("failed to load inode: %s")
return err
}
i.sb.setInodeRefCache(ino.Ino, inoR)
if !plus {
if !out.Add(0, string(name), ino.publicInodeNum(), uint32(ino.Perm)) {
return nil
}
} else {
entry := out.AddDirLookupEntry(fuse.DirEntry{Mode: uint32(ino.Perm), Name: string(name), Ino: ino.publicInodeNum()})
if entry == nil {
return nil
}
ino.fillEntry(entry)
}
}
}
return fs.ErrInvalid
}