mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-28 23:52:04 -05:00
parent
36220851e4
commit
edf9f0a354
3 changed files with 171 additions and 29 deletions
|
@ -99,11 +99,14 @@ type RootMapping struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rm *RootMapping) clean() {
|
func (rm *RootMapping) clean() {
|
||||||
rm.From = filepath.Clean(rm.From)
|
rm.From = strings.Trim(filepath.Clean(rm.From), filepathSeparator)
|
||||||
rm.To = filepath.Clean(rm.To)
|
rm.To = filepath.Clean(rm.To)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r RootMapping) filename(name string) string {
|
func (r RootMapping) filename(name string) string {
|
||||||
|
if name == "" {
|
||||||
|
return r.To
|
||||||
|
}
|
||||||
return filepath.Join(r.To, strings.TrimPrefix(name, r.From))
|
return filepath.Join(r.To, strings.TrimPrefix(name, r.From))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,39 +156,45 @@ func (fs *RootMappingFs) Dirs(base string) ([]FileMetaInfo, error) {
|
||||||
|
|
||||||
// LstatIfPossible returns the os.FileInfo structure describing a given file.
|
// LstatIfPossible returns the os.FileInfo structure describing a given file.
|
||||||
func (fs *RootMappingFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
|
func (fs *RootMappingFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
|
||||||
fis, b, err := fs.doLstat(name, false)
|
fis, _, b, err := fs.doLstat(name, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, b, err
|
return nil, b, err
|
||||||
}
|
}
|
||||||
return fis[0], b, nil
|
return fis[0], b, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *RootMappingFs) virtualDirOpener(name string, isRoot bool) func() (afero.File, error) {
|
func (fs *RootMappingFs) virtualDirOpener(name string, isRoot bool) func() (afero.File, error) {
|
||||||
return func() (afero.File, error) { return &rootMappingFile{name: name, isRoot: isRoot, fs: fs}, nil }
|
return func() (afero.File, error) { return &rootMappingFile{name: name, isRoot: isRoot, fs: fs}, nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *RootMappingFs) doLstat(name string, allowMultiple bool) ([]FileMetaInfo, bool, error) {
|
func (fs *RootMappingFs) doLstat(name string, allowMultiple bool) ([]FileMetaInfo, []FileMetaInfo, bool, error) {
|
||||||
|
|
||||||
if fs.isRoot(name) {
|
if fs.isRoot(name) {
|
||||||
return []FileMetaInfo{newDirNameOnlyFileInfo(name, true, fs.virtualDirOpener(name, true))}, false, nil
|
return []FileMetaInfo{newDirNameOnlyFileInfo(name, true, fs.virtualDirOpener(name, true))}, nil, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
roots := fs.getRoots(name)
|
roots := fs.getRoots(name)
|
||||||
|
rootsWithPrefix := fs.getRootsWithPrefix(name)
|
||||||
|
hasRootMappingsBelow := len(rootsWithPrefix) != 0
|
||||||
|
|
||||||
if len(roots) == 0 {
|
if len(roots) == 0 {
|
||||||
roots := fs.getRootsWithPrefix(name)
|
if hasRootMappingsBelow {
|
||||||
if len(roots) != 0 {
|
// No exact matches, but we have root mappings below name,
|
||||||
// We have root mappings below name, let's make it look like
|
// let's make it look like a directory.
|
||||||
// a directory.
|
return []FileMetaInfo{newDirNameOnlyFileInfo(name, true, fs.virtualDirOpener(name, false))}, nil, false, nil
|
||||||
return []FileMetaInfo{newDirNameOnlyFileInfo(name, true, fs.virtualDirOpener(name, false))}, false, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, false, os.ErrNotExist
|
return nil, nil, false, os.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We may have a mapping for both static and static/subdir.
|
||||||
|
// These will not show in any Readdir so append them
|
||||||
|
// manually.
|
||||||
|
rootsInDir := fs.filterRootsBelow(rootsWithPrefix, name)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
fis []FileMetaInfo
|
fis []FileMetaInfo
|
||||||
|
dirs []FileMetaInfo
|
||||||
b bool
|
b bool
|
||||||
fi os.FileInfo
|
fi os.FileInfo
|
||||||
root RootMapping
|
root RootMapping
|
||||||
|
@ -198,18 +207,30 @@ func (fs *RootMappingFs) doLstat(name string, allowMultiple bool) ([]FileMetaInf
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return nil, false, err
|
return nil, nil, false, err
|
||||||
}
|
}
|
||||||
fim := fi.(FileMetaInfo)
|
fim := fi.(FileMetaInfo)
|
||||||
fis = append(fis, fim)
|
fis = append(fis, fim)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(fis) == 0 {
|
for _, root = range rootsInDir {
|
||||||
return nil, false, os.ErrNotExist
|
fi, _, err := fs.statRoot(root, "")
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil, nil, false, err
|
||||||
|
}
|
||||||
|
fim := fi.(FileMetaInfo)
|
||||||
|
dirs = append(dirs, fim)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fis) == 0 && len(dirs) == 0 {
|
||||||
|
return nil, nil, false, os.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
if allowMultiple || len(fis) == 1 {
|
if allowMultiple || len(fis) == 1 {
|
||||||
return fis, b, nil
|
return fis, dirs, b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open it in this composite filesystem.
|
// Open it in this composite filesystem.
|
||||||
|
@ -217,7 +238,7 @@ func (fs *RootMappingFs) doLstat(name string, allowMultiple bool) ([]FileMetaInf
|
||||||
return fs.Open(name)
|
return fs.Open(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return []FileMetaInfo{decorateFileInfo(fi, fs, opener, "", "", root.Meta)}, b, nil
|
return []FileMetaInfo{decorateFileInfo(fi, fs, opener, "", "", root.Meta)}, nil, b, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,7 +248,7 @@ func (fs *RootMappingFs) Open(name string) (afero.File, error) {
|
||||||
return &rootMappingFile{name: name, fs: fs, isRoot: true}, nil
|
return &rootMappingFile{name: name, fs: fs, isRoot: true}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fis, _, err := fs.doLstat(name, true)
|
fis, dirs, _, err := fs.doLstat(name, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -239,10 +260,26 @@ func (fs *RootMappingFs) Open(name string) (afero.File, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &rootMappingFile{File: f, fs: fs, name: name, meta: meta}, nil
|
|
||||||
|
f = &rootMappingFile{File: f, fs: fs, name: name, meta: meta}
|
||||||
|
|
||||||
|
if len(dirs) > 0 {
|
||||||
|
return &readDirDirsAppender{File: f, dirs: dirs}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return fs.newUnionFile(fis...)
|
f, err := fs.newUnionFile(fis...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(dirs) > 0 {
|
||||||
|
return &readDirDirsAppender{File: f, dirs: dirs}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return f, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,17 +311,22 @@ func (fs *RootMappingFs) getRoots(name string) []RootMapping {
|
||||||
|
|
||||||
rm := v.([]RootMapping)
|
rm := v.([]RootMapping)
|
||||||
|
|
||||||
if fs.filter != nil {
|
return fs.applyFilterToRoots(rm)
|
||||||
var filtered []RootMapping
|
}
|
||||||
for _, m := range rm {
|
|
||||||
if fs.filter(m) {
|
func (fs *RootMappingFs) applyFilterToRoots(rm []RootMapping) []RootMapping {
|
||||||
filtered = append(filtered, m)
|
if fs.filter == nil {
|
||||||
}
|
return rm
|
||||||
}
|
|
||||||
return filtered
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return rm
|
var filtered []RootMapping
|
||||||
|
for _, m := range rm {
|
||||||
|
if fs.filter(m) {
|
||||||
|
filtered = append(filtered, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return filtered
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *RootMappingFs) getRootsWithPrefix(prefix string) []RootMapping {
|
func (fs *RootMappingFs) getRootsWithPrefix(prefix string) []RootMapping {
|
||||||
|
@ -299,7 +341,30 @@ func (fs *RootMappingFs) getRootsWithPrefix(prefix string) []RootMapping {
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
return roots
|
return fs.applyFilterToRoots(roots)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter out the mappings inside the name directory.
|
||||||
|
func (fs *RootMappingFs) filterRootsBelow(roots []RootMapping, name string) []RootMapping {
|
||||||
|
if len(roots) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
sepCount := strings.Count(name, filepathSeparator)
|
||||||
|
var filtered []RootMapping
|
||||||
|
for _, x := range roots {
|
||||||
|
if name == x.From {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Count(x.From, filepathSeparator)-sepCount != 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
filtered = append(filtered, x)
|
||||||
|
|
||||||
|
}
|
||||||
|
return filtered
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *RootMappingFs) newUnionFile(fis ...FileMetaInfo) (afero.File, error) {
|
func (fs *RootMappingFs) newUnionFile(fis ...FileMetaInfo) (afero.File, error) {
|
||||||
|
@ -373,6 +438,9 @@ func (fs *RootMappingFs) statRoot(root RootMapping, name string) (os.FileInfo, b
|
||||||
}
|
}
|
||||||
|
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
|
if name == "" {
|
||||||
|
name = root.From
|
||||||
|
}
|
||||||
_, name = filepath.Split(name)
|
_, name = filepath.Split(name)
|
||||||
fi = newDirNameOnlyFileInfo(name, false, opener)
|
fi = newDirNameOnlyFileInfo(name, false, opener)
|
||||||
}
|
}
|
||||||
|
@ -389,6 +457,32 @@ type rootMappingFile struct {
|
||||||
isRoot bool
|
isRoot bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type readDirDirsAppender struct {
|
||||||
|
afero.File
|
||||||
|
dirs []FileMetaInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *readDirDirsAppender) Readdir(count int) ([]os.FileInfo, error) {
|
||||||
|
fis, err := f.File.Readdir(count)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dir := range f.dirs {
|
||||||
|
fis = append(fis, dir)
|
||||||
|
}
|
||||||
|
return fis, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *readDirDirsAppender) Readdirnames(count int) ([]string, error) {
|
||||||
|
fis, err := f.Readdir(count)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return fileInfosToNames(fis), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (f *rootMappingFile) Close() error {
|
func (f *rootMappingFile) Close() error {
|
||||||
if f.File == nil {
|
if f.File == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -238,6 +238,53 @@ func TestRootMappingFsMount(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRootMappingFsMountOverlap(t *testing.T) {
|
||||||
|
assert := require.New(t)
|
||||||
|
fs := NewBaseFileDecorator(afero.NewMemMapFs())
|
||||||
|
|
||||||
|
assert.NoError(afero.WriteFile(fs, filepath.FromSlash("da/a.txt"), []byte("some no content"), 0755))
|
||||||
|
assert.NoError(afero.WriteFile(fs, filepath.FromSlash("db/b.txt"), []byte("some no content"), 0755))
|
||||||
|
assert.NoError(afero.WriteFile(fs, filepath.FromSlash("dc/c.txt"), []byte("some no content"), 0755))
|
||||||
|
assert.NoError(afero.WriteFile(fs, filepath.FromSlash("de/e.txt"), []byte("some no content"), 0755))
|
||||||
|
|
||||||
|
rm := []RootMapping{
|
||||||
|
RootMapping{
|
||||||
|
From: "static",
|
||||||
|
To: "da",
|
||||||
|
},
|
||||||
|
RootMapping{
|
||||||
|
From: "static/b",
|
||||||
|
To: "db",
|
||||||
|
},
|
||||||
|
RootMapping{
|
||||||
|
From: "static/b/c",
|
||||||
|
To: "dc",
|
||||||
|
},
|
||||||
|
RootMapping{
|
||||||
|
From: "/static/e/",
|
||||||
|
To: "de",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
rfs, err := NewRootMappingFs(fs, rm...)
|
||||||
|
assert.NoError(err)
|
||||||
|
|
||||||
|
getDirnames := func(name string) []string {
|
||||||
|
name = filepath.FromSlash(name)
|
||||||
|
f, err := rfs.Open(name)
|
||||||
|
assert.NoError(err)
|
||||||
|
defer f.Close()
|
||||||
|
names, err := f.Readdirnames(-1)
|
||||||
|
assert.NoError(err)
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal([]string{"a.txt", "b", "e"}, getDirnames("static"))
|
||||||
|
assert.Equal([]string{"b.txt", "c"}, getDirnames("static/b"))
|
||||||
|
assert.Equal([]string{"c.txt"}, getDirnames("static/b/c"))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestRootMappingFsOs(t *testing.T) {
|
func TestRootMappingFsOs(t *testing.T) {
|
||||||
assert := require.New(t)
|
assert := require.New(t)
|
||||||
fs := afero.NewOsFs()
|
fs := afero.NewOsFs()
|
||||||
|
|
|
@ -463,6 +463,7 @@ func (b *sourceFilesystemsBuilder) createMainOverlayFs(p *paths.Paths) (*filesys
|
||||||
}
|
}
|
||||||
|
|
||||||
isMainProject := mod.Owner() == nil
|
isMainProject := mod.Owner() == nil
|
||||||
|
// TODO(bep) embed mount + move any duplicate/overlap
|
||||||
modsReversed[i] = mountsDescriptor{
|
modsReversed[i] = mountsDescriptor{
|
||||||
mounts: mod.Mounts(),
|
mounts: mod.Mounts(),
|
||||||
dir: dir,
|
dir: dir,
|
||||||
|
|
Loading…
Reference in a new issue