Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ jobs:
env:
CGO_ENABLED: '0'
run: |
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o goofys-amd64 .
GOOS=linux GOARCH=arm64 go build -ldflags "-s -w" -o goofys-arm64 .
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X main.Version=$GITHUB_REF_NAME" -o goofys-amd64 .
GOOS=linux GOARCH=arm64 go build -ldflags "-s -w -X main.Version=$GITHUB_REF_NAME" -o goofys-arm64 .
cp goofys-amd64 goofys

- name: Create release and upload assets
Expand Down
257 changes: 257 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
// Copyright 2015 - 2017 Ka-Hing Cheung
// Copyright 2015 - 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
goofys "github.com/kahing/goofys/api"
. "github.com/kahing/goofys/api/common"
. "github.com/kahing/goofys/internal"

"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"time"

"context"

"github.com/jacobsa/fuse"
"github.com/kardianos/osext"
"github.com/urfave/cli"

daemon "github.com/sevlyar/go-daemon"
)

var log = GetLogger("main")

func registerSIGINTHandler(fs *Goofys, flags *FlagStorage) {
// Register for SIGINT.
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)

// Start a goroutine that will unmount when the signal is received.
go func() {
for {
s := <-signalChan
if s == syscall.SIGUSR1 {
log.Infof("Received %v", s)
fs.SigUsr1()
continue
}

if len(flags.Cache) == 0 {
log.Infof("Received %v, attempting to unmount...", s)

err := TryUnmount(flags.MountPoint)
if err != nil {
log.Errorf("Failed to unmount in response to %v: %v", s, err)
} else {
log.Printf("Successfully unmounted %v in response to %v",
flags.MountPoint, s)
return
}
} else {
log.Infof("Received %v", s)
// wait for catfs to die and cleanup
}
}
}()
}

var waitedForSignal os.Signal

func waitForSignal(wg *sync.WaitGroup) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGUSR1, syscall.SIGUSR2)

wg.Add(1)
go func() {
waitedForSignal = <-signalChan
wg.Done()
}()
}

func kill(pid int, s os.Signal) (err error) {
p, err := os.FindProcess(pid)
if err != nil {
return err
}

defer p.Release()

err = p.Signal(s)
if err != nil {
return err
}
return
}

// Mount the file system based on the supplied arguments, returning a
// fuse.MountedFileSystem that can be joined to wait for unmounting.
func mount(
ctx context.Context,
bucketName string,
flags *FlagStorage) (fs *Goofys, mfs *fuse.MountedFileSystem, err error) {

return goofys.Mount(ctx, bucketName, flags)
}

func massagePath() {
for _, e := range os.Environ() {
if strings.HasPrefix(e, "PATH=") {
return
}
}

// mount -a seems to run goofys without PATH
// usually fusermount is in /bin
os.Setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")
}

func massageArg0() {
// an absolute argv[0] (e.g. the /usr/bin/goofys multi-call symlink from
// fstab) must survive the daemon re-exec unresolved so the re-executed
// child dispatches the same way
if filepath.IsAbs(os.Args[0]) {
return
}
var err error
os.Args[0], err = osext.Executable()
if err != nil {
panic(fmt.Sprintf("Unable to discover current executable: %v", err))
}
}

