2016-03-21 19:28:42 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-10-16 20:20:09 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-10-16 20:20:09 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-10-16 20:20:09 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-03 07:37:57 -04:00
|
|
|
"regexp"
|
2016-08-08 14:25:00 -04:00
|
|
|
"runtime"
|
2013-11-28 09:27:09 -05:00
|
|
|
"strings"
|
2015-02-17 16:21:37 -05:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/cast"
|
2015-02-17 16:21:37 -05:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2017-02-04 22:20:06 -05:00
|
|
|
"golang.org/x/text/unicode/norm"
|
2013-09-05 01:28:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Input interface {
|
|
|
|
Files() []*File
|
|
|
|
}
|
|
|
|
|
|
|
|
type Filesystem struct {
|
|
|
|
files []*File
|
|
|
|
Base string
|
|
|
|
AvoidPaths []string
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
SourceSpec
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func (sp SourceSpec) NewFilesystem(base string, avoidPaths ...string) *Filesystem {
|
|
|
|
return &Filesystem{SourceSpec: sp, Base: base, AvoidPaths: avoidPaths}
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
func (f *Filesystem) FilesByExts(exts ...string) []*File {
|
|
|
|
var newFiles []*File
|
|
|
|
|
|
|
|
if len(exts) == 0 {
|
|
|
|
return f.Files()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, x := range f.Files() {
|
|
|
|
for _, e := range exts {
|
|
|
|
if x.Ext() == strings.TrimPrefix(e, ".") {
|
|
|
|
newFiles = append(newFiles, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newFiles
|
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
func (f *Filesystem) Files() []*File {
|
2013-10-01 17:27:09 -04:00
|
|
|
if len(f.files) < 1 {
|
|
|
|
f.captureFiles()
|
|
|
|
}
|
2013-09-05 01:28:59 -04:00
|
|
|
return f.files
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
// add populates a file in the Filesystem.files
|
2013-09-20 20:03:43 -04:00
|
|
|
func (f *Filesystem) add(name string, reader io.Reader) (err error) {
|
2014-10-16 20:20:09 -04:00
|
|
|
var file *File
|
2013-09-20 20:03:43 -04:00
|
|
|
|
2016-08-08 14:25:00 -04:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
// When a file system is HFS+, its filepath is in NFD form.
|
|
|
|
name = norm.NFC.String(name)
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
file, err = f.SourceSpec.NewFileFromAbs(f.Base, name, reader)
|
2013-11-28 09:27:09 -05:00
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
if err == nil {
|
|
|
|
f.files = append(f.files, file)
|
2013-09-20 20:03:43 -04:00
|
|
|
}
|
2014-10-16 20:20:09 -04:00
|
|
|
return err
|
2013-09-20 20:03:43 -04:00
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
func (f *Filesystem) captureFiles() {
|
2013-09-20 20:03:43 -04:00
|
|
|
walker := func(filePath string, fi os.FileInfo, err error) error {
|
2013-09-05 01:28:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-07 16:31:57 -04:00
|
|
|
b, err := f.ShouldRead(filePath, fi)
|
2016-01-07 21:48:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if b {
|
2017-02-04 22:20:06 -05:00
|
|
|
rd, err := NewLazyFileReader(f.Fs.Source, filePath)
|
2015-02-17 16:21:37 -05:00
|
|
|
if err != nil {
|
2016-01-07 21:48:13 -05:00
|
|
|
return err
|
2015-02-17 16:21:37 -05:00
|
|
|
}
|
2016-01-07 21:48:13 -05:00
|
|
|
f.add(filePath, rd)
|
2014-12-10 10:48:51 -05:00
|
|
|
}
|
2016-01-07 21:48:13 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-12-10 10:48:51 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if f.Fs == nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
panic("Must have a fs")
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
err := helpers.SymbolicWalk(f.Fs.Source, f.Base, walker)
|
2016-02-14 06:16:03 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
2016-11-21 17:00:44 -05:00
|
|
|
if err == helpers.ErrWalkRootTooShort {
|
2016-07-28 03:30:58 -04:00
|
|
|
panic("The root path is too short. If this is a test, make sure to init the content paths.")
|
|
|
|
}
|
2016-02-14 06:16:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
}
|
2015-03-07 06:58:39 -05:00
|
|
|
|
2017-06-07 16:31:57 -04:00
|
|
|
func (f *Filesystem) ShouldRead(filePath string, fi os.FileInfo) (bool, error) {
|
2016-01-07 21:48:13 -05:00
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
link, err := filepath.EvalSymlinks(filePath)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", filePath, err)
|
|
|
|
return false, nil
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
linkfi, err := f.Fs.Source.Stat(link)
|
2015-03-07 06:58:39 -05:00
|
|
|
if err != nil {
|
2016-01-07 21:48:13 -05:00
|
|
|
jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
|
|
|
|
return false, nil
|
2015-03-07 06:58:39 -05:00
|
|
|
}
|
2016-01-07 21:48:13 -05:00
|
|
|
if !linkfi.Mode().IsRegular() {
|
|
|
|
jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filePath)
|
|
|
|
}
|
|
|
|
return false, nil
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
if fi.IsDir() {
|
2017-02-04 22:20:06 -05:00
|
|
|
if f.avoid(filePath) || f.isNonProcessablePath(filePath) {
|
2016-01-07 21:48:13 -05:00
|
|
|
return false, filepath.SkipDir
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if f.isNonProcessablePath(filePath) {
|
2016-01-07 21:48:13 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
|
|
|
|
2013-09-20 20:03:43 -04:00
|
|
|
func (f *Filesystem) avoid(filePath string) bool {
|
2013-09-05 01:28:59 -04:00
|
|
|
for _, avoid := range f.AvoidPaths {
|
2013-09-20 20:03:43 -04:00
|
|
|
if avoid == filePath {
|
2013-09-05 01:28:59 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-25 22:25:33 -04:00
|
|
|
func (sp SourceSpec) isNonProcessablePath(filePath string) bool {
|
2014-02-28 00:26:17 -05:00
|
|
|
base := filepath.Base(filePath)
|
2016-03-21 12:22:27 -04:00
|
|
|
if strings.HasPrefix(base, ".") ||
|
|
|
|
strings.HasPrefix(base, "#") ||
|
|
|
|
strings.HasSuffix(base, "~") {
|
2014-02-28 00:26:17 -05:00
|
|
|
return true
|
|
|
|
}
|
2017-09-25 22:25:33 -04:00
|
|
|
ignoreFiles := cast.ToStringSlice(sp.Cfg.Get("ignoreFiles"))
|
2015-06-03 07:37:57 -04:00
|
|
|
if len(ignoreFiles) > 0 {
|
|
|
|
for _, ignorePattern := range ignoreFiles {
|
2015-06-03 12:54:30 -04:00
|
|
|
match, err := regexp.MatchString(ignorePattern, filePath)
|
|
|
|
if err != nil {
|
|
|
|
helpers.DistinctErrorLog.Printf("Invalid regexp '%s' in ignoreFiles: %s", ignorePattern, err)
|
|
|
|
return false
|
|
|
|
} else if match {
|
2015-06-03 07:37:57 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-28 00:26:17 -05:00
|
|
|
return false
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|