-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilehandle.go
67 lines (56 loc) · 1.48 KB
/
filehandle.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
// SPDX-FileCopyrightText: 2022 Weston Schmidt <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package githubfs
import (
"bytes"
"fmt"
"io/fs"
"sync"
)
// ensure the File matches the interface
var _ fs.File = (*fileHandle)(nil)
// fileHandle is the external file given out that can be read and closed.
type fileHandle struct {
m sync.Mutex
info fileInfo
content *bytes.Buffer
closed bool
}
func newFileHandle(info fileInfo, content []byte) *fileHandle {
return &fileHandle{
info: info,
content: bytes.NewBuffer(content),
}
}
// Stat returns a FileInfo describing the file.
func (f *fileHandle) Stat() (fs.FileInfo, error) {
f.m.Lock()
defer f.m.Unlock()
if f.closed {
return nil, fmt.Errorf("stat %s %w", f.info.name, fs.ErrClosed)
}
return &f.info, nil
}
// Read reads up to len(b) bytes from the File and stores them in b. It returns
// the number of bytes read and any error encountered. At end of file, Read
// returns 0, io.EOF.
func (f *fileHandle) Read(b []byte) (int, error) {
f.m.Lock()
defer f.m.Unlock()
if f.closed {
return 0, fmt.Errorf("read %s %w", f.info.name, fs.ErrClosed)
}
return f.content.Read(b)
}
// Close closes the File, rendering it unusable for I/O. Close will return an
// error if it has already been called.
func (f *fileHandle) Close() error {
f.m.Lock()
defer f.m.Unlock()
if f.closed {
return fmt.Errorf("close %s %w", f.info.name, fs.ErrClosed)
}
f.closed = true
f.content = nil
return nil
}