-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuse_adapter.go
More file actions
330 lines (281 loc) · 8.24 KB
/
fuse_adapter.go
File metadata and controls
330 lines (281 loc) · 8.24 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//go:build linux || darwin
// +build linux darwin
package toolfs
import (
"context"
"strings"
"syscall"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
)
// ToolFSRoot is the root node for the ToolFS FUSE filesystem
type ToolFSRoot struct {
fs.Inode
toolfs *ToolFS
}
// Ensure ToolFSRoot implements the NodeOnAdder interface
var _ fs.NodeOnAdder = (*ToolFSRoot)(nil)
// NewToolFSRoot creates a new root node for FUSE filesystem
func NewToolFSRoot(toolfs *ToolFS) *ToolFSRoot {
return &ToolFSRoot{
toolfs: toolfs,
}
}
// OnAdd is called when the filesystem is mounted
func (r *ToolFSRoot) OnAdd(ctx context.Context) {
// Add memory directory
memNode := &ToolFSDir{
toolfs: r.toolfs,
path: r.toolfs.rootPath + "/memory",
}
memInode := r.NewPersistentInode(ctx, memNode, fs.StableAttr{
Mode: syscall.S_IFDIR | 0o755,
})
r.AddChild("memory", memInode, false)
// Add RAG directory
ragNode := &ToolFSDir{
toolfs: r.toolfs,
path: r.toolfs.rootPath + "/rag",
}
ragInode := r.NewPersistentInode(ctx, ragNode, fs.StableAttr{
Mode: syscall.S_IFDIR | 0o755,
})
r.AddChild("rag", ragInode, false)
// Add mounted directories
for mountPoint := range r.toolfs.mounts {
relPath := r.toolfs.normalizeMountPoint(mountPoint)
if relPath != "" {
mountNode := &ToolFSDir{
toolfs: r.toolfs,
path: mountPoint,
}
mountInode := r.NewPersistentInode(ctx, mountNode, fs.StableAttr{
Mode: syscall.S_IFDIR | 0o755,
})
r.AddChild(relPath, mountInode, false)
}
}
// Add skill mounts
for mountPoint := range r.toolfs.skillMounts {
relPath := r.toolfs.normalizeMountPoint(mountPoint)
if relPath != "" {
skillNode := &ToolFSDir{
toolfs: r.toolfs,
path: mountPoint,
}
skillInode := r.NewPersistentInode(ctx, skillNode, fs.StableAttr{
Mode: syscall.S_IFDIR | 0o755,
})
r.AddChild(relPath, skillInode, false)
}
}
}
// normalizeMountPoint extracts the relative path from a full mount point
func (fs *ToolFS) normalizeMountPoint(mountPoint string) string {
mountPoint = normalizeVirtualPath(mountPoint)
root := normalizeVirtualPath(fs.rootPath)
if mountPoint == root {
return ""
}
if strings.HasPrefix(mountPoint, root+"/") {
rel := strings.TrimPrefix(mountPoint, root+"/")
if idx := strings.Index(rel, "/"); idx != -1 {
return rel[:idx]
}
return rel
}
// Extract last component
parts := strings.Split(strings.TrimPrefix(mountPoint, root), "/")
if len(parts) > 0 && parts[0] != "" {
return parts[0]
}
return ""
}
// ToolFSDir represents a directory in the ToolFS FUSE filesystem
type ToolFSDir struct {
fs.Inode
toolfs *ToolFS
path string
}
// Ensure ToolFSDir implements the required interfaces
var (
_ fs.NodeReaddirer = (*ToolFSDir)(nil)
_ fs.NodeLookuper = (*ToolFSDir)(nil)
)
// Readdir implements NodeReaddirer interface
func (d *ToolFSDir) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
entries, err := d.toolfs.ListDir(d.path)
if err != nil {
return nil, syscall.EIO
}
var dirEntries []fuse.DirEntry
for _, name := range entries {
// Determine if it's a directory
isDir := strings.HasSuffix(name, "/")
if isDir {
name = strings.TrimSuffix(name, "/")
}
mode := uint32(syscall.S_IFREG | 0o644)
if isDir {
mode = syscall.S_IFDIR | 0o755
}
dirEntries = append(dirEntries, fuse.DirEntry{
Name: name,
Mode: mode,
Ino: 0, // Inode number (0 for virtual filesystem)
})
}
return fs.NewListDirStream(dirEntries), 0
}
// Lookup implements NodeLookuper interface
func (d *ToolFSDir) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
childPath := d.path + "/" + name
// Check if it's a file or directory
info, err := d.toolfs.Stat(childPath)
if err != nil {
return nil, syscall.ENOENT
}
if info.IsDir {
childNode := &ToolFSDir{
toolfs: d.toolfs,
path: childPath,
}
childInode := d.NewPersistentInode(ctx, childNode, fs.StableAttr{
Mode: syscall.S_IFDIR | 0o755,
})
return childInode, 0
} else {
childNode := &ToolFSFile{
toolfs: d.toolfs,
path: childPath,
}
childInode := d.NewPersistentInode(ctx, childNode, fs.StableAttr{
Mode: syscall.S_IFREG | 0o644,
})
return childInode, 0
}
}
// ToolFSFile represents a file in the ToolFS FUSE filesystem
type ToolFSFile struct {
fs.Inode
toolfs *ToolFS
path string
}
// Ensure ToolFSFile implements the required interfaces
var (
_ fs.NodeOpener = (*ToolFSFile)(nil)
_ fs.NodeGetattrer = (*ToolFSFile)(nil)
)
// Open implements NodeOpener interface
func (f *ToolFSFile) Open(ctx context.Context, flags uint32) (fs.FileHandle, uint32, syscall.Errno) {
return &ToolFSFileHandle{
toolfs: f.toolfs,
path: f.path,
}, fuse.FOPEN_KEEP_CACHE, 0
}
// Getattr implements NodeGetattrer interface
func (f *ToolFSFile) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
info, err := f.toolfs.Stat(f.path)
if err != nil {
return syscall.ENOENT
}
out.Size = uint64(info.Size)
out.Mtime = uint64(info.ModTime.Unix())
out.Mode = syscall.S_IFREG | 0o644
return 0
}
// ToolFSFileHandle is a file handle for ToolFS files
type ToolFSFileHandle struct {
toolfs *ToolFS
path string
}
// Ensure ToolFSFileHandle implements the required interfaces
var (
_ fs.FileReader = (*ToolFSFileHandle)(nil)
_ fs.FileWriter = (*ToolFSFileHandle)(nil)
)
// Read implements FileReader interface
func (fh *ToolFSFileHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
data, err := fh.toolfs.ReadFile(fh.path)
if err != nil {
return nil, syscall.EIO
}
if off >= int64(len(data)) {
return fuse.ReadResultData(nil), 0
}
end := int(off) + len(dest)
if end > len(data) {
end = len(data)
}
// Copy data to a new slice to ensure it's not invalidated
resultLen := end - int(off)
if resultLen <= 0 {
return fuse.ReadResultData(nil), 0
}
result := make([]byte, resultLen)
copy(result, data[off:end])
return fuse.ReadResultData(result), 0
}
// Write implements FileWriter interface
func (fh *ToolFSFileHandle) Write(ctx context.Context, data []byte, off int64) (uint32, syscall.Errno) {
// Read existing file
existing, err := fh.toolfs.ReadFile(fh.path)
if err != nil {
existing = []byte{}
}
// Resize if needed
newSize := int(off) + len(data)
if newSize > len(existing) {
temp := make([]byte, newSize)
copy(temp, existing)
existing = temp
}
// Write new data
copy(existing[off:], data)
// Write back
if err := fh.toolfs.WriteFile(fh.path, existing); err != nil {
return 0, syscall.EIO
}
return uint32(len(data)), 0
}
// MountToolFS mounts a ToolFS instance as a FUSE filesystem at the specified mount point
// This allows accessing ToolFS through standard filesystem operations like cat, ls, etc.
//
// Example:
//
// fs := NewToolFS("/toolfs")
// err := MountToolFS(fs, "/mnt/toolfs")
// if err != nil {
// log.Fatal(err)
// }
// // Now you can: cat /mnt/toolfs/memory/entry1
func MountToolFS(toolfs *ToolFS, mountPoint string, options *fuse.MountOptions) error {
opts := &fs.Options{}
if options != nil {
opts.MountOptions = *options
} else {
opts.MountOptions = fuse.MountOptions{
Options: []string{"default_permissions"},
}
}
root := NewToolFSRoot(toolfs)
server, err := fs.Mount(mountPoint, root, opts)
if err != nil {
return err
}
// Note: fs.Mount() automatically starts the serving loop in the background.
// We should NOT call server.Serve() again, as it will panic with "Serve() must only be called once".
// The server is already serving requests, so we can just return.
// We don't call WaitMount() here to avoid blocking.
// WaitMount() waits for the first request, which may never come if the mount point
// isn't accessed immediately. The mount should still work without it.
// If you need to ensure the mount is ready, access the mount point after calling MountToolFS.
_ = server // Keep reference to server
return nil
}
// MountToolFSWithSession mounts ToolFS with a specific session for access control
func MountToolFSWithSession(toolfs *ToolFS, mountPoint string, session *Session, options *fuse.MountOptions) error {
// Note: Session-based access control would need to be integrated into the FUSE handlers
// For now, this is a placeholder for future implementation
return MountToolFS(toolfs, mountPoint, options)
}