Commit 1fa28282 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

fs: more godoc

Change-Id: I1fe9bf5c77a332002b3b0a15b75853102549b511
parent 6506f8b1
...@@ -33,7 +33,6 @@ ...@@ -33,7 +33,6 @@
// initialized by calling NewInode or NewPersistentInode before being // initialized by calling NewInode or NewPersistentInode before being
// manipulated further, eg. // manipulated further, eg.
// //
//
// type myNode struct { // type myNode struct {
// Inode // Inode
// } // }
...@@ -83,6 +82,9 @@ import ( ...@@ -83,6 +82,9 @@ import (
// InodeEmbedder is an interface for structs that embed Inode. // InodeEmbedder is an interface for structs that embed Inode.
// //
// InodeEmbedder objects usually should implement some of the NodeXxxx
// interfaces, to provide user-defined file system behaviors.
//
// In general, if an InodeEmbedder does not implement specific // In general, if an InodeEmbedder does not implement specific
// filesystem methods, the filesystem will react as if it is a // filesystem methods, the filesystem will react as if it is a
// read-only filesystem with a predefined tree structure. See // read-only filesystem with a predefined tree structure. See
...@@ -358,10 +360,17 @@ type NodeRenamer interface { ...@@ -358,10 +360,17 @@ type NodeRenamer interface {
Rename(ctx context.Context, name string, newParent InodeEmbedder, newName string, flags uint32) syscall.Errno Rename(ctx context.Context, name string, newParent InodeEmbedder, newName string, flags uint32) syscall.Errno
} }
// FileHandle is a resource identifier for opened files. FileHandles // FileHandle is a resource identifier for opened files. Usually, a
// are useful in two cases: First, if the underlying storage systems // FileHandle should implement some of the FileXxxx interfaces.
// needs a handle for reading/writing. See the function //
// `NewLoopbackFile` for an example. Second, it is useful for // All of the FileXxxx operations can also be implemented at the
// InodeEmbedder level, for example, one can implement NodeReader
// instead of FileReader.
//
// FileHandles are useful in two cases: First, if the underlying
// storage systems needs a handle for reading/writing. This is the
// case with Unix system calls, which need a file descriptor (See also
// the function `NewLoopbackFile`). Second, it is useful for
// implementing files whose contents are not tied to an inode. For // implementing files whose contents are not tied to an inode. For
// example, a file like `/proc/interrupts` has no fixed content, but // example, a file like `/proc/interrupts` has no fixed content, but
// changes on each open call. This means that each file handle must // changes on each open call. This means that each file handle must
......
...@@ -55,19 +55,24 @@ func (root *inMemoryFS) OnAdd(ctx context.Context) { ...@@ -55,19 +55,24 @@ func (root *inMemoryFS) OnAdd(ctx context.Context) {
p = ch p = ch
} }
// Make a file out of the content bytes. This type
// provides the open/read/flush methods.
embedder := &fs.MemRegularFile{
Data: []byte(content),
}
// Create the file. The Inode must be persistent, // Create the file. The Inode must be persistent,
// because its life time is not under control of the // because its life time is not under control of the
// kernel. // kernel.
child := p.NewPersistentInode(ctx, &fs.MemRegularFile{ child := p.NewPersistentInode(ctx, embedder, fs.StableAttr{})
Data: []byte(content),
}, fs.StableAttr{})
// And add it // And add it
p.AddChild(base, child, true) p.AddChild(base, child, true)
} }
} }
// This demonstrates how to build a file system in memory. // This demonstrates how to build a file system in memory. The
// read/write logic for the file is provided by the MemRegularFile type.
func Example() { func Example() {
// This is where we'll mount the FS // This is where we'll mount the FS
mntDir, _ := ioutil.TempDir("", "") mntDir, _ := ioutil.TempDir("", "")
......
...@@ -372,7 +372,8 @@ func (n *loopbackNode) Setattr(ctx context.Context, f FileHandle, in *fuse.SetAt ...@@ -372,7 +372,8 @@ func (n *loopbackNode) Setattr(ctx context.Context, f FileHandle, in *fuse.SetAt
} }
// NewLoopback returns a root node for a loopback file system whose // NewLoopback returns a root node for a loopback file system whose
// root is at the given root. // root is at the given root. This node implements all NodeXxxxer
// operations available.
func NewLoopbackRoot(root string) (InodeEmbedder, error) { func NewLoopbackRoot(root string) (InodeEmbedder, error) {
var st syscall.Stat_t var st syscall.Stat_t
err := syscall.Stat(root, &st) err := syscall.Stat(root, &st)
......
...@@ -13,7 +13,6 @@ import ( ...@@ -13,7 +13,6 @@ import (
// MemRegularFile is a filesystem node that holds a read-only data // MemRegularFile is a filesystem node that holds a read-only data
// slice in memory. // slice in memory.
type MemRegularFile struct { type MemRegularFile struct {
Inode Inode
Data []byte Data []byte
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment