2022-02-13 19:50:16 +08:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2022-07-29 15:17:28 +08:00
|
|
|
"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
|
|
|
"os"
|
2022-03-07 09:04:21 +08:00
|
|
|
"syscall"
|
2022-02-13 19:50:16 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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 {
|
2022-03-07 09:04:21 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-18 16:45:43 +08:00
|
|
|
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},
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-02-18 16:45:43 +08:00
|
|
|
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
|
|
|
|
}
|