github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/samples/mount_sample/mount.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // A simple tool for mounting sample file systems, used by the tests in 16 // samples/. 17 package main 18 19 import ( 20 "context" 21 "errors" 22 "flag" 23 "fmt" 24 "log" 25 "os" 26 "runtime" 27 "syscall" 28 29 "github.com/scaleoutsean/fusego" 30 "github.com/scaleoutsean/fusego/samples/flushfs" 31 ) 32 33 var fType = flag.String("type", "", "The name of the samples/ sub-dir.") 34 var fMountPoint = flag.String("mount_point", "", "Path to mount point.") 35 var fReadyFile = flag.Uint64("ready_file", 0, "FD to signal when ready.") 36 37 var fFlushesFile = flag.Uint64("flushfs.flushes_file", 0, "") 38 var fFsyncsFile = flag.Uint64("flushfs.fsyncs_file", 0, "") 39 var fFlushError = flag.Int("flushfs.flush_error", 0, "") 40 var fFsyncError = flag.Int("flushfs.fsync_error", 0, "") 41 42 var fReadOnly = flag.Bool("read_only", false, "Mount in read-only mode.") 43 var fDebug = flag.Bool("debug", false, "Enable debug logging.") 44 45 func makeFlushFS() (fuse.Server, error) { 46 // Check the flags. 47 if *fFlushesFile == 0 || *fFsyncsFile == 0 { 48 return nil, fmt.Errorf("You must set the flushfs flags.") 49 } 50 51 // Set up the files. 52 flushes := os.NewFile(uintptr(*fFlushesFile), "(flushes file)") 53 fsyncs := os.NewFile(uintptr(*fFsyncsFile), "(fsyncs file)") 54 55 // Set up errors. 56 var flushErr error 57 var fsyncErr error 58 59 if *fFlushError != 0 { 60 flushErr = syscall.Errno(*fFlushError) 61 } 62 63 if *fFsyncError != 0 { 64 fsyncErr = syscall.Errno(*fFsyncError) 65 } 66 67 // Report flushes and fsyncs by writing the contents followed by a newline. 68 report := func(f *os.File, outErr error) func(string) error { 69 return func(s string) error { 70 buf := []byte(s) 71 buf = append(buf, '\n') 72 73 if _, err := f.Write(buf); err != nil { 74 return fmt.Errorf("Write: %v", err) 75 } 76 77 return outErr 78 } 79 } 80 81 reportFlush := report(flushes, flushErr) 82 reportFsync := report(fsyncs, fsyncErr) 83 84 // Create the file system. 85 return flushfs.NewFileSystem(reportFlush, reportFsync) 86 } 87 88 func makeFS() (fuse.Server, error) { 89 switch *fType { 90 default: 91 return nil, fmt.Errorf("Unknown FS type: %v", *fType) 92 93 case "flushfs": 94 return makeFlushFS() 95 } 96 } 97 98 func getReadyFile() (*os.File, error) { 99 if *fReadyFile == 0 { 100 return nil, errors.New("You must set --ready_file.") 101 } 102 103 return os.NewFile(uintptr(*fReadyFile), "(ready file)"), nil 104 } 105 106 func main() { 107 flag.Parse() 108 109 // Allow parallelism in the file system implementation, to help flush out 110 // bugs like https://github.com/scaleoutsean/fusego/issues/4. 111 runtime.GOMAXPROCS(2) 112 113 // Grab the file to signal when ready. 114 readyFile, err := getReadyFile() 115 if err != nil { 116 log.Fatalf("getReadyFile: %v", err) 117 } 118 119 // Create an appropriate file system. 120 server, err := makeFS() 121 if err != nil { 122 log.Fatalf("makeFS: %v", err) 123 } 124 125 // Mount the file system. 126 if *fMountPoint == "" { 127 log.Fatalf("You must set --mount_point.") 128 } 129 130 cfg := &fuse.MountConfig{ 131 ReadOnly: *fReadOnly, 132 } 133 134 if *fDebug { 135 cfg.DebugLogger = log.New(os.Stderr, "fuse: ", 0) 136 } 137 138 mfs, err := fuse.Mount(*fMountPoint, server, cfg) 139 if err != nil { 140 log.Fatalf("Mount: %v", err) 141 } 142 143 // Signal that it is ready. 144 _, err = readyFile.Write([]byte("x")) 145 if err != nil { 146 log.Fatalf("readyFile.Write: %v", err) 147 } 148 149 // Wait for it to be unmounted. 150 if err = mfs.Join(context.Background()); err != nil { 151 log.Fatalf("Join: %v", err) 152 } 153 }