package hugofs

import "github.com/gohugoio/hugo/hugofs"

Package hugofs provides the file systems used by Hugo.

Package hugofs provides the file systems used by Hugo.

Index

Variables

var AppendDirsMerger overlayfs.DirsMerger = func(lofi, bofi []fs.DirEntry) []fs.DirEntry {
	for _, fi1 := range bofi {
		var found bool

		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
}

AppendDirsMerger merges two directories keeping all regular files with the first slice as the base. Duplicate directories in the second slice will be ignored. This strategy is used for the i18n and data fs where we need all entries.

var LanguageDirsMerger overlayfs.DirsMerger = func(lofi, bofi []fs.DirEntry) []fs.DirEntry {
	for _, fi1 := range bofi {
		fim1 := fi1.(FileMetaInfo)
		var found bool
		for _, fi2 := range lofi {
			fim2 := fi2.(FileMetaInfo)
			if fi1.Name() == fi2.Name() && fim1.Meta().Lang == fim2.Meta().Lang {
				found = true
				break
			}
		}
		if !found {
			lofi = append(lofi, fi1)
		}
	}

	return lofi
}

LanguageDirsMerger implements the overlayfs.DirsMerger func, which is used to merge two directories.

var (

	// NoOpFs provides a no-op filesystem that implements the afero.Fs
	// interface.
	NoOpFs = &noOpFs{}
)
var Os = &afero.OsFs{}

Os points to the (real) Os filesystem.

Functions

func AddFileInfoToError

func AddFileInfoToError(err error, fi FileMetaInfo, fs afero.Fs) error

AddFileInfoToError adds file info to the given error.

func Glob

func Glob(fs afero.Fs, pattern string, handle func(fi FileMetaInfo) (bool, error)) error

Glob walks the fs and passes all matches to the handle func. The handle func can return true to signal a stop.

func IsOsFs

func IsOsFs(fs afero.Fs) bool

IsOsFs returns whether fs is an OsFs or if it fs wraps an OsFs. TODO(bep) make this more robust.

func MakeReadableAndRemoveAllModulePkgDir

func MakeReadableAndRemoveAllModulePkgDir(fs afero.Fs, dir string) (int, error)

MakeReadableAndRemoveAllModulePkgDir makes any subdir in dir readable and then removes the root. TODO(bep) move this to a more suitable place.

func NewBaseFileDecorator

func NewBaseFileDecorator(fs afero.Fs, callbacks ...func(fi FileMetaInfo)) afero.Fs

NewBaseFileDecorator decorates the given Fs to provide the real filename and an Opener func.

func NewBasePathFs

func NewBasePathFs(source afero.Fs, path string) afero.Fs

NewBasePathFs creates a new BasePathFs.

func NewComponentFs

func NewComponentFs(opts ComponentFsOptions) *componentFs

NewComponentFs creates a new component filesystem.

func NewCreateCountingFs

func NewCreateCountingFs(fs afero.Fs) afero.Fs

func NewHasBytesReceiver

func NewHasBytesReceiver(delegate afero.Fs, shouldCheck func(name string) bool, hasBytesCallback func(name string, match []byte), patterns ...[]byte) afero.Fs

func NewHashingFs

func NewHashingFs(delegate afero.Fs, hashReceiver FileHashReceiver) afero.Fs

NewHashingFs creates a new filesystem that will receive MD5 checksums of any written file content on Close. Note that this is probably not a good idea for "full build" situations, but when doing fast render mode, the amount of files published is low, and it would be really nice to know exactly which of these files where actually changed. Note that this will only work for file operations that use the io.Writer to write content to file, but that is fine for the "publish content" use case.

func NewReadOnlyFs

func NewReadOnlyFs(source afero.Fs) afero.Fs

NewReadOnlyFs creates a new ReadOnlyFs.

func NewStacktracerFs

func NewStacktracerFs(fs afero.Fs, pattern string) afero.Fs

NewStacktracerFs wraps the given fs printing stack traces for file creates matching the given regexp pattern.

func WalkFilesystems

func WalkFilesystems(fs afero.Fs, fn WalkFn) bool

WalkFilesystems walks fs recursively and calls fn. If fn returns true, walking is stopped.

func WrapFilesystem

func WrapFilesystem(container, content afero.Fs) afero.Fs

WrapFilesystem is typically used to wrap a afero.BasePathFs to allow access to the underlying filesystem if needed.

Types

type ComponentFsOptions

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
}

type ComponentPath

type ComponentPath struct {
	Component string
	Path      string
	Lang      string
	Watch     bool
}

func (ComponentPath) ComponentPathJoined

func (c ComponentPath) ComponentPathJoined() string

type DirOnlyOps

type DirOnlyOps interface {
	io.Closer
	Name() string
	Readdir(count int) ([]os.FileInfo, error)
	Readdirnames(n int) ([]string, error)
	Stat() (os.FileInfo, error)
}

