This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modemapfs.go
80 lines (69 loc) · 1.78 KB
/
modemapfs.go
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
// Copyright 2013 The Go 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 mapfs file provides an implementation of the FileSystem
// interface based on the contents of a map[string]string.
package vfs // import "github.com/thomasf/vfs"
import (
"fmt"
"os"
pathpkg "path"
"strings"
"github.com/pkg/errors"
)
// ModeMap wraps a FileSystem and adds custom FileMode return values for Stat calls.
func ModeMap(fs FileSystem, m map[string]os.FileMode) FileSystem {
return mapModeFS{fs, m}
}
func SafeModeMap(fs FileSystem, m map[string]os.FileMode) FileSystemFunc {
return func() (FileSystem, error) {
for path, _ := range m {
if strings.HasPrefix(path, "/") {
return nil, errors.Errorf("mount paths may not contain a leading '/': %s", path)
}
}
return mapModeFS{fs, m}, nil
}
}
type mapModeFS struct {
FileSystem
m map[string]os.FileMode
}
func (fs mapModeFS) String() string {
return fmt.Sprintf("modemap(%v)", fs.FileSystem.String())
}
func (fs mapModeFS) Lstat(p string) (os.FileInfo, error) {
fi, err := fs.FileSystem.Lstat(p)
if err != nil {
return nil, err
}
mode, ok := fs.m[filename(p)]
if ok {
return modeFileInfo(fi, mode), nil
}
return fi, nil
}
func (fs mapModeFS) Stat(p string) (os.FileInfo, error) {
fi, err := fs.FileSystem.Stat(p)
if err != nil {
return nil, err
}
mode, ok := fs.m[filename(p)]
if ok {
return modeFileInfo(fi, mode), nil
}
return fi, nil
}
func (fs mapModeFS) ReadDir(p string) ([]os.FileInfo, error) {
fis, err := fs.FileSystem.ReadDir(p)
if err != nil {
return fis, err
}
for i, fi := range fis {
mode, ok := fs.m[filename(pathpkg.Join(p, fi.Name()))]
if ok {
fis[i] = modeFileInfo(fi, mode)
}
}
return fis, nil
}