github.com/hanwen/go-fuse@v1.0.0/example/hello/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  // A Go mirror of libfuse's hello.c
     6  
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"log"
    12  
    13  	"github.com/hanwen/go-fuse/fuse"
    14  	"github.com/hanwen/go-fuse/fuse/nodefs"
    15  	"github.com/hanwen/go-fuse/fuse/pathfs"
    16  )
    17  
    18  type HelloFs struct {
    19  	pathfs.FileSystem
    20  }
    21  
    22  func (me *HelloFs) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
    23  	switch name {
    24  	case "file.txt":
    25  		return &fuse.Attr{
    26  			Mode: fuse.S_IFREG | 0644, Size: uint64(len(name)),
    27  		}, fuse.OK
    28  	case "":
    29  		return &fuse.Attr{
    30  			Mode: fuse.S_IFDIR | 0755,
    31  		}, fuse.OK
    32  	}
    33  	return nil, fuse.ENOENT
    34  }
    35  
    36  func (me *HelloFs) OpenDir(name string, context *fuse.Context) (c []fuse.DirEntry, code fuse.Status) {
    37  	if name == "" {
    38  		c = []fuse.DirEntry{{Name: "file.txt", Mode: fuse.S_IFREG}}
    39  		return c, fuse.OK
    40  	}
    41  	return nil, fuse.ENOENT
    42  }
    43  
    44  func (me *HelloFs) Open(name string, flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) {
    45  	if name != "file.txt" {
    46  		return nil, fuse.ENOENT
    47  	}
    48  	if flags&fuse.O_ANYWRITE != 0 {
    49  		return nil, fuse.EPERM
    50  	}
    51  	return nodefs.NewDataFile([]byte(name)), fuse.OK
    52  }
    53  
    54  func main() {
    55  	flag.Parse()
    56  	if len(flag.Args()) < 1 {
    57  		log.Fatal("Usage:\n  hello MOUNTPOINT")
    58  	}
    59  	nfs := pathfs.NewPathNodeFs(&HelloFs{FileSystem: pathfs.NewDefaultFileSystem()}, nil)
    60  	server, _, err := nodefs.MountRoot(flag.Arg(0), nfs.Root(), nil)
    61  	if err != nil {
    62  		log.Fatalf("Mount fail: %v\n", err)
    63  	}
    64  	server.Serve()
    65  }