github.com/hanwen/go-fuse@v1.0.0/fuse/nodefs/api.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  // The nodefs package offers a high level API that resembles the
     6  // kernel's idea of what an FS looks like.  File systems can have
     7  // multiple hard-links to one file, for example. It is also suited if
     8  // the data to represent fits in memory: you can construct the
     9  // complete file system tree at mount time
    10  package nodefs
    11  
    12  import (
    13  	"time"
    14  
    15  	"github.com/hanwen/go-fuse/fuse"
    16  )
    17  
    18  // The Node interface implements the user-defined file system
    19  // functionality
    20  type Node interface {
    21  	// Inode and SetInode are basic getter/setters.  They are
    22  	// called by the FileSystemConnector. You get them for free by
    23  	// embedding the result of NewDefaultNode() in your node
    24  	// struct.
    25  	Inode() *Inode
    26  	SetInode(node *Inode)
    27  
    28  	// OnMount is called on the root node just after a mount is
    29  	// executed, either when the actual root is mounted, or when a
    30  	// filesystem is mounted in-process. The passed-in
    31  	// FileSystemConnector gives access to Notify methods and
    32  	// Debug settings.
    33  	OnMount(conn *FileSystemConnector)
    34  
    35  	// OnUnmount is executed just before a submount is removed,
    36  	// and when the process receives a forget for the FUSE root
    37  	// node.
    38  	OnUnmount()
    39  
    40  	// Lookup finds a child node to this node; it is only called
    41  	// for directory Nodes. Lookup may be called on nodes that are
    42  	// already known.
    43  	Lookup(out *fuse.Attr, name string, context *fuse.Context) (*Inode, fuse.Status)
    44  
    45  	// Deletable() should return true if this node may be discarded once
    46  	// the kernel forgets its reference.
    47  	// If it returns false, OnForget will never get called for this node. This
    48  	// is appropriate if the filesystem has no persistent backing store
    49  	// (in-memory filesystems) where discarding the node loses the stored data.
    50  	// Deletable will be called from within the treeLock critical section, so you
    51  	// cannot look at other nodes.
    52  	Deletable() bool
    53  
    54  	// OnForget is called when the kernel forgets its reference to this node and
    55  	// sends a FORGET request. It should perform cleanup and free memory as
    56  	// appropriate for the filesystem.
    57  	// OnForget is not called if the node is a directory and has children.
    58  	// This is called from within a treeLock critical section.
    59  	OnForget()
    60  
    61  	// Misc.
    62  	Access(mode uint32, context *fuse.Context) (code fuse.Status)
    63  	Readlink(c *fuse.Context) ([]byte, fuse.Status)
    64  
    65  	// Namespace operations; these are only called on directory Nodes.
    66  
    67  	// Mknod should create the node, add it to the receiver's
    68  	// inode, and return it
    69  	Mknod(name string, mode uint32, dev uint32, context *fuse.Context) (newNode *Inode, code fuse.Status)
    70  
    71  	// Mkdir should create the directory Inode, add it to the
    72  	// receiver's Inode, and return it
    73  	Mkdir(name string, mode uint32, context *fuse.Context) (newNode *Inode, code fuse.Status)
    74  	Unlink(name string, context *fuse.Context) (code fuse.Status)
    75  	Rmdir(name string, context *fuse.Context) (code fuse.Status)
    76  
    77  	// Symlink should create a child inode to the receiver, and
    78  	// return it.
    79  	Symlink(name string, content string, context *fuse.Context) (*Inode, fuse.Status)
    80  	Rename(oldName string, newParent Node, newName string, context *fuse.Context) (code fuse.Status)
    81  
    82  	// Link should return the Inode of the resulting link. In
    83  	// a POSIX conformant file system, this should add 'existing'
    84  	// to the receiver, and return the Inode corresponding to
    85  	// 'existing'.
    86  	Link(name string, existing Node, context *fuse.Context) (newNode *Inode, code fuse.Status)
    87  
    88  	// Create should return an open file, and the Inode for that file.
    89  	Create(name string, flags uint32, mode uint32, context *fuse.Context) (file File, child *Inode, code fuse.Status)
    90  
    91  	// Open opens a file, and returns a File which is associated
    92  	// with a file handle. It is OK to return (nil, OK) here. In
    93  	// that case, the Node should implement Read or Write
    94  	// directly.
    95  	Open(flags uint32, context *fuse.Context) (file File, code fuse.Status)
    96  	OpenDir(context *fuse.Context) ([]fuse.DirEntry, fuse.Status)
    97  	Read(file File, dest []byte, off int64, context *fuse.Context) (fuse.ReadResult, fuse.Status)
    98  	Write(file File, data []byte, off int64, context *fuse.Context) (written uint32, code fuse.Status)
    99  
   100  	// XAttrs
   101  	GetXAttr(attribute string, context *fuse.Context) (data []byte, code fuse.Status)
   102  	RemoveXAttr(attr string, context *fuse.Context) fuse.Status
   103  	SetXAttr(attr string, data []byte, flags int, context *fuse.Context) fuse.Status
   104  	ListXAttr(context *fuse.Context) (attrs []string, code fuse.Status)
   105  
   106  	// File locking
   107  	//
   108  	// GetLk returns existing lock information for file.
   109  	GetLk(file File, owner uint64, lk *fuse.FileLock, flags uint32, out *fuse.FileLock, context *fuse.Context) (code fuse.Status)
   110  
   111  	// Sets or clears the lock described by lk on file.
   112  	SetLk(file File, owner uint64, lk *fuse.FileLock, flags uint32, context *fuse.Context) (code fuse.Status)
   113  
   114  	// Sets or clears the lock described by lk. This call blocks until the operation can be completed.
   115  	SetLkw(file File, owner uint64, lk *fuse.FileLock, flags uint32, context *fuse.Context) (code fuse.Status)
   116  
   117  	// Attributes
   118  	GetAttr(out *fuse.Attr, file File, context *fuse.Context) (code fuse.Status)
   119  	Chmod(file File, perms uint32, context *fuse.Context) (code fuse.Status)
   120  	Chown(file File, uid uint32, gid uint32, context *fuse.Context) (code fuse.Status)
   121  	Truncate(file File, size uint64, context *fuse.Context) (code fuse.Status)
   122  	Utimens(file File, atime *time.Time, mtime *time.Time, context *fuse.Context) (code fuse.Status)
   123  	Fallocate(file File, off uint64, size uint64, mode uint32, context *fuse.Context) (code fuse.Status)
   124  
   125  	StatFs() *fuse.StatfsOut
   126  }
   127  
   128  // A File object is returned from FileSystem.Open and
   129  // FileSystem.Create.  Include the NewDefaultFile return value into
   130  // the struct to inherit a null implementation.
   131  type File interface {
   132  	// Called upon registering the filehandle in the inode. This
   133  	// is useful in that PathFS API, where Create/Open have no
   134  	// access to the Inode at hand.
   135  	SetInode(*Inode)
   136  
   137  	// The String method is for debug printing.
   138  	String() string
   139  
   140  	// Wrappers around other File implementations, should return
   141  	// the inner file here.
   142  	InnerFile() File
   143  
   144  	Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status)
   145  	Write(data []byte, off int64) (written uint32, code fuse.Status)
   146  
   147  	// File locking
   148  	GetLk(owner uint64, lk *fuse.FileLock, flags uint32, out *fuse.FileLock) (code fuse.Status)
   149  	SetLk(owner uint64, lk *fuse.FileLock, flags uint32) (code fuse.Status)
   150  	SetLkw(owner uint64, lk *fuse.FileLock, flags uint32) (code fuse.Status)
   151  
   152  	// Flush is called for close() call on a file descriptor. In
   153  	// case of duplicated descriptor, it may be called more than
   154  	// once for a file.
   155  	Flush() fuse.Status
   156  
   157  	// This is called to before the file handle is forgotten. This
   158  	// method has no return value, so nothing can synchronizes on
   159  	// the call. Any cleanup that requires specific synchronization or
   160  	// could fail with I/O errors should happen in Flush instead.
   161  	Release()
   162  	Fsync(flags int) (code fuse.Status)
   163  
   164  	// The methods below may be called on closed files, due to
   165  	// concurrency.  In that case, you should return EBADF.
   166  	Truncate(size uint64) fuse.Status
   167  	GetAttr(out *fuse.Attr) fuse.Status
   168  	Chown(uid uint32, gid uint32) fuse.Status
   169  	Chmod(perms uint32) fuse.Status
   170  	Utimens(atime *time.Time, mtime *time.Time) fuse.Status
   171  	Allocate(off uint64, size uint64, mode uint32) (code fuse.Status)
   172  }
   173  
   174  // Wrap a File return in this to set FUSE flags.  Also used internally
   175  // to store open file data.
   176  type WithFlags struct {
   177  	File
   178  
   179  	// For debugging.
   180  	Description string
   181  
   182  	// Put FOPEN_* flags here.
   183  	FuseFlags uint32
   184  
   185  	// O_RDWR, O_TRUNCATE, etc.
   186  	OpenFlags uint32
   187  }
   188  
   189  // Options contains time out options for a node FileSystem.  The
   190  // default copied from libfuse and set in NewMountOptions() is
   191  // (1s,1s,0s).
   192  type Options struct {
   193  	EntryTimeout    time.Duration
   194  	AttrTimeout     time.Duration
   195  	NegativeTimeout time.Duration
   196  
   197  	// If set, replace all uids with given UID.
   198  	// NewOptions() will set this to the daemon's
   199  	// uid/gid.
   200  	*fuse.Owner
   201  
   202  	// This option exists for compatibility and is ignored.
   203  	PortableInodes bool
   204  
   205  	// If set, print debug information.
   206  	Debug bool
   207  
   208  	// If set, issue Lookup rather than GetAttr calls for known
   209  	// children. This allows the filesystem to update its inode
   210  	// hierarchy in response to kernel calls.
   211  	LookupKnownChildren bool
   212  }