2015-07-18 21:19:50 +00:00
|
|
|
// +build !openbsd
|
|
|
|
|
2015-04-07 19:10:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-07-19 13:14:47 +00:00
|
|
|
"sync"
|
2015-04-07 19:10:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/restic/restic"
|
|
|
|
"github.com/restic/restic/backend"
|
|
|
|
"github.com/restic/restic/crypto"
|
|
|
|
"github.com/restic/restic/pack"
|
|
|
|
"github.com/restic/restic/repository"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
"bazil.org/fuse/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CmdMount struct {
|
|
|
|
global *GlobalOptions
|
2015-07-18 19:31:34 +00:00
|
|
|
ready chan struct{}
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
_, err := parser.AddCommand("mount",
|
|
|
|
"mount a repository",
|
|
|
|
"The mount command mounts a repository read-only to a given directory",
|
2015-07-18 19:31:34 +00:00
|
|
|
&CmdMount{
|
|
|
|
global: &globalOpts,
|
|
|
|
ready: make(chan struct{}, 1),
|
|
|
|
})
|
2015-04-07 19:10:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd CmdMount) Usage() string {
|
|
|
|
return "MOUNTPOINT"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd CmdMount) Execute(args []string) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage())
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := cmd.global.OpenRepository()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.LoadIndex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mountpoint := args[0]
|
2015-07-19 12:08:34 +00:00
|
|
|
if _, err := os.Stat(mountpoint); os.IsNotExist(err) {
|
|
|
|
cmd.global.Verbosef("Mountpoint %s doesn't exist, creating it\n", mountpoint)
|
|
|
|
err = os.Mkdir(mountpoint, os.ModeDir|0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c, err := fuse.Mount(
|
|
|
|
mountpoint,
|
|
|
|
fuse.ReadOnly(),
|
|
|
|
fuse.FSName("restic"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := fs.Tree{}
|
2015-07-18 19:49:08 +00:00
|
|
|
root.Add("snapshots", &snapshots{
|
|
|
|
repo: repo,
|
2015-07-19 13:14:47 +00:00
|
|
|
knownSnapshots: make(map[string]snapshotWithId),
|
2015-07-18 19:49:08 +00:00
|
|
|
})
|
2015-04-07 19:10:53 +00:00
|
|
|
|
2015-07-19 12:08:34 +00:00
|
|
|
cmd.global.Printf("Now serving %s at %s\n", repo.Backend().Location(), mountpoint)
|
|
|
|
cmd.global.Printf("Don't forget to umount after quitting!\n")
|
2015-04-07 19:10:53 +00:00
|
|
|
|
2015-07-18 19:31:34 +00:00
|
|
|
cmd.ready <- struct{}{}
|
|
|
|
|
2015-04-07 19:10:53 +00:00
|
|
|
err = fs.Serve(c, &root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
<-c.Ready
|
|
|
|
return c.MountError
|
|
|
|
}
|
|
|
|
|
2015-07-19 13:14:47 +00:00
|
|
|
type snapshotWithId struct {
|
|
|
|
*restic.Snapshot
|
|
|
|
backend.ID
|
|
|
|
}
|
|
|
|
|
2015-07-18 19:52:10 +00:00
|
|
|
// These lines statically ensure that a *snapshots implement the given
|
|
|
|
// interfaces; a misplaced refactoring of the implementation that breaks
|
|
|
|
// the interface will be catched by the compiler
|
2015-04-07 19:10:53 +00:00
|
|
|
var _ = fs.HandleReadDirAller(&snapshots{})
|
|
|
|
var _ = fs.NodeStringLookuper(&snapshots{})
|
|
|
|
|
|
|
|
type snapshots struct {
|
|
|
|
repo *repository.Repository
|
2015-07-18 19:49:08 +00:00
|
|
|
|
2015-07-19 13:14:47 +00:00
|
|
|
// knownSnapshots maps snapshot timestamp to the snapshot
|
|
|
|
sync.RWMutex
|
|
|
|
knownSnapshots map[string]snapshotWithId
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 12:08:34 +00:00
|
|
|
func (sn *snapshots) Attr(ctx context.Context, attr *fuse.Attr) error {
|
|
|
|
attr.Inode = 0
|
|
|
|
attr.Mode = os.ModeDir | 0555
|
2015-07-18 17:07:59 +00:00
|
|
|
return nil
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 13:14:47 +00:00
|
|
|
func (sn *snapshots) updateCache(ctx context.Context) error {
|
|
|
|
sn.Lock()
|
|
|
|
defer sn.Unlock()
|
|
|
|
|
2015-07-18 19:31:11 +00:00
|
|
|
for id := range sn.repo.List(backend.Snapshot, ctx.Done()) {
|
2015-04-07 19:10:53 +00:00
|
|
|
snapshot, err := restic.LoadSnapshot(sn.repo, id)
|
|
|
|
if err != nil {
|
2015-07-19 13:14:47 +00:00
|
|
|
return err
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
2015-07-19 13:14:47 +00:00
|
|
|
sn.knownSnapshots[snapshot.Time.Format(time.RFC3339)] = snapshotWithId{snapshot, id}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sn *snapshots) get(name string) (snapshot snapshotWithId, ok bool) {
|
|
|
|
sn.Lock()
|
|
|
|
snapshot, ok = sn.knownSnapshots[name]
|
|
|
|
sn.Unlock()
|
|
|
|
return snapshot, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sn *snapshots) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
|
|
|
|
err := sn.updateCache(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sn.RLock()
|
|
|
|
defer sn.RUnlock()
|
|
|
|
|
|
|
|
ret := make([]fuse.Dirent, 0)
|
|
|
|
for _, snapshot := range sn.knownSnapshots {
|
2015-04-07 19:10:53 +00:00
|
|
|
ret = append(ret, fuse.Dirent{
|
2015-07-19 13:14:47 +00:00
|
|
|
Inode: binary.BigEndian.Uint64(snapshot.ID[:8]),
|
2015-04-07 19:10:53 +00:00
|
|
|
Type: fuse.DT_Dir,
|
|
|
|
Name: snapshot.Time.Format(time.RFC3339),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sn *snapshots) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
2015-07-19 13:14:47 +00:00
|
|
|
snapshot, ok := sn.get(name)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// We don't know about it, update the cache
|
|
|
|
err := sn.updateCache(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
snapshot, ok = sn.get(name)
|
|
|
|
if !ok {
|
|
|
|
// We still don't know about it, this time it really doesn't exist
|
|
|
|
return nil, fuse.ENOENT
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-18 19:49:08 +00:00
|
|
|
|
2015-07-19 12:10:19 +00:00
|
|
|
return newDirFromSnapshot(sn.repo, snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Statically ensure that *dir implement those interface
|
|
|
|
var _ = fs.HandleReadDirAller(&dir{})
|
|
|
|
var _ = fs.NodeStringLookuper(&dir{})
|
|
|
|
|
|
|
|
type dir struct {
|
|
|
|
repo *repository.Repository
|
|
|
|
children map[string]*restic.Node
|
|
|
|
inode uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDir(repo *repository.Repository, node *restic.Node) (*dir, error) {
|
|
|
|
tree, err := restic.LoadTree(repo, node.Subtree)
|
2015-07-18 19:49:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-19 12:10:19 +00:00
|
|
|
children := make(map[string]*restic.Node)
|
|
|
|
for _, child := range tree.Nodes {
|
|
|
|
children[child.Name] = child
|
|
|
|
}
|
|
|
|
|
2015-07-18 19:49:08 +00:00
|
|
|
return &dir{
|
2015-07-19 12:10:19 +00:00
|
|
|
repo: repo,
|
|
|
|
children: children,
|
|
|
|
inode: node.Inode,
|
2015-07-18 19:49:08 +00:00
|
|
|
}, nil
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 12:10:19 +00:00
|
|
|
func newDirFromSnapshot(repo *repository.Repository, snapshot snapshotWithId) (*dir, error) {
|
|
|
|
tree, err := restic.LoadTree(repo, snapshot.Tree)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
children := make(map[string]*restic.Node)
|
|
|
|
for _, node := range tree.Nodes {
|
|
|
|
children[node.Name] = node
|
|
|
|
}
|
2015-04-07 19:10:53 +00:00
|
|
|
|
2015-07-19 12:10:19 +00:00
|
|
|
return &dir{
|
|
|
|
repo: repo,
|
|
|
|
children: children,
|
|
|
|
inode: binary.BigEndian.Uint64(snapshot.ID),
|
|
|
|
}, nil
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 17:07:59 +00:00
|
|
|
func (d *dir) Attr(ctx context.Context, a *fuse.Attr) error {
|
2015-04-07 19:10:53 +00:00
|
|
|
a.Inode = d.inode
|
|
|
|
a.Mode = os.ModeDir | 0555
|
2015-07-18 17:07:59 +00:00
|
|
|
return nil
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
|
2015-07-19 12:10:19 +00:00
|
|
|
ret := make([]fuse.Dirent, 0, len(d.children))
|
2015-04-07 19:10:53 +00:00
|
|
|
|
2015-07-19 12:10:19 +00:00
|
|
|
for _, node := range d.children {
|
2015-04-07 19:10:53 +00:00
|
|
|
var typ fuse.DirentType
|
|
|
|
switch {
|
|
|
|
case node.Mode.IsDir():
|
|
|
|
typ = fuse.DT_Dir
|
|
|
|
case node.Mode.IsRegular():
|
|
|
|
typ = fuse.DT_File
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, fuse.Dirent{
|
|
|
|
Inode: node.Inode,
|
|
|
|
Type: typ,
|
|
|
|
Name: node.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
2015-07-19 12:10:19 +00:00
|
|
|
child, ok := d.children[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case child.Mode.IsDir():
|
|
|
|
return newDir(d.repo, child)
|
|
|
|
case child.Mode.IsRegular():
|
|
|
|
return newFile(d.repo, child)
|
|
|
|
default:
|
|
|
|
return nil, fuse.ENOENT
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-18 19:52:10 +00:00
|
|
|
// Statically ensure that *file implements the given interface
|
2015-04-07 19:10:53 +00:00
|
|
|
var _ = fs.HandleReader(&file{})
|
|
|
|
|
|
|
|
type file struct {
|
|
|
|
repo *repository.Repository
|
|
|
|
node *restic.Node
|
|
|
|
|
|
|
|
sizes []uint32
|
2015-07-19 12:11:33 +00:00
|
|
|
blobs [][]byte
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 12:10:19 +00:00
|
|
|
func newFile(repo *repository.Repository, node *restic.Node) (*file, error) {
|
2015-04-07 19:10:53 +00:00
|
|
|
sizes := make([]uint32, len(node.Content))
|
2015-07-19 12:08:34 +00:00
|
|
|
for i, blobId := range node.Content {
|
|
|
|
_, _, _, length, err := repo.Index().Lookup(blobId)
|
2015-04-07 19:10:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sizes[i] = uint32(length) - crypto.Extension
|
|
|
|
}
|
|
|
|
|
|
|
|
return &file{
|
2015-07-19 12:11:33 +00:00
|
|
|
repo: repo,
|
|
|
|
node: node,
|
|
|
|
sizes: sizes,
|
|
|
|
blobs: make([][]byte, len(node.Content)),
|
2015-04-07 19:10:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-07-18 17:07:59 +00:00
|
|
|
func (f *file) Attr(ctx context.Context, a *fuse.Attr) error {
|
2015-04-07 19:10:53 +00:00
|
|
|
a.Inode = f.node.Inode
|
|
|
|
a.Mode = f.node.Mode
|
|
|
|
a.Size = f.node.Size
|
2015-07-18 17:07:59 +00:00
|
|
|
return nil
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 12:11:33 +00:00
|
|
|
func (f *file) getBlobAt(i int) (blob []byte, err error) {
|
|
|
|
if f.blobs[i] != nil {
|
|
|
|
blob = f.blobs[i]
|
|
|
|
} else {
|
|
|
|
blob, err = f.repo.LoadBlob(pack.Data, f.node.Content[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f.blobs[i] = blob
|
|
|
|
}
|
|
|
|
|
|
|
|
return blob, nil
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:10:53 +00:00
|
|
|
func (f *file) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
|
|
|
off := req.Offset
|
|
|
|
|
2015-07-19 12:11:33 +00:00
|
|
|
// Skip blobs before the offset
|
|
|
|
startContent := 0
|
|
|
|
for off > int64(f.sizes[startContent]) {
|
|
|
|
off -= int64(f.sizes[startContent])
|
|
|
|
startContent++
|
|
|
|
}
|
2015-04-07 19:10:53 +00:00
|
|
|
|
2015-07-19 12:11:33 +00:00
|
|
|
content := make([]byte, req.Size)
|
|
|
|
allContent := content
|
|
|
|
for i := startContent; i < len(f.sizes); i++ {
|
|
|
|
blob, err := f.getBlobAt(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 12:11:33 +00:00
|
|
|
blob = blob[off:]
|
2015-04-07 19:10:53 +00:00
|
|
|
off = 0
|
|
|
|
|
|
|
|
var copied int
|
2015-07-19 12:11:33 +00:00
|
|
|
if len(blob) > len(content) {
|
|
|
|
copied = copy(content[0:], blob[:len(content)])
|
2015-04-07 19:10:53 +00:00
|
|
|
} else {
|
2015-07-19 12:11:33 +00:00
|
|
|
copied = copy(content[0:], blob)
|
2015-04-07 19:10:53 +00:00
|
|
|
}
|
|
|
|
content = content[copied:]
|
|
|
|
if len(content) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.Data = allContent
|
|
|
|
return nil
|
|
|
|
}
|