2022-02-13 13:29:43 +00:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
2022-03-07 01:04:21 +00:00
|
|
|
"syscall"
|
2022-02-13 13:29:43 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-02-25 08:53:27 +00:00
|
|
|
/*
|
|
|
|
What is an inode?
|
|
|
|
If the file is an hardlinked file:
|
|
|
|
use the hardlink id as inode
|
|
|
|
Otherwise:
|
|
|
|
use the file path as inode
|
|
|
|
|
|
|
|
When creating a link:
|
|
|
|
use the original file inode
|
|
|
|
*/
|
2022-02-13 13:29:43 +00:00
|
|
|
|
|
|
|
/** Create a hard link to a file */
|
|
|
|
func (wfs *WFS) Link(cancel <-chan struct{}, in *fuse.LinkIn, name string, out *fuse.EntryOut) (code fuse.Status) {
|
|
|
|
|
2022-03-06 06:10:43 +00:00
|
|
|
if wfs.IsOverQuota {
|
2022-03-07 01:04:21 +00:00
|
|
|
return fuse.Status(syscall.ENOSPC)
|
2022-03-06 06:10:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 13:29:43 +00:00
|
|
|
if s := checkName(name); s != fuse.OK {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-02-18 08:45:43 +00:00
|
|
|
newParentPath, code := wfs.inodeToPath.GetPath(in.NodeId)
|
|
|
|
if code != fuse.OK {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
oldEntryPath, code := wfs.inodeToPath.GetPath(in.Oldnodeid)
|
|
|
|
if code != fuse.OK {
|
|
|
|
return
|
|
|
|
}
|
2022-02-13 13:29:43 +00:00
|
|
|
oldParentPath, _ := oldEntryPath.DirAndName()
|
|
|
|
|
|
|
|
oldEntry, status := wfs.maybeLoadEntry(oldEntryPath)
|
|
|
|
if status != fuse.OK {
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
|
|
|
// update old file to hardlink mode
|
|
|
|
if len(oldEntry.HardLinkId) == 0 {
|
2022-02-25 08:53:27 +00:00
|
|
|
oldEntry.HardLinkId = filer.NewHardLinkId()
|
2022-02-13 13:29:43 +00:00
|
|
|
oldEntry.HardLinkCounter = 1
|
|
|
|
}
|
|
|
|
oldEntry.HardLinkCounter++
|
|
|
|
updateOldEntryRequest := &filer_pb.UpdateEntryRequest{
|
|
|
|
Directory: oldParentPath,
|
|
|
|
Entry: oldEntry,
|
|
|
|
Signatures: []int32{wfs.signature},
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateLink 1.2 : update new file to hardlink mode
|
|
|
|
oldEntry.Attributes.Mtime = time.Now().Unix()
|
|
|
|
request := &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: string(newParentPath),
|
|
|
|
Entry: &filer_pb.Entry{
|
|
|
|
Name: name,
|
|
|
|
IsDirectory: false,
|
|
|
|
Attributes: oldEntry.Attributes,
|
2022-11-15 14:33:36 +00:00
|
|
|
Chunks: oldEntry.GetChunks(),
|
2022-02-13 13:29:43 +00:00
|
|
|
Extended: oldEntry.Extended,
|
|
|
|
HardLinkId: oldEntry.HardLinkId,
|
|
|
|
HardLinkCounter: oldEntry.HardLinkCounter,
|
|
|
|
},
|
|
|
|
Signatures: []int32{wfs.signature},
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply changes to the filer, and also apply to local metaCache
|
|
|
|
err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
|
|
|
|
wfs.mapPbIdFromLocalToFiler(request.Entry)
|
|
|
|
defer wfs.mapPbIdFromFilerToLocal(request.Entry)
|
|
|
|
|
|
|
|
if err := filer_pb.UpdateEntry(client, updateOldEntryRequest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(updateOldEntryRequest.Directory, updateOldEntryRequest.Entry))
|
|
|
|
|
|
|
|
if err := filer_pb.CreateEntry(client, request); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
newEntryPath := newParentPath.Child(name)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("Link %v -> %s: %v", oldEntryPath, newEntryPath, err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:37:07 +00:00
|
|
|
wfs.inodeToPath.AddPath(oldEntry.Attributes.Inode, newEntryPath)
|
2022-02-13 13:29:43 +00:00
|
|
|
|
2022-07-23 20:37:07 +00:00
|
|
|
wfs.outputPbEntry(out, oldEntry.Attributes.Inode, request.Entry)
|
2022-02-13 13:29:43 +00:00
|
|
|
|
|
|
|
return fuse.OK
|
|
|
|
}
|