-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
373 lines (326 loc) · 10.3 KB
/
Copy pathfile.go
File metadata and controls
373 lines (326 loc) · 10.3 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package github
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
)
// File implements afero.File and represents a file or directory in the GitHub repository.
// For regular files, it maintains separate read (bytes.Reader) and write (bytes.Buffer) state
// to support efficient operations while avoiding GitHub's blob-replacement semantics.
// For directories, it holds preloaded entries and an iteration offset.
type File struct {
fs *Fs // Reference to the file system this file belongs to.
path string
name string // base name only
// read state
reader *bytes.Reader
// write state
buf *bytes.Buffer
dirty bool
// metadata
flag int
isDir bool
// directory iteration state
dirEntries []os.FileInfo
dirOffset int
closed bool
}
// NewFile creates a new File instance with the provided Fs, path, and name.
// The file is not opened for reading or writing until Read or Write is called.
func NewFile(fs *Fs, path, name string) *File {
return &File{
fs: fs,
path: path,
name: name,
}
}
// Name returns the base name of the file.
func (f *File) Name() string { return f.name }
// Readdir reads the next count directory entries and returns them as a slice of os.FileInfo.
// It maintains an internal offset to support multiple calls.
// If count <= 0, it returns all remaining entries.
// Returns io.EOF when all entries have been read.
// Returns an error if the file is not a directory or is closed.
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
if f.closed {
return nil, os.ErrClosed
}
if !f.isDir {
return nil, &os.PathError{Op: "readdir", Path: f.path, Err: errors.New("not a directory")}
}
remaining := f.dirEntries[f.dirOffset:]
if count <= 0 {
f.dirOffset = len(f.dirEntries)
return remaining, nil
}
if len(remaining) == 0 {
return nil, io.EOF
}
if count > len(remaining) {
count = len(remaining)
}
entries := remaining[:count]
f.dirOffset += count
return entries, nil
}
// ReaddirAll returns all directory entries and resets the internal offset for subsequent reads.
// Returns an error if the file is not a directory or is closed.
func (f *File) ReaddirAll() ([]os.FileInfo, error) {
if f.closed {
return nil, os.ErrClosed
}
if !f.isDir {
return nil, &os.PathError{
Op: "readdirall",
Path: f.path,
Err: errors.New("not a directory"),
}
}
// Reset cursor so we always return the full listing.
f.dirOffset = 0
return f.Readdir(-1)
}
// Readdirnames returns a slice of the names of the next count directory entries.
// Returns an error if the file is not a directory or is closed.
func (f *File) Readdirnames(count int) ([]string, error) {
infos, err := f.Readdir(count)
if err != nil {
return nil, err
}
names := make([]string, len(infos))
for i, info := range infos {
names[i] = info.Name()
}
return names, nil
}
// Stat returns a FileInfo describing the file.
// It returns the current size from either the reader (if readable) or buffer (if writable).
// Returns an error if the file is closed.
func (f *File) Stat() (os.FileInfo, error) {
if f.closed {
return nil, os.ErrClosed
}
return &GitHubFileInfo{
name: f.name,
size: f.currentSize(),
isDir: f.isDir,
}, nil
}
// Sync flushes any pending writes to GitHub and clears the dirty flag.
// Returns an error if the file is closed or the flush operation fails.
func (f *File) Sync() error {
if f.closed {
return os.ErrClosed
}
if f.dirty {
return f.flush()
}
return nil
}
// Truncate changes the file size to the given size.
// If size is greater than the current size, the file is extended with zero bytes.
// If size is less than the current size, the file is truncated.
// Returns an error if the file is closed or not writable.
func (f *File) Truncate(size int64) error {
if f.closed {
return os.ErrClosed
}
if !f.writable() {
return &os.PathError{Op: "truncate", Path: f.path, Err: fs.ErrPermission}
}
data := f.buf.Bytes()
if int64(len(data)) >= size {
data = data[:size]
} else {
data = append(data, make([]byte, size-int64(len(data)))...)
}
f.buf.Reset()
f.buf.Write(data)
f.dirty = true
return nil
}
// WriteString writes the string s to the file and returns the number of bytes written.
// It marks the file as dirty. Returns an error if the file is closed, is a directory, or not writable.
func (f *File) WriteString(s string) (n int, err error) {
return f.Write([]byte(s))
}
// Close closes the file. If the file has been modified (dirty), it flushes the changes to GitHub first.
// Returns an error if the file is already closed or if the flush operation fails.
func (f *File) Close() error {
if f.closed {
return os.ErrClosed
}
f.closed = true
if f.dirty {
if err := f.flush(); err != nil {
return &os.PathError{Op: "close", Path: f.path, Err: err}
}
}
return nil
}
// Read reads up to len(p) bytes from the file and stores them in p.
// It returns the number of bytes read and any error encountered.
// Returns an error if the file is closed, is a directory, or not readable.
func (f *File) Read(p []byte) (n int, err error) {
if f.closed {
return 0, os.ErrClosed
}
if f.isDir {
return 0, &os.PathError{Op: "read", Path: f.path, Err: errors.New("is a directory")}
}
if !f.readable() {
return 0, &os.PathError{Op: "read", Path: f.path, Err: fs.ErrPermission}
}
r := f.getReader()
return r.Read(p)
}
// ReadAt reads up to len(p) bytes from the file starting at byte offset off
// and stores them in p. It returns the number of bytes read and any error encountered.
// Returns an error if the file is closed or not readable.
func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
if f.closed {
return 0, os.ErrClosed
}
if !f.readable() {
return 0, &os.PathError{Op: "readat", Path: f.path, Err: fs.ErrPermission}
}
r := f.getReader()
return r.ReadAt(p, off)
}
// Seek changes the read position to the specified offset relative to whence
// (io.SeekStart, io.SeekCurrent, or io.SeekEnd).
// It returns the new absolute offset and any error encountered.
// Returns an error if the file is closed, is a directory, or whence is invalid.
func (f *File) Seek(offset int64, whence int) (int64, error) {
if f.closed {
return 0, os.ErrClosed
}
if f.isDir {
return 0, &os.PathError{Op: "seek", Path: f.path, Err: errors.New("is a directory")}
}
// For write-only mode, seek within the write buffer.
if !f.readable() {
size := int64(f.buf.Len())
var abs int64
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
// We don't track a write cursor; use buf length as proxy.
abs = size + offset
case io.SeekEnd:
abs = size + offset
default:
return 0, errors.New("invalid whence")
}
if abs < 0 {
return 0, errors.New("negative position")
}
return abs, nil
}
return f.getReader().Seek(offset, whence)
}
// Write writes len(p) bytes from p to the file and returns the number of bytes written.
// It marks the file as dirty. Returns an error if the file is closed, is a directory, or not writable.
func (f *File) Write(p []byte) (int, error) {
if f.closed {
return 0, os.ErrClosed
}
if f.isDir {
return 0, &os.PathError{Op: "write", Path: f.path, Err: errors.New("is a directory")}
}
if !f.writable() {
return 0, &os.PathError{Op: "write", Path: f.path, Err: fs.ErrPermission}
}
n, err := f.buf.Write(p)
if n > 0 {
f.dirty = true
}
return n, err
}
// WriteAt writes len(p) bytes from p to the file starting at byte offset off.
// It expands the buffer if necessary and returns the number of bytes written.
// It marks the file as dirty. Returns an error if the file is closed or not writable.
func (f *File) WriteAt(p []byte, off int64) (int, error) {
if f.closed {
return 0, os.ErrClosed
}
if !f.writable() {
return 0, &os.PathError{Op: "writeat", Path: f.path, Err: fs.ErrPermission}
}
// Expand buffer to at least off+len(p).
data := f.buf.Bytes()
needed := int(off) + len(p)
if needed > len(data) {
extra := make([]byte, needed-len(data))
data = append(data, extra...)
}
copy(data[off:], p)
f.buf.Reset()
f.buf.Write(data)
f.dirty = true
return len(p), nil
}
// RangeReader returns an io.ReadCloser for reading a specific byte range from the file.
// It first tries to serve the range from the in-memory reader if available and sufficient.
// Otherwise, it issues an HTTP Range request to raw.githubusercontent.com for efficient partial downloads.
// The returned reader's context is cancelled when the reader is closed.
// Returns an error if the file is closed, is a directory, not readable, or range parameters are invalid.
func (f *File) RangeReader(offset, length int64) (io.ReadCloser, error) {
if f.closed {
return nil, os.ErrClosed
}
if f.isDir {
return nil, &os.PathError{Op: "rangeread", Path: f.path, Err: errors.New("is a directory")}
}
if offset < 0 || length <= 0 {
return nil, &os.PathError{Op: "rangeread", Path: f.path, Err: errors.New("invalid range")}
}
// If the requested range is within the current reader, serve it directly without an HTTP request.
// This optimizes for recently read content that may still be in memory.
if f.reader != nil {
size := f.reader.Size()
if offset+length <= size {
buf := make([]byte, length)
n, err := f.reader.ReadAt(buf, offset)
if err != nil && !errors.Is(err, io.EOF) {
return nil, err
}
return io.NopCloser(bytes.NewReader(buf[:n])), nil
}
}
// Otherwise, make an HTTP Range request to fetch the specified byte range from GitHub.
rawURL, err := f.rawContentURL()
if err != nil {
return nil, err
}
ctx, cancel := f.fs.newContext()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
if err != nil {
cancel()
return nil, &os.PathError{Op: "rangeread", Path: f.path, Err: err}
}
// Set the Range header to request the specific byte range. The end byte is inclusive, so we subtract 1 from the length.
end := offset + length - 1
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", offset, end))
resp, err := f.fs.httpClient().Do(req)
if err != nil {
cancel()
return nil, &os.PathError{Op: "rangeread", Path: f.path, Err: err}
}
if resp.StatusCode != http.StatusPartialContent && resp.StatusCode != http.StatusOK {
cancel()
_ = resp.Body.Close()
return nil, &os.PathError{
Op: "rangeread",
Path: f.path,
Err: fmt.Errorf("unexpected HTTP status %d", resp.StatusCode),
}
}
// Wrap body so that the context is cancelled when the caller closes.
return &contextCloser{ReadCloser: resp.Body, cancel: cancel}, nil
}