| 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 provides the file systems used by Hugo. |
| 15 |
package hugofs |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"io/fs" |
| 22 |
"os" |
| 23 |
"path/filepath" |
| 24 |
"reflect" |
| 25 |
"runtime" |
| 26 |
"sort" |
| 27 |
"sync" |
| 28 |
"time" |
| 29 |
|
| 30 |
"github.com/gohugoio/hugo/hugofs/hglob" |
| 31 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 32 |
|
| 33 |
"golang.org/x/text/unicode/norm" |
| 34 |
|
| 35 |
"github.com/gohugoio/hugo/common/herrors" |
| 36 |
"github.com/gohugoio/hugo/common/hreflect" |
| 37 |
"github.com/gohugoio/hugo/common/htime" |
| 38 |
"github.com/gohugoio/hugo/common/hugio" |
| 39 |
"github.com/gohugoio/hugo/common/paths" |
| 40 |
|
| 41 |
"github.com/spf13/afero" |
| 42 |
) |
| 43 |
|
| 44 |
func NewFileMeta() *FileMeta { |
| 45 |
return &FileMeta{} |
| 46 |
} |
| 47 |
|
| 48 |
// A subset of modules.Module used to avoid circular dependencies. |
| 49 |
// Note that it's safe to type assert to the full modules.Module when needed. |
| 50 |
type Module interface { |
| 51 |
// Returns the path to this module. |
| 52 |
// This will either be the module path, e.g. "github.com/gohugoio/myshortcodes", |
| 53 |
// or the path below your /theme folder, e.g. "mytheme". |
| 54 |
Path() string |
| 55 |
|
| 56 |
// Directory holding files for this module. |
| 57 |
Dir() string |
| 58 |
|
| 59 |
// Returns whether this is a Go Module. |
| 60 |
IsGoMod() bool |
| 61 |
|
| 62 |
// The module version. |
| 63 |
Version() string |
| 64 |
} |
| 65 |
|
| 66 |
// FileMeta holds metadata about a file or directory. |
| 67 |
type FileMeta struct { |
| 68 |
PathInfo *paths.Path |
| 69 |
Name string |
| 70 |
Filename string |
| 71 |
|
| 72 |
BaseDir string |
| 73 |
SourceRoot string |
| 74 |
Module Module |
| 75 |
ModuleOrdinal int |
| 76 |
Component string |
| 77 |
|
| 78 |
Weight int |
| 79 |
IsProject bool |
| 80 |
Watch bool |
| 81 |
|
| 82 |
// This file/directory will be built for these sites. |
| 83 |
SitesMatrix sitesmatrix.VectorStore |
| 84 |
|
| 85 |
// This file/directory complements these other sites. |
| 86 |
SitesComplements sitesmatrix.VectorStore |
| 87 |
|
| 88 |
OpenFunc func() (afero.File, error) |
| 89 |
JoinStatFunc func(name string) (FileMetaInfo, error) |
| 90 |
|
| 91 |
// Include only files or directories that match. |
| 92 |
InclusionFilter *hglob.FilenameFilter |
| 93 |
|
| 94 |
// Rename the name part of the file (not the directory). |
| 95 |
// Returns the new name and a boolean indicating if the file |
| 96 |
// should be included. |
| 97 |
Rename func(name string, toFrom bool) (string, bool) |
| 98 |
} |
| 99 |
|
| 100 |
func (m *FileMeta) Copy() *FileMeta { |
| 101 |
if m == nil { |
| 102 |
return NewFileMeta() |
| 103 |
} |
| 104 |
c := *m |
| 105 |
return &c |
| 106 |
} |
| 107 |
|
| 108 |
func (m *FileMeta) Merge(from *FileMeta) { |
| 109 |
if m == nil || from == nil { |
| 110 |
return |
| 111 |
} |
| 112 |
dstv := reflect.Indirect(reflect.ValueOf(m)) |
| 113 |
srcv := reflect.Indirect(reflect.ValueOf(from)) |
| 114 |
|
| 115 |
for i := range dstv.NumField() { |
| 116 |
v := dstv.Field(i) |
| 117 |
if !v.CanSet() { |
| 118 |
continue |
| 119 |
} |
| 120 |
if !hreflect.IsTruthfulValue(v) { |
| 121 |
v.Set(srcv.Field(i)) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if m.InclusionFilter == nil { |
| 126 |
m.InclusionFilter = from.InclusionFilter |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func (f *FileMeta) Open() (afero.File, error) { |
| 131 |
if f.OpenFunc == nil { |
| 132 |
return nil, errors.New("OpenFunc not set") |
| 133 |
} |
| 134 |
return f.OpenFunc() |
| 135 |
} |
| 136 |
|
| 137 |
func (f *FileMeta) ReadAll() ([]byte, error) { |
| 138 |
file, err := f.Open() |
| 139 |
if err != nil { |
| 140 |
return nil, err |
| 141 |
} |
| 142 |
defer file.Close() |
| 143 |
return hugio.ReadAll(file) |
| 144 |
} |
| 145 |
|
| 146 |
func (f *FileMeta) JoinStat(name string) (FileMetaInfo, error) { |
| 147 |
if f.JoinStatFunc == nil { |
| 148 |
return nil, os.ErrNotExist |
| 149 |
} |
| 150 |
return f.JoinStatFunc(name) |
| 151 |
} |
| 152 |
|
| 153 |
func (m *FileMeta) ModulePath() string { |
| 154 |
if m.Module == nil { |
| 155 |
return "" |
| 156 |
} |
| 157 |
return m.Module.Path() |
| 158 |
} |
| 159 |
|
| 160 |
func (m *FileMeta) MatchSiteVectorCoarse(v sitesmatrix.Vector) bool { |
| 161 |
language := v.Language() |
| 162 |
if !(m.SitesMatrix.HasLanguage(language) || m.SitesComplements.HasLanguage(language)) { |
| 163 |
return false |
| 164 |
} |
| 165 |
|
| 166 |
return m.MatchSiteVectorCoarseExcludeLanguage(v) |
| 167 |
} |
| 168 |
|
| 169 |
func (m *FileMeta) MatchSiteVectorCoarseExcludeLanguage(v sitesmatrix.Vector) bool { |
| 170 |
version := v.Version() |
| 171 |
if !(m.SitesMatrix.HasVersion(version) || m.SitesComplements.HasVersion(version)) { |
| 172 |
return false |
| 173 |
} |
| 174 |
|
| 175 |
role := v.Role() |
| 176 |
// lint:ignore S1008 preserve the symmetry from above. |
| 177 |
if !(m.SitesMatrix.HasRole(role) || m.SitesComplements.HasRole(role)) { |
| 178 |
return false |
| 179 |
} |
| 180 |
|
| 181 |
return true |
| 182 |
} |
| 183 |
|
| 184 |
type FileMetaInfo interface { |
| 185 |
fs.DirEntry |
| 186 |
MetaProvider |
| 187 |
|
| 188 |
// This is a real hybrid as it also implements the fs.FileInfo interface. |
| 189 |
FileInfoOptionals |
| 190 |
} |
| 191 |
|
| 192 |
type MetaProvider interface { |
| 193 |
Meta() *FileMeta |
| 194 |
} |
| 195 |
|
| 196 |
type FileInfoOptionals interface { |
| 197 |
Size() int64 |
| 198 |
Mode() fs.FileMode |
| 199 |
ModTime() time.Time |
| 200 |
Sys() any |
| 201 |
} |
| 202 |
|
| 203 |
type FileNameIsDir interface { |
| 204 |
Name() string |
| 205 |
IsDir() bool |
| 206 |
} |
| 207 |
|
| 208 |
type FileInfoProvider interface { |
| 209 |
FileInfo() FileMetaInfo |
| 210 |
} |
| 211 |
|
| 212 |
// DirOnlyOps is a subset of the afero.File interface covering |
| 213 |
// the methods needed for directory operations. |
| 214 |
type DirOnlyOps interface { |
| 215 |
io.Closer |
| 216 |
Name() string |
| 217 |
Readdir(count int) ([]os.FileInfo, error) |
| 218 |
Readdirnames(n int) ([]string, error) |
| 219 |
Stat() (os.FileInfo, error) |
| 220 |
} |
| 221 |
|
| 222 |
type dirEntryMeta struct { |
| 223 |
fs.DirEntry |
| 224 |
m *FileMeta |
| 225 |
|
| 226 |
fi fs.FileInfo |
| 227 |
fiInit sync.Once |
| 228 |
} |
| 229 |
|
| 230 |
func (fi *dirEntryMeta) Meta() *FileMeta { |
| 231 |
return fi.m |
| 232 |
} |
| 233 |
|
| 234 |
// Filename returns the full filename. |
| 235 |
func (fi *dirEntryMeta) Filename() string { |
| 236 |
return fi.m.Filename |
| 237 |
} |
| 238 |
|
| 239 |
func (fi *dirEntryMeta) fileInfo() fs.FileInfo { |
| 240 |
var err error |
| 241 |
fi.fiInit.Do(func() { |
| 242 |
fi.fi, err = fi.DirEntry.Info() |
| 243 |
}) |
| 244 |
if err != nil { |
| 245 |
panic(err) |
| 246 |
} |
| 247 |
return fi.fi |
| 248 |
} |
| 249 |
|
| 250 |
func (fi *dirEntryMeta) Size() int64 { |
| 251 |
return fi.fileInfo().Size() |
| 252 |
} |
| 253 |
|
| 254 |
func (fi *dirEntryMeta) Mode() fs.FileMode { |
| 255 |
return fi.fileInfo().Mode() |
| 256 |
} |
| 257 |
|
| 258 |
func (fi *dirEntryMeta) ModTime() time.Time { |
| 259 |
return fi.fileInfo().ModTime() |
| 260 |
} |
| 261 |
|
| 262 |
func (fi *dirEntryMeta) Sys() any { |
| 263 |
return fi.fileInfo().Sys() |
| 264 |
} |
| 265 |
|
| 266 |
// Name returns the file's name. |
| 267 |
func (fi *dirEntryMeta) Name() string { |
| 268 |
if name := fi.m.Name; name != "" { |
| 269 |
return name |
| 270 |
} |
| 271 |
return fi.DirEntry.Name() |
| 272 |
} |
| 273 |
|
| 274 |
// dirEntry is an adapter from os.FileInfo to fs.DirEntry |
| 275 |
type dirEntry struct { |
| 276 |
fs.FileInfo |
| 277 |
} |
| 278 |
|
| 279 |
var _ fs.DirEntry = dirEntry{} |
| 280 |
|
| 281 |
func (d dirEntry) Type() fs.FileMode { return d.FileInfo.Mode().Type() } |
| 282 |
|
| 283 |
func (d dirEntry) Info() (fs.FileInfo, error) { return d.FileInfo, nil } |
| 284 |
|
| 285 |
func NewFileMetaInfo(fi FileNameIsDir, m *FileMeta) FileMetaInfo { |
| 286 |
if m == nil { |
| 287 |
panic("FileMeta must be set") |
| 288 |
} |
| 289 |
if fim, ok := fi.(MetaProvider); ok { |
| 290 |
m.Merge(fim.Meta()) |
| 291 |
} |
| 292 |
switch v := fi.(type) { |
| 293 |
case fs.DirEntry: |
| 294 |
return &dirEntryMeta{DirEntry: v, m: m} |
| 295 |
case fs.FileInfo: |
| 296 |
return &dirEntryMeta{DirEntry: dirEntry{v}, m: m} |
| 297 |
case nil: |
| 298 |
return &dirEntryMeta{DirEntry: dirEntry{}, m: m} |
| 299 |
default: |
| 300 |
panic(fmt.Sprintf("Unsupported type: %T", fi)) |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
type dirNameOnlyFileInfo struct { |
| 305 |
name string |
| 306 |
modTime time.Time |
| 307 |
} |
| 308 |
|
| 309 |
func (fi *dirNameOnlyFileInfo) Name() string { |
| 310 |
return fi.name |
| 311 |
} |
| 312 |
|
| 313 |
func (fi *dirNameOnlyFileInfo) Size() int64 { |
| 314 |
panic("not implemented") |
| 315 |
} |
| 316 |
|
| 317 |
func (fi *dirNameOnlyFileInfo) Mode() os.FileMode { |
| 318 |
return os.ModeDir |
| 319 |
} |
| 320 |
|
| 321 |
func (fi *dirNameOnlyFileInfo) ModTime() time.Time { |
| 322 |
return fi.modTime |
| 323 |
} |
| 324 |
|
| 325 |
func (fi *dirNameOnlyFileInfo) IsDir() bool { |
| 326 |
return true |
| 327 |
} |
| 328 |
|
| 329 |
func (fi *dirNameOnlyFileInfo) Sys() any { |
| 330 |
return nil |
| 331 |
} |
| 332 |
|
| 333 |
func newDirNameOnlyFileInfo(name string, meta *FileMeta, fileOpener func() (afero.File, error)) FileMetaInfo { |
| 334 |
name = normalizeFilename(name) |
| 335 |
_, base := filepath.Split(name) |
| 336 |
|
| 337 |
m := meta.Copy() |
| 338 |
if m.Filename == "" { |
| 339 |
m.Filename = name |
| 340 |
} |
| 341 |
m.OpenFunc = fileOpener |
| 342 |
|
| 343 |
return NewFileMetaInfo( |
| 344 |
&dirNameOnlyFileInfo{name: base, modTime: htime.Now()}, |
| 345 |
m, |
| 346 |
) |
| 347 |
} |
| 348 |
|
| 349 |
func decorateFileInfo(fi FileNameIsDir, opener func() (afero.File, error), filename string, inMeta *FileMeta) FileMetaInfo { |
| 350 |
var meta *FileMeta |
| 351 |
var fim FileMetaInfo |
| 352 |
|
| 353 |
var ok bool |
| 354 |
if fim, ok = fi.(FileMetaInfo); ok { |
| 355 |
meta = fim.Meta() |
| 356 |
} else { |
| 357 |
meta = NewFileMeta() |
| 358 |
fim = NewFileMetaInfo(fi, meta) |
| 359 |
} |
| 360 |
|
| 361 |
if opener != nil { |
| 362 |
meta.OpenFunc = opener |
| 363 |
} |
| 364 |
|
| 365 |
nfilename := normalizeFilename(filename) |
| 366 |
if nfilename != "" { |
| 367 |
meta.Filename = nfilename |
| 368 |
} |
| 369 |
|
| 370 |
meta.Merge(inMeta) |
| 371 |
|
| 372 |
return fim |
| 373 |
} |
| 374 |
|
| 375 |
func DirEntriesToFileMetaInfos(fis []fs.DirEntry) []FileMetaInfo { |
| 376 |
fims := make([]FileMetaInfo, len(fis)) |
| 377 |
for i, v := range fis { |
| 378 |
fim := v.(FileMetaInfo) |
| 379 |
fims[i] = fim |
| 380 |
} |
| 381 |
return fims |
| 382 |
} |
| 383 |
|
| 384 |
func normalizeFilename(filename string) string { |
| 385 |
if filename == "" { |
| 386 |
return "" |
| 387 |
} |
| 388 |
if runtime.GOOS == "darwin" { |
| 389 |
// When a file system is HFS+, its filepath is in NFD form. |
| 390 |
return norm.NFC.String(filename) |
| 391 |
} |
| 392 |
return filename |
| 393 |
} |
| 394 |
|
| 395 |
func sortDirEntries(fis []fs.DirEntry) { |
| 396 |
sort.Slice(fis, func(i, j int) bool { |
| 397 |
fimi, fimj := fis[i].(FileMetaInfo), fis[j].(FileMetaInfo) |
| 398 |
return fimi.Meta().Filename < fimj.Meta().Filename |
| 399 |
}) |
| 400 |
} |
| 401 |
|
| 402 |
// AddFileInfoToError adds file info to the given error. |
| 403 |
func AddFileInfoToError(err error, fi FileMetaInfo, fs afero.Fs) error { |
| 404 |
if err == nil { |
| 405 |
return nil |
| 406 |
} |
| 407 |
|
| 408 |
meta := fi.Meta() |
| 409 |
filename := meta.Filename |
| 410 |
|
| 411 |
// Check if it's already added. |
| 412 |
for _, ferr := range herrors.UnwrapFileErrors(err) { |
| 413 |
pos := ferr.Position() |
| 414 |
errfilename := pos.Filename |
| 415 |
if errfilename == "" { |
| 416 |
pos.Filename = filename |
| 417 |
ferr.UpdatePosition(pos) |
| 418 |
} |
| 419 |
|
| 420 |
if errfilename == "" || errfilename == filename { |
| 421 |
if filename != "" && ferr.ErrorContext() == nil { |
| 422 |
f, ioerr := fs.Open(filename) |
| 423 |
if ioerr != nil { |
| 424 |
return err |
| 425 |
} |
| 426 |
defer f.Close() |
| 427 |
ferr.UpdateContent(f, nil) |
| 428 |
} |
| 429 |
return err |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
lineMatcher := herrors.NopLineMatcher |
| 434 |
|
| 435 |
if textSegmentErr, ok := err.(*herrors.TextSegmentError); ok { |
| 436 |
lineMatcher = herrors.ContainsMatcher(textSegmentErr.Segment) |
| 437 |
} |
| 438 |
|
| 439 |
return herrors.NewFileErrorFromFile(err, filename, fs, lineMatcher) |
| 440 |
} |