-
Notifications
You must be signed in to change notification settings - Fork 567
/
file.go
178 lines (154 loc) · 3.67 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
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
package fuse
import (
"context"
"io"
"io/ioutil"
"math"
"os"
"sync"
"syscall"
"time"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/pachyderm/pachyderm/src/client/pfs"
)
type file struct {
name string
attr *fuse.Attr
cancel func()
pfsFile *pfs.File
file *os.File
counter *counter
err error
}
func newFile(fs *filesystem, name string) (*file, fuse.Status) {
attr, status := fs.getAttr(name)
if status != fuse.OK {
return nil, status
}
ctx, cancel := context.WithCancel(fs.c.Ctx())
c := fs.c.WithCtx(ctx)
f, err := ioutil.TempFile("", "pfs-fuse")
if err != nil {
return nil, fuse.ToStatus(err)
}
if err := os.Remove(f.Name()); err != nil {
return nil, fuse.ToStatus(err)
}
_, pfsFile, err := fs.parsePath(name)
if err != nil {
return nil, toStatus(err)
}
if pfsFile == nil {
return nil, fuse.Status(syscall.EISDIR)
}
counter := newCounter()
// Argument order is important here because it means that writes to w must
// complete writing to f before being written to counter. Thus counter can
// tell us conclusively at least (but not at most) a certain number of
// bytes has been written to f.
w := io.MultiWriter(f, counter)
result := &file{
attr: attr,
cancel: cancel,
pfsFile: pfsFile,
file: f,
counter: counter,
}
go func() {
if err := c.GetFile(pfsFile.Commit.Repo.Name, pfsFile.Commit.ID, pfsFile.Path, 0, 0, w); err != nil {
result.err = err
counter.cancel()
}
}()
return result, fuse.OK
}
func (f *file) Write(data []byte, off int64) (written uint32, code fuse.Status) {
return 0, fuse.EROFS
}
func (f *file) SetInode(*nodefs.Inode) {}
func (f *file) String() string {
return f.name
}
func (f *file) InnerFile() nodefs.File {
return nil
}
func (f *file) Read(dest []byte, offset int64) (fuse.ReadResult, fuse.Status) {
waitn := offset + int64(len(dest))
if waitn > int64(f.attr.Size) {
waitn = int64(f.attr.Size)
}
f.counter.wait(waitn)
// check if there was an error reading the file
if f.err != nil {
return nil, toStatus(f.err)
}
if err := f.file.Sync(); err != nil {
return nil, toStatus(err)
}
return fuse.ReadResultFd(f.file.Fd(), offset, len(dest)), fuse.OK
}
func (f *file) Flock(flags int) fuse.Status {
return fuse.ENOSYS
}
func (f *file) Flush() fuse.Status {
// For reasons I don't understand Flush gets called when reading files.
return fuse.OK
}
func (f *file) Release() {
f.cancel()
}
func (f *file) Fsync(flags int) (code fuse.Status) {
return fuse.EROFS
}
func (f *file) Truncate(size uint64) fuse.Status {
return fuse.EROFS
}
func (f *file) GetAttr(out *fuse.Attr) fuse.Status {
*out = *f.attr
return fuse.OK
}
func (f *file) Chown(uid uint32, gid uint32) fuse.Status {
return fuse.EROFS
}
func (f *file) Chmod(perms uint32) fuse.Status {
return fuse.EROFS
}
func (f *file) Utimens(atime *time.Time, mtime *time.Time) fuse.Status {
return fuse.EROFS
}
func (f *file) Allocate(off uint64, size uint64, mode uint32) fuse.Status {
return fuse.EROFS
}
type counter struct {
n int64
mu sync.Mutex
cond *sync.Cond
}
func newCounter() *counter {
result := &counter{}
result.cond = sync.NewCond(&result.mu)
return result
}
func (c *counter) Write(p []byte) (int, error) {
c.mu.Lock()
defer c.mu.Unlock()
c.n += int64(len(p))
c.cond.Broadcast()
return len(p), nil
}
// wait until more than n bytes have been written
func (c *counter) wait(n int64) {
c.mu.Lock()
defer c.mu.Unlock()
for c.n < n {
c.cond.Wait()
}
}
// cancel indicates that an error has occurred which will prevent any further
// calls to Write, it causes all calls to wait() to return
func (c *counter) cancel() {
c.mu.Lock()
defer c.mu.Unlock()
c.n = math.MaxInt64
}