This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
file.go
114 lines (100 loc) · 2.12 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
113
114
package gdriver
import (
"errors"
"io"
"sync"
)
type File interface {
Info() *FileInfo
Write([]byte) (int, error)
Read([]byte) (int, error)
Close() error
}
type readFile struct {
Driver *GDriver
*FileInfo
reader io.ReadCloser
once sync.Once
}
func (f *readFile) Info() *FileInfo {
return f.FileInfo
}
func (f *readFile) getReader() error {
var lastErr error
f.once.Do(func() {
response, err := f.Driver.srv.Files.Get(f.item.Id).Download()
if err != nil {
lastErr = err
return
}
f.reader = response.Body
})
return lastErr
}
func (f *readFile) Write(p []byte) (int, error) {
return 0, errors.New("open the file with O_WRONLY for writing")
}
func (f *readFile) Read(p []byte) (int, error) {
if err := f.getReader(); err != nil {
return 0, err
}
return f.reader.Read(p)
}
func (f *readFile) Close() error {
if err := f.getReader(); err != nil {
return err
}
return f.reader.Close()
}
type writeFile struct {
Driver *GDriver
Path string
*FileInfo
writer *io.PipeWriter
mu sync.Mutex
doneChan chan struct{}
putError error
}
func (f *writeFile) Info() *FileInfo {
return f.FileInfo
}
func (f *writeFile) getWriter() error {
f.mu.Lock()
if f.doneChan == nil {
var reader io.Reader
// open a pipe and use the writer part for Write()
reader, f.writer = io.Pipe()
// the channel is used to notify the Close() or Write() function if something goes wrong
f.doneChan = make(chan struct{})
go func() {
if f.FileInfo == nil {
f.FileInfo, f.putError = f.Driver.PutFile(f.Path, reader)
} else {
f.putError = f.Driver.updateFileContents(f.FileInfo.item.Id, reader)
}
f.doneChan <- struct{}{}
}()
}
err := f.putError
f.mu.Unlock()
return err
}
func (f *writeFile) Write(p []byte) (int, error) {
if err := f.getWriter(); err != nil {
return 0, err
}
return f.writer.Write(p)
}
func (f *writeFile) Read(p []byte) (int, error) {
return 0, errors.New("open the file with O_RDONLY for writing")
}
func (f *writeFile) Close() error {
closeErr := f.writer.Close()
if f.doneChan != nil {
<-f.doneChan
if err := f.putError; err != nil {
return err
}
}
return closeErr
}