File: union.go

package info (click to toggle)
golang-github-shurcool-httpfs 0.0~git20230704.f1e31cf-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (106 lines) | stat: -rw-r--r-- 2,936 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Package union offers a simple http.FileSystem that can unify multiple filesystems at various mount points.
package union

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
	"time"
)

// New creates an union filesystem with the provided mapping of mount points to filesystems.
//
// Each mount point must be of form "/mydir". It must start with a '/', and contain a single directory name.
func New(mapping map[string]http.FileSystem) http.FileSystem {
	u := &unionFS{
		ns: make(map[string]http.FileSystem),
		root: &dirInfo{
			name: "/",
		},
	}
	for mountPoint, fs := range mapping {
		u.bind(mountPoint, fs)
	}
	return u
}

type unionFS struct {
	ns   map[string]http.FileSystem // Key is mount point, e.g., "/mydir".
	root *dirInfo
}

// bind mounts fs at mountPoint.
// mountPoint must be of form "/mydir". It must start with a '/', and contain a single directory name.
func (u *unionFS) bind(mountPoint string, fs http.FileSystem) {
	u.ns[mountPoint] = fs
	u.root.entries = append(u.root.entries, &dirInfo{
		name: mountPoint[1:],
	})
}

// Open opens the named file.
func (u *unionFS) Open(path string) (http.File, error) {
	// TODO: Maybe clean path?
	if path == "/" {
		return &dir{
			dirInfo: u.root,
		}, nil
	}
	for prefix, fs := range u.ns {
		if path == prefix || strings.HasPrefix(path, prefix+"/") {
			innerPath := path[len(prefix):]
			if innerPath == "" {
				innerPath = "/"
			}
			return fs.Open(innerPath)
		}
	}
	return nil, &os.PathError{Op: "open", Path: path, Err: os.ErrNotExist}
}

// dirInfo is a static definition of a directory.
type dirInfo struct {
	name    string
	entries []os.FileInfo
}

func (d *dirInfo) Read([]byte) (int, error) {
	return 0, fmt.Errorf("cannot Read from directory %s", d.name)
}
func (d *dirInfo) Close() error               { return nil }
func (d *dirInfo) Stat() (os.FileInfo, error) { return d, nil }

func (d *dirInfo) Name() string       { return d.name }
func (d *dirInfo) Size() int64        { return 0 }
func (d *dirInfo) Mode() os.FileMode  { return 0755 | os.ModeDir }
func (d *dirInfo) ModTime() time.Time { return time.Time{} } // Actual mod time is not computed because it's expensive and rarely needed.
func (d *dirInfo) IsDir() bool        { return true }
func (d *dirInfo) Sys() interface{}   { return nil }

// dir is an opened dir instance.
type dir struct {
	*dirInfo
	pos int // Position within entries for Seek and Readdir.
}

func (d *dir) Seek(offset int64, whence int) (int64, error) {
	if offset == 0 && whence == io.SeekStart {
		d.pos = 0
		return 0, nil
	}
	return 0, fmt.Errorf("unsupported Seek in directory %s", d.dirInfo.name)
}

func (d *dir) Readdir(count int) ([]os.FileInfo, error) {
	if d.pos >= len(d.dirInfo.entries) && count > 0 {
		return nil, io.EOF
	}
	if count <= 0 || count > len(d.dirInfo.entries)-d.pos {
		count = len(d.dirInfo.entries) - d.pos
	}
	e := d.dirInfo.entries[d.pos : d.pos+count]
	d.pos += count
	return e, nil
}