-
Notifications
You must be signed in to change notification settings - Fork 0
/
filename_filter_fs.go
178 lines (146 loc) · 3.9 KB
/
filename_filter_fs.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2021 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hugofs
import (
"os"
"strings"
"syscall"
"time"
"github.com/spf13/afero"
"github.com/sunwei/hugo-playground/hugofs/glob"
)
var (
_ FilesystemUnwrapper = (*filenameFilterFs)(nil)
)
func newFilenameFilterFs(fs afero.Fs, base string, filter *glob.FilenameFilter) afero.Fs {
return &filenameFilterFs{
fs: fs,
base: base,
filter: filter,
}
}
// filenameFilterFs is a filesystem that filters by filename.
type filenameFilterFs struct {
base string
fs afero.Fs
filter *glob.FilenameFilter
}
func (fs *filenameFilterFs) UnwrapFilesystem() afero.Fs {
return fs.fs
}
func (fs *filenameFilterFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
fi, b, err := fs.fs.(afero.Lstater).LstatIfPossible(name)
if err != nil {
return nil, false, err
}
if !fs.filter.Match(name, fi.IsDir()) {
return nil, false, os.ErrNotExist
}
return fi, b, nil
}
func (fs *filenameFilterFs) Open(name string) (afero.File, error) {
fi, err := fs.fs.Stat(name)
if err != nil {
return nil, err
}
if !fs.filter.Match(name, fi.IsDir()) {
return nil, os.ErrNotExist
}
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return f, nil
}
return &filenameFilterDir{
File: f,
base: fs.base,
filter: fs.filter,
}, nil
}
func (fs *filenameFilterFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
return fs.Open(name)
}
func (fs *filenameFilterFs) Stat(name string) (os.FileInfo, error) {
fi, _, err := fs.LstatIfPossible(name)
return fi, err
}
func (fs *filenameFilterFs) getOpener(name string) func() (afero.File, error) {
return func() (afero.File, error) {
return fs.Open(name)
}
}
type filenameFilterDir struct {
afero.File
base string
filter *glob.FilenameFilter
}
func (f *filenameFilterDir) Readdir(count int) ([]os.FileInfo, error) {
fis, err := f.File.Readdir(-1)
if err != nil {
return nil, err
}
var result []os.FileInfo
for _, fi := range fis {
fim := fi.(FileMetaInfo)
if f.filter.Match(strings.TrimPrefix(fim.Meta().Filename, f.base), fim.IsDir()) {
result = append(result, fi)
}
}
return result, nil
}
func (f *filenameFilterDir) Readdirnames(count int) ([]string, error) {
dirsi, err := f.Readdir(count)
if err != nil {
return nil, err
}
dirs := make([]string, len(dirsi))
for i, d := range dirsi {
dirs[i] = d.Name()
}
return dirs, nil
}
func (fs *filenameFilterFs) Chmod(n string, m os.FileMode) error {
return syscall.EPERM
}
func (fs *filenameFilterFs) Chtimes(n string, a, m time.Time) error {
return syscall.EPERM
}
func (fs *filenameFilterFs) Chown(n string, uid, gid int) error {
return syscall.EPERM
}
func (fs *filenameFilterFs) ReadDir(name string) ([]os.FileInfo, error) {
panic("not implemented")
}
func (fs *filenameFilterFs) Remove(n string) error {
return syscall.EPERM
}
func (fs *filenameFilterFs) RemoveAll(p string) error {
return syscall.EPERM
}
func (fs *filenameFilterFs) Rename(o, n string) error {
return syscall.EPERM
}
func (fs *filenameFilterFs) Create(n string) (afero.File, error) {
return nil, syscall.EPERM
}
func (fs *filenameFilterFs) Name() string {
return "FinameFilterFS"
}
func (fs *filenameFilterFs) Mkdir(n string, p os.FileMode) error {
return syscall.EPERM
}
func (fs *filenameFilterFs) MkdirAll(n string, p os.FileMode) error {
return syscall.EPERM
}