/
jpgfs.go
96 lines (81 loc) · 1.55 KB
/
jpgfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
)
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s SOURCE MOUNTPOINT\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
flag.Usage = Usage
flag.Parse()
if flag.NArg() != 2 {
Usage()
os.Exit(2)
}
source := flag.Arg(0)
mountpoint := flag.Arg(1)
filesystem := FS{}
walker := Walker{Path: source, CachePath: "/home/chris/.cache/jpgfs/"}
filesystem.tree = walker.Walk()
c, err := fuse.Mount(
mountpoint,
fuse.FSName(source),
fuse.Subtype("jpgfs"),
fuse.LocalVolume(),
fuse.VolumeName("jpgfs"),
)
if err != nil {
log.Fatal(err)
}
defer c.Close()
err = fs.Serve(c, filesystem)
if err != nil {
log.Fatal(err)
}
// check if the mount process has an error to report
<-c.Ready
if err := c.MountError; err != nil {
log.Fatal(err)
}
}
type FS struct {
tree fs.Tree
}
func (f FS) Root() (fs.Node, error) {
return f.tree.Root()
}
// File implements both Node and Handle for the hello file.
type File struct {
path string
size uint64
mode os.FileMode
Atime time.Time
Mtime time.Time
Ctime time.Time
Uid uint32
Gid uint32
}
func (f File) Attr(ctx context.Context, a *fuse.Attr) error {
a.Mode = f.mode
a.Size = f.size
a.Atime = f.Atime
a.Mtime = f.Mtime
a.Ctime = f.Ctime
a.Crtime = f.Ctime
a.Uid = f.Uid
a.Gid = f.Gid
return nil
}
func (f File) ReadAll(ctx context.Context) ([]byte, error) {
return ioutil.ReadFile(f.path)
}