-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
111 lines (96 loc) · 1.94 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
package gcsfs
import (
"io"
"io/fs"
"path"
"cloud.google.com/go/storage"
"github.com/jarxorg/wfs"
)
type gcsFile struct {
*content
fsys *GCSFS
obj gcsObject
attrs *storage.ObjectAttrs
in io.ReadCloser
}
var (
_ fs.File = (*gcsFile)(nil)
_ fs.FileInfo = (*gcsFile)(nil)
)
func newGcsFile(fsys *GCSFS, obj gcsObject, attrs *storage.ObjectAttrs) *gcsFile {
return &gcsFile{
content: newFileContent(attrs),
fsys: fsys,
obj: obj,
attrs: attrs,
}
}
// Read reads bytes from this file.
func (f *gcsFile) Read(p []byte) (int, error) {
if f.in == nil {
var err error
f.in, err = f.obj.newReader(f.fsys.Context())
if err != nil {
return 0, toPathError(err, "Read", f.attrs.Name)
}
}
return f.in.Read(p)
}
// Stat returns the fs.FileInfo of this file.
func (f *gcsFile) Stat() (fs.FileInfo, error) {
return f, nil
}
// Close closes streams.
func (f *gcsFile) Close() error {
var err error
if f.in != nil {
err = f.in.Close()
f.in = nil
}
return err
}
type gcsWriterFile struct {
*content
fsys *GCSFS
name string
obj gcsObject
out io.WriteCloser
}
var (
_ wfs.WriterFile = (*gcsWriterFile)(nil)
_ fs.FileInfo = (*gcsWriterFile)(nil)
)
func newGcsWriterFile(fsys *GCSFS, obj gcsObject, name string) *gcsWriterFile {
return &gcsWriterFile{
content: &content{
name: path.Base(name),
},
fsys: fsys,
obj: obj,
name: name,
}
}
// Write writes the specified bytes to this file.
func (f *gcsWriterFile) Write(p []byte) (int, error) {
if f.out == nil {
f.out = f.obj.newWriter(f.fsys.Context())
}
return f.out.Write(p)
}
// Close closes streams.
func (f *gcsWriterFile) Close() error {
if f.out != nil {
err := f.out.Close()
f.out = nil
return err
}
return nil
}
// Read reads bytes from this file.
func (f *gcsWriterFile) Read(p []byte) (int, error) {
return 0, nil
}
// Stat returns the fs.FileInfo of this file.
func (f *gcsWriterFile) Stat() (fs.FileInfo, error) {
return f, nil
}