~kris/9p

llm9p

ref: a3dc06aa1a37febcbe7d8d340be3d9b5a2a3f6ea llm9p/internal/protocol/fs.go -rw-r--r-- 4.9 KiB
a3dc06aa — pdfinn feat(llm9p): Implement clone-based session architecture 7 months ago
                                                                                
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
package protocol

import (
	"io"
	"sync/atomic"
	"time"
)

// File is the interface that files must implement.
// This is the core abstraction for anything exposed via 9P.
type File interface {
	// Stat returns the file's metadata
	Stat() Stat

	// Open prepares the file for reading/writing
	Open(mode uint8) error

	// Read reads up to len(p) bytes starting at offset
	Read(p []byte, offset int64) (n int, err error)

	// Write writes len(p) bytes starting at offset
	Write(p []byte, offset int64) (n int, err error)

	// Close releases any resources
	Close() error
}

// Dir is the interface that directories must implement
type Dir interface {
	File

	// Children returns the directory's children
	Children() []File

	// Lookup finds a child by name
	Lookup(name string) (File, error)
}

// pathCounter generates unique path IDs for qids
var pathCounter uint64

func NextPath() uint64 {
	return atomic.AddUint64(&pathCounter, 1)
}

// BaseFile provides a default implementation of common File methods
type BaseFile struct {
	Name_   string
	Mode_   uint32
	Uid_    string
	Gid_    string
	Qid_    Qid
	Mtime_  time.Time
	Length_ uint64
}

// NewBaseFile creates a new base file
func NewBaseFile(name string, mode uint32) *BaseFile {
	now := time.Now()
	qtype := QTFILE
	if mode&DMDIR != 0 {
		qtype = QTDIR
	}
	return &BaseFile{
		Name_:  name,
		Mode_:  mode,
		Uid_:   "llm",
		Gid_:   "llm",
		Mtime_: now,
		Qid_: Qid{
			Type:    qtype,
			Version: 0,
			Path:    NextPath(),
		},
	}
}

func (f *BaseFile) Stat() Stat {
	return Stat{
		Type:   0,
		Dev:    0,
		Qid:    f.Qid_,
		Mode:   f.Mode_,
		Atime:  uint32(f.Mtime_.Unix()),
		Mtime:  uint32(f.Mtime_.Unix()),
		Length: f.Length_,
		Name:   f.Name_,
		Uid:    f.Uid_,
		Gid:    f.Gid_,
		Muid:   f.Uid_,
	}
}

func (f *BaseFile) Open(mode uint8) error                   { return nil }
func (f *BaseFile) Close() error                            { return nil }
func (f *BaseFile) Read(p []byte, offset int64) (int, error)  { return 0, io.EOF }
func (f *BaseFile) Write(p []byte, offset int64) (int, error) { return 0, ErrPermission }

// SetLength updates the file length
func (f *BaseFile) SetLength(n uint64) {
	f.Length_ = n
	f.Mtime_ = time.Now()
	f.Qid_.Version++
}

// StaticFile is a file with static content
type StaticFile struct {
	*BaseFile
	Content []byte
}

// NewStaticFile creates a file with static content
func NewStaticFile(name string, content []byte) *StaticFile {
	f := &StaticFile{
		BaseFile: NewBaseFile(name, 0444),
		Content:  content,
	}
	f.Length_ = uint64(len(content))
	return f
}

func (f *StaticFile) Read(p []byte, offset int64) (int, error) {
	if offset >= int64(len(f.Content)) {
		return 0, io.EOF
	}
	n := copy(p, f.Content[offset:])
	return n, nil
}

// StaticDir is a directory with static children
type StaticDir struct {
	*BaseFile
	children map[string]File
	order    []string // preserve order for listing
}

// NewStaticDir creates a new static directory
func NewStaticDir(name string) *StaticDir {
	return &StaticDir{
		BaseFile: NewBaseFile(name, DMDIR|0555),
		children: make(map[string]File),
		order:    make([]string, 0),
	}
}

// AddChild adds a child to the directory
func (d *StaticDir) AddChild(f File) {
	name := f.Stat().Name
	if _, exists := d.children[name]; !exists {
		d.order = append(d.order, name)
	}
	d.children[name] = f
}

func (d *StaticDir) Children() []File {
	result := make([]File, len(d.order))
	for i, name := range d.order {
		result[i] = d.children[name]
	}
	return result
}

func (d *StaticDir) Lookup(name string) (File, error) {
	if f, ok := d.children[name]; ok {
		return f, nil
	}
	return nil, ErrNotFound
}

func (d *StaticDir) Read(p []byte, offset int64) (int, error) {
	// Directory read returns packed stat entries
	var buf []byte
	for _, f := range d.Children() {
		stat := f.Stat()
		entry := make([]byte, 256)
		n := stat.Encode(entry)
		buf = append(buf, entry[:n]...)
	}

	if offset >= int64(len(buf)) {
		return 0, io.EOF
	}

	n := copy(p, buf[offset:])
	return n, nil
}

// DynamicFile is a file whose content is generated on read
type DynamicFile struct {
	*BaseFile
	Generator func() []byte
}

// NewDynamicFile creates a file with dynamic content
func NewDynamicFile(name string, generator func() []byte) *DynamicFile {
	return &DynamicFile{
		BaseFile:  NewBaseFile(name, 0444),
		Generator: generator,
	}
}

func (f *DynamicFile) Read(p []byte, offset int64) (int, error) {
	content := f.Generator()
	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	n := copy(p, content[offset:])
	return n, nil
}

func (f *DynamicFile) Stat() Stat {
	s := f.BaseFile.Stat()
	s.Length = uint64(len(f.Generator()))
	return s
}

// Errors
type Error string

func (e Error) Error() string { return string(e) }

const (
	ErrNotFound   Error = "file not found"
	ErrPermission Error = "permission denied"
	ErrNotDir     Error = "not a directory"
	ErrIsDir      Error = "is a directory"
	ErrBadFid     Error = "bad fid"
	ErrFidInUse   Error = "fid already in use"
	ErrBadOffset  Error = "bad offset"
)