2018-05-23 10:08:46 +00:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
2018-05-27 18:52:26 +00:00
|
|
|
"context"
|
2018-05-23 10:08:46 +00:00
|
|
|
"fmt"
|
2019-05-03 07:24:35 +00:00
|
|
|
"mime"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2019-01-01 10:33:57 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2018-05-27 18:52:26 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2019-03-27 21:25:18 +00:00
|
|
|
"github.com/gabriel-vasile/mimetype"
|
2018-12-30 08:51:44 +00:00
|
|
|
"github.com/seaweedfs/fuse"
|
|
|
|
"github.com/seaweedfs/fuse/fs"
|
2019-02-18 20:11:52 +00:00
|
|
|
"google.golang.org/grpc"
|
2018-05-23 10:08:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileHandle struct {
|
|
|
|
// cache file has been written to
|
2018-05-28 19:30:17 +00:00
|
|
|
dirtyPages *ContinuousDirtyPages
|
2018-07-06 06:16:34 +00:00
|
|
|
contentType string
|
2018-09-22 07:11:46 +00:00
|
|
|
dirtyMetadata bool
|
|
|
|
handle uint64
|
2018-06-06 09:09:57 +00:00
|
|
|
|
2018-05-25 07:57:25 +00:00
|
|
|
f *File
|
|
|
|
RequestId fuse.RequestID // unique ID for request
|
|
|
|
NodeId fuse.NodeID // file or directory the request is about
|
|
|
|
Uid uint32 // user ID of process making request
|
|
|
|
Gid uint32 // group ID of process making request
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 09:09:57 +00:00
|
|
|
func newFileHandle(file *File, uid, gid uint32) *FileHandle {
|
|
|
|
return &FileHandle{
|
|
|
|
f: file,
|
|
|
|
dirtyPages: newDirtyPages(file),
|
|
|
|
Uid: uid,
|
|
|
|
Gid: gid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
var _ = fs.Handle(&FileHandle{})
|
2018-05-27 18:52:26 +00:00
|
|
|
|
2018-05-24 08:22:37 +00:00
|
|
|
// var _ = fs.HandleReadAller(&FileHandle{})
|
|
|
|
var _ = fs.HandleReader(&FileHandle{})
|
2018-05-23 10:08:46 +00:00
|
|
|
var _ = fs.HandleFlusher(&FileHandle{})
|
|
|
|
var _ = fs.HandleWriter(&FileHandle{})
|
|
|
|
var _ = fs.HandleReleaser(&FileHandle{})
|
|
|
|
|
2018-05-24 08:22:37 +00:00
|
|
|
func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-06-06 09:09:57 +00:00
|
|
|
glog.V(4).Infof("%s read fh %d: [%d,%d)", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(req.Size))
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-06-06 09:09:57 +00:00
|
|
|
// this value should come from the filer instead of the old f
|
2018-09-22 07:11:46 +00:00
|
|
|
if len(fh.f.entry.Chunks) == 0 {
|
2018-09-23 17:01:00 +00:00
|
|
|
glog.V(1).Infof("empty fh %v/%v", fh.f.dir.Path, fh.f.Name)
|
|
|
|
return nil
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 08:22:37 +00:00
|
|
|
buff := make([]byte, req.Size)
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-12-30 08:51:44 +00:00
|
|
|
if fh.f.entryViewCache == nil {
|
|
|
|
fh.f.entryViewCache = filer2.NonOverlappingVisibleIntervals(fh.f.entry.Chunks)
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkViews := filer2.ViewFromVisibleIntervals(fh.f.entryViewCache, req.Offset, req.Size)
|
2018-05-24 08:22:37 +00:00
|
|
|
|
2019-05-03 07:24:35 +00:00
|
|
|
totalRead, err := filer2.ReadIntoBuffer(ctx, fh.f.wfs, fh.f.fullpath(), buff, chunkViews, req.Offset)
|
2018-05-24 08:22:37 +00:00
|
|
|
|
|
|
|
resp.Data = buff[:totalRead]
|
|
|
|
|
|
|
|
return err
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the file handle
|
|
|
|
func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
|
2018-05-24 03:55:24 +00:00
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
// write the request to volume servers
|
2018-05-24 03:55:24 +00:00
|
|
|
|
2018-06-06 09:09:57 +00:00
|
|
|
glog.V(4).Infof("%+v/%v write fh %d: [%d,%d)", fh.f.dir.Path, fh.f.Name, fh.handle, req.Offset, req.Offset+int64(len(req.Data)))
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-05-31 05:02:21 +00:00
|
|
|
chunks, err := fh.dirtyPages.AddPage(ctx, req.Offset, req.Data)
|
2018-05-23 10:08:46 +00:00
|
|
|
if err != nil {
|
2018-09-07 20:11:43 +00:00
|
|
|
glog.Errorf("%+v/%v write fh %d: [%d,%d): %v", fh.f.dir.Path, fh.f.Name, fh.handle, req.Offset, req.Offset+int64(len(req.Data)), err)
|
2018-05-28 19:30:17 +00:00
|
|
|
return fmt.Errorf("write %s/%s at [%d,%d): %v", fh.f.dir.Path, fh.f.Name, req.Offset, req.Offset+int64(len(req.Data)), err)
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 19:30:17 +00:00
|
|
|
resp.Size = len(req.Data)
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-05-31 03:24:57 +00:00
|
|
|
if req.Offset == 0 {
|
2019-03-27 21:25:18 +00:00
|
|
|
// detect mime type
|
|
|
|
var possibleExt string
|
|
|
|
fh.contentType, possibleExt = mimetype.Detect(req.Data)
|
|
|
|
if ext := path.Ext(fh.f.Name); ext != possibleExt {
|
|
|
|
fh.contentType = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
|
2018-05-31 03:24:57 +00:00
|
|
|
fh.dirtyMetadata = true
|
|
|
|
}
|
|
|
|
|
2019-01-05 23:16:39 +00:00
|
|
|
fh.f.addChunks(chunks)
|
|
|
|
|
|
|
|
if len(chunks) > 0 {
|
2018-05-28 19:30:17 +00:00
|
|
|
fh.dirtyMetadata = true
|
|
|
|
}
|
2018-05-23 10:08:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
|
|
|
|
|
2018-06-06 09:09:57 +00:00
|
|
|
glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
|
|
|
|
|
2018-12-28 11:27:48 +00:00
|
|
|
fh.dirtyPages.releaseResource()
|
|
|
|
|
2018-11-14 07:53:17 +00:00
|
|
|
fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-05-25 08:27:21 +00:00
|
|
|
fh.f.isOpen = false
|
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
|
|
|
|
// fflush works at fh level
|
|
|
|
// send the data to the OS
|
2018-06-06 09:09:57 +00:00
|
|
|
glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-05-28 19:30:17 +00:00
|
|
|
chunk, err := fh.dirtyPages.FlushToStorage(ctx)
|
|
|
|
if err != nil {
|
2018-11-18 19:51:38 +00:00
|
|
|
glog.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
|
|
|
|
return fmt.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
2019-01-05 23:16:39 +00:00
|
|
|
|
|
|
|
fh.f.addChunk(chunk)
|
2018-05-28 19:30:17 +00:00
|
|
|
|
|
|
|
if !fh.dirtyMetadata {
|
2018-05-23 10:08:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-03 07:24:35 +00:00
|
|
|
return fh.f.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-09-22 07:11:46 +00:00
|
|
|
if fh.f.entry.Attributes != nil {
|
|
|
|
fh.f.entry.Attributes.Mime = fh.contentType
|
|
|
|
fh.f.entry.Attributes.Uid = req.Uid
|
|
|
|
fh.f.entry.Attributes.Gid = req.Gid
|
2018-12-17 07:20:08 +00:00
|
|
|
fh.f.entry.Attributes.Mtime = time.Now().Unix()
|
|
|
|
fh.f.entry.Attributes.Crtime = time.Now().Unix()
|
|
|
|
fh.f.entry.Attributes.FileMode = uint32(0770)
|
2018-07-06 06:16:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 07:11:46 +00:00
|
|
|
request := &filer_pb.CreateEntryRequest{
|
2018-05-25 07:57:25 +00:00
|
|
|
Directory: fh.f.dir.Path,
|
2018-09-22 07:11:46 +00:00
|
|
|
Entry: fh.f.entry,
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 10:33:57 +00:00
|
|
|
//glog.V(1).Infof("%s/%s set chunks: %v", fh.f.dir.Path, fh.f.Name, len(fh.f.entry.Chunks))
|
|
|
|
//for i, chunk := range fh.f.entry.Chunks {
|
|
|
|
// glog.V(4).Infof("%s/%s chunks %d: %v [%d,%d)", fh.f.dir.Path, fh.f.Name, i, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
|
|
|
|
//}
|
|
|
|
|
|
|
|
chunks, garbages := filer2.CompactFileChunks(fh.f.entry.Chunks)
|
|
|
|
fh.f.entry.Chunks = chunks
|
2019-01-05 23:16:39 +00:00
|
|
|
// fh.f.entryViewCache = nil
|
2019-03-16 00:20:24 +00:00
|
|
|
fh.f.wfs.deleteFileChunks(ctx, garbages)
|
2019-01-01 10:33:57 +00:00
|
|
|
|
2018-09-22 07:11:46 +00:00
|
|
|
if _, err := client.CreateEntry(ctx, request); err != nil {
|
2018-05-23 10:08:46 +00:00
|
|
|
return fmt.Errorf("update fh: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2018-05-24 08:22:37 +00:00
|
|
|
|
2019-02-18 20:11:52 +00:00
|
|
|
func deleteFileIds(ctx context.Context, grpcDialOption grpc.DialOption, client filer_pb.SeaweedFilerClient, fileIds []string) error {
|
2019-01-01 10:33:57 +00:00
|
|
|
|
|
|
|
var vids []string
|
|
|
|
for _, fileId := range fileIds {
|
2019-05-03 07:24:35 +00:00
|
|
|
vids = append(vids, filer2.VolumeId(fileId))
|
2019-01-01 10:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lookupFunc := func(vids []string) (map[string]operation.LookupResult, error) {
|
|
|
|
|
|
|
|
m := make(map[string]operation.LookupResult)
|
|
|
|
|
|
|
|
glog.V(4).Infof("remove file lookup volume id locations: %v", vids)
|
|
|
|
resp, err := client.LookupVolume(ctx, &filer_pb.LookupVolumeRequest{
|
|
|
|
VolumeIds: vids,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vid := range vids {
|
|
|
|
lr := operation.LookupResult{
|
|
|
|
VolumeId: vid,
|
|
|
|
Locations: nil,
|
|
|
|
}
|
|
|
|
locations := resp.LocationsMap[vid]
|
|
|
|
for _, loc := range locations.Locations {
|
|
|
|
lr.Locations = append(lr.Locations, operation.Location{
|
|
|
|
Url: loc.Url,
|
|
|
|
PublicUrl: loc.PublicUrl,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
m[vid] = lr
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
2019-02-18 20:11:52 +00:00
|
|
|
_, err := operation.DeleteFilesWithLookupVolumeId(grpcDialOption, fileIds, lookupFunc)
|
2019-01-01 10:33:57 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|