| 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 |
"context" |
| 19 |
"fmt" |
| 20 |
iofs "io/fs" |
| 21 |
"os" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"github.com/bep/overlayfs" |
| 25 |
"github.com/gohugoio/hugo/common/paths" |
| 26 |
"github.com/gohugoio/hugo/config" |
| 27 |
"github.com/spf13/afero" |
| 28 |
) |
| 29 |
|
| 30 |
// Os points to the (real) Os filesystem. |
| 31 |
var Os = &afero.OsFs{} |
| 32 |
|
| 33 |
// Fs holds the core filesystems used by Hugo. |
| 34 |
type Fs struct { |
| 35 |
// Source is Hugo's source file system. |
| 36 |
// Note that this will always be a "plain" Afero filesystem: |
| 37 |
// * afero.OsFs when running in production |
| 38 |
// * afero.MemMapFs for many of the tests. |
| 39 |
Source afero.Fs |
| 40 |
|
| 41 |
// PublishDir is where Hugo publishes its rendered content. |
| 42 |
// It's mounted inside publishDir (default /public). |
| 43 |
PublishDir afero.Fs |
| 44 |
|
| 45 |
// PublishDirStatic is the file system used for static files. |
| 46 |
PublishDirStatic afero.Fs |
| 47 |
|
| 48 |
// PublishDirServer is the file system used for serving the public directory with Hugo's development server. |
| 49 |
// This will typically be the same as PublishDir, but not if --renderStaticToDisk is set. |
| 50 |
PublishDirServer afero.Fs |
| 51 |
|
| 52 |
// Os is an OS file system. |
| 53 |
// NOTE: Field is currently unused. |
| 54 |
Os afero.Fs |
| 55 |
|
| 56 |
// WorkingDirReadOnly is a read-only file system |
| 57 |
// restricted to the project working dir. |
| 58 |
WorkingDirReadOnly afero.Fs |
| 59 |
|
| 60 |
// WorkingDirWritable is a writable file system |
| 61 |
// restricted to the project working dir. |
| 62 |
WorkingDirWritable afero.Fs |
| 63 |
} |
| 64 |
|
| 65 |
func NewDefault(cfg config.Provider) *Fs { |
| 66 |
workingDir, publishDir := getWorkingPublishDir(cfg) |
| 67 |
fs := Os |
| 68 |
return newFs(fs, fs, workingDir, publishDir) |
| 69 |
} |
| 70 |
|
| 71 |
// NewFrom creates a new Fs based on the provided Afero Fs |
| 72 |
// as source and destination file systems. |
| 73 |
// Useful for testing. |
| 74 |
func NewFrom(fs afero.Fs, conf config.BaseConfig) *Fs { |
| 75 |
return newFs(fs, fs, conf.WorkingDir, conf.PublishDir) |
| 76 |
} |
| 77 |
|
| 78 |
func NewFromOld(fs afero.Fs, cfg config.Provider) *Fs { |
| 79 |
workingDir, publishDir := getWorkingPublishDir(cfg) |
| 80 |
return newFs(fs, fs, workingDir, publishDir) |
| 81 |
} |
| 82 |
|
| 83 |
// NewFromSourceAndDestination creates a new Fs based on the provided Afero Fss |
| 84 |
// as the source and destination file systems. |
| 85 |
func NewFromSourceAndDestination(source, destination afero.Fs, cfg config.Provider) *Fs { |
| 86 |
workingDir, publishDir := getWorkingPublishDir(cfg) |
| 87 |
return newFs(source, destination, workingDir, publishDir) |
| 88 |
} |
| 89 |
|
| 90 |
func getWorkingPublishDir(cfg config.Provider) (string, string) { |
| 91 |
workingDir := cfg.GetString("workingDir") |
| 92 |
publishDir := cfg.GetString("publishDirDynamic") |
| 93 |
if publishDir == "" { |
| 94 |
publishDir = cfg.GetString("publishDir") |
| 95 |
} |
| 96 |
return workingDir, publishDir |
| 97 |
} |
| 98 |
|
| 99 |
func newFs(source, destination afero.Fs, workingDir, publishDir string) *Fs { |
| 100 |
if publishDir == "" { |
| 101 |
panic("publishDir is empty") |
| 102 |
} |
| 103 |
|
| 104 |
if workingDir == "." { |
| 105 |
workingDir = "" |
| 106 |
} |
| 107 |
|
| 108 |
// Sanity check |
| 109 |
if IsOsFs(source) && len(workingDir) < 2 { |
| 110 |
panic("workingDir is too short") |
| 111 |
} |
| 112 |
|
| 113 |
// If this does not exist, it will be created later. |
| 114 |
absPublishDir := paths.AbsPathify(workingDir, publishDir) |
| 115 |
|
| 116 |
pubFs := NewBasePathFs(destination, absPublishDir) |
| 117 |
|
| 118 |
return &Fs{ |
| 119 |
Source: source, |
| 120 |
PublishDir: pubFs, |
| 121 |
PublishDirServer: pubFs, |
| 122 |
PublishDirStatic: pubFs, |
| 123 |
Os: &afero.OsFs{}, |
| 124 |
WorkingDirReadOnly: getWorkingDirFsReadOnly(source, workingDir), |
| 125 |
WorkingDirWritable: getWorkingDirFsWritable(source, workingDir), |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func getWorkingDirFsReadOnly(base afero.Fs, workingDir string) afero.Fs { |
| 130 |
if workingDir == "" { |
| 131 |
return NewReadOnlyFs(base) |
| 132 |
} |
| 133 |
return NewBasePathFs(NewReadOnlyFs(base), workingDir) |
| 134 |
} |
| 135 |
|
| 136 |
func getWorkingDirFsWritable(base afero.Fs, workingDir string) afero.Fs { |
| 137 |
if workingDir == "" { |
| 138 |
return base |
| 139 |
} |
| 140 |
return NewBasePathFs(base, workingDir) |
| 141 |
} |
| 142 |
|
| 143 |
func isWrite(flag int) bool { |
| 144 |
return flag&os.O_RDWR != 0 || flag&os.O_WRONLY != 0 |
| 145 |
} |
| 146 |
|
| 147 |
// MakeReadableAndRemoveAllModulePkgDir makes any subdir in dir readable and then |
| 148 |
// removes the root. |
| 149 |
// TODO(bep) move this to a more suitable place. |
| 150 |
func MakeReadableAndRemoveAllModulePkgDir(fs afero.Fs, dir string) (int, error) { |
| 151 |
// Safe guard |
| 152 |
// Note that the base directory changed from pkg to gomod_cache in Go 1.23. |
| 153 |
if !strings.Contains(dir, "pkg") && !strings.Contains(dir, "gomod") { |
| 154 |
panic(fmt.Sprint("invalid dir:", dir)) |
| 155 |
} |
| 156 |
|
| 157 |
counter := 0 |
| 158 |
afero.Walk(fs, dir, func(path string, info os.FileInfo, err error) error { |
| 159 |
if err != nil { |
| 160 |
return nil |
| 161 |
} |
| 162 |
if info.IsDir() { |
| 163 |
counter++ |
| 164 |
fs.Chmod(path, 0o777) |
| 165 |
} |
| 166 |
return nil |
| 167 |
}) |
| 168 |
|
| 169 |
return counter, fs.RemoveAll(dir) |
| 170 |
} |
| 171 |
|
| 172 |
// IsOsFs returns whether fs is an OsFs or if it fs wraps an OsFs. |
| 173 |
// TODO(bep) make this more robust. |
| 174 |
func IsOsFs(fs afero.Fs) bool { |
| 175 |
var isOsFs bool |
| 176 |
WalkFilesystems(fs, func(fs afero.Fs) bool { |
| 177 |
switch fs.(type) { |
| 178 |
case *afero.MemMapFs: |
| 179 |
isOsFs = false |
| 180 |
case *afero.OsFs: |
| 181 |
isOsFs = true |
| 182 |
} |
| 183 |
return isOsFs |
| 184 |
}) |
| 185 |
return isOsFs |
| 186 |
} |
| 187 |
|
| 188 |
// FilesystemsUnwrapper returns the underlying filesystems. |
| 189 |
type FilesystemsUnwrapper interface { |
| 190 |
UnwrapFilesystems() []afero.Fs |
| 191 |
} |
| 192 |
|
| 193 |
// FilesystemUnwrapper returns the underlying filesystem. |
| 194 |
type FilesystemUnwrapper interface { |
| 195 |
UnwrapFilesystem() afero.Fs |
| 196 |
} |
| 197 |
|
| 198 |
// WalkFn is the walk func for WalkFilesystems. |
| 199 |
type WalkFn func(fs afero.Fs) bool |
| 200 |
|
| 201 |
// WalkFilesystems walks fs recursively and calls fn. |
| 202 |
// If fn returns true, walking is stopped. |
| 203 |
func WalkFilesystems(fs afero.Fs, fn WalkFn) bool { |
| 204 |
if fn(fs) { |
| 205 |
return true |
| 206 |
} |
| 207 |
|
| 208 |
if afs, ok := fs.(FilesystemUnwrapper); ok { |
| 209 |
if WalkFilesystems(afs.UnwrapFilesystem(), fn) { |
| 210 |
return true |
| 211 |
} |
| 212 |
} else if bfs, ok := fs.(FilesystemsUnwrapper); ok { |
| 213 |
for _, sf := range bfs.UnwrapFilesystems() { |
| 214 |
if WalkFilesystems(sf, fn) { |
| 215 |
return true |
| 216 |
} |
| 217 |
} |
| 218 |
} else if cfs, ok := fs.(overlayfs.FilesystemIterator); ok { |
| 219 |
for i := range cfs.NumFilesystems() { |
| 220 |
if WalkFilesystems(cfs.Filesystem(i), fn) { |
| 221 |
return true |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return false |
| 227 |
} |
| 228 |
|
| 229 |
var _ FilesystemUnwrapper = (*filesystemsWrapper)(nil) |
| 230 |
|
| 231 |
// NewBasePathFs creates a new BasePathFs. |
| 232 |
func NewBasePathFs(source afero.Fs, path string) afero.Fs { |
| 233 |
return WrapFilesystem(afero.NewBasePathFs(source, path), source) |
| 234 |
} |
| 235 |
|
| 236 |
// NewReadOnlyFs creates a new ReadOnlyFs. |
| 237 |
func NewReadOnlyFs(source afero.Fs) afero.Fs { |
| 238 |
return WrapFilesystem(afero.NewReadOnlyFs(source), source) |
| 239 |
} |
| 240 |
|
| 241 |
// WrapFilesystem is typically used to wrap a afero.BasePathFs to allow |
| 242 |
// access to the underlying filesystem if needed. |
| 243 |
func WrapFilesystem(container, content afero.Fs) afero.Fs { |
| 244 |
return filesystemsWrapper{Fs: container, content: content} |
| 245 |
} |
| 246 |
|
| 247 |
var _ afero.Lstater = (*filesystemsWrapper)(nil) |
| 248 |
|
| 249 |
type filesystemsWrapper struct { |
| 250 |
afero.Fs |
| 251 |
content afero.Fs |
| 252 |
} |
| 253 |
|
| 254 |
func (w filesystemsWrapper) UnwrapFilesystem() afero.Fs { |
| 255 |
return w.content |
| 256 |
} |
| 257 |
|
| 258 |
func (w filesystemsWrapper) LstatIfPossible(name string) (os.FileInfo, bool, error) { |
| 259 |
if lstater, ok := w.Fs.(afero.Lstater); ok { |
| 260 |
return lstater.LstatIfPossible(name) |
| 261 |
} |
| 262 |
fi, err := w.Fs.Stat(name) |
| 263 |
return fi, false, err |
| 264 |
} |
| 265 |
|
| 266 |
type ReadDirWithContextDir interface { |
| 267 |
ReadDirWithContext(context context.Context, count int) ([]iofs.DirEntry, context.Context, error) |
| 268 |
} |
| 269 |
|
| 270 |
func ReadDirWithContext(ctx context.Context, f DirOnlyOps, count int) ([]iofs.DirEntry, context.Context, error) { |
| 271 |
if ff, ok := f.(ReadDirWithContextDir); ok { |
| 272 |
return ff.ReadDirWithContext(ctx, count) |
| 273 |
} |
| 274 |
v, err := f.(iofs.ReadDirFile).ReadDir(count) |
| 275 |
if err != nil { |
| 276 |
return nil, ctx, err |
| 277 |
} |
| 278 |
return v, ctx, nil |
| 279 |
} |
| 280 |
|
| 281 |
// LstatIfPossible tries to use LstatIfPossible if the filesystem supports it, otherwise it falls back to Stat. |
| 282 |
func LstatIfPossible(fs afero.Fs, name string) (os.FileInfo, error) { |
| 283 |
if lstater, ok := fs.(afero.Lstater); ok { |
| 284 |
fi, _, err := lstater.LstatIfPossible(name) |
| 285 |
return fi, err |
| 286 |
} |
| 287 |
fi, err := fs.Stat(name) |
| 288 |
return fi, err |
| 289 |
} |