api.go 5.76 KB
Newer Older
1 2
// The fuse package provides APIs to implement filesystems in
// userspace, using libfuse on Linux.
3
package fuse
Han-Wen Nienhuys's avatar
Han-Wen Nienhuys committed
4

5 6 7
import (
	"os"
)
8 9 10 11 12 13 14 15 16

// Types for users to implement.


// A filesystem API that uses paths rather than inodes.  A minimal
// file system should have at least a functional GetAttr method.
// Typically, each call happens in its own goroutine, so take care to
// make the file system thread-safe.
//
17
// Include DefaultFileSystem to provide a default null implementation of
18 19 20
// required methods.
type FileSystem interface {
	// Attributes
21
	GetAttr(name string) (*os.FileInfo, Status)
22 23

	// These should update the file's ctime too.
24 25
	Chmod(name string, mode uint32) (code Status)
	Chown(name string, uid uint32, gid uint32) (code Status)
26
	Utimens(name string, AtimeNs uint64, MtimeNs uint64) (code Status)
Han-Wen Nienhuys's avatar
Han-Wen Nienhuys committed
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	Truncate(name string, offset uint64) (code Status)

	Access(name string, mode uint32) (code Status)

	// Tree structure
	Link(oldName string, newName string) (code Status)
	Mkdir(name string, mode uint32) Status
	Mknod(name string, mode uint32, dev uint32) Status
	Rename(oldName string, newName string) (code Status)
	Rmdir(name string) (code Status)
	Unlink(name string) (code Status)

	// Extended attributes.
	GetXAttr(name string, attribute string) (data []byte, code Status)
	ListXAttr(name string) (attributes []string, code Status)
	RemoveXAttr(name string, attr string) Status
	SetXAttr(name string, attr string, data []byte, flags int) Status

	// Called after mount.
	Mount(connector *FileSystemConnector) Status
	Unmount()

50 51
	// File handling.  If opening for writing, the file's mtime
	// should be updated too.
52 53
	Open(name string, flags uint32) (file File, code Status)
	Create(name string, flags uint32, mode uint32) (file File, code Status)
Han-Wen Nienhuys's avatar
Han-Wen Nienhuys committed
54

55 56
	// Flush() gets called as a file opened for read/write.
	Flush(name string) Status
Han-Wen Nienhuys's avatar
Han-Wen Nienhuys committed
57

58 59
	// Directory handling
	OpenDir(name string) (stream chan DirEntry, code Status)
Han-Wen Nienhuys's avatar
Han-Wen Nienhuys committed
60

61 62 63 64 65 66 67 68 69 70 71
	// Symlinks.
	Symlink(value string, linkName string) (code Status)
	Readlink(name string) (string, Status)
}

// A File object should be returned from FileSystem.Open and
// FileSystem.Create.  Include DefaultFile into the struct to inherit
// a default null implementation.
//
// TODO - should File be thread safe?
type File interface {
72
	Read(*ReadIn, BufferPool) ([]byte, Status)
73
	Write(*WriteIn, []byte) (written uint32, code Status)
74
	Truncate(size uint64) Status
75

76
	GetAttr() (*os.FileInfo, Status)
77 78 79
	Chown(uid uint32, gid uint32) Status
	Chmod(perms uint32) Status
	Utimens(atimeNs uint64, mtimeNs uint64) Status
80 81 82
	Flush() Status
	Release()
	Fsync(*FsyncIn) (code Status)
Han-Wen Nienhuys's avatar
Han-Wen Nienhuys committed
83
	Ioctl(input *IoctlIn) (output *IoctlOut, data []byte, code Status)
84 85 86 87 88
}

// MountOptions contains time out options for a FileSystem.  The
// default copied from libfuse and set in NewMountOptions() is
// (1s,1s,0s).
89
type FileSystemOptions struct {
90 91 92
	EntryTimeout    float64
	AttrTimeout     float64
	NegativeTimeout float64
93 94 95 96

	// If set, replace all uids with given UID.  NewFileSystemOptions() will set
	// this to the daemon's uid/gid.
	*Owner
97 98
}

99 100 101 102
type MountOptions struct {
	AllowOther      bool
}

103 104 105 106 107 108
// DefaultFileSystem implements a FileSystem that returns ENOSYS for every operation.
type DefaultFileSystem struct{}

// DefaultFile returns ENOSYS for every operation.
type DefaultFile struct{}

109
// RawFileSystem is an interface closer to the FUSE wire protocol.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
//
// Unless you really know what you are doing, you should not implement
// this, but rather the FileSystem interface; the details of getting
// interactions with open files, renames, and threading right etc. are
// somewhat tricky and not very interesting.
//
// Include DefaultRawFileSystem to inherit a null implementation.
type RawFileSystem interface {
	Destroy(h *InHeader, input *InitIn)
	Lookup(header *InHeader, name string) (out *EntryOut, status Status)
	Forget(header *InHeader, input *ForgetIn)

	// Attributes.
	GetAttr(header *InHeader, input *GetAttrIn) (out *AttrOut, code Status)
	SetAttr(header *InHeader, input *SetAttrIn) (out *AttrOut, code Status)

	// Modifying structure.
	Mknod(header *InHeader, input *MknodIn, name string) (out *EntryOut, code Status)
	Mkdir(header *InHeader, input *MkdirIn, name string) (out *EntryOut, code Status)
	Unlink(header *InHeader, name string) (code Status)
	Rmdir(header *InHeader, name string) (code Status)
	Rename(header *InHeader, input *RenameIn, oldName string, newName string) (code Status)
	Link(header *InHeader, input *LinkIn, filename string) (out *EntryOut, code Status)

	Symlink(header *InHeader, pointedTo string, linkName string) (out *EntryOut, code Status)
	Readlink(header *InHeader) (out []byte, code Status)
	Access(header *InHeader, input *AccessIn) (code Status)

	// Extended attributes.
	GetXAttr(header *InHeader, attr string) (data []byte, code Status)
	ListXAttr(header *InHeader) (attributes []byte, code Status)
	SetXAttr(header *InHeader, input *SetXAttrIn, attr string, data []byte) Status
	RemoveXAttr(header *InHeader, attr string) (code Status)

	// File handling.
	Create(header *InHeader, input *CreateIn, name string) (flags uint32, handle uint64, out *EntryOut, code Status)
	Open(header *InHeader, input *OpenIn) (flags uint32, handle uint64, status Status)
147
	Read(*ReadIn, BufferPool) ([]byte, Status)
148

149 150 151 152 153 154 155 156 157 158
	Release(header *InHeader, input *ReleaseIn)
	Write(*WriteIn, []byte) (written uint32, code Status)
	Flush(*FlushIn) Status
	Fsync(*FsyncIn) (code Status)

	// Directory handling
	OpenDir(header *InHeader, input *OpenIn) (flags uint32, handle uint64, status Status)
	ReadDir(header *InHeader, input *ReadIn) (*DirEntryList, Status)
	ReleaseDir(header *InHeader, input *ReleaseIn)
	FsyncDir(header *InHeader, input *FsyncIn) (code Status)
Han-Wen Nienhuys's avatar
Han-Wen Nienhuys committed
159 160 161

	//
	Ioctl(header *InHeader, input *IoctlIn) (output *IoctlOut, data []byte, code Status)
162 163 164 165
}

// DefaultRawFileSystem returns ENOSYS for every operation.
type DefaultRawFileSystem struct{}