// Run executes the goofys command line. It is exported so multi-call
// binaries can embed goofys and dispatch to it; note it may daemonize by
// re-executing os.Args[0] and can terminate the process on fatal errors.
func Run(args []string, versionHash string) int {
VersionNumber = "0.25.0"
VersionHash = versionHash

massagePath()

app := NewApp()

var flags *FlagStorage
var child *os.Process

app.Action = func(c *cli.Context) (err error) {
// We should get two arguments exactly. Otherwise error out.
if len(c.Args()) != 2 {
fmt.Fprintf(
os.Stderr,
"Error: %s takes exactly two arguments.\n\n",
app.Name)
cli.ShowAppHelp(c)
os.Exit(1)
}

// Populate and parse flags.
bucketName := c.Args()[0]
flags = PopulateFlags(c)
if flags == nil {
cli.ShowAppHelp(c)
err = fmt.Errorf("invalid arguments")
return
}
defer func() {
time.Sleep(time.Second)
flags.Cleanup()
}()

if !flags.Foreground {
var wg sync.WaitGroup
waitForSignal(&wg)

massageArg0()

ctx := new(daemon.Context)
child, err = ctx.Reborn()

if err != nil {
panic(fmt.Sprintf("unable to daemonize: %v", err))
}

InitLoggers(!flags.Foreground && child == nil)

if child != nil {
// attempt to wait for child to notify parent
wg.Wait()
if waitedForSignal == syscall.SIGUSR1 {
return
} else {
return fuse.EINVAL
}
} else {
// kill our own waiting goroutine
kill(os.Getpid(), syscall.SIGUSR1)
wg.Wait()
defer ctx.Release()
}

} else {
InitLoggers(!flags.Foreground)
}

// Mount the file system.
var mfs *fuse.MountedFileSystem
var fs *Goofys
fs, mfs, err = mount(
context.Background(),
bucketName,
flags)

if err != nil {
if !flags.Foreground {
kill(os.Getppid(), syscall.SIGUSR2)
}
log.Fatalf("Mounting file system: %v", err)
// fatal also terminates itself
} else {
if !flags.Foreground {
kill(os.Getppid(), syscall.SIGUSR1)
}
log.Println("File system has been successfully mounted.")
// Let the user unmount with Ctrl-C
// (SIGINT). But if cache is on, catfs will
// receive the signal and we would detect that exiting
registerSIGINTHandler(fs, flags)

// Wait for the file system to be unmounted.
err = mfs.Join(context.Background())
if err != nil {
err = fmt.Errorf("MountedFileSystem.Join: %v", err)
return
}

log.Println("Successfully exiting.")
}
return
}

err := app.Run(MassageMountFlags(args))
if err != nil {
if flags != nil && !flags.Foreground && child != nil {
log.Fatalln("Unable to mount file system, see syslog for details")
}
return 1
}
return 0
}
57 changes: 47 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kahing/goofys

go 1.14
go 1.24

require (
cloud.google.com/go/storage v1.14.0
Expand All @@ -11,27 +11,64 @@ require (
github.com/Azure/go-autorest/autorest/adal v0.9.13
github.com/Azure/go-autorest/autorest/azure/auth v0.5.7
github.com/Azure/go-autorest/autorest/azure/cli v0.4.2
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/aws/aws-sdk-go v1.44.37
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/gofrs/uuid v4.2.0+incompatible
github.com/google/uuid v1.2.0
github.com/gopherjs/gopherjs v0.0.0-20210413103415-7d3cbed7d026 // indirect
github.com/jacobsa/fuse v0.0.0-20221016084658-a4cd154343d8
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/mitchellh/go-homedir v1.1.0
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
github.com/sevlyar/go-daemon v0.1.5
github.com/shirou/gopsutil v0.0.0-20190731134726-d80c43f9c984
github.com/shirou/gopsutil/v4 v4.25.6
github.com/sirupsen/logrus v1.4.3-0.20190807103436-de736cf91b92
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/urfave/cli v1.21.1-0.20190807111034-521735b7608a
golang.org/x/oauth2 v0.0.0-20210323180902-22b0adad7558
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.2.0
golang.org/x/sys v0.28.0
google.golang.org/api v0.43.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
gopkg.in/ini.v1 v1.51.0
)

require (
cloud.google.com/go v0.79.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/ebitengine/purego v0.8.4 // indirect
github.com/form3tech-oss/jwt-go v3.2.2+incompatible // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/gopherjs/gopherjs v0.0.0-20210413103415-7d3cbed7d026 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mattn/go-ieproxy v0.0.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
golang.org/x/mod v0.4.1 // indirect
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210319143718-93e7006c17a6 // indirect
google.golang.org/grpc v1.36.0 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading
Loading