github.com/hanwen/go-fuse@v1.0.0/example/zipfs/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/fuse/nodefs"
    20  	"github.com/hanwen/go-fuse/zipfs"
    21  )
    22  
    23  func main() {
    24  	// Scans the arg list and sets up flags
    25  	debug := flag.Bool("debug", false, "print debugging messages.")
    26  	profile := flag.String("profile", "", "record cpu profile.")
    27  	mem_profile := flag.String("mem-profile", "", "record memory profile.")
    28  	command := flag.String("run", "", "run this command after mounting.")
    29  	ttl := flag.Float64("ttl", 1.0, "attribute/entry cache TTL.")
    30  	flag.Parse()
    31  	if flag.NArg() < 2 {
    32  		fmt.Fprintf(os.Stderr, "usage: %s MOUNTPOINT ZIP-FILE\n", os.Args[0])
    33  		os.Exit(2)
    34  	}
    35  
    36  	var profFile, memProfFile io.Writer
    37  	var err error
    38  	if *profile != "" {
    39  		profFile, err = os.Create(*profile)
    40  		if err != nil {
    41  			log.Fatalf("os.Create: %v", err)
    42  		}
    43  	}
    44  	if *mem_profile != "" {
    45  		memProfFile, err = os.Create(*mem_profile)
    46  		if err != nil {
    47  			log.Fatalf("os.Create: %v", err)
    48  		}
    49  	}
    50  
    51  	root, err := zipfs.NewArchiveFileSystem(flag.Arg(1))
    52  	if err != nil {
    53  		fmt.Fprintf(os.Stderr, "NewArchiveFileSystem failed: %v\n", err)
    54  		os.Exit(1)
    55  	}
    56  
    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), 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  }