seaweedfs/weed/mount/weedfs_symlink.go

92 lines
2.3 KiB
Go
Raw Normal View History

2022-02-13 19:50:16 +08:00
package mount
import (
"context"
"fmt"
"os"
"syscall"
"time"
2022-02-13 19:50:16 +08:00
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
2022-02-13 19:50:16 +08:00
)
2022-02-13 20:22:02 +08:00
/** Create a symbolic link */
2022-02-13 19:50:16 +08:00
func (wfs *WFS) Symlink(cancel <-chan struct{}, header *fuse.InHeader, target string, name string, out *fuse.EntryOut) (code fuse.Status) {
2022-03-06 14:10:43 +08:00
if wfs.IsOverQuota {
return fuse.Status(syscall.ENOSPC)
2022-03-06 14:10:43 +08:00
}
2022-02-13 19:50:16 +08:00
if s := checkName(name); s != fuse.OK {
return s
}
dirPath, code := wfs.inodeToPath.GetPath(header.NodeId)
if code != fuse.OK {
return
}
2022-02-13 19:50:16 +08:00
entryFullPath := dirPath.Child(name)
request := &filer_pb.CreateEntryRequest{
Directory: string(dirPath),
Entry: &filer_pb.Entry{
Name: name,
IsDirectory: false,
Attributes: &filer_pb.FuseAttributes{
Mtime: time.Now().Unix(),
Crtime: time.Now().Unix(),
2022-02-18 16:47:02 +08:00
FileMode: uint32(os.FileMode(0777) | os.ModeSymlink),
2022-02-13 19:50:16 +08:00
Uid: header.Uid,
Gid: header.Gid,
SymlinkTarget: target,
},
},
Signatures: []int32{wfs.signature},
SkipCheckParentDirectory: true,
2022-02-13 19:50:16 +08:00
}
err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
wfs.mapPbIdFromLocalToFiler(request.Entry)
defer wfs.mapPbIdFromFilerToLocal(request.Entry)
if err := filer_pb.CreateEntry(client, request); err != nil {
return fmt.Errorf("symlink %s: %v", entryFullPath, err)
}
wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
return nil
})
if err != nil {
glog.V(0).Infof("Symlink %s => %s: %v", entryFullPath, target, err)
return fuse.EIO
}
2022-03-14 15:03:29 +08:00
inode := wfs.inodeToPath.Lookup(entryFullPath, request.Entry.Attributes.Crtime, false, false, 0, true)
2022-02-13 19:50:16 +08:00
wfs.outputPbEntry(out, inode, request.Entry)
return fuse.OK
}
func (wfs *WFS) Readlink(cancel <-chan struct{}, header *fuse.InHeader) (out []byte, code fuse.Status) {
entryFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
if code != fuse.OK {
return
}
2022-02-13 19:50:16 +08:00
entry, status := wfs.maybeLoadEntry(entryFullPath)
if status != fuse.OK {
return nil, status
}
if os.FileMode(entry.Attributes.FileMode)&os.ModeSymlink == 0 {
return nil, fuse.EINVAL
}
return []byte(entry.Attributes.SymlinkTarget), fuse.OK
}