-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
231 lines (211 loc) · 5.42 KB
/
files.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"fmt"
"log"
"os"
"os/exec"
"sync"
"syscall"
"time"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/jakrach/lazyfs/protobuf"
)
// LazyFile represents a placeholder for a checkpointed file, potentially with
// a local copy to operate on.
type LazyFile struct {
e *protobuf.RegFileEntry
cached bool
remote string
inner *os.File
lock sync.Mutex
}
// fetchRemote SCP's the source file from the saved remote host.
func (f *LazyFile) fetchRemote() error {
fname := f.e.GetName()
cmd := exec.Command("scp", f.remote+":"+fname, fname)
log.Println(cmd)
err := cmd.Run()
if err != nil {
return err
}
fd, err := os.Open(fname)
if err != nil {
return err
}
fd.Chmod(os.FileMode(f.e.GetMode()))
fd.Close()
return nil
}
// NewLazyFile creates a new lazy file structure.
func NewLazyFile(e *protobuf.RegFileEntry, remote string) *LazyFile {
return &LazyFile{
e: e,
cached: false,
inner: nil,
remote: remote,
}
}
// String prints the local and original filename, as well as if it is cached.
func (f *LazyFile) String() string {
str := "PLACEHOLDER: %s"
if f.cached {
str = "CACHED: %s"
}
return fmt.Sprintf(str, f.e.GetName())
}
// Read fetches the remote file if it is not cached locally. Reads from the
// local copy of the file.
func (f *LazyFile) Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status) {
if !f.cached {
err := f.fetchRemote()
if err != nil {
return nil, fuse.ToStatus(err)
}
f.inner, err = os.OpenFile(f.e.GetName(), int(f.e.GetFlags()),
os.FileMode(f.e.GetMode()))
if err != nil {
return nil, fuse.ToStatus(err)
}
f.cached = true
}
f.lock.Lock()
r := fuse.ReadResultFd(f.inner.Fd(), off, len(dest))
f.lock.Unlock()
return r, fuse.OK
}
// Write fetches the remote file if it is not cached locally. Writes to the
// local copy of the file.
func (f *LazyFile) Write(data []byte, off int64) (written uint32, code fuse.Status) {
if !f.cached {
err := f.fetchRemote()
if err != nil {
return 0, fuse.ToStatus(err)
}
f.inner, err = os.OpenFile(f.e.GetName(), int(f.e.GetFlags()),
os.FileMode(f.e.GetMode()))
if err != nil {
return 0, fuse.ToStatus(err)
}
f.cached = true
}
f.lock.Lock()
n, err := f.inner.WriteAt(data, off)
f.lock.Unlock()
return uint32(n), fuse.ToStatus(err)
}
// Flush if cached, flushes the local file. Otherwise, always does nothing
// and returns OK.
func (f *LazyFile) Flush() fuse.Status {
if f.cached {
f.lock.Lock()
newFd, err := syscall.Dup(int(f.inner.Fd()))
f.lock.Unlock()
if err != nil {
return fuse.ToStatus(err)
}
err = syscall.Close(newFd)
return fuse.ToStatus(err)
}
return fuse.OK
}
// Release if cached, closes the local file. Otherwise, does nothing.
func (f *LazyFile) Release() {
if f.cached {
f.lock.Lock()
f.inner.Close()
f.lock.Unlock()
}
}
// Fsync if cached, syncs the local file as normal. Otherwise, always does
// nothing and returns OK.
func (f *LazyFile) Fsync(flags int) fuse.Status {
if f.cached {
f.lock.Lock()
r := fuse.ToStatus(syscall.Fsync(int(f.inner.Fd())))
f.lock.Unlock()
return r
}
return fuse.OK
}
// Truncate if cached, truncates the local file as normal. Otherwise, always
// does nothing and returns OK.
func (f *LazyFile) Truncate(size uint64) fuse.Status {
if f.cached {
f.lock.Lock()
r := fuse.ToStatus(syscall.Ftruncate(int(f.inner.Fd()), int64(size)))
f.lock.Unlock()
return r
}
return fuse.OK
}
// GetAttr if cached, gets the attributes for the local file as normal.
// Otherwise, always does nothing and returns OK.
func (f *LazyFile) GetAttr(out *fuse.Attr) fuse.Status {
if f.cached {
st := syscall.Stat_t{}
f.lock.Lock()
err := syscall.Fstat(int(f.inner.Fd()), &st)
f.lock.Unlock()
if err != nil {
return fuse.ToStatus(err)
}
out.FromStat(&st)
}
return fuse.OK
}
// Chown if cached, calls chown on the local file. Otherwise, always does
// nothing and returns OK.
func (f *LazyFile) Chown(uid uint32, gid uint32) fuse.Status {
if f.cached {
f.lock.Lock()
r := fuse.ToStatus(f.inner.Chown(int(uid), int(gid)))
f.lock.Unlock()
return r
}
return fuse.OK
}
// Chmod if cached, calls chmod on the local file. Otherwise, always does
// nothing and returns OK.
func (f *LazyFile) Chmod(perms uint32) fuse.Status {
if f.cached {
f.lock.Lock()
r := fuse.ToStatus(f.inner.Chmod(os.FileMode(perms)))
f.lock.Unlock()
return r
}
return fuse.OK
}
// Allocate if cached, calls allocate on the local file. Otherwise, always does
// nothing and returns OK.
func (f *LazyFile) Allocate(off uint64, size uint64, mode uint32) fuse.Status {
if f.cached {
f.lock.Lock()
err := syscall.Fallocate(int(f.inner.Fd()), mode, int64(off),
int64(size))
f.lock.Unlock()
if err != nil {
return fuse.ToStatus(err)
}
}
return fuse.OK
}
// InnerFile lazy files do not implement this function.
func (f *LazyFile) InnerFile() nodefs.File { return nil }
// Utimens lazy files do not implement this function.
func (f *LazyFile) Utimens(atime *time.Time, mtime *time.Time) fuse.Status {
return fuse.OK
}
// Flock if cached, calls flock on the file. Otherwise, always does nothing
// and returns OK.
func (f *LazyFile) Flock(flags int) fuse.Status {
if f.cached {
f.lock.Lock()
r := fuse.ToStatus(syscall.Flock(int(f.inner.Fd()), flags))
f.lock.Unlock()
return r
}
return fuse.OK
}
// SetInode lazy files do not implement this function.
func (f *LazyFile) SetInode(inode *nodefs.Inode) {}