| 1 |
//go:build !windows |
| 2 |
// +build !windows |
| 3 |
|
| 4 |
package desync |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"sync" |
| 12 |
"syscall" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/hanwen/go-fuse/v2/fs" |
| 16 |
"github.com/hanwen/go-fuse/v2/fuse" |
| 17 |
) |
| 18 |
|
| 19 |
type MountFS interface { |
| 20 |
fs.InodeEmbedder |
| 21 |
|
| 22 |
Close() error |
| 23 |
} |
| 24 |
|
| 25 |
// IndexMountFS is used to FUSE mount an index file (as a blob, not an archive). |
| 26 |
// It presents a single file underneath the mountpoint. |
| 27 |
type IndexMountFS struct { |
| 28 |
fs.Inode |
| 29 |
|
| 30 |
FName string // File name in the mountpoint |
| 31 |
Idx Index // Index of the blob |
| 32 |
Store Store |
| 33 |
} |
| 34 |
|
| 35 |
var _ fs.NodeOnAdder = &IndexMountFS{} |
| 36 |
var _ MountFS = &IndexMountFS{} |
| 37 |
|
| 38 |
// NewIndexMountFS initializes a FUSE filesystem mount based on an index and a chunk store. |
| 39 |
func NewIndexMountFS(idx Index, name string, s Store) *IndexMountFS { |
| 40 |
return &IndexMountFS{ |
| 41 |
FName: name, |
| 42 |
Idx: idx, |
| 43 |
Store: s, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// OnAdd is used to build the static filesystem structure at the start of the mount. |
| 48 |
func (r *IndexMountFS) OnAdd(ctx context.Context) { |
| 49 |
n := &indexFile{ |
| 50 |
idx: r.Idx, |
| 51 |
store: r.Store, |
| 52 |
mtime: time.Now(), |
| 53 |
} |
| 54 |
ch := r.NewPersistentInode(ctx, n, fs.StableAttr{Mode: fuse.S_IFREG}) |
| 55 |
r.AddChild(r.FName, ch, false) |
| 56 |
} |
| 57 |
|
| 58 |
func (r *IndexMountFS) Close() error { |
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
var _ fs.NodeGetattrer = &indexFile{} |
| 63 |
var _ fs.NodeOpener = &indexFile{} |
| 64 |
|
| 65 |
type indexFile struct { |
| 66 |
fs.Inode |
| 67 |
|
| 68 |
idx Index // Index of the blob |
| 69 |
store Store |
| 70 |
|
| 71 |
mtime time.Time |
| 72 |
} |
| 73 |
|
| 74 |
func (n *indexFile) Open(ctx context.Context, flags uint32) (fs.FileHandle, uint32, syscall.Errno) { |
| 75 |
fh := newIndexFileHandle(n.idx, n.store) |
| 76 |
return fh, fuse.FOPEN_KEEP_CACHE, fs.OK |
| 77 |
} |
| 78 |
|
| 79 |
func (n *indexFile) Read(ctx context.Context, fh fs.FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { |
| 80 |
f := fh.(*indexFileHandle) |
| 81 |
return f.read(dest, off) |
| 82 |
} |
| 83 |
|
| 84 |
func (n *indexFile) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno { |
| 85 |
out.Mode = fuse.S_IFREG | 0444 |
| 86 |
out.Size = uint64(n.idx.Length()) |
| 87 |
out.Mtime = uint64(n.mtime.Unix()) |
| 88 |
return fs.OK |
| 89 |
} |
| 90 |
|
| 91 |
// indexFileHandle represents a (read-only) file handle on a blob in a FUSE mounted filesystem |
| 92 |
type indexFileHandle struct { |
| 93 |
r *IndexPos |
| 94 |
|
| 95 |
// perhaps not needed, but in case something is trying to use the same filehandle concurrently |
| 96 |
mu sync.Mutex |
| 97 |
} |
| 98 |
|
| 99 |
// NewIndexMountFile initializes a blob file opened in a FUSE mount. |
| 100 |
func newIndexFileHandle(idx Index, s Store) *indexFileHandle { |
| 101 |
return &indexFileHandle{ |
| 102 |
r: NewIndexReadSeeker(idx, s), |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// read from a blob file in a FUSE mount. |
| 107 |
func (f *indexFileHandle) read(dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { |
| 108 |
f.mu.Lock() |
| 109 |
defer f.mu.Unlock() |
| 110 |
if _, err := f.r.Seek(off, io.SeekStart); err != nil { |
| 111 |
fmt.Fprintln(os.Stderr, err) |
| 112 |
return nil, syscall.EIO |
| 113 |
} |
| 114 |
n, err := f.r.Read(dest) |
| 115 |
if err != nil && err != io.EOF { |
| 116 |
fmt.Fprintln(os.Stderr, err) |
| 117 |
return nil, syscall.EIO |
| 118 |
} |
| 119 |
return fuse.ReadResultData(dest[:n]), fs.OK |
| 120 |
} |
| 121 |
|
| 122 |
// MountIndex mounts an index file under a FUSE mount point. The mount will only expose a single |
| 123 |
// blob file as represented by the index. |
| 124 |
func MountIndex(ctx context.Context, idx Index, ifs MountFS, path string, s Store, n int) error { |
| 125 |
opts := &fs.Options{} |
| 126 |
server, err := fs.Mount(path, ifs, opts) |
| 127 |
if err != nil { |
| 128 |
return err |
| 129 |
} |
| 130 |
go func() { // Unmount the server when the context expires |
| 131 |
<-ctx.Done() |
| 132 |
if err := ifs.Close(); err != nil { |
| 133 |
fmt.Fprintln(os.Stderr, "error during unmount:", err) |
| 134 |
} |
| 135 |
server.Unmount() |
| 136 |
}() |
| 137 |
server.Wait() |
| 138 |
return nil |
| 139 |
} |