| 1 |
// Copyright 2026 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 |
| 15 |
|
| 16 |
import ( |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/common/herrors" |
| 21 |
"github.com/gohugoio/hugo/common/hmaps" |
| 22 |
"github.com/spf13/afero" |
| 23 |
) |
| 24 |
|
| 25 |
// symlinkChecker caches Lstat results for directories. |
| 26 |
// We assume that symlinks are not created or removed while Hugo is running. |
| 27 |
type symlinkChecker struct { |
| 28 |
fs afero.Fs |
| 29 |
cache *hmaps.Cache[string, bool] |
| 30 |
} |
| 31 |
|
| 32 |
func newSymlinkChecker(fs afero.Fs) *symlinkChecker { |
| 33 |
return &symlinkChecker{fs: fs, cache: hmaps.NewCacheWithOptions[string, bool](hmaps.CacheOptions{Size: 10000})} |
| 34 |
} |
| 35 |
|
| 36 |
// isSymlink reports whether name is a symlink. A missing name is not a symlink. |
| 37 |
func (c *symlinkChecker) isSymlink(name string) (bool, error) { |
| 38 |
return c.cache.GetOrCreate(name, func() (bool, error) { |
| 39 |
fi, err := LstatIfPossible(c.fs, name) |
| 40 |
if err != nil { |
| 41 |
if herrors.IsNotExist(err) { |
| 42 |
return false, nil |
| 43 |
} |
| 44 |
return false, err |
| 45 |
} |
| 46 |
return fi.Mode()&os.ModeSymlink != 0, nil |
| 47 |
}) |
| 48 |
} |
| 49 |
|
| 50 |
// hasSymlinkParent reports whether dir or any of its parents below base is a symlink. |
| 51 |
// base itself is not checked; an empty base walks to the root of fs. |
| 52 |
func (c *symlinkChecker) hasSymlinkParent(base, dir string) (bool, error) { |
| 53 |
for dir != base && dir != "." { |
| 54 |
parent := filepath.Dir(dir) |
| 55 |
if parent == dir { |
| 56 |
return false, nil |
| 57 |
} |
| 58 |
symlink, err := c.isSymlink(dir) |
| 59 |
if err != nil || symlink { |
| 60 |
return symlink, err |
| 61 |
} |
| 62 |
dir = parent |
| 63 |
} |
| 64 |
return false, nil |
| 65 |
} |
| 66 |
|
| 67 |
// isSymlinkOrHasSymlinkParent reports whether name is a symlink or has a symlinked parent below base. |
| 68 |
// Parents are only checked when base is set; absolute mount sources may legitimately |
| 69 |
// live below symlinked directories (e.g. /tmp on macOS). |
| 70 |
func (c *symlinkChecker) isSymlinkOrHasSymlinkParent(base, name string) (bool, error) { |
| 71 |
symlink, err := c.isSymlink(name) |
| 72 |
if err != nil || symlink { |
| 73 |
return symlink, err |
| 74 |
} |
| 75 |
if base == "" { |
| 76 |
return false, nil |
| 77 |
} |
| 78 |
return c.hasSymlinkParent(filepath.Clean(base), filepath.Dir(name)) |
| 79 |
} |
| 80 |
|
| 81 |
// NewDropSymlinksFs returns an afero.Fs wrapper that treats symlinks as non-existing files. |
| 82 |
func NewDropSymlinksFs(base afero.Fs) *DropSymlinksFs { |
| 83 |
return &DropSymlinksFs{Fs: base, symlinks: newSymlinkChecker(base)} |
| 84 |
} |
| 85 |
|
| 86 |
// DropSymlinksFs is an afero.Fs wrapper that treats symlinks as non-existing files. |
| 87 |
type DropSymlinksFs struct { |
| 88 |
afero.Fs |
| 89 |
symlinks *symlinkChecker |
| 90 |
} |
| 91 |
|
| 92 |
func (fs *DropSymlinksFs) Open(name string) (afero.File, error) { |
| 93 |
if _, err := fs.Stat(name); err != nil { |
| 94 |
return nil, err |
| 95 |
} |
| 96 |
f, err := fs.Fs.Open(name) |
| 97 |
if err != nil { |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
return f, nil |
| 101 |
} |
| 102 |
|
| 103 |
func (fs *DropSymlinksFs) Stat(name string) (os.FileInfo, error) { |
| 104 |
fi, err := LstatIfPossible(fs.Fs, name) |
| 105 |
if err != nil { |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
if fi.Mode()&os.ModeSymlink != 0 { |
| 109 |
return nil, os.ErrNotExist |
| 110 |
} |
| 111 |
symlinkParent, err := fs.symlinks.hasSymlinkParent("", filepath.Dir(name)) |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
if symlinkParent { |
| 116 |
return nil, os.ErrNotExist |
| 117 |
} |
| 118 |
return fi, nil |
| 119 |
} |