mirror of
https://github.com/hanzoai/go-fuse.git
synced 2026-10-30 14:46:18 +00:00
github.com has no runner for hanzo-build-linux-amd64, so a caller under .github/workflows is a gate that can never be scheduled. This is the ~7-line caller on the plane that can (git.hanzo.ai git-runner fleet), pinned @v1. Also fixes the one defect the new gate found on its first run: example/loopback passed an UNBUFFERED channel to signal.Notify, which drops the SIGTERM that unmounts the filesystem whenever the receiving goroutine is not already parked. Buffered by 1, per os/signal's own documented contract. Co-authored-by: Hanzo Dev <[email protected]>
146 lines
4.1 KiB
Go
146 lines
4.1 KiB
Go
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// This is main program driver for the loopback filesystem from
|
|
// github.com/hanwen/go-fuse/fs/, a filesystem that shunts operations
|
|
// to an underlying file system.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"path"
|
|
"runtime/pprof"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/hanzoai/go-fuse/v2/fs"
|
|
"github.com/hanzoai/go-fuse/v2/fuse"
|
|
)
|
|
|
|
func writeMemProfile(fn string, sigs <-chan os.Signal) {
|
|
i := 0
|
|
for range sigs {
|
|
fn := fmt.Sprintf("%s-%d.memprof", fn, i)
|
|
i++
|
|
|
|
log.Printf("Writing mem profile to %s\n", fn)
|
|
f, err := os.Create(fn)
|
|
if err != nil {
|
|
log.Printf("Create: %v", err)
|
|
continue
|
|
}
|
|
pprof.WriteHeapProfile(f)
|
|
if err := f.Close(); err != nil {
|
|
log.Printf("close %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
log.SetFlags(log.Lmicroseconds)
|
|
// Scans the arg list and sets up flags
|
|
debug := flag.Bool("debug", false, "print debugging messages.")
|
|
other := flag.Bool("allow-other", false, "mount with -o allowother.")
|
|
idmap := flag.Bool("idmapped", false, "enable id-mapped mount")
|
|
quiet := flag.Bool("q", false, "quiet")
|
|
ro := flag.Bool("ro", false, "mount read-only")
|
|
directmount := flag.Bool("directmount", false, "try to call the mount syscall instead of executing fusermount")
|
|
directmountstrict := flag.Bool("directmountstrict", false, "like directmount, but don't fall back to fusermount")
|
|
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to this file")
|
|
memprofile := flag.String("memprofile", "", "write memory profile to this file")
|
|
flag.Parse()
|
|
if flag.NArg() < 2 {
|
|
fmt.Printf("usage: %s MOUNTPOINT ORIGINAL\n", path.Base(os.Args[0]))
|
|
fmt.Printf("\noptions:\n")
|
|
flag.PrintDefaults()
|
|
os.Exit(2)
|
|
}
|
|
if *cpuprofile != "" {
|
|
if !*quiet {
|
|
fmt.Printf("Writing cpu profile to %s\n", *cpuprofile)
|
|
}
|
|
f, err := os.Create(*cpuprofile)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(3)
|
|
}
|
|
pprof.StartCPUProfile(f)
|
|
defer pprof.StopCPUProfile()
|
|
}
|
|
if *memprofile != "" {
|
|
if !*quiet {
|
|
log.Printf("send SIGUSR1 to %d to dump memory profile", os.Getpid())
|
|
}
|
|
profSig := make(chan os.Signal, 1)
|
|
signal.Notify(profSig, syscall.SIGUSR1)
|
|
go writeMemProfile(*memprofile, profSig)
|
|
}
|
|
if *cpuprofile != "" || *memprofile != "" {
|
|
if !*quiet {
|
|
fmt.Printf("Note: You must unmount gracefully, otherwise the profile file(s) will stay empty!\n")
|
|
}
|
|
}
|
|
|
|
orig := flag.Arg(1)
|
|
loopbackRoot, err := fs.NewLoopbackRoot(orig)
|
|
if err != nil {
|
|
log.Fatalf("NewLoopbackRoot(%s): %v\n", orig, err)
|
|
}
|
|
|
|
sec := time.Second
|
|
opts := &fs.Options{
|
|
// The timeout options are to be compatible with libfuse defaults,
|
|
// making benchmarking easier.
|
|
AttrTimeout: &sec,
|
|
EntryTimeout: &sec,
|
|
|
|
NullPermissions: true, // Leave file permissions on "000" files as-is
|
|
|
|
MountOptions: fuse.MountOptions{
|
|
AllowOther: *other,
|
|
Debug: *debug,
|
|
DirectMount: *directmount,
|
|
DirectMountStrict: *directmountstrict,
|
|
IDMappedMount: *idmap,
|
|
FsName: orig, // First column in "df -T": original dir
|
|
Name: "loopback", // Second column in "df -T" will be shown as "fuse." + Name
|
|
},
|
|
}
|
|
if opts.AllowOther {
|
|
// Make the kernel check file permissions for us
|
|
opts.MountOptions.Options = append(opts.MountOptions.Options, "default_permissions")
|
|
}
|
|
if *ro {
|
|
opts.MountOptions.Options = append(opts.MountOptions.Options, "ro")
|
|
}
|
|
// Enable diagnostics logging
|
|
if !*quiet {
|
|
opts.Logger = log.New(os.Stderr, "", 0)
|
|
}
|
|
server, err := fs.Mount(flag.Arg(0), loopbackRoot, opts)
|
|
if err != nil {
|
|
log.Fatalf("Mount fail: %v\n", err)
|
|
}
|
|
if !*quiet {
|
|
fmt.Println("Mounted!")
|
|
}
|
|
|
|
// Buffered by 1: signal.Notify never blocks, so an unbuffered channel drops
|
|
// the SIGTERM whenever this goroutine is not already parked in the receive —
|
|
// and then the mount is never unmounted. (Found by `go vet ./...`, the gate
|
|
// below.)
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
go func() {
|
|
<-c
|
|
server.Unmount()
|
|
}()
|
|
|
|
server.Wait()
|
|
}
|