/
files.go
182 lines (155 loc) · 4.29 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
// Copyright 2019 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fs
import (
"context"
"time"
// "time"
"syscall"
ffs "github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
log "github.com/sirupsen/logrus"
)
// NewsdfsFile creates a FileHandle out of a file descriptor. All
// operations are implemented.
func NewsdfsFile(fd int64, path string) ffs.FileHandle {
return &sdfsFile{fd: fd, path: path}
}
type sdfsFile struct {
fd int64
path string
}
var _ = (ffs.FileHandle)((*sdfsFile)(nil))
var _ = (ffs.FileReleaser)((*sdfsFile)(nil))
var _ = (ffs.FileGetattrer)((*sdfsFile)(nil))
var _ = (ffs.FileReader)((*sdfsFile)(nil))
var _ = (ffs.FileWriter)((*sdfsFile)(nil))
var _ = (ffs.FileFlusher)((*sdfsFile)(nil))
var _ = (ffs.FileFsyncer)((*sdfsFile)(nil))
var _ = (ffs.FileSetattrer)((*sdfsFile)(nil))
func (f *sdfsFile) Read(ctx context.Context, buf []byte, off int64) (res fuse.ReadResult, errno syscall.Errno) {
rs, err := con.Read(ctx, f.fd, off, int32(len(buf)))
copy(buf, rs)
if err != nil {
log.Debugf("read error %v \n", err)
return nil, ToErrno(err)
}
r := fuse.ReadResultData(rs)
return r, ffs.OK
}
func (f *sdfsFile) Write(ctx context.Context, data []byte, off int64) (uint32, syscall.Errno) {
err := con.Write(ctx, f.fd, data, off, int32(len(data)))
if err != nil {
log.Debugf("write error %v \n", err)
return 0, ToErrno(err)
}
return uint32(len(data)), ffs.OK
}
func (f *sdfsFile) Release(ctx context.Context) syscall.Errno {
if f.fd != -1 {
err := con.Release(ctx, f.fd)
f.fd = -1
if err != nil {
log.Debugf("error during close %v", err)
}
return ToErrno(err)
}
return syscall.EBADF
}
func (f *sdfsFile) Flush(ctx context.Context) syscall.Errno {
err := con.Flush(ctx, f.path, f.fd)
if err != nil {
log.Debugf("error during flush %v", err)
}
return ToErrno(err)
}
func (f *sdfsFile) Fsync(ctx context.Context, flags uint32) (errno syscall.Errno) {
r := ffs.ToErrno(con.Fsync(ctx, f.path, f.fd))
return r
}
func (f *sdfsFile) Setattr(ctx context.Context, in *fuse.SetAttrIn, out *fuse.AttrOut) syscall.Errno {
if m, ok := in.GetMode(); ok {
if err := con.Chmod(ctx, f.path, int32(m)); err != nil {
if err != nil {
log.Debugf("error during setattr %v", err)
}
return ToErrno(err)
}
}
uid, uok := in.GetUID()
gid, gok := in.GetGID()
if uok || gok {
suid := -1
sgid := -1
if uok {
suid = int(uid)
}
if gok {
sgid = int(gid)
}
if err := con.Chown(ctx, f.path, int32(sgid), int32(suid)); err != nil {
return ToErrno(err)
}
}
mtime, mok := in.GetMTime()
atime, aok := in.GetATime()
if mok || aok {
ap := &atime
mp := &mtime
if !aok {
ap = nil
}
if !mok {
mp = nil
}
at := fuse.UtimeToTimespec(ap).Sec
mt := fuse.UtimeToTimespec(mp).Sec
if err := con.Utime(ctx, f.path, at, mt); err != nil {
log.Debugf("error setting utime for %s %v", f.path, err)
return ffs.ToErrno(err)
}
}
if sz, ok := in.GetSize(); ok {
if err := con.Truncate(ctx, f.path, int64(sz)); err != nil {
log.Debugf("error truncate for %s %v", f.path, err)
return ffs.ToErrno(err)
}
}
fi, err := con.GetAttr(ctx, f.path)
if err != nil {
log.Debugf("error getattr for %s %v", f.path, err)
return ToErrno(err)
}
atim := time.Unix(0, fi.Atime*int64(time.Millisecond))
ctim := time.Unix(0, fi.Ctim*int64(time.Millisecond))
mtim := time.Unix(0, fi.Mtim*int64(time.Millisecond))
out.SetTimes(&atim, &mtim, &ctim)
out.Size = uint64(fi.Size)
out.Blocks = uint64(fi.Size / 512)
out.Owner.Uid = uint32(fi.Uid)
out.Owner.Gid = uint32(fi.Gid)
out.Mode = uint32(fi.Mode)
out.Blksize = 512
return ffs.OK
}
func (f *sdfsFile) Getattr(ctx context.Context, a *fuse.AttrOut) syscall.Errno {
fi, err := con.GetAttr(ctx, f.path)
if err != nil {
if err != nil {
log.Debugf("error during getattr %v", err)
}
return ToErrno(err)
}
atime := time.Unix(0, fi.Atime*int64(time.Millisecond))
ctime := time.Unix(0, fi.Ctim*int64(time.Millisecond))
mtime := time.Unix(0, fi.Mtim*int64(time.Millisecond))
a.SetTimes(&atime, &mtime, &ctime)
a.Size = uint64(fi.Size)
a.Blocks = uint64(fi.Size / 512)
a.Owner.Uid = uint32(fi.Uid)
a.Owner.Gid = uint32(fi.Gid)
a.Mode = uint32(fi.Mode)
a.Blksize = 512
return ffs.OK
}