Debian Codegraph @

examples/hellofs/hello.go

raw symbols SCIP
1 // Hellofs implements a simple "hello world" file system.
2 package main
3
4 import (
5 "flag"
6 "fmt"
7 "log"
8 "os"
9
10 "github.com/jacobsa/bazilfuse"
11 "github.com/jacobsa/bazilfuse/fs"
12 _ "github.com/jacobsa/bazilfuse/fs/fstestutil"
13 "golang.org/x/net/context"
14 )
15
16 var Usage = func() {
17 fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
18 fmt.Fprintf(os.Stderr, " %s MOUNTPOINT\n", os.Args[0])
19 flag.PrintDefaults()
20 }
21
22 func main() {
23 flag.Usage = Usage
24 flag.Parse()
25
26 if flag.NArg() != 1 {
27 Usage()
28 os.Exit(2)
29 }
30 mountpoint := flag.Arg(0)
31
32 c, err := fuse.Mount(
33 mountpoint,
34 fuse.FSName("helloworld"),
35 fuse.Subtype("hellofs"),
36 fuse.LocalVolume(),
37 fuse.VolumeName("Hello world!"),
38 )
39 if err != nil {
40 log.Fatal(err)
41 }
42 defer c.Close()
43
44 err = fs.Serve(c, FS{})
45 if err != nil {
46 log.Fatal(err)
47 }
48
49 // check if the mount process has an error to report
50 <-c.Ready
51 if err := c.MountError; err != nil {
52 log.Fatal(err)
53 }
54 }
55
56 // FS implements the hello world file system.
57 type FS struct{}
58
59 func (FS) Root() (fs.Node, error) {
60 return Dir{}, nil
61 }
62
63 // Dir implements both Node and Handle for the root directory.
64 type Dir struct{}
65
66 func (Dir) Attr(ctx context.Context, a *fuse.Attr) error {
67 a.Inode = 1
68 a.Mode = os.ModeDir | 0555
69 return nil
70 }
71
72 func (Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
73 if name == "hello" {
74 return File{}, nil
75 }
76 return nil, fuse.ENOENT
77 }
78
79 var dirDirs = []fuse.Dirent{
80 {Inode: 2, Name: "hello", Type: fuse.DT_File},
81 }
82
83 func (Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
84 return dirDirs, nil
85 }
86
87 // File implements both Node and Handle for the hello file.
88 type File struct{}
89
90 const greeting = "hello, world\n"
91
92 func (File) Attr(ctx context.Context, a *fuse.Attr) error {
93 a.Inode = 2
94 a.Mode = 0444
95 a.Size = uint64(len(greeting))
96 return nil
97 }
98
99 func (File) ReadAll(ctx context.Context) ([]byte, error) {
100 return []byte(greeting), nil
101 }