From e85ca10a1a426e9808024f65dd0921b241296796 Mon Sep 17 00:00:00 2001 From: chrislu Date: Sun, 13 Feb 2022 01:34:19 -0800 Subject: [PATCH] add mkdir --- weed/mount/weedfs_attr.go | 10 ++++- weed/mount/weedfs_dir_lookup.go | 6 ++- weed/mount/weedfs_dir_mkrm.go | 72 +++++++++++++++++++++++++++++++++ weed/mount/weedfs_dir_read.go | 2 +- weed/mount/wfs_save.go | 8 ++++ 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 weed/mount/weedfs_dir_mkrm.go diff --git a/weed/mount/weedfs_attr.go b/weed/mount/weedfs_attr.go index 4354baa86..8dbf65fe8 100644 --- a/weed/mount/weedfs_attr.go +++ b/weed/mount/weedfs_attr.go @@ -109,7 +109,15 @@ func (wfs *WFS) setAttrByFilerEntry(out *fuse.Attr, inode uint64, entry *filer.E out.Gid = entry.Attr.Gid } -func (wfs *WFS) outputEntry(out *fuse.EntryOut, inode uint64, entry *filer.Entry) { +func (wfs *WFS) outputPbEntry(out *fuse.EntryOut, inode uint64, entry *filer_pb.Entry) { + out.NodeId = inode + out.Generation = 1 + out.EntryValid = 1 + out.AttrValid = 1 + wfs.setAttrByPbEntry(&out.Attr, inode, entry) +} + +func (wfs *WFS) outputFilerEntry(out *fuse.EntryOut, inode uint64, entry *filer.Entry) { out.NodeId = inode out.Generation = 1 out.EntryValid = 1 diff --git a/weed/mount/weedfs_dir_lookup.go b/weed/mount/weedfs_dir_lookup.go index b74948f65..477cfad0a 100644 --- a/weed/mount/weedfs_dir_lookup.go +++ b/weed/mount/weedfs_dir_lookup.go @@ -16,6 +16,10 @@ import ( func (wfs *WFS) Lookup(cancel <-chan struct{}, header *fuse.InHeader, name string, out *fuse.EntryOut) (code fuse.Status) { + if s := checkName(name); s != fuse.OK { + return s + } + dirPath := wfs.inodeToPath.GetPath(header.NodeId) fullFilePath := dirPath.Child(name) @@ -48,7 +52,7 @@ func (wfs *WFS) Lookup(cancel <-chan struct{}, header *fuse.InHeader, name strin inode := wfs.inodeToPath.GetInode(fullFilePath) - wfs.outputEntry(out, inode, localEntry) + wfs.outputFilerEntry(out, inode, localEntry) return fuse.OK diff --git a/weed/mount/weedfs_dir_mkrm.go b/weed/mount/weedfs_dir_mkrm.go new file mode 100644 index 000000000..670275915 --- /dev/null +++ b/weed/mount/weedfs_dir_mkrm.go @@ -0,0 +1,72 @@ +package mount + +import ( + "context" + "fmt" + "github.com/chrislusf/seaweedfs/weed/filer" + "github.com/chrislusf/seaweedfs/weed/glog" + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" + "github.com/hanwen/go-fuse/v2/fuse" + "os" + "time" +) + +func (wfs *WFS) Mkdir(cancel <-chan struct{}, in *fuse.MkdirIn, name string, out *fuse.EntryOut) (code fuse.Status) { + + if s := checkName(name); s != fuse.OK { + return s + } + + newEntry := &filer_pb.Entry{ + Name: name, + IsDirectory: true, + Attributes: &filer_pb.FuseAttributes{ + Mtime: time.Now().Unix(), + Crtime: time.Now().Unix(), + FileMode: uint32(os.ModeDir) | in.Mode&^uint32(wfs.option.Umask), + Uid: in.Uid, + Gid: in.Gid, + }, + } + + dirFullPath := wfs.inodeToPath.GetPath(in.NodeId) + + entryFullPath := dirFullPath.Child(name) + + err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { + + wfs.mapPbIdFromLocalToFiler(newEntry) + defer wfs.mapPbIdFromFilerToLocal(newEntry) + + request := &filer_pb.CreateEntryRequest{ + Directory: string(dirFullPath), + Entry: newEntry, + Signatures: []int32{wfs.signature}, + } + + glog.V(1).Infof("mkdir: %v", request) + if err := filer_pb.CreateEntry(client, request); err != nil { + glog.V(0).Infof("mkdir %s: %v", entryFullPath, err) + return err + } + + if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil { + return fmt.Errorf("local mkdir dir %s: %v", entryFullPath, err) + } + + return nil + }) + + glog.V(0).Infof("mkdir %s: %v", entryFullPath, err) + + if err != nil { + return fuse.EIO + } + + inode := wfs.inodeToPath.GetInode(entryFullPath) + + wfs.outputPbEntry(out, inode, newEntry) + + return fuse.OK + +} diff --git a/weed/mount/weedfs_dir_read.go b/weed/mount/weedfs_dir_read.go index 40e164fc9..96fd36ffe 100644 --- a/weed/mount/weedfs_dir_read.go +++ b/weed/mount/weedfs_dir_read.go @@ -73,7 +73,7 @@ func (wfs *WFS) doReadDirectory(input *fuse.ReadIn, out *fuse.DirEntryList, isPl if entryOut == nil { return false } - wfs.outputEntry(entryOut, inode, entry) + wfs.outputFilerEntry(entryOut, inode, entry) } return true } diff --git a/weed/mount/wfs_save.go b/weed/mount/wfs_save.go index 240c010d8..0cac30453 100644 --- a/weed/mount/wfs_save.go +++ b/weed/mount/wfs_save.go @@ -8,6 +8,7 @@ import ( "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/chrislusf/seaweedfs/weed/util" "github.com/hanwen/go-fuse/v2/fuse" + "syscall" ) func (wfs *WFS) saveEntry(path util.FullPath, entry *filer_pb.Entry) (code fuse.Status) { @@ -57,3 +58,10 @@ func (wfs *WFS) mapPbIdFromLocalToFiler(entry *filer_pb.Entry) { } entry.Attributes.Uid, entry.Attributes.Gid = wfs.option.UidGidMapper.LocalToFiler(entry.Attributes.Uid, entry.Attributes.Gid) } + +func checkName(name string) fuse.Status { + if len(name) >= 256 { + return fuse.Status(syscall.ENAMETOOLONG) + } + return fuse.OK +}