diff options
Diffstat (limited to 'weed/filesys/file.go')
| -rw-r--r-- | weed/filesys/file.go | 125 |
1 files changed, 84 insertions, 41 deletions
diff --git a/weed/filesys/file.go b/weed/filesys/file.go index 7aa1016d7..9e1342370 100644 --- a/weed/filesys/file.go +++ b/weed/filesys/file.go @@ -43,32 +43,33 @@ func (file *File) fullpath() util.FullPath { return util.NewFullPath(file.dir.FullPath(), file.Name) } -func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error { +func (file *File) Attr(ctx context.Context, attr *fuse.Attr) (err error) { glog.V(4).Infof("file Attr %s, open:%v, existing attr: %+v", file.fullpath(), file.isOpen, attr) - if file.isOpen <= 0 { - if err := file.maybeLoadEntry(ctx); err != nil { + entry := file.entry + if file.isOpen <= 0 || entry == nil { + if entry, err = file.maybeLoadEntry(ctx); err != nil { return err } } attr.Inode = file.fullpath().AsInode() attr.Valid = time.Second - attr.Mode = os.FileMode(file.entry.Attributes.FileMode) - attr.Size = filer.FileSize(file.entry) + attr.Mode = os.FileMode(entry.Attributes.FileMode) + attr.Size = filer.FileSize(entry) if file.isOpen > 0 { - attr.Size = file.entry.Attributes.FileSize + attr.Size = entry.Attributes.FileSize glog.V(4).Infof("file Attr %s, open:%v, size: %d", file.fullpath(), file.isOpen, attr.Size) } - attr.Crtime = time.Unix(file.entry.Attributes.Crtime, 0) - attr.Mtime = time.Unix(file.entry.Attributes.Mtime, 0) - attr.Gid = file.entry.Attributes.Gid - attr.Uid = file.entry.Attributes.Uid + attr.Crtime = time.Unix(entry.Attributes.Crtime, 0) + attr.Mtime = time.Unix(entry.Attributes.Mtime, 0) + attr.Gid = entry.Attributes.Gid + attr.Uid = entry.Attributes.Uid attr.Blocks = attr.Size/blockSize + 1 attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit) - if file.entry.HardLinkCounter > 0 { - attr.Nlink = uint32(file.entry.HardLinkCounter) + if entry.HardLinkCounter > 0 { + attr.Nlink = uint32(entry.HardLinkCounter) } return nil @@ -79,11 +80,12 @@ func (file *File) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp glog.V(4).Infof("file Getxattr %s", file.fullpath()) - if err := file.maybeLoadEntry(ctx); err != nil { + entry, err := file.maybeLoadEntry(ctx) + if err != nil { return err } - return getxattr(file.entry, req, resp) + return getxattr(entry, req, resp) } func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) { @@ -104,7 +106,8 @@ func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *f glog.V(4).Infof("%v file setattr %+v", file.fullpath(), req) - if err := file.maybeLoadEntry(ctx); err != nil { + _, err := file.maybeLoadEntry(ctx) + if err != nil { return err } if file.isOpen > 0 { @@ -141,7 +144,7 @@ func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *f } } file.entry.Chunks = chunks - file.entryViewCache = nil + file.entryViewCache, _ = filer.NonOverlappingVisibleIntervals(filer.LookupFn(file.wfs), chunks) file.reader = nil file.wfs.deleteFileChunks(truncatedChunks) } @@ -186,7 +189,7 @@ func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *f return nil } - return file.saveEntry() + return file.saveEntry(file.entry) } @@ -194,15 +197,16 @@ func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name) - if err := file.maybeLoadEntry(ctx); err != nil { + entry, err := file.maybeLoadEntry(ctx) + if err != nil { return err } - if err := setxattr(file.entry, req); err != nil { + if err := setxattr(entry, req); err != nil { return err } - return file.saveEntry() + return file.saveEntry(entry) } @@ -210,15 +214,16 @@ func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name) - if err := file.maybeLoadEntry(ctx); err != nil { + entry, err := file.maybeLoadEntry(ctx) + if err != nil { return err } - if err := removexattr(file.entry, req); err != nil { + if err := removexattr(entry, req); err != nil { return err } - return file.saveEntry() + return file.saveEntry(entry) } @@ -226,11 +231,12 @@ func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, res glog.V(4).Infof("file Listxattr %s", file.fullpath()) - if err := file.maybeLoadEntry(ctx); err != nil { + entry, err := file.maybeLoadEntry(ctx) + if err != nil { return err } - if err := listxattr(file.entry, req, resp); err != nil { + if err := listxattr(entry, req, resp); err != nil { return err } @@ -252,30 +258,61 @@ func (file *File) Forget() { file.wfs.fsNodeCache.DeleteFsNode(t) } -func (file *File) maybeLoadEntry(ctx context.Context) error { - if (file.entry != nil && len(file.entry.HardLinkId) != 0) || file.isOpen > 0 { - return nil +func (file *File) maybeLoadEntry(ctx context.Context) (entry *filer_pb.Entry, err error) { + entry = file.entry + if file.isOpen > 0 { + return entry, nil } - entry, err := file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name) + if entry != nil { + if len(entry.HardLinkId) == 0 { + // only always reload hard link + return entry, nil + } + } + entry, err = file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name) if err != nil { glog.V(3).Infof("maybeLoadEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err) - return err + return entry, err } if entry != nil { file.setEntry(entry) + } else { + glog.Warningf("maybeLoadEntry not found entry %s/%s: %v", file.dir.FullPath(), file.Name, err) } - return nil + return entry, nil +} + +func lessThan(a, b *filer_pb.FileChunk) bool { + if a.Mtime == b.Mtime { + return a.Fid.FileKey < b.Fid.FileKey + } + return a.Mtime < b.Mtime } func (file *File) addChunks(chunks []*filer_pb.FileChunk) { - sort.Slice(chunks, func(i, j int) bool { - if chunks[i].Mtime == chunks[j].Mtime { - return chunks[i].Fid.FileKey < chunks[j].Fid.FileKey + // find the earliest incoming chunk + newChunks := chunks + earliestChunk := newChunks[0] + for i:=1;i<len(newChunks);i++{ + if lessThan(earliestChunk, newChunks[i]) { + earliestChunk = newChunks[i] + } + } + + // pick out-of-order chunks from existing chunks + for _, chunk := range file.entry.Chunks { + if lessThan(earliestChunk, chunk) { + chunks = append(chunks, chunk) } - return chunks[i].Mtime < chunks[j].Mtime + } + + // sort incoming chunks + sort.Slice(chunks, func(i, j int) bool { + return lessThan(chunks[i], chunks[j]) }) + // add to entry view cache for _, chunk := range chunks { file.entryViewCache = filer.MergeIntoVisibles(file.entryViewCache, chunk) } @@ -284,24 +321,30 @@ func (file *File) addChunks(chunks []*filer_pb.FileChunk) { glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks)) - file.entry.Chunks = append(file.entry.Chunks, chunks...) + file.entry.Chunks = append(file.entry.Chunks, newChunks...) } func (file *File) setEntry(entry *filer_pb.Entry) { file.entry = entry - file.entryViewCache, _ = filer.NonOverlappingVisibleIntervals(filer.LookupFn(file.wfs), file.entry.Chunks) + file.entryViewCache, _ = filer.NonOverlappingVisibleIntervals(filer.LookupFn(file.wfs), entry.Chunks) + file.reader = nil +} + +func (file *File) clearEntry() { + file.entry = nil + file.entryViewCache = nil file.reader = nil } -func (file *File) saveEntry() error { +func (file *File) saveEntry(entry *filer_pb.Entry) error { return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { - file.wfs.mapPbIdFromLocalToFiler(file.entry) - defer file.wfs.mapPbIdFromFilerToLocal(file.entry) + file.wfs.mapPbIdFromLocalToFiler(entry) + defer file.wfs.mapPbIdFromFilerToLocal(entry) request := &filer_pb.UpdateEntryRequest{ Directory: file.dir.FullPath(), - Entry: file.entry, + Entry: entry, Signatures: []int32{file.wfs.signature}, } |
