| 1 |
//go:build !windows |
| 2 |
// +build !windows |
| 3 |
|
| 4 |
package desync |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"io" |
| 9 |
"syscall" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/hanwen/go-fuse/v2/fs" |
| 13 |
"github.com/hanwen/go-fuse/v2/fuse" |
| 14 |
) |
| 15 |
|
| 16 |
// SparseMountFS is used to FUSE mount an index file (as a blob, not an archive). |
| 17 |
// It uses a (local) sparse file as cache to improve performance. Every chunk that |
| 18 |
// is being read is written into the sparse file |
| 19 |
type SparseMountFS struct { |
| 20 |
fs.Inode |
| 21 |
|
| 22 |
FName string // File name in the mountpoint |
| 23 |
sf *SparseFile |
| 24 |
} |
| 25 |
|
| 26 |
var _ fs.NodeOnAdder = &SparseMountFS{} |
| 27 |
var _ MountFS = &SparseMountFS{} |
| 28 |
|
| 29 |
// NewSparseMountFS initializes a FUSE filesystem mount based on an index, a sparse file and a chunk store. |
| 30 |
func NewSparseMountFS(idx Index, name string, s Store, sparseFile string, opt SparseFileOptions) (*SparseMountFS, error) { |
| 31 |
sf, err := NewSparseFile(sparseFile, idx, s, opt) |
| 32 |
if err != nil { |
| 33 |
return nil, err |
| 34 |
} |
| 35 |
return &SparseMountFS{ |
| 36 |
FName: name, |
| 37 |
sf: sf, |
| 38 |
}, err |
| 39 |
} |
| 40 |
|
| 41 |
// OnAdd is used to build the static filesystem structure at the start of the mount. |
| 42 |
func (r *SparseMountFS) OnAdd(ctx context.Context) { |
| 43 |
n := &sparseIndexFile{ |
| 44 |
sf: r.sf, |
| 45 |
mtime: time.Now(), |
| 46 |
size: r.sf.Length(), |
| 47 |
} |
| 48 |
ch := r.NewPersistentInode(ctx, n, fs.StableAttr{Mode: fuse.S_IFREG}) |
| 49 |
r.AddChild(r.FName, ch, false) |
| 50 |
} |
| 51 |
|
| 52 |
// Save the state of the sparse file. |
| 53 |
func (r *SparseMountFS) WriteState() error { |
| 54 |
return r.sf.WriteState() |
| 55 |
} |
| 56 |
|
| 57 |
// Close the sparse file and save its state. |
| 58 |
func (r *SparseMountFS) Close() error { |
| 59 |
return r.sf.WriteState() |
| 60 |
} |
| 61 |
|
| 62 |
var _ fs.NodeGetattrer = &indexFile{} |
| 63 |
var _ fs.NodeOpener = &indexFile{} |
| 64 |
|
| 65 |
type sparseIndexFile struct { |
| 66 |
fs.Inode |
| 67 |
sf *SparseFile |
| 68 |
size int64 |
| 69 |
mtime time.Time |
| 70 |
} |
| 71 |
|
| 72 |
func (n *sparseIndexFile) Open(ctx context.Context, flags uint32) (fs.FileHandle, uint32, syscall.Errno) { |
| 73 |
fh, err := n.sf.Open() |
| 74 |
if err != nil { |
| 75 |
Log.WithError(err).Error("failed to open sparse file") |
| 76 |
return fh, fuse.FOPEN_KEEP_CACHE, syscall.EIO |
| 77 |
} |
| 78 |
return fh, fuse.FOPEN_KEEP_CACHE, fs.OK |
| 79 |
} |
| 80 |
|
| 81 |
func (n *sparseIndexFile) Read(ctx context.Context, fh fs.FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { |
| 82 |
f := fh.(*SparseFileHandle) |
| 83 |
length, err := f.ReadAt(dest, off) |
| 84 |
if err != nil { |
| 85 |
if err == io.EOF { |
| 86 |
return fuse.ReadResultData(dest[:length]), fs.OK |
| 87 |
} |
| 88 |
Log.WithError(err).Error("failed to read sparse file") |
| 89 |
return fuse.ReadResultData(dest[:length]), syscall.EIO |
| 90 |
} |
| 91 |
return fuse.ReadResultData(dest[:length]), fs.OK |
| 92 |
} |
| 93 |
|
| 94 |
func (n *sparseIndexFile) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno { |
| 95 |
out.Mode = fuse.S_IFREG | 0444 |
| 96 |
out.Size = uint64(n.size) |
| 97 |
out.Mtime = uint64(n.mtime.Unix()) |
| 98 |
return fs.OK |
| 99 |
} |