File: statfs.go

package info (click to toggle)
golang-github-hanwen-go-fuse 2.1.0%2Bgit20220822.58a7e14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,292 kB
  • sloc: cpp: 78; sh: 43; makefile: 16
file content (70 lines) | stat: -rw-r--r-- 1,377 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package benchmark

import (
	"context"
	"path/filepath"
	"strings"
	"syscall"

	"github.com/hanwen/go-fuse/v2/fs"
	"github.com/hanwen/go-fuse/v2/fuse"
)

type StatFS struct {
	fs.Inode

	files map[string]fuse.Attr
}

var _ = (fs.NodeOnAdder)((*StatFS)(nil))

func (r *StatFS) OnAdd(ctx context.Context) {
	for nm, a := range r.files {
		r.addFile(nm, a)
	}
	r.files = nil
}

func (r *StatFS) AddFile(name string, a fuse.Attr) {
	if r.files == nil {
		r.files = map[string]fuse.Attr{}
	}

	r.files[name] = a
}

func (r *StatFS) addFile(name string, a fuse.Attr) {
	dir, base := filepath.Split(name)

	p := r.EmbeddedInode()

	// Add directories leading up to the file.
	for _, component := range strings.Split(dir, "/") {
		if len(component) == 0 {
			continue
		}
		ch := p.GetChild(component)
		if ch == nil {
			// Create a directory
			ch = p.NewPersistentInode(context.Background(), &fs.Inode{},
				fs.StableAttr{Mode: syscall.S_IFDIR})
			// Add it
			p.AddChild(component, ch, true)
		}

		p = ch
	}

	// Create the file
	child := p.NewPersistentInode(context.Background(), &fs.MemRegularFile{
		Data: make([]byte, a.Size),
		Attr: a,
	}, fs.StableAttr{})

	// And add it
	p.AddChild(base, child, true)
}