1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
// Copyright 2019 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 provides the file systems used by Hugo.
package hugofs
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"sync"
"time"
"github.com/gohugoio/hugo/hugofs/glob"
"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"
)
func NewFileMeta() *FileMeta {
return &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).
Rename func(name string, toFrom bool) string
}
func (m *FileMeta) Copy() *FileMeta {
if m == nil {
return NewFileMeta()
}
c := *m
return &c
}
func (m *FileMeta) Merge(from *FileMeta) {
if m == nil || from == nil {
return
}
dstv := reflect.Indirect(reflect.ValueOf(m))
srcv := reflect.Indirect(reflect.ValueOf(from))
for i := 0; i < dstv.NumField(); i++ {
v := dstv.Field(i)
if !v.CanSet() {
continue
}
if !hreflect.IsTruthfulValue(v) {
v.Set(srcv.Field(i))
}
}
if m.InclusionFilter == nil {
m.InclusionFilter = from.InclusionFilter
}
}
func (f *FileMeta) Open() (afero.File, error) {
if f.OpenFunc == nil {
return nil, errors.New("OpenFunc not set")
}
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
}
return f.JoinStatFunc(name)
}
type FileMetaInfo interface {
fs.DirEntry
MetaProvider
// This is a real hybrid as it also implements the fs.FileInfo interface.
FileInfoOptionals
}
type MetaProvider interface {
Meta() *FileMeta
}
type FileInfoOptionals interface {
Size() int64
Mode() fs.FileMode
ModTime() time.Time
Sys() any
}
type FileNameIsDir interface {
Name() string
IsDir() bool
}
type FileInfoProvider interface {
FileInfo() FileMetaInfo
}
// DirOnlyOps is a subset of the afero.File interface covering
// the methods needed for directory operations.
type DirOnlyOps interface {
io.Closer
Name() string
Readdir(count int) ([]os.FileInfo, error)
Readdirnames(n int) ([]string, error)
Stat() (os.FileInfo, error)
}
type dirEntryMeta struct {
fs.DirEntry
m *FileMeta
fi fs.FileInfo
fiInit sync.Once
}
func (fi *dirEntryMeta) Meta() *FileMeta {
return fi.m
}
// Filename returns the full filename.
func (fi *dirEntryMeta) Filename() string {
return fi.m.Filename
}
func (fi *dirEntryMeta) fileInfo() fs.FileInfo {
var err error
fi.fiInit.Do(func() {
fi.fi, err = fi.DirEntry.Info()
})
if err != nil {
panic(err)
}
return fi.fi
}
func (fi *dirEntryMeta) Size() int64 {
return fi.fileInfo().Size()
}
func (fi *dirEntryMeta) Mode() fs.FileMode {
return fi.fileInfo().Mode()
}
func (fi *dirEntryMeta) ModTime() time.Time {
return fi.fileInfo().ModTime()
}
func (fi *dirEntryMeta) Sys() any {
return fi.fileInfo().Sys()
}
// Name returns the file's name.
func (fi *dirEntryMeta) Name() string {
if name := fi.m.Name; name != "" {
return name
}
return fi.DirEntry.Name()
}
// dirEntry is an adapter from os.FileInfo to fs.DirEntry
type dirEntry struct {
fs.FileInfo
}
var _ fs.DirEntry = dirEntry{}
func (d dirEntry) Type() fs.FileMode { return d.FileInfo.Mode().Type() }
func (d dirEntry) Info() (fs.FileInfo, error) { return d.FileInfo, nil }
func NewFileMetaInfo(fi FileNameIsDir, m *FileMeta) FileMetaInfo {
if m == nil {
panic("FileMeta must be set")
}
if fim, ok := fi.(MetaProvider); ok {
m.Merge(fim.Meta())
}
switch v := fi.(type) {
case fs.DirEntry:
return &dirEntryMeta{DirEntry: v, m: m}
case fs.FileInfo:
return &dirEntryMeta{DirEntry: dirEntry{v}, m: m}
case nil:
return &dirEntryMeta{DirEntry: dirEntry{}, m: m}
default:
panic(fmt.Sprintf("Unsupported type: %T", fi))
}
}
type dirNameOnlyFileInfo struct {
name string
modTime time.Time
}
func (fi *dirNameOnlyFileInfo) Name() string {
return fi.name
}
func (fi *dirNameOnlyFileInfo) Size() int64 {
panic("not implemented")
}
func (fi *dirNameOnlyFileInfo) Mode() os.FileMode {
return os.ModeDir
}
func (fi *dirNameOnlyFileInfo) ModTime() time.Time {
return fi.modTime
}
func (fi *dirNameOnlyFileInfo) IsDir() bool {
return true
}
func (fi *dirNameOnlyFileInfo) Sys() any {
return nil
}
func newDirNameOnlyFileInfo(name string, meta *FileMeta, fileOpener func() (afero.File, error)) FileMetaInfo {
name = normalizeFilename(name)
_, base := filepath.Split(name)
m := meta.Copy()
if m.Filename == "" {
m.Filename = name
}
m.OpenFunc = fileOpener
return NewFileMetaInfo(
&dirNameOnlyFileInfo{name: base, modTime: htime.Now()},
m,
)
}
func decorateFileInfo(fi FileNameIsDir, opener func() (afero.File, error), filename string, inMeta *FileMeta) FileMetaInfo {
var meta *FileMeta
var fim FileMetaInfo
var ok bool
if fim, ok = fi.(FileMetaInfo); ok {
meta = fim.Meta()
} else {
meta = NewFileMeta()
fim = NewFileMetaInfo(fi, meta)
}
if opener != nil {
meta.OpenFunc = opener
}
nfilename := normalizeFilename(filename)
if nfilename != "" {
meta.Filename = nfilename
}
meta.Merge(inMeta)
return fim
}
func DirEntriesToFileMetaInfos(fis []fs.DirEntry) []FileMetaInfo {
fims := make([]FileMetaInfo, len(fis))
for i, v := range fis {
fim := v.(FileMetaInfo)
fims[i] = fim
}
return fims
}
func normalizeFilename(filename string) string {
if filename == "" {
return ""
}
if runtime.GOOS == "darwin" {
// When a file system is HFS+, its filepath is in NFD form.
return norm.NFC.String(filename)
}
return filename
}
func sortDirEntries(fis []fs.DirEntry) {
sort.Slice(fis, func(i, j int) bool {
fimi, fimj := fis[i].(FileMetaInfo), fis[j].(FileMetaInfo)
return fimi.Meta().Filename < fimj.Meta().Filename
})
}
// AddFileInfoToError adds file info to the given error.
func AddFileInfoToError(err error, fi FileMetaInfo, fs afero.Fs) error {
if err == nil {
return nil
}
meta := fi.Meta()
filename := meta.Filename
// Check if it's already added.
for _, ferr := range herrors.UnwrapFileErrors(err) {
pos := ferr.Position()
errfilename := pos.Filename
if errfilename == "" {
pos.Filename = filename
ferr.UpdatePosition(pos)
}
if errfilename == "" || errfilename == filename {
if filename != "" && ferr.ErrorContext() == nil {
f, ioerr := fs.Open(filename)
if ioerr != nil {
return err
}
defer f.Close()
ferr.UpdateContent(f, nil)
}
return err
}
}
lineMatcher := herrors.NopLineMatcher
if textSegmentErr, ok := err.(*herrors.TextSegmentError); ok {
lineMatcher = herrors.ContainsMatcher(textSegmentErr.Segment)
}
return herrors.NewFileErrorFromFile(err, filename, fs, lineMatcher)
}
|