2018-12-26 06:45:44 +00:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-24 10:06:44 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-12-26 06:45:44 +00:00
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2018-12-26 06:45:44 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2019-01-17 01:17:19 +00:00
|
|
|
"github.com/seaweedfs/fuse"
|
|
|
|
"github.com/seaweedfs/fuse/fs"
|
2018-12-26 06:45:44 +00:00
|
|
|
)
|
|
|
|
|
2020-09-24 10:06:44 +00:00
|
|
|
var _ = fs.NodeLinker(&Dir{})
|
2018-12-26 06:45:44 +00:00
|
|
|
var _ = fs.NodeSymlinker(&Dir{})
|
|
|
|
var _ = fs.NodeReadlinker(&File{})
|
|
|
|
|
2020-09-28 05:38:30 +00:00
|
|
|
const (
|
|
|
|
HARD_LINK_MARKER = '\x01'
|
|
|
|
)
|
|
|
|
|
2020-09-24 10:06:44 +00:00
|
|
|
func (dir *Dir) Link(ctx context.Context, req *fuse.LinkRequest, old fs.Node) (fs.Node, error) {
|
|
|
|
|
|
|
|
oldFile, ok := old.(*File)
|
|
|
|
if !ok {
|
|
|
|
glog.Errorf("old node is not a file: %+v", old)
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(4).Infof("Link: %v/%v -> %v/%v", oldFile.dir.FullPath(), oldFile.Name, dir.FullPath(), req.NewName)
|
|
|
|
|
2020-10-30 08:23:23 +00:00
|
|
|
if _, err := oldFile.maybeLoadEntry(ctx); err != nil {
|
2020-09-24 10:06:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-10 07:08:38 +00:00
|
|
|
oldEntry := oldFile.getEntry()
|
|
|
|
if oldEntry == nil {
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
|
2020-09-24 10:06:44 +00:00
|
|
|
// update old file to hardlink mode
|
2021-03-10 07:08:38 +00:00
|
|
|
if len(oldEntry.HardLinkId) == 0 {
|
|
|
|
oldEntry.HardLinkId = append(util.RandomBytes(16), HARD_LINK_MARKER)
|
|
|
|
oldEntry.HardLinkCounter = 1
|
2020-09-24 16:43:00 +00:00
|
|
|
}
|
2021-03-10 07:08:38 +00:00
|
|
|
oldEntry.HardLinkCounter++
|
2020-09-24 16:43:00 +00:00
|
|
|
updateOldEntryRequest := &filer_pb.UpdateEntryRequest{
|
|
|
|
Directory: oldFile.dir.FullPath(),
|
2021-03-16 09:59:26 +00:00
|
|
|
Entry: oldEntry,
|
2020-09-24 16:43:00 +00:00
|
|
|
Signatures: []int32{dir.wfs.signature},
|
2020-09-24 10:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateLink 1.2 : update new file to hardlink mode
|
|
|
|
request := &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: dir.FullPath(),
|
|
|
|
Entry: &filer_pb.Entry{
|
2020-09-24 16:43:00 +00:00
|
|
|
Name: req.NewName,
|
|
|
|
IsDirectory: false,
|
2021-03-16 09:59:26 +00:00
|
|
|
Attributes: oldEntry.Attributes,
|
2021-03-10 07:08:38 +00:00
|
|
|
Chunks: oldEntry.Chunks,
|
|
|
|
Extended: oldEntry.Extended,
|
|
|
|
HardLinkId: oldEntry.HardLinkId,
|
|
|
|
HardLinkCounter: oldEntry.HardLinkCounter,
|
2020-09-24 10:06:44 +00:00
|
|
|
},
|
|
|
|
Signatures: []int32{dir.wfs.signature},
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply changes to the filer, and also apply to local metaCache
|
|
|
|
err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
|
|
|
|
dir.wfs.mapPbIdFromLocalToFiler(request.Entry)
|
|
|
|
defer dir.wfs.mapPbIdFromFilerToLocal(request.Entry)
|
|
|
|
|
2020-09-24 16:43:00 +00:00
|
|
|
if err := filer_pb.UpdateEntry(client, updateOldEntryRequest); err != nil {
|
|
|
|
glog.V(0).Infof("Link %v/%v -> %s/%s: %v", oldFile.dir.FullPath(), oldFile.Name, dir.FullPath(), req.NewName, err)
|
|
|
|
return fuse.EIO
|
2020-09-24 10:06:44 +00:00
|
|
|
}
|
2020-09-24 16:43:00 +00:00
|
|
|
dir.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(updateOldEntryRequest.Directory, updateOldEntryRequest.Entry))
|
2020-09-24 10:06:44 +00:00
|
|
|
|
|
|
|
if err := filer_pb.CreateEntry(client, request); err != nil {
|
|
|
|
glog.V(0).Infof("Link %v/%v -> %s/%s: %v", oldFile.dir.FullPath(), oldFile.Name, dir.FullPath(), req.NewName, err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-03-02 16:54:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
|
2020-09-24 10:06:44 +00:00
|
|
|
// create new file node
|
|
|
|
newNode := dir.newFile(req.NewName, request.Entry)
|
|
|
|
newFile := newNode.(*File)
|
2020-10-30 08:23:23 +00:00
|
|
|
if _, err := newFile.maybeLoadEntry(ctx); err != nil {
|
2020-09-24 10:06:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newFile, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-26 06:45:44 +00:00
|
|
|
func (dir *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) {
|
|
|
|
|
2020-08-14 07:22:21 +00:00
|
|
|
glog.V(4).Infof("Symlink: %v/%v to %v", dir.FullPath(), req.NewName, req.Target)
|
2018-12-26 06:45:44 +00:00
|
|
|
|
|
|
|
request := &filer_pb.CreateEntryRequest{
|
2020-03-26 07:08:14 +00:00
|
|
|
Directory: dir.FullPath(),
|
2018-12-26 06:45:44 +00:00
|
|
|
Entry: &filer_pb.Entry{
|
|
|
|
Name: req.NewName,
|
|
|
|
IsDirectory: false,
|
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
2019-07-24 07:03:05 +00:00
|
|
|
FileMode: uint32((os.FileMode(0777) | os.ModeSymlink) &^ dir.wfs.option.Umask),
|
2018-12-26 06:45:44 +00:00
|
|
|
Uid: req.Uid,
|
|
|
|
Gid: req.Gid,
|
|
|
|
SymlinkTarget: req.Target,
|
|
|
|
},
|
|
|
|
},
|
2020-08-29 06:48:48 +00:00
|
|
|
Signatures: []int32{dir.wfs.signature},
|
2018-12-26 06:45:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2020-09-03 07:07:22 +00:00
|
|
|
|
|
|
|
dir.wfs.mapPbIdFromLocalToFiler(request.Entry)
|
|
|
|
defer dir.wfs.mapPbIdFromFilerToLocal(request.Entry)
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
if err := filer_pb.CreateEntry(client, request); err != nil {
|
2020-03-26 07:08:14 +00:00
|
|
|
glog.V(0).Infof("symlink %s/%s: %v", dir.FullPath(), req.NewName, err)
|
2018-12-26 06:45:44 +00:00
|
|
|
return fuse.EIO
|
|
|
|
}
|
2020-04-22 22:40:47 +00:00
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
dir.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
|
2020-04-22 22:40:47 +00:00
|
|
|
|
2018-12-26 06:45:44 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
symlink := dir.newFile(req.NewName, request.Entry)
|
|
|
|
|
|
|
|
return symlink, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *File) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
|
|
|
|
|
2020-10-30 08:23:23 +00:00
|
|
|
entry, err := file.maybeLoadEntry(ctx)
|
|
|
|
if err != nil {
|
2018-12-26 06:45:44 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:59:26 +00:00
|
|
|
if os.FileMode(entry.Attributes.FileMode)&os.ModeSymlink == 0 {
|
2018-12-26 06:45:44 +00:00
|
|
|
return "", fuse.Errno(syscall.EINVAL)
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:59:26 +00:00
|
|
|
glog.V(4).Infof("Readlink: %v/%v => %v", file.dir.FullPath(), file.Name, entry.Attributes.SymlinkTarget)
|
2018-12-26 06:45:44 +00:00
|
|
|
|
2021-03-16 09:59:26 +00:00
|
|
|
return entry.Attributes.SymlinkTarget, nil
|
2018-12-26 06:45:44 +00:00
|
|
|
|
|
|
|
}
|