-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
112 lines (94 loc) · 1.86 KB
/
file.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
// SPDX-FileCopyrightText: 2022 Weston Schmidt <weston_schmidt@alumni.purdue.edu>
// SPDX-License-Identifier: Apache-2.0
package githubfs
import (
"fmt"
"io"
"io/fs"
"sync"
"time"
)
// file provides the concrete fs.File implementation for the filesystem.
type file struct {
m sync.Mutex
gfs *FS
parent *dir
owner string
repo string
info fileInfo
url string
content []byte
}
type fileOpt func(f *file)
func withContent(content []byte) fileOpt {
return func(f *file) {
f.content = content
f.info.size = int64(len(content))
}
}
func withUrl(url string) fileOpt {
return func(f *file) {
f.url = url
}
}
func withModTime(t time.Time) fileOpt {
return func(f *file) {
f.info.modTime = t
}
}
func withMode(mode fs.FileMode) fileOpt {
return func(f *file) {
f.info.mode = mode
}
}
func withSize(size int) fileOpt {
return func(f *file) {
if int64(len(f.content)) == 0 {
f.info.size = int64(size)
}
}
}
func newFile(parent *dir, name string, opts ...fileOpt) *file {
f := file{
gfs: parent.gfs,
parent: parent,
owner: parent.org,
repo: parent.repo,
info: fileInfo{
name: name,
mode: fs.FileMode(0644),
},
}
for _, opt := range opts {
opt(&f)
}
return &f
}
func (f *file) newFileHandle() (*fileHandle, error) {
f.m.Lock()
defer f.m.Unlock()
if int64(len(f.content)) != f.info.size {
resp, err := f.gfs.httpClient.Get(f.url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("http status code not 200: %d\n", resp.StatusCode)
}
bod, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
f.content = bod
f.info.size = int64(len(bod))
}
return newFileHandle(f.info, f.content), nil
}
func (f *file) toDirEntry() *dirEntry {
f.m.Lock()
defer f.m.Unlock()
return &dirEntry{
info: &f.info,
}
}