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