DirOnlyOps is a subset of the afero.File interface covering the methods needed for directory operations.

type DuplicatesReporter

type DuplicatesReporter interface {
	ReportDuplicates() string
}

DuplicatesReporter reports about duplicate filenames.

type FileHashReceiver

type FileHashReceiver interface {
	OnFileClose(name string, checksum uint64)
}

FileHashReceiver will receive the filename an the content's MD5 sum on file close.

type FileInfoOptionals

type FileInfoOptionals interface {
	Size() int64
	Mode() fs.FileMode
	ModTime() time.Time
	Sys() any
}

type FileInfoProvider

type FileInfoProvider interface {
	FileInfo() FileMetaInfo
}

type FileMeta

type FileMeta struct {
	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).
	// Returns the new name and a boolean indicating if the file
	// should be included.
	Rename func(name string, toFrom bool) (string, bool)
}

func NewFileMeta

func NewFileMeta() *FileMeta

func (*FileMeta) Copy

func (m *FileMeta) Copy() *FileMeta

func (*FileMeta) JoinStat

func (f *FileMeta) JoinStat(name string) (FileMetaInfo, error)

func (*FileMeta) Merge

func (m *FileMeta) Merge(from *FileMeta)

func (*FileMeta) Open

func (f *FileMeta) Open() (afero.File, error)

func (*FileMeta) ReadAll

func (f *FileMeta) ReadAll() ([]byte, error)

type FileMetaInfo

type FileMetaInfo interface {
	fs.DirEntry
	MetaProvider

	// This is a real hybrid as it also implements the fs.FileInfo interface.
	FileInfoOptionals
}

func DirEntriesToFileMetaInfos

func DirEntriesToFileMetaInfos(fis []fs.DirEntry) []FileMetaInfo

func NewFileMetaInfo

func NewFileMetaInfo(fi FileNameIsDir, m *FileMeta) FileMetaInfo

type FileNameIsDir

type FileNameIsDir interface {
	Name() string
	IsDir() bool
}

type FilesystemUnwrapper

type FilesystemUnwrapper interface {
	UnwrapFilesystem() afero.Fs
}

FilesystemUnwrapper returns the underlying filesystem.

type FilesystemsUnwrapper

type FilesystemsUnwrapper interface {
	UnwrapFilesystems() []afero.Fs
}

FilesystemsUnwrapper returns the underlying filesystems.

type Fs

type Fs struct {
	// Source is Hugo's source file system.
	// Note that this will always be a "plain" Afero filesystem:
	// * afero.OsFs when running in production
	// * afero.MemMapFs for many of the tests.
	Source afero.Fs

	// PublishDir is where Hugo publishes its rendered content.
	// It's mounted inside publishDir (default /public).
	PublishDir afero.Fs

	// PublishDirStatic is the file system used for static files.
	PublishDirStatic afero.Fs

	// PublishDirServer is the file system used for serving the public directory with Hugo's development server.
	// This will typically be the same as PublishDir, but not if --renderStaticToDisk is set.
	PublishDirServer afero.Fs

	// Os is an OS file system.
	// NOTE: Field is currently unused.
	Os afero.Fs

	// WorkingDirReadOnly is a read-only file system
	// restricted to the project working dir.
	WorkingDirReadOnly afero.Fs

	// WorkingDirWritable is a writable file system
	// restricted to the project working dir.
	WorkingDirWritable afero.Fs
}

Fs holds the core filesystems used by Hugo.

func NewDefault

func NewDefault(cfg config.Provider) *Fs

func NewFrom

func NewFrom(fs afero.Fs, conf config.BaseConfig) *Fs

NewFrom creates a new Fs based on the provided Afero Fs as source and destination file systems. Useful for testing.

func NewFromOld

func NewFromOld(fs afero.Fs, cfg config.Provider) *Fs

func NewFromSourceAndDestination

func NewFromSourceAndDestination(source, destination afero.Fs, cfg config.Provider) *Fs

NewFromSourceAndDestination creates a new Fs based on the provided Afero Fss as the source and destination file systems.

type MetaProvider

type MetaProvider interface {
	Meta() *FileMeta
}

type OpenFilesFs

type OpenFilesFs struct {
	afero.Fs
	// contains filtered or unexported fields
}

OpenFilesFs is a wrapper around afero.Fs that keeps track of open files.

func (*OpenFilesFs) Create

func (fs *OpenFilesFs) Create(name string) (afero.File, error)

func (*OpenFilesFs) Open

func (fs *OpenFilesFs) Open(name string) (afero.File, error)

func (*OpenFilesFs) OpenFile

func (fs *OpenFilesFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error)

func (*OpenFilesFs) OpenFiles

func (fs *OpenFilesFs) OpenFiles() map[string]int

func (*OpenFilesFs) UnwrapFilesystem

func (fs *OpenFilesFs) UnwrapFilesystem() afero.Fs

type Reseter

type Reseter interface {
	Reset()
}

Reseter is implemented by some of the stateful filesystems.

type ReverseLookupProvder

type ReverseLookupProvder interface {
	ReverseLookup(filename string) ([]ComponentPath, error)
	ReverseLookupComponent(component, filename string) ([]ComponentPath, error)
}

type RootMapping

type RootMapping struct {
	From          string    // The virtual mount.
	FromBase      string    // The base directory of the virtual mount.
	To            string    // The source directory or file.
	ToBase        string    // The base of To. May be empty if an absolute path was provided.
	Module        string    // The module path/ID.
	ModuleOrdinal int       // The module ordinal starting with 0 which is the project.
	IsProject     bool      // Whether this is a mount in the main project.
	Meta          *FileMeta // File metadata (lang etc.)
	// contains filtered or unexported fields
}

RootMapping describes a virtual file or directory mount.

type RootMappingFs

type RootMappingFs struct {
	afero.Fs
	// contains filtered or unexported fields
}

A RootMappingFs maps several roots into one. Note that the root of this filesystem is directories only, and they will be returned in Readdir and Readdirnames in the order given.

func NewRootMappingFs

func NewRootMappingFs(fs afero.Fs, rms ...RootMapping) (*RootMappingFs, error)

NewRootMappingFs creates a new RootMappingFs on top of the provided with root mappings with some optional metadata about the root. Note that From represents a virtual root that maps to the actual filename in To.

func (RootMappingFs) Filter

func (fs RootMappingFs) Filter(f func(m RootMapping) bool) *RootMappingFs

Filter creates a copy of this filesystem with only mappings matching a filter.

func (*RootMappingFs) Key

func (fs *RootMappingFs) Key() string

func (*RootMappingFs) Mounts

func (fs *RootMappingFs) Mounts(base string) ([]FileMetaInfo, error)

func (*RootMappingFs) Open

func (fs *RootMappingFs) Open(name string) (afero.File, error)

Open opens the named file for reading.

func (*RootMappingFs) ReverseLookup

func (fs *RootMappingFs) ReverseLookup(filename string) ([]ComponentPath, error)

func (fs *RootMappingFs) ReverseStat(filename string) ([]FileMetaInfo, error)

func (*RootMappingFs) ReverseLookupComponent

func (fs *RootMappingFs) ReverseLookupComponent(component, filename string) ([]ComponentPath, error)

func (*RootMappingFs) Stat

func (fs *RootMappingFs) Stat(name string) (os.FileInfo, error)

Stat returns the os.FileInfo structure describing a given file. If there is an error, it will be of type *os.PathError. If multiple roots are found, the last one will be used.

func (*RootMappingFs) UnwrapFilesystem

func (fs *RootMappingFs) UnwrapFilesystem() afero.Fs

type WalkFn

type WalkFn func(fs afero.Fs) bool

WalkFn is the walk func for WalkFilesystems.

type WalkFunc

type WalkFunc func(path string, info FileMetaInfo) error

type WalkHook

type WalkHook func(dir FileMetaInfo, path string, readdir []FileMetaInfo) ([]FileMetaInfo, error)

type Walkway

type Walkway struct {
	// contains filtered or unexported fields
}

func NewWalkway

func NewWalkway(cfg WalkwayConfig) *Walkway

func (*Walkway) Walk

func (w *Walkway) Walk() error

type WalkwayConfig

type WalkwayConfig struct {
	// The filesystem to walk.
	Fs afero.Fs

	// The root to start from in Fs.
	Root string

	// The logger to use.
	Logger     loggers.Logger
	PathParser *paths.PathParser

	// One or both of these may be pre-set.
	Info       FileMetaInfo               // The start info.
	DirEntries []FileMetaInfo             // The start info's dir entries.
	IgnoreFile func(filename string) bool // Optional

	// Will be called in order.
	HookPre  WalkHook // Optional.
	WalkFn   WalkFunc
	HookPost WalkHook // Optional.

	// Some optional flags.
	FailOnNotExist bool // If set, return an error if a directory is not found.
	SortDirEntries bool // If set, sort the dir entries by Name before calling the WalkFn, default is ReaDir order.
}

Source Files

component_fs.go createcounting_fs.go decorators.go dirsmerger.go fileinfo.go filename_filter_fs.go fs.go glob.go hasbytes_fs.go hashing_fs.go noop_fs.go openfiles_fs.go rootmapping_fs.go stacktracer_fs.go walk.go

Directories

PathSynopsis
hugofs/files
hugofs/glob
Version
v0.144.2 (latest)
Published
Feb 19, 2025
Platform
linux/amd64
Imports
33 packages
Last checked
13 hours ago

Tools for package owners.