| 1 |
// Copyright 2019 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 |
"context" |
| 18 |
"fmt" |
| 19 |
"path/filepath" |
| 20 |
"sort" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/common/herrors" |
| 24 |
"github.com/gohugoio/hugo/common/loggers" |
| 25 |
"github.com/gohugoio/hugo/common/paths" |
| 26 |
"github.com/gohugoio/hugo/media" |
| 27 |
|
| 28 |
"github.com/spf13/afero" |
| 29 |
) |
| 30 |
|
| 31 |
type ( |
| 32 |
WalkFunc func(ctx context.Context, path string, info FileMetaInfo) error |
| 33 |
WalkHook func(ctx context.Context, dir FileMetaInfo, path string, readdir []FileMetaInfo) ([]FileMetaInfo, error) |
| 34 |
) |
| 35 |
|
| 36 |
type Walkway struct { |
| 37 |
logger loggers.Logger |
| 38 |
|
| 39 |
// Prevent a walkway to be walked more than once. |
| 40 |
walked bool |
| 41 |
|
| 42 |
// Config from client. |
| 43 |
cfg WalkwayConfig |
| 44 |
} |
| 45 |
|
| 46 |
type WalkwayConfig struct { |
| 47 |
// The filesystem to walk. |
| 48 |
Fs afero.Fs |
| 49 |
|
| 50 |
// The root to start from in Fs. |
| 51 |
Root string |
| 52 |
|
| 53 |
// The logger to use. |
| 54 |
Logger loggers.Logger |
| 55 |
PathParser *paths.PathParser |
| 56 |
|
| 57 |
// One or both of these may be pre-set. |
| 58 |
Info FileMetaInfo // The start info. |
| 59 |
DirEntries []FileMetaInfo // The start info's dir entries. |
| 60 |
Ctx context.Context // Optional starting context. |
| 61 |
|
| 62 |
IgnoreFile func(filename string) bool // Optional |
| 63 |
|
| 64 |
// Will be called in order. |
| 65 |
HookPre WalkHook // Optional. |
| 66 |
WalkFn WalkFunc |
| 67 |
HookPost WalkHook // Optional. |
| 68 |
|
| 69 |
// Some optional flags. |
| 70 |
FailOnNotExist bool // If set, return an error if a directory is not found. |
| 71 |
SortDirEntries bool // If set, sort the dir entries by Name before calling the WalkFn, default is ReaDir order. |
| 72 |
} |
| 73 |
|
| 74 |
func NewWalkway(cfg WalkwayConfig) *Walkway { |
| 75 |
if cfg.Fs == nil { |
| 76 |
panic("fs must be set") |
| 77 |
} |
| 78 |
|
| 79 |
if cfg.PathParser == nil { |
| 80 |
cfg.PathParser = media.DefaultPathParser |
| 81 |
} |
| 82 |
|
| 83 |
logger := cfg.Logger |
| 84 |
if logger == nil { |
| 85 |
logger = loggers.NewDefault() |
| 86 |
} |
| 87 |
|
| 88 |
return &Walkway{ |
| 89 |
cfg: cfg, |
| 90 |
logger: logger, |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func (w *Walkway) Walk() error { |
| 95 |
if w.walked { |
| 96 |
panic("this walkway is already walked") |
| 97 |
} |
| 98 |
w.walked = true |
| 99 |
|
| 100 |
if w.cfg.Fs == NoOpFs { |
| 101 |
return nil |
| 102 |
} |
| 103 |
|
| 104 |
ctx := w.cfg.Ctx |
| 105 |
if ctx == nil { |
| 106 |
ctx = context.Background() |
| 107 |
} |
| 108 |
|
| 109 |
return w.walk(ctx, w.cfg.Root, w.cfg.Info, w.cfg.DirEntries) |
| 110 |
} |
| 111 |
|
| 112 |
// checkErr returns true if the error is handled. |
| 113 |
func (w *Walkway) checkErr(filename string, err error) bool { |
| 114 |
if herrors.IsNotExist(err) && !w.cfg.FailOnNotExist { |
| 115 |
// The file may be removed in process. |
| 116 |
// This may be a ERROR situation, but it is not possible |
| 117 |
// to determine as a general case. |
| 118 |
w.logger.Warnf("File %q not found, skipping.", filename) |
| 119 |
return true |
| 120 |
} |
| 121 |
|
| 122 |
return false |
| 123 |
} |
| 124 |
|
| 125 |
// walk recursively descends path, calling walkFn. |
| 126 |
func (w *Walkway) walk(ctx context.Context, path string, info FileMetaInfo, dirEntries []FileMetaInfo) error { |
| 127 |
pathRel := strings.TrimPrefix(path, w.cfg.Root) |
| 128 |
|
| 129 |
if info == nil { |
| 130 |
var err error |
| 131 |
fi, err := w.cfg.Fs.Stat(path) |
| 132 |
if err != nil { |
| 133 |
if path == w.cfg.Root && herrors.IsNotExist(err) { |
| 134 |
return nil |
| 135 |
} |
| 136 |
if w.checkErr(path, err) { |
| 137 |
return nil |
| 138 |
} |
| 139 |
return fmt.Errorf("walk: stat: %s", err) |
| 140 |
} |
| 141 |
info = fi.(FileMetaInfo) |
| 142 |
} |
| 143 |
|
| 144 |
err := w.cfg.WalkFn(ctx, path, info) |
| 145 |
if err != nil { |
| 146 |
if info.IsDir() && err == filepath.SkipDir { |
| 147 |
return nil |
| 148 |
} |
| 149 |
return err |
| 150 |
} |
| 151 |
|
| 152 |
if !info.IsDir() { |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
if dirEntries == nil { |
| 157 |
f, err := w.cfg.Fs.Open(path) |
| 158 |
if err != nil { |
| 159 |
if w.checkErr(path, err) { |
| 160 |
return nil |
| 161 |
} |
| 162 |
return fmt.Errorf("walk: open: path: %q filename: %q: %s", path, info.Meta().Filename, err) |
| 163 |
} |
| 164 |
fis, newCtx, err := ReadDirWithContext(ctx, f, -1) |
| 165 |
ctx = newCtx |
| 166 |
|
| 167 |
f.Close() |
| 168 |
if err != nil { |
| 169 |
if w.checkErr(path, err) { |
| 170 |
return nil |
| 171 |
} |
| 172 |
return fmt.Errorf("walk: Readdir: %w", err) |
| 173 |
} |
| 174 |
|
| 175 |
dirEntries = DirEntriesToFileMetaInfos(fis) |
| 176 |
for _, fi := range dirEntries { |
| 177 |
if fi.Meta().PathInfo == nil { |
| 178 |
fi.Meta().PathInfo = w.cfg.PathParser.Parse("", filepath.Join(pathRel, fi.Name())) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if w.cfg.SortDirEntries { |
| 183 |
sort.Slice(dirEntries, func(i, j int) bool { |
| 184 |
return dirEntries[i].Name() < dirEntries[j].Name() |
| 185 |
}) |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
if w.cfg.IgnoreFile != nil { |
| 191 |
n := 0 |
| 192 |
for _, fi := range dirEntries { |
| 193 |
if !w.cfg.IgnoreFile(fi.Meta().Filename) { |
| 194 |
dirEntries[n] = fi |
| 195 |
n++ |
| 196 |
} |
| 197 |
} |
| 198 |
dirEntries = dirEntries[:n] |
| 199 |
} |
| 200 |
|
| 201 |
if w.cfg.HookPre != nil { |
| 202 |
var err error |
| 203 |
dirEntries, err = w.cfg.HookPre(ctx, info, path, dirEntries) |
| 204 |
if err != nil { |
| 205 |
if err == filepath.SkipDir { |
| 206 |
return nil |
| 207 |
} |
| 208 |
return err |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
for _, fim := range dirEntries { |
| 213 |
nextPath := filepath.Join(path, fim.Name()) |
| 214 |
err := w.walk(ctx, nextPath, fim, nil) |
| 215 |
if err != nil { |
| 216 |
if !fim.IsDir() || err != filepath.SkipDir { |
| 217 |
return err |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if w.cfg.HookPost != nil { |
| 223 |
var err error |
| 224 |
dirEntries, err = w.cfg.HookPost(ctx, info, path, dirEntries) |
| 225 |
if err != nil { |
| 226 |
if err == filepath.SkipDir { |
| 227 |
return nil |
| 228 |
} |
| 229 |
return err |
| 230 |
} |
| 231 |
} |
| 232 |
return nil |
| 233 |
} |