Debian Codegraph @

hugofs/filename_filter_fs.go

raw
1 // Copyright 2021 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hugofs
15
16 import (
17 "io/fs"
18 "os"
19 "strings"
20 "syscall"
21 "time"
22
23 "github.com/gohugoio/hugo/hugofs/hglob"
24 "github.com/spf13/afero"
25 )
26
27 var _ FilesystemUnwrapper = (*filenameFilterFs)(nil)
28
29 func newFilenameFilterFs(fs afero.Fs, base string, filter *hglob.FilenameFilter) afero.Fs {
30 return &filenameFilterFs{
31 fs: fs,
32 base: base,
33 filter: filter,
34 }
35 }
36
37 // filenameFilterFs is a filesystem that filters by filename.
38 type filenameFilterFs struct {
39 base string
40 fs afero.Fs
41
42 filter *hglob.FilenameFilter
43 }
44
45 func (fs *filenameFilterFs) UnwrapFilesystem() afero.Fs {
46 return fs.fs
47 }
48
49 func (fs *filenameFilterFs) Open(name string) (afero.File, error) {
50 fi, err := fs.fs.Stat(name)
51 if err != nil {
52 return nil, err
53 }
54
55 if !fs.filter.Match(name, fi.IsDir()) {
56 return nil, os.ErrNotExist
57 }
58
59 f, err := fs.fs.Open(name)
60 if err != nil {
61 return nil, err
62 }
63
64 if !fi.IsDir() {
65 return f, nil
66 }
67
68 return &filenameFilterDir{
69 File: f,
70 base: fs.base,
71 filter: fs.filter,
72 }, nil
73 }
74
75 func (fs *filenameFilterFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
76 return fs.Open(name)
77 }
78
79 func (fs *filenameFilterFs) Stat(name string) (os.FileInfo, error) {
80 fi, err := fs.fs.Stat(name)
81 if err != nil {
82 return nil, err
83 }
84 if !fs.filter.Match(name, fi.IsDir()) {
85 return nil, os.ErrNotExist
86 }
87 return fi, nil
88 }
89
90 type filenameFilterDir struct {
91 afero.File
92 base string
93 filter *hglob.FilenameFilter
94 }
95
96 func (f *filenameFilterDir) ReadDir(n int) ([]fs.DirEntry, error) {
97 des, err := f.File.(fs.ReadDirFile).ReadDir(n)
98 if err != nil {
99 return nil, err
100 }
101 i := 0
102 for _, de := range des {
103 fim := de.(FileMetaInfo)
104 rel := strings.TrimPrefix(fim.Meta().Filename, f.base)
105 if f.filter.Match(rel, de.IsDir()) {
106 des[i] = de
107 i++
108 }
109 }
110 return des[:i], nil
111 }
112
113 func (f *filenameFilterDir) Readdir(count int) ([]os.FileInfo, error) {
114 panic("not supported: Use ReadDir")
115 }
116
117 func (f *filenameFilterDir) Readdirnames(count int) ([]string, error) {
118 des, err := f.ReadDir(count)
119 if err != nil {
120 return nil, err
121 }
122
123 dirs := make([]string, len(des))
124 for i, d := range des {
125 dirs[i] = d.Name()
126 }
127 return dirs, nil
128 }
129
130 func (fs *filenameFilterFs) Chmod(n string, m os.FileMode) error {
131 return syscall.EPERM
132 }
133
134 func (fs *filenameFilterFs) Chtimes(n string, a, m time.Time) error {
135 return syscall.EPERM
136 }
137
138 func (fs *filenameFilterFs) Chown(n string, uid, gid int) error {
139 return syscall.EPERM
140 }
141
142 func (fs *filenameFilterFs) ReadDir(name string) ([]os.FileInfo, error) {
143 panic("not implemented")
144 }
145
146 func (fs *filenameFilterFs) Remove(n string) error {
147 return syscall.EPERM
148 }
149
150 func (fs *filenameFilterFs) RemoveAll(p string) error {
151 return syscall.EPERM
152 }
153
154 func (fs *filenameFilterFs) Rename(o, n string) error {
155 return syscall.EPERM
156 }
157
158 func (fs *filenameFilterFs) Create(n string) (afero.File, error) {
159 return nil, syscall.EPERM
160 }
161
162 func (fs *filenameFilterFs) Name() string {
163 return "FinameFilterFS"
164 }
165
166 func (fs *filenameFilterFs) Mkdir(n string, p os.FileMode) error {
167 return syscall.EPERM
168 }
169
170 func (fs *filenameFilterFs) MkdirAll(n string, p os.FileMode) error {
171 return syscall.EPERM
172 }