2022-03-28 20:23:47 +00:00
|
|
|
//go:build darwin || freebsd || linux
|
2020-05-12 09:30:41 +00:00
|
|
|
// +build darwin freebsd linux
|
2015-08-16 20:27:07 +00:00
|
|
|
|
2015-07-19 12:28:11 +00:00
|
|
|
package fuse
|
|
|
|
|
|
|
|
import (
|
2020-10-05 21:09:52 +00:00
|
|
|
"context"
|
2020-06-14 18:48:52 +00:00
|
|
|
"sort"
|
|
|
|
|
2020-06-14 11:47:13 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2018-01-12 06:20:12 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2022-11-12 13:52:37 +00:00
|
|
|
"github.com/anacrolix/fuse"
|
|
|
|
"github.com/anacrolix/fuse/fs"
|
2015-07-19 12:28:11 +00:00
|
|
|
)
|
|
|
|
|
2016-02-14 20:24:02 +00:00
|
|
|
// The default block size to report in stat
|
|
|
|
const blockSize = 512
|
|
|
|
|
2020-06-14 11:47:13 +00:00
|
|
|
// Statically ensure that *file and *openFile implement the given interfaces
|
|
|
|
var _ = fs.HandleReader(&openFile{})
|
|
|
|
var _ = fs.NodeListxattrer(&file{})
|
|
|
|
var _ = fs.NodeGetxattrer(&file{})
|
|
|
|
var _ = fs.NodeOpener(&file{})
|
2015-07-19 12:28:11 +00:00
|
|
|
|
|
|
|
type file struct {
|
2017-06-18 13:11:32 +00:00
|
|
|
root *Root
|
|
|
|
node *restic.Node
|
|
|
|
inode uint64
|
2020-06-14 11:47:13 +00:00
|
|
|
}
|
2015-07-19 12:28:11 +00:00
|
|
|
|
2020-06-14 11:47:13 +00:00
|
|
|
type openFile struct {
|
|
|
|
file
|
2020-06-14 18:48:52 +00:00
|
|
|
// cumsize[i] holds the cumulative size of blobs[:i].
|
|
|
|
cumsize []uint64
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:29:33 +00:00
|
|
|
func newFile(root *Root, inode uint64, node *restic.Node) (fusefile *file, err error) {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("create new file for %v with %d blobs", node.Name, len(node.Content))
|
2015-07-19 12:28:11 +00:00
|
|
|
return &file{
|
2017-06-18 14:28:55 +00:00
|
|
|
inode: inode,
|
2017-06-18 13:11:32 +00:00
|
|
|
root: root,
|
|
|
|
node: node,
|
2015-07-19 12:28:11 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:18:09 +00:00
|
|
|
func (f *file) Attr(_ context.Context, a *fuse.Attr) error {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("Attr(%v)", f.node.Name)
|
2017-06-18 13:11:32 +00:00
|
|
|
a.Inode = f.inode
|
2015-07-19 12:28:11 +00:00
|
|
|
a.Mode = f.node.Mode
|
|
|
|
a.Size = f.node.Size
|
2023-03-07 21:12:08 +00:00
|
|
|
a.Blocks = (f.node.Size + blockSize - 1) / blockSize
|
2016-02-14 20:24:02 +00:00
|
|
|
a.BlockSize = blockSize
|
2017-02-10 20:16:48 +00:00
|
|
|
a.Nlink = uint32(f.node.Links)
|
2015-07-26 18:41:29 +00:00
|
|
|
|
2017-06-18 13:11:32 +00:00
|
|
|
if !f.root.cfg.OwnerIsRoot {
|
2015-07-26 18:41:29 +00:00
|
|
|
a.Uid = f.node.UID
|
|
|
|
a.Gid = f.node.GID
|
|
|
|
}
|
2015-07-21 20:11:30 +00:00
|
|
|
a.Atime = f.node.AccessTime
|
|
|
|
a.Ctime = f.node.ChangeTime
|
|
|
|
a.Mtime = f.node.ModTime
|
2017-02-11 20:50:03 +00:00
|
|
|
|
2015-07-19 12:28:11 +00:00
|
|
|
return nil
|
2017-02-11 20:50:03 +00:00
|
|
|
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 17:18:09 +00:00
|
|
|
func (f *file) Open(_ context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
|
2020-06-14 11:47:13 +00:00
|
|
|
debug.Log("open file %v with %d blobs", f.node.Name, len(f.node.Content))
|
|
|
|
|
|
|
|
var bytes uint64
|
|
|
|
cumsize := make([]uint64, 1+len(f.node.Content))
|
|
|
|
for i, id := range f.node.Content {
|
|
|
|
size, found := f.root.repo.LookupBlobSize(id, restic.DataBlob)
|
|
|
|
if !found {
|
|
|
|
return nil, errors.Errorf("id %v not found in repository", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes += uint64(size)
|
|
|
|
cumsize[i+1] = bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
var of = openFile{file: *f}
|
|
|
|
|
|
|
|
if bytes != f.node.Size {
|
|
|
|
debug.Log("sizes do not match: node.Size %v != size %v, using real size", f.node.Size, bytes)
|
|
|
|
// Make a copy of the node with correct size
|
|
|
|
nodenew := *f.node
|
|
|
|
nodenew.Size = bytes
|
|
|
|
of.file.node = &nodenew
|
|
|
|
}
|
|
|
|
of.cumsize = cumsize
|
|
|
|
|
|
|
|
return &of, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *openFile) getBlobAt(ctx context.Context, i int) (blob []byte, err error) {
|
2020-06-17 10:17:55 +00:00
|
|
|
|
2021-09-11 11:26:10 +00:00
|
|
|
blob, ok := f.root.blobCache.Get(f.node.Content[i])
|
2020-06-17 10:17:55 +00:00
|
|
|
if ok {
|
|
|
|
return blob, nil
|
2017-01-24 11:38:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 15:41:22 +00:00
|
|
|
blob, err = f.root.repo.LoadBlob(ctx, restic.DataBlob, f.node.Content[i], nil)
|
2015-07-26 12:25:01 +00:00
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("LoadBlob(%v, %v) failed: %v", f.node.Name, f.node.Content[i], err)
|
2022-08-19 18:26:35 +00:00
|
|
|
return nil, unwrapCtxCanceled(err)
|
2015-07-26 12:25:01 +00:00
|
|
|
}
|
2020-06-17 10:17:55 +00:00
|
|
|
|
2021-09-11 11:26:10 +00:00
|
|
|
f.root.blobCache.Add(f.node.Content[i], blob)
|
2015-07-26 12:25:01 +00:00
|
|
|
|
2020-03-10 15:41:22 +00:00
|
|
|
return blob, nil
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 11:47:13 +00:00
|
|
|
func (f *openFile) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("Read(%v, %v, %v), file size %v", f.node.Name, req.Size, req.Offset, f.node.Size)
|
2020-06-14 18:48:52 +00:00
|
|
|
offset := uint64(req.Offset)
|
2015-07-19 12:28:11 +00:00
|
|
|
|
2020-06-14 11:47:13 +00:00
|
|
|
// as stated in https://godoc.org/bazil.org/fuse/fs#HandleReader there
|
|
|
|
// is no need to check if offset > size
|
2016-07-29 18:55:09 +00:00
|
|
|
|
2016-07-29 19:18:32 +00:00
|
|
|
// handle special case: file is empty
|
|
|
|
if f.node.Size == 0 {
|
|
|
|
resp.Data = resp.Data[:0]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-19 12:28:11 +00:00
|
|
|
// Skip blobs before the offset
|
2020-06-14 18:48:52 +00:00
|
|
|
startContent := -1 + sort.Search(len(f.cumsize), func(i int) bool {
|
|
|
|
return f.cumsize[i] > offset
|
|
|
|
})
|
|
|
|
offset -= f.cumsize[startContent]
|
2015-07-19 12:28:11 +00:00
|
|
|
|
2015-07-26 12:25:01 +00:00
|
|
|
dst := resp.Data[0:req.Size]
|
|
|
|
readBytes := 0
|
|
|
|
remainingBytes := req.Size
|
2020-06-14 11:47:13 +00:00
|
|
|
|
|
|
|
// The documentation of bazil/fuse actually says that synchronization is
|
|
|
|
// required (see https://godoc.org/bazil.org/fuse#hdr-Service_Methods):
|
|
|
|
//
|
|
|
|
// Multiple goroutines may call service methods simultaneously;
|
|
|
|
// the methods being called are responsible for appropriate synchronization.
|
|
|
|
//
|
|
|
|
// However, no lock needed here as getBlobAt can be called conurrently
|
|
|
|
// (blobCache has it's own locking)
|
2020-06-14 18:48:52 +00:00
|
|
|
for i := startContent; remainingBytes > 0 && i < len(f.cumsize)-1; i++ {
|
2017-06-04 09:16:55 +00:00
|
|
|
blob, err := f.getBlobAt(ctx, i)
|
2015-07-19 12:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-26 12:25:01 +00:00
|
|
|
if offset > 0 {
|
2017-05-16 23:28:39 +00:00
|
|
|
blob = blob[offset:]
|
2015-07-26 12:25:01 +00:00
|
|
|
offset = 0
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
2015-07-26 12:25:01 +00:00
|
|
|
|
|
|
|
copied := copy(dst, blob)
|
|
|
|
remainingBytes -= copied
|
|
|
|
readBytes += copied
|
|
|
|
|
|
|
|
dst = dst[copied:]
|
|
|
|
}
|
|
|
|
resp.Data = resp.Data[:readBytes]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:18:09 +00:00
|
|
|
func (f *file) Listxattr(_ context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
|
2017-02-02 11:23:13 +00:00
|
|
|
debug.Log("Listxattr(%v, %v)", f.node.Name, req.Size)
|
|
|
|
for _, attr := range f.node.ExtendedAttributes {
|
|
|
|
resp.Append(attr.Name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:18:09 +00:00
|
|
|
func (f *file) Getxattr(_ context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
|
2017-02-02 11:23:13 +00:00
|
|
|
debug.Log("Getxattr(%v, %v, %v)", f.node.Name, req.Name, req.Size)
|
|
|
|
attrval := f.node.GetExtendedAttribute(req.Name)
|
|
|
|
if attrval != nil {
|
|
|
|
resp.Xattr = attrval
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fuse.ErrNoXattr
|
|
|
|
}
|