github.com/hanwen/go-fuse@v1.0.0/example/statfs/main.go (about) 1 // Copyright 2016 the Go-FUSE Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "flag" 9 "fmt" 10 "io" 11 "log" 12 "os" 13 "os/exec" 14 "runtime" 15 "runtime/pprof" 16 "strings" 17 "time" 18 19 "github.com/hanwen/go-fuse/benchmark" 20 "github.com/hanwen/go-fuse/fuse/nodefs" 21 "github.com/hanwen/go-fuse/fuse/pathfs" 22 ) 23 24 func main() { 25 // Scans the arg list and sets up flags 26 debug := flag.Bool("debug", false, "print debugging messages.") 27 profile := flag.String("profile", "", "record cpu profile.") 28 mem_profile := flag.String("mem-profile", "", "record memory profile.") 29 command := flag.String("run", "", "run this command after mounting.") 30 ttl := flag.Float64("ttl", 1.0, "attribute/entry cache TTL.") 31 flag.Parse() 32 if flag.NArg() < 2 { 33 fmt.Fprintf(os.Stderr, "usage: %s MOUNTPOINT FILENAMES-FILE\n", os.Args[0]) 34 os.Exit(2) 35 } 36 37 var profFile, memProfFile io.Writer 38 var err error 39 if *profile != "" { 40 profFile, err = os.Create(*profile) 41 if err != nil { 42 log.Fatalf("os.Create: %v", err) 43 } 44 } 45 if *mem_profile != "" { 46 memProfFile, err = os.Create(*mem_profile) 47 if err != nil { 48 log.Fatalf("os.Create: %v", err) 49 } 50 } 51 fs := benchmark.NewStatFS() 52 lines := benchmark.ReadLines(flag.Arg(1)) 53 for _, l := range lines { 54 fs.AddFile(l) 55 } 56 nfs := pathfs.NewPathNodeFs(fs, nil) 57 opts := &nodefs.Options{ 58 AttrTimeout: time.Duration(*ttl * float64(time.Second)), 59 EntryTimeout: time.Duration(*ttl * float64(time.Second)), 60 Debug: *debug, 61 } 62 state, _, err := nodefs.MountRoot(flag.Arg(0), nfs.Root(), opts) 63 if err != nil { 64 fmt.Printf("Mount fail: %v\n", err) 65 os.Exit(1) 66 } 67 68 runtime.GC() 69 if profFile != nil { 70 pprof.StartCPUProfile(profFile) 71 defer pprof.StopCPUProfile() 72 } 73 74 if *command != "" { 75 args := strings.Split(*command, " ") 76 cmd := exec.Command(args[0], args[1:]...) 77 cmd.Stdout = os.Stdout 78 cmd.Start() 79 } 80 81 state.Serve() 82 if memProfFile != nil { 83 pprof.WriteHeapProfile(memProfFile) 84 } 85 }