Commit bfaeb760 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

nodefs: comment ExampleNewPersistentInode

parent 3377c503
...@@ -208,43 +208,58 @@ func (zr *zipRoot) OnAdd(ctx context.Context) { ...@@ -208,43 +208,58 @@ func (zr *zipRoot) OnAdd(ctx context.Context) {
} }
} }
// ExampleNewPersistentInode shows how to create a in-memory file // Persistent inodes can be used to create an in-memory
// system a prefabricated tree. // prefabricated file system tree.
func ExampleNewPersistentInode() { func ExampleInode_NewPersistentInode() {
// This is where we'll mount the FS
mntDir, _ := ioutil.TempDir("", "") mntDir, _ := ioutil.TempDir("", "")
files := map[string]string{ files := map[string]string{
"file": "content", "file": "content",
"subdir/other-file": "content", "subdir/other-file": "other-content",
} }
root := &Inode{} root := &Inode{}
server, err := Mount(mntDir, root, &Options{ populate := func(ctx context.Context) {
MountOptions: fuse.MountOptions{Debug: true},
OnAdd: func(ctx context.Context) {
for name, content := range files { for name, content := range files {
dir, base := filepath.Split(name) dir, base := filepath.Split(name)
p := root p := root
// Add directories leading up to the file.
for _, component := range strings.Split(dir, "/") { for _, component := range strings.Split(dir, "/") {
if len(component) == 0 { if len(component) == 0 {
continue continue
} }
ch := p.GetChild(component) ch := p.GetChild(component)
if ch == nil { if ch == nil {
// Create a directory
ch = p.NewPersistentInode(ctx, &Inode{}, ch = p.NewPersistentInode(ctx, &Inode{},
NodeAttr{Mode: syscall.S_IFDIR}) NodeAttr{Mode: syscall.S_IFDIR})
// Add it
p.AddChild(component, ch, true) p.AddChild(component, ch, true)
} }
p = ch p = ch
} }
// Create the file
child := p.NewPersistentInode(ctx, &MemRegularFile{ child := p.NewPersistentInode(ctx, &MemRegularFile{
Data: []byte(content), Data: []byte(content),
}, NodeAttr{}) }, NodeAttr{})
// And add it
p.AddChild(base, child, true) p.AddChild(base, child, true)
} }
}, }
server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{Debug: true},
// This adds read permissions to the files and
// directories, which is necessary for doing a chdir
// into the mount.
DefaultPermissions: true,
OnAdd: populate,
}) })
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
......
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