2018-05-06 13:47:16 +08:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
2018-05-28 02:52:26 +08:00
|
|
|
"context"
|
2019-01-06 14:23:44 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2018-05-28 02:52:26 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
2018-05-08 16:59:43 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-05-10 14:18:02 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-12-30 16:51:44 +08:00
|
|
|
"github.com/seaweedfs/fuse"
|
|
|
|
"github.com/seaweedfs/fuse/fs"
|
2018-05-06 13:47:16 +08:00
|
|
|
)
|
|
|
|
|
2018-09-28 00:42:24 +08:00
|
|
|
const blockSize = 512
|
2018-09-28 00:33:36 +08:00
|
|
|
|
2018-05-08 16:59:43 +08:00
|
|
|
var _ = fs.Node(&File{})
|
2018-05-22 18:26:38 +08:00
|
|
|
var _ = fs.NodeOpener(&File{})
|
2018-05-16 15:54:27 +08:00
|
|
|
var _ = fs.NodeFsyncer(&File{})
|
2018-05-21 15:00:28 +08:00
|
|
|
var _ = fs.NodeSetattrer(&File{})
|
2018-05-08 16:59:43 +08:00
|
|
|
|
2018-05-06 13:47:16 +08:00
|
|
|
type File struct {
|
2018-12-30 16:51:44 +08:00
|
|
|
Name string
|
|
|
|
dir *Dir
|
|
|
|
wfs *WFS
|
|
|
|
entry *filer_pb.Entry
|
2019-01-01 07:10:14 +08:00
|
|
|
entryViewCache []filer2.VisibleInterval
|
2018-12-30 16:51:44 +08:00
|
|
|
isOpen bool
|
2018-05-06 13:47:16 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
func (file *File) fullpath() string {
|
|
|
|
return filepath.Join(file.dir.Path, file.Name)
|
|
|
|
}
|
2018-05-14 17:02:17 +08:00
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error {
|
2018-05-14 17:02:17 +08:00
|
|
|
|
2019-12-14 02:05:43 +08:00
|
|
|
glog.V(4).Infof("file Attr %s", file.fullpath())
|
|
|
|
|
2018-09-17 03:37:06 +08:00
|
|
|
if err := file.maybeLoadAttributes(ctx); err != nil {
|
|
|
|
return err
|
2018-05-14 17:02:17 +08:00
|
|
|
}
|
2018-05-08 16:59:43 +08:00
|
|
|
|
2018-09-22 15:11:46 +08:00
|
|
|
attr.Mode = os.FileMode(file.entry.Attributes.FileMode)
|
|
|
|
attr.Size = filer2.TotalSize(file.entry.Chunks)
|
|
|
|
attr.Mtime = time.Unix(file.entry.Attributes.Mtime, 0)
|
|
|
|
attr.Gid = file.entry.Attributes.Gid
|
|
|
|
attr.Uid = file.entry.Attributes.Uid
|
2018-09-28 00:33:36 +08:00
|
|
|
attr.Blocks = attr.Size/blockSize + 1
|
2018-10-14 15:18:52 +08:00
|
|
|
attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit)
|
2018-05-20 04:51:44 +08:00
|
|
|
|
2018-05-14 17:02:17 +08:00
|
|
|
return nil
|
2018-05-08 16:59:43 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-22 18:26:38 +08:00
|
|
|
func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2019-12-14 02:05:43 +08:00
|
|
|
glog.V(4).Infof("file %v open %+v", file.fullpath(), req)
|
2018-05-21 15:00:28 +08:00
|
|
|
|
2018-05-25 16:27:21 +08:00
|
|
|
file.isOpen = true
|
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid)
|
|
|
|
|
|
|
|
resp.Handle = fuse.HandleID(handle.handle)
|
|
|
|
|
|
|
|
glog.V(3).Infof("%v file open handle id = %d", file.fullpath(), handle.handle)
|
|
|
|
|
|
|
|
return handle, nil
|
2018-05-21 15:00:28 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
|
|
|
|
|
2018-09-17 03:37:06 +08:00
|
|
|
if err := file.maybeLoadAttributes(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-22 15:11:46 +08:00
|
|
|
glog.V(3).Infof("%v file setattr %+v, old:%+v", file.fullpath(), req, file.entry.Attributes)
|
2018-05-22 18:26:38 +08:00
|
|
|
if req.Valid.Size() {
|
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
glog.V(3).Infof("%v file setattr set size=%v", file.fullpath(), req.Size)
|
2018-05-22 18:26:38 +08:00
|
|
|
if req.Size == 0 {
|
2018-05-25 14:20:12 +08:00
|
|
|
// fmt.Printf("truncate %v \n", fullPath)
|
2018-09-22 15:11:46 +08:00
|
|
|
file.entry.Chunks = nil
|
2018-12-30 16:51:44 +08:00
|
|
|
file.entryViewCache = nil
|
2018-05-22 18:26:38 +08:00
|
|
|
}
|
2018-09-22 15:11:46 +08:00
|
|
|
file.entry.Attributes.FileSize = req.Size
|
2018-05-22 18:26:38 +08:00
|
|
|
}
|
|
|
|
if req.Valid.Mode() {
|
2018-09-22 15:11:46 +08:00
|
|
|
file.entry.Attributes.FileMode = uint32(req.Mode)
|
2018-05-22 18:26:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Valid.Uid() {
|
2018-09-22 15:11:46 +08:00
|
|
|
file.entry.Attributes.Uid = req.Uid
|
2018-05-22 18:26:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Valid.Gid() {
|
2018-09-22 15:11:46 +08:00
|
|
|
file.entry.Attributes.Gid = req.Gid
|
2018-05-22 18:26:38 +08:00
|
|
|
}
|
|
|
|
|
2018-12-29 14:37:18 +08:00
|
|
|
if req.Valid.Crtime() {
|
|
|
|
file.entry.Attributes.Crtime = req.Crtime.Unix()
|
|
|
|
}
|
|
|
|
|
2018-05-22 18:26:38 +08:00
|
|
|
if req.Valid.Mtime() {
|
2018-09-22 15:11:46 +08:00
|
|
|
file.entry.Attributes.Mtime = req.Mtime.Unix()
|
2018-05-21 15:00:28 +08:00
|
|
|
}
|
|
|
|
|
2019-02-16 02:00:27 +08:00
|
|
|
if file.isOpen {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
return file.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
|
2018-07-19 17:17:36 +08:00
|
|
|
|
|
|
|
request := &filer_pb.UpdateEntryRequest{
|
|
|
|
Directory: file.dir.Path,
|
2018-09-22 15:12:10 +08:00
|
|
|
Entry: file.entry,
|
2018-07-19 17:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("set attr file entry: %v", request)
|
|
|
|
_, err := client.UpdateEntry(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("UpdateEntry file %s/%s: %v", file.dir.Path, file.Name, err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2018-05-21 15:00:28 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:54:27 +08:00
|
|
|
func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
|
|
|
|
// fsync works at OS level
|
2018-06-06 14:37:41 +08:00
|
|
|
// write the file chunks to the filerGrpcAddress
|
2018-05-24 11:55:24 +08:00
|
|
|
glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
|
2018-05-20 04:51:44 +08:00
|
|
|
|
2018-05-08 16:59:43 +08:00
|
|
|
return nil
|
2018-05-06 13:47:16 +08:00
|
|
|
}
|
2018-09-17 03:37:06 +08:00
|
|
|
|
|
|
|
func (file *File) maybeLoadAttributes(ctx context.Context) error {
|
2018-09-22 15:11:46 +08:00
|
|
|
if file.entry == nil || !file.isOpen {
|
2018-09-17 03:37:06 +08:00
|
|
|
item := file.wfs.listDirectoryEntriesCache.Get(file.fullpath())
|
2019-12-14 02:35:23 +08:00
|
|
|
var entry *filer_pb.Entry
|
2018-09-17 03:37:06 +08:00
|
|
|
if item != nil && !item.Expired() {
|
2019-12-14 02:35:23 +08:00
|
|
|
entry = item.Value().(*filer_pb.Entry)
|
|
|
|
}
|
|
|
|
if entry != nil {
|
2019-12-14 02:05:43 +08:00
|
|
|
|
|
|
|
glog.V(4).Infof("file read attr cache hit %s", file.fullpath())
|
|
|
|
|
2019-01-06 07:21:56 +08:00
|
|
|
file.setEntry(entry)
|
2018-09-17 03:37:06 +08:00
|
|
|
// glog.V(1).Infof("file attr read cached %v attributes", file.Name)
|
|
|
|
} else {
|
2019-12-14 02:05:43 +08:00
|
|
|
|
|
|
|
glog.V(3).Infof("file read attr cache miss %s", file.fullpath())
|
|
|
|
|
2019-05-03 15:24:35 +08:00
|
|
|
err := file.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-17 03:37:06 +08:00
|
|
|
|
2018-09-22 15:11:46 +08:00
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
2018-09-17 03:37:06 +08:00
|
|
|
Name: file.Name,
|
2018-09-22 15:11:46 +08:00
|
|
|
Directory: file.dir.Path,
|
2018-09-17 03:37:06 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 15:11:46 +08:00
|
|
|
resp, err := client.LookupDirectoryEntry(ctx, request)
|
2018-09-17 03:37:06 +08:00
|
|
|
if err != nil {
|
2018-12-29 07:10:23 +08:00
|
|
|
glog.V(3).Infof("file attr read file %v: %v", request, err)
|
|
|
|
return fuse.ENOENT
|
2018-09-17 03:37:06 +08:00
|
|
|
}
|
|
|
|
|
2019-01-06 07:21:56 +08:00
|
|
|
file.setEntry(resp.Entry)
|
2018-09-17 03:37:06 +08:00
|
|
|
|
2018-12-29 07:10:23 +08:00
|
|
|
glog.V(3).Infof("file attr %v %+v: %d", file.fullpath(), file.entry.Attributes, filer2.TotalSize(file.entry.Chunks))
|
2018-09-17 03:37:06 +08:00
|
|
|
|
2019-01-04 14:21:39 +08:00
|
|
|
// file.wfs.listDirectoryEntriesCache.Set(file.fullpath(), file.entry, file.wfs.option.EntryCacheTtl)
|
2018-11-08 03:35:13 +08:00
|
|
|
|
2018-09-17 03:37:06 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-06 07:16:39 +08:00
|
|
|
|
|
|
|
func (file *File) addChunk(chunk *filer_pb.FileChunk) {
|
|
|
|
if chunk != nil {
|
|
|
|
file.addChunks([]*filer_pb.FileChunk{chunk})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
|
2019-01-06 14:23:44 +08:00
|
|
|
|
|
|
|
sort.Slice(chunks, func(i, j int) bool {
|
|
|
|
return chunks[i].Mtime < chunks[j].Mtime
|
|
|
|
})
|
|
|
|
|
|
|
|
var newVisibles []filer2.VisibleInterval
|
2019-01-06 07:16:39 +08:00
|
|
|
for _, chunk := range chunks {
|
2019-01-06 14:23:44 +08:00
|
|
|
newVisibles = filer2.MergeIntoVisibles(file.entryViewCache, newVisibles, chunk)
|
|
|
|
t := file.entryViewCache[:0]
|
|
|
|
file.entryViewCache = newVisibles
|
|
|
|
newVisibles = t
|
2019-01-06 07:16:39 +08:00
|
|
|
}
|
2019-01-06 14:23:44 +08:00
|
|
|
|
2019-06-22 03:05:00 +08:00
|
|
|
glog.V(3).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks))
|
|
|
|
|
2019-01-06 14:23:44 +08:00
|
|
|
file.entry.Chunks = append(file.entry.Chunks, chunks...)
|
2019-01-06 07:16:39 +08:00
|
|
|
}
|
2019-01-06 07:21:56 +08:00
|
|
|
|
|
|
|
func (file *File) setEntry(entry *filer_pb.Entry) {
|
|
|
|
file.entry = entry
|
|
|
|
file.entryViewCache = filer2.NonOverlappingVisibleIntervals(file.entry.Chunks)
|
|
|
|
}
|