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 14package hugofs 15 16import ( 17 "fmt" 18 "os" 19 "regexp" 20 "runtime" 21 22 "github.com/gohugoio/hugo/common/types" 23 24 "github.com/spf13/afero" 25) 26 27// Make sure we don't accidentally use this in the real Hugo. 28var _ types.DevMarker = (*stacktracerFs)(nil) 29 30// NewStacktracerFs wraps the given fs printing stack traces for file creates 31// matching the given regexp pattern. 32func NewStacktracerFs(fs afero.Fs, pattern string) afero.Fs { 33 return &stacktracerFs{Fs: fs, re: regexp.MustCompile(pattern)} 34} 35 36// stacktracerFs can be used in hard-to-debug development situations where 37// you get some input you don't understand where comes from. 38type stacktracerFs struct { 39 afero.Fs 40 41 // Will print stacktrace for every file creates matching this pattern. 42 re *regexp.Regexp 43} 44 45func (fs *stacktracerFs) DevOnly() { 46} 47 48func (fs *stacktracerFs) onCreate(filename string) { 49 if fs.re.MatchString(filename) { 50 trace := make([]byte, 1500) 51 runtime.Stack(trace, true) 52 fmt.Printf("\n===========\n%q:\n%s\n", filename, trace) 53 } 54} 55 56func (fs *stacktracerFs) Create(name string) (afero.File, error) { 57 f, err := fs.Fs.Create(name) 58 if err == nil { 59 fs.onCreate(name) 60 } 61 return f, err 62} 63 64func (fs *stacktracerFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) { 65 f, err := fs.Fs.OpenFile(name, flag, perm) 66 if err == nil && isWrite(flag) { 67 fs.onCreate(name) 68 } 69 return f, err 70} 71