summaryrefslogtreecommitdiffstats
path: root/hugofs
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-12-24 19:11:05 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-01-27 16:28:14 +0100
commit7285e74090852b5d52f25e577850fa75f4aa8573 (patch)
tree54d07cb4a7de2db5c89f2590266595f0aca6cbd6 /hugofs
parent5fd1e7490305570872d3899f5edda950903c5213 (diff)
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaningdevelop2024
There are some breaking changes in this commit, see #11455. Closes #11455 Closes #11549 This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build. The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate. A list of the notable new features: * A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server. * A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently. * You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs. We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections). Memory Limit * Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory. New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively. This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive): Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers. Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds. Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role). Fixes #10169 Fixes #10364 Fixes #10482 Fixes #10630 Fixes #10656 Fixes #10694 Fixes #10918 Fixes #11262 Fixes #11439 Fixes #11453 Fixes #11457 Fixes #11466 Fixes #11540 Fixes #11551 Fixes #11556 Fixes #11654 Fixes #11661 Fixes #11663 Fixes #11664 Fixes #11669 Fixes #11671 Fixes #11807 Fixes #11808 Fixes #11809 Fixes #11815 Fixes #11840 Fixes #11853 Fixes #11860 Fixes #11883 Fixes #11904 Fixes #7388 Fixes #7425 Fixes #7436 Fixes #7544 Fixes #7882 Fixes #7960 Fixes #8255 Fixes #8307 Fixes #8863 Fixes #8927 Fixes #9192 Fixes #9324
Diffstat (limited to 'hugofs')
-rw-r--r--hugofs/component_fs.go284
-rw-r--r--hugofs/decorators.go143
-rw-r--r--hugofs/dirsmerger.go (renamed from hugofs/language_merge.go)32
-rw-r--r--hugofs/fileinfo.go265
-rw-r--r--hugofs/fileinfo_test.go4
-rw-r--r--hugofs/filename_filter_fs.go50
-rw-r--r--hugofs/filename_filter_fs_test.go13
-rw-r--r--hugofs/files/classifier.go95
-rw-r--r--hugofs/files/classifier_test.go11
-rw-r--r--hugofs/filter_fs.go344
-rw-r--r--hugofs/filter_fs_test.go46
-rw-r--r--hugofs/fs.go40
-rw-r--r--hugofs/fs_test.go7
-rw-r--r--hugofs/glob.go17
-rw-r--r--hugofs/glob/filename_filter.go31
-rw-r--r--hugofs/glob/filename_filter_test.go2
-rw-r--r--hugofs/glob/glob.go12
-rw-r--r--hugofs/glob_test.go36
-rw-r--r--hugofs/hasbytes_fs.go3
-rw-r--r--hugofs/noop_fs.go49
-rw-r--r--hugofs/nosymlink_fs.go160
-rw-r--r--hugofs/nosymlink_test.go146
-rw-r--r--hugofs/openfiles_fs.go110
-rw-r--r--hugofs/rootmapping_fs.go446
-rw-r--r--hugofs/rootmapping_fs_test.go174
-rw-r--r--hugofs/slice_fs.go303
-rw-r--r--hugofs/walk.go260
-rw-r--r--hugofs/walk_test.go127
28 files changed, 1303 insertions, 1907 deletions
diff --git a/hugofs/component_fs.go b/hugofs/component_fs.go
new file mode 100644
index 000000000..c55f15957
--- /dev/null
+++ b/hugofs/component_fs.go
@@ -0,0 +1,284 @@
+// Copyright 2024 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 (
+ iofs "io/fs"
+ "os"
+ "path"
+ "runtime"
+ "sort"
+
+ "github.com/gohugoio/hugo/common/herrors"
+ "github.com/gohugoio/hugo/common/hstrings"
+ "github.com/gohugoio/hugo/common/paths"
+ "github.com/gohugoio/hugo/hugofs/files"
+ "github.com/spf13/afero"
+ "golang.org/x/text/unicode/norm"
+)
+
+// NewComponentFs creates a new component filesystem.
+func NewComponentFs(opts ComponentFsOptions) *componentFs {
+ if opts.Component == "" {
+ panic("ComponentFsOptions.PathParser.Component must be set")
+ }
+ if opts.Fs == nil {
+ panic("ComponentFsOptions.Fs must be set")
+ }
+ bfs := NewBasePathFs(opts.Fs, opts.Component)
+ return &componentFs{Fs: bfs, opts: opts}
+}
+
+var _ FilesystemUnwrapper = (*componentFs)(nil)
+
+// componentFs is a filesystem that holds one of the Hugo components, e.g. content, layouts etc.
+type componentFs struct {
+ afero.Fs
+
+ opts ComponentFsOptions
+}
+
+func (fs *componentFs) UnwrapFilesystem() afero.Fs {
+ return fs.Fs
+}
+
+type componentFsDir struct {
+ *noOpRegularFileOps
+ DirOnlyOps
+ name string // the name passed to Open
+ fs *componentFs
+}
+
+// ReadDir reads count entries from this virtual directorie and
+// sorts the entries according to the component filesystem rules.
+func (f *componentFsDir) ReadDir(count int) ([]iofs.DirEntry, error) {
+ fis, err := f.DirOnlyOps.(iofs.ReadDirFile).ReadDir(-1)
+ if err != nil {
+ return nil, err
+ }
+
+ // Filter out any symlinks.
+ n := 0
+ for _, fi := range fis {
+ // IsDir will always be false for symlinks.
+ keep := fi.IsDir()
+ if !keep {
+ // This is unfortunate, but is the only way to determine if it is a symlink.
+ info, err := fi.Info()
+ if err != nil {
+ if herrors.IsNotExist(err) {
+ continue
+ }
+ return nil, err
+ }
+ if info.Mode()&os.ModeSymlink == 0 {
+ keep = true
+ }
+ }
+ if keep {
+ fis[n] = fi
+ n++
+ }
+ }
+
+ fis = fis[:n]
+
+ for _, fi := range fis {
+ s := path.Join(f.name, fi.Name())
+ _ = f.fs.applyMeta(fi, s)
+
+ }
+
+ sort.Slice(fis, func(i, j int) bool {
+ fimi, fimj := fis[i].(FileMetaInfo), fis[j].(FileMetaInfo)
+ if fimi.IsDir() != fimj.IsDir() {
+ return fimi.IsDir()
+ }
+ fimim, fimjm := fimi.Meta(), fimj.Meta()
+
+ if fimim.ModuleOrdinal != fimjm.ModuleOrdinal {
+ switch f.fs.opts.Component {
+ case files.ComponentFolderI18n:
+ // The way the language files gets loaded means that
+ // we need to provide the least important files first (e.g. the theme files).
+ return fimim.ModuleOrdinal > fimjm.ModuleOrdinal
+ default:
+ return fimim.ModuleOrdinal < fimjm.ModuleOrdinal
+ }
+ }
+
+ pii, pij := fimim.PathInfo, fimjm.PathInfo
+ if pii != nil {
+ basei, basej := pii.Base(), pij.Base()
+ exti, extj := pii.Ext(), pij.Ext()
+ if f.fs.opts.Component == files.ComponentFolderContent {
+ // Pull bundles to the top.
+ if pii.IsBundle() != pij.IsBundle() {
+ return pii.IsBundle()
+ }
+ }
+
+ if exti != extj {
+ // This pulls .md above .html.
+ return exti > extj
+ }
+
+ if basei != basej {
+ return basei < basej
+ }
+ }
+
+ if fimim.Weight != fimjm.Weight {
+ return fimim.Weight > fimjm.Weight
+ }
+
+ return fimi.Name() < fimj.Name()
+ })
+
+ if f.fs.opts.Component == files.ComponentFolderContent {
+ // Finally filter out any duplicate content files, e.g. page.md and page.html.
+ n := 0
+ seen := map[hstrings.Tuple]bool{}
+ for _, fi := range fis {
+ fim := fi.(FileMetaInfo)
+ pi := fim.Meta().PathInfo
+ keep := fim.IsDir() || !pi.IsContent()
+
+ if !keep {
+ baseLang := hstrings.Tuple{First: pi.Base(), Second: fim.Meta().Lang}
+ if !seen[baseLang] {
+ keep = true
+ seen[baseLang] = true
+ }
+ }
+
+ if keep {
+ fis[n] = fi
+ n++
+ }
+ }
+
+ fis = fis[:n]
+ }
+
+ return fis, nil
+}
+
+func (f *componentFsDir) Stat() (iofs.FileInfo, error) {
+ fi, err := f.DirOnlyOps.Stat()
+ if err != nil {
+ return nil, err
+ }
+ return f.fs.applyMeta(fi, f.name), nil
+}
+
+func (fs *componentFs) Stat(name string) (os.FileInfo, error) {
+ fi, err := fs.Fs.Stat(name)
+ if err != nil {
+ return nil, err
+ }
+ return fs.applyMeta(fi, name), nil
+}
+
+func (fs *componentFs) applyMeta(fi FileNameIsDir, name string) FileMetaInfo {
+ if runtime.GOOS == "darwin" {
+ name = norm.NFC.String(name)
+ }
+ fim := fi.(FileMetaInfo)
+ meta := fim.Meta()
+ meta.PathInfo = fs.opts.PathParser.Parse(fs.opts.Component, name)
+ if !fim.IsDir() {
+ if fileLang := meta.PathInfo.Lang(); fileLang != "" {
+ // A valid lang set in filename.
+ // Give priority to myfile.sv.txt inside the sv filesystem.
+ meta.Weight++
+ meta.Lang = fileLang
+ }
+ }
+
+ if meta.Lang == "" {
+ meta.Lang = fs.opts.DefaultContentLanguage
+ }
+
+ langIdx, found := fs.opts.PathParser.LanguageIndex[meta.Lang]
+ if !found {
+ panic("no language found for " + meta.Lang)
+ }
+ meta.LangIndex = langIdx
+
+ if fi.IsDir() {
+ meta.OpenFunc = func() (afero.File, error) {
+ return fs.Open(name)
+ }
+ }
+
+ return fim
+}
+
+func (f *componentFsDir) Readdir(count int) ([]os.FileInfo, error) {
+ panic("not supported: Use ReadDir")
+}
+
+func (f *componentFsDir) Readdirnames(count int) ([]string, error) {
+ dirsi, err := f.DirOnlyOps.(iofs.ReadDirFile).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
+}
+
+type ComponentFsOptions struct {
+ // The filesystem where one or more components are mounted.
+ Fs afero.Fs
+
+ // The component name, e.g. "content", "layouts" etc.
+ Component string
+
+ DefaultContentLanguage string
+
+ // The parser used to parse paths provided by this filesystem.
+ PathParser paths.PathParser
+}
+
+func (fs *componentFs) Open(name string) (afero.File, error) {
+ f, err := fs.Fs.Open(name)
+ if err != nil {
+ return nil, err
+ }
+
+ fi, err := f.Stat()
+ if err != nil {
+ if err != errIsDir {
+ f.Close()
+ return nil, err
+ }
+ } else if !fi.IsDir() {
+ return f, nil
+ }
+
+ return &componentFsDir{
+ DirOnlyOps: f,
+ name: name,
+ fs: fs,
+ }, nil
+}
+
+func (fs *componentFs) ReadDir(name string) ([]os.FileInfo, error) {
+ panic("not implemented")
+}
diff --git a/hugofs/decorators.go b/hugofs/decorators.go
index 47b4266df..405c81ce4 100644
--- a/hugofs/decorators.go
+++ b/hugofs/decorators.go
@@ -15,63 +15,25 @@ package hugofs
import (
"fmt"
+ "io/fs"
"os"
"path/filepath"
- "strings"
- "github.com/gohugoio/hugo/common/herrors"
"github.com/spf13/afero"
)
-var (
- _ FilesystemUnwrapper = (*baseFileDecoratorFs)(nil)
-)
+var _ FilesystemUnwrapper = (*baseFileDecoratorFs)(nil)
func decorateDirs(fs afero.Fs, meta *FileMeta) afero.Fs {
ffs := &baseFileDecoratorFs{Fs: fs}
- decorator := func(fi os.FileInfo, name string) (os.FileInfo, error) {
+ decorator := func(fi FileNameIsDir, name string) (FileNameIsDir, error) {
if !fi.IsDir() {
// Leave regular files as they are.
return fi, nil
}
- return decorateFileInfo(fi, fs, nil, "", "", meta), nil
- }
-
- ffs.decorate = decorator
-
- return ffs
-}
-
-func decoratePath(fs afero.Fs, createPath func(name string) string) afero.Fs {
- ffs := &baseFileDecoratorFs{Fs: fs}
-
- decorator := func(fi os.FileInfo, name string) (os.FileInfo, error) {
- path := createPath(name)
-
- return decorateFileInfo(fi, fs, nil, "", path, nil), nil
- }
-
- ffs.decorate = decorator
-
- return ffs
-}
-
-// DecorateBasePathFs adds Path info to files and directories in the
-// provided BasePathFs, using the base as base.
-func DecorateBasePathFs(base *afero.BasePathFs) afero.Fs {
- basePath, _ := base.RealPath("")
- if !strings.HasSuffix(basePath, filepathSeparator) {
- basePath += filepathSeparator
- }
-
- ffs := &baseFileDecoratorFs{Fs: base}
-
- decorator := func(fi os.FileInfo, name string) (os.FileInfo, error) {
- path := strings.TrimPrefix(name, basePath)
-
- return decorateFileInfo(fi, base, nil, "", path, nil), nil
+ return decorateFileInfo(fi, nil, "", meta), nil
}
ffs.decorate = decorator
@@ -84,7 +46,7 @@ func DecorateBasePathFs(base *afero.BasePathFs) afero.Fs {
func NewBaseFileDecorator(fs afero.Fs, callbacks ...func(fi FileMetaInfo)) afero.Fs {
ffs := &baseFileDecoratorFs{Fs: fs}
- decorator := func(fi os.FileInfo, filename string) (os.FileInfo, error) {
+ decorator := func(fi FileNameIsDir, filename string) (FileNameIsDir, error) {
// Store away the original in case it's a symlink.
meta := NewFileMeta()
meta.Name = fi.Name()
@@ -92,38 +54,24 @@ func NewBaseFileDecorator(fs afero.Fs, callbacks ...func(fi FileMetaInfo)) afero
if fi.IsDir() {
meta.JoinStatFunc = func(name string) (FileMetaInfo, error) {
joinedFilename := filepath.Join(filename, name)
- fi, _, err := lstatIfPossible(fs, joinedFilename)
+ fi, err := fs.Stat(joinedFilename)
if err != nil {
return nil, err
}
-
- fi, err = ffs.decorate(fi, joinedFilename)
+ fim, err := ffs.decorate(fi, joinedFilename)
if err != nil {
return nil, err
}
- return fi.(FileMetaInfo), nil
- }
- }
-
- isSymlink := isSymlink(fi)
- if isSymlink {
- meta.OriginalFilename = filename
- var link string
- var err error
- link, fi, err = evalSymlinks(fs, filename)
- if err != nil {
- return nil, err
+ return fim.(FileMetaInfo), nil
}
- filename = link
- meta.IsSymlink = true
}
opener := func() (afero.File, error) {
return ffs.open(filename)
}
- fim := decorateFileInfo(fi, ffs, opener, filename, "", meta)
+ fim := decorateFileInfo(fi, opener, filename, meta)
for _, cb := range callbacks {
cb(fim)
@@ -136,23 +84,9 @@ func NewBaseFileDecorator(fs afero.Fs, callbacks ...func(fi FileMetaInfo)) afero
return ffs
}
-func evalSymlinks(fs afero.Fs, filename string) (string, os.FileInfo, error) {
- link, err := filepath.EvalSymlinks(filename)
- if err != nil {
- return "", nil, err
- }
-
- fi, err := fs.Stat(link)
- if err != nil {
- return "", nil, err
- }
-
- return link, fi, nil
-}
-
type baseFileDecoratorFs struct {
afero.Fs
- decorate func(fi os.FileInfo, filename string) (os.FileInfo, error)
+ decorate func(fi FileNameIsDir, name string) (FileNameIsDir, error)
}
func (fs *baseFileDecoratorFs) UnwrapFilesystem() afero.Fs {
@@ -165,29 +99,11 @@ func (fs *baseFileDecoratorFs) Stat(name string) (os.FileInfo, error) {
return nil, err
}
- return fs.decorate(fi, name)
-}
-
-func (fs *baseFileDecoratorFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
- var (
- fi os.FileInfo
- err error
- ok bool
- )
-
- if lstater, isLstater := fs.Fs.(afero.Lstater); isLstater {
- fi, ok, err = lstater.LstatIfPossible(name)
- } else {
- fi, err = fs.Fs.Stat(name)
- }
-
+ fim, err := fs.decorate(fi, name)
if err != nil {
- return nil, false, err
+ return nil, err
}
-
- fi, err = fs.decorate(fi, name)
-
- return fi, ok, err
+ return fim.(os.FileInfo), nil
}
func (fs *baseFileDecoratorFs) Open(name string) (afero.File, error) {
@@ -207,35 +123,32 @@ type baseFileDecoratorFile struct {
fs *baseFileDecoratorFs
}
-func (l *baseFileDecoratorFile) Readdir(c int) (ofi []os.FileInfo, err error) {
- dirnames, err := l.File.Readdirnames(c)
+func (l *baseFileDecoratorFile) ReadDir(n int) ([]fs.DirEntry, error) {
+ fis, err := l.File.(fs.ReadDirFile).ReadDir(-1)
if err != nil {
return nil, err
}
- fisp := make([]os.FileInfo, 0, len(dirnames))
+ fisp := make([]fs.DirEntry, len(fis))
- for _, dirname := range dirnames {
- filename := dirname
-
- if l.Name() != "" && l.Name() != filepathSeparator {
- filename = filepath.Join(l.Name(), dirname)
+ for i, fi := range fis {
+ filename := fi.Name()
+ if l.Name() != "" {
+ filename = filepath.Join(l.Name(), fi.Name())
}
- // We need to resolve any symlink info.
- fi, _, err := lstatIfPossible(l.fs.Fs, filename)
- if err != nil {
- if herrors.IsNotExist(err) {
- continue
- }
- return nil, err
- }
- fi, err = l.fs.decorate(fi, filename)
+ fid, err := l.fs.decorate(fi, filename)
if err != nil {
return nil, fmt.Errorf("decorate: %w", err)
}
- fisp = append(fisp, fi)
+
+ fisp[i] = fid.(fs.DirEntry)
+
}
return fisp, err
}
+
+func (l *baseFileDecoratorFile) Readdir(c int) (ofi []os.FileInfo, err error) {
+ panic("not supported: Use ReadDir")
+}
diff --git a/hugofs/language_merge.go b/hugofs/dirsmerger.go
index a2fa411a9..392353e27 100644
--- a/hugofs/language_merge.go
+++ b/hugofs/dirsmerger.go
@@ -1,4 +1,4 @@
-// Copyright 2022 The Hugo Authors. All rights reserved.
+// Copyright 2024 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.
@@ -14,12 +14,14 @@
package hugofs
import (
- "os"
+ "io/fs"
+
+ "github.com/bep/overlayfs"
)
// LanguageDirsMerger implements the overlayfs.DirsMerger func, which is used
// to merge two directories.
-var LanguageDirsMerger = func(lofi, bofi []os.FileInfo) []os.FileInfo {
+var LanguageDirsMerger overlayfs.DirsMerger = func(lofi, bofi []fs.DirEntry) []fs.DirEntry {
for _, fi1 := range bofi {
fim1 := fi1.(FileMetaInfo)
var found bool
@@ -37,3 +39,27 @@ var LanguageDirsMerger = func(lofi, bofi []os.FileInfo) []os.FileInfo {
return lofi
}
+
+// AppendDirsMerger merges two directories keeping all regular files
+// with the first slice as the base.
+// Duplicate directories in the secnond slice will be ignored.
+// This strategy is used for the i18n and data fs where we need all entries.
+var AppendDirsMerger overlayfs.DirsMerger = func(lofi, bofi []fs.DirEntry) []fs.DirEntry {
+ for _, fi1 := range bofi {
+ var found bool
+ // Remove duplicate directories.
+ if fi1.IsDir() {
+ for _, fi2 := range lofi {
+ if fi2.IsDir() && fi2.Name() == fi1.Name() {
+ found = true
+ break
+ }
+ }
+ }
+ if !found {
+ lofi = append(lofi, fi1)
+ }
+ }
+
+ return lofi
+}
diff --git a/hugofs/fileinfo.go b/hugofs/fileinfo.go
index 773352ea8..6d6122c0c 100644
--- a/hugofs/fileinfo.go
+++ b/hugofs/fileinfo.go
@@ -16,21 +16,25 @@ package hugofs
import (
"errors"
+ "fmt"
+ "io"
+ "io/fs"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
- "strings"
+ "sync"
"time"
"github.com/gohugoio/hugo/hugofs/glob"
- "github.com/gohugoio/hugo/hugofs/files"
"golang.org/x/text/unicode/norm"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/hreflect"
"github.com/gohugoio/hugo/common/htime"
+ "github.com/gohugoio/hugo/common/paths"
"github.com/spf13/afero"
)
@@ -39,48 +43,37 @@ func NewFileMeta() *FileMeta {
return &FileMeta{}
}
-// PathFile returns the relative file path for the file source.
-func (f *FileMeta) PathFile() string {
- if f.BaseDir == "" {
- return ""
- }
- return strings.TrimPrefix(strings.TrimPrefix(f.Filename, f.BaseDir), filepathSeparator)
-}
-
type FileMeta struct {
- Name string
- Filename string
- Path string
- PathWalk string
- OriginalFilename string
- BaseDir string
-
- SourceRoot string
- MountRoot string
- Module string
-
- Weight int
- IsOrdered bool
- IsSymlink bool
- IsRootFile bool
- IsProject bool
- Watch bool
-
- Classifier files.ContentClass
-
- SkipDir bool
-
- Lang string
- TranslationBaseName string
- TranslationBaseNameWithExt string
- Translations []string
-
- Fs afero.Fs
+ PathInfo *paths.Path
+ Name string
+ Filename string
+
+ BaseDir string
+ SourceRoot string
+ Module string
+ ModuleOrdinal int
+ Component string
+
+ Weight int
+ IsProject bool
+ Watch bool
+
+ // The lang associated with this file. This may be
+ // either the language set in the filename or
+ // the language defined in the source mount configuration.
+ Lang string
+ // The language index for the above lang. This is the index
+ // in the sorted list of languages/sites.
+ LangIndex int
+
OpenFunc func() (afero.File, error)
JoinStatFunc func(name string) (FileMetaInfo, error)
// Include only files or directories that match.
InclusionFilter *glob.FilenameFilter
+
+ // Rename the name part of the file (not the directory).
+ Rename func(name string, toFrom bool) string
}
func (m *FileMeta) Copy() *FileMeta {
@@ -120,6 +113,15 @@ func (f *FileMeta) Open() (afero.File, error) {
return f.OpenFunc()
}
+func (f *FileMeta) ReadAll() ([]byte, error) {
+ file, err := f.Open()
+ if err != nil {
+ return nil, err
+ }
+ defer file.Close()
+ return io.ReadAll(file)
+}
+
func (f *FileMeta) JoinStat(name string) (FileMetaInfo, error) {
if f.JoinStatFunc == nil {
return nil, os.ErrNotExist
@@ -128,50 +130,123 @@ func (f *FileMeta) JoinStat(name string) (FileMetaInfo, error) {
}
type FileMetaInfo interface {
- os.FileInfo
- // Meta is for internal use.
+ fs.DirEntry
+ MetaProvider
+
+ // This is a real hybrid as it also implements the fs.FileInfo interface.
+ FileInfoOptionals
+}
+
+type MetaProvider interface {
Meta() *FileMeta
}