2015-01-14 21:08:48 +00:00
|
|
|
// +build debug
|
|
|
|
|
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-05-01 22:48:26 +00:00
|
|
|
"path/filepath"
|
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
|
|
|
"restic/fs"
|
2015-01-14 21:08:48 +00:00
|
|
|
"runtime"
|
2015-05-01 23:13:07 +00:00
|
|
|
"strconv"
|
2015-01-14 21:08:48 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-10-26 19:10:03 +00:00
|
|
|
"time"
|
2016-08-29 17:18:57 +00:00
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2015-01-14 21:08:48 +00:00
|
|
|
)
|
|
|
|
|
2015-10-26 19:10:03 +00:00
|
|
|
type process struct {
|
2016-09-27 20:23:34 +00:00
|
|
|
fn string
|
2015-10-26 19:10:03 +00:00
|
|
|
goroutine int
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:08:48 +00:00
|
|
|
var opts struct {
|
2015-04-30 05:05:58 +00:00
|
|
|
logger *log.Logger
|
2016-09-27 20:23:34 +00:00
|
|
|
funcs map[string]bool
|
|
|
|
files map[string]bool
|
2015-10-26 19:10:03 +00:00
|
|
|
last map[process]time.Time
|
2015-01-14 21:08:48 +00:00
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
2015-05-01 13:39:01 +00:00
|
|
|
// make sure that all the initialization happens before the init() functions
|
|
|
|
// are called, cf https://golang.org/ref/spec#Package_initialization
|
|
|
|
var _ = initDebug()
|
|
|
|
|
|
|
|
func initDebug() bool {
|
2015-04-30 05:05:58 +00:00
|
|
|
initDebugLogger()
|
|
|
|
initDebugTags()
|
2015-01-14 21:08:48 +00:00
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "debug enabled\n")
|
2015-05-01 13:39:01 +00:00
|
|
|
|
|
|
|
return true
|
2015-04-30 05:05:58 +00:00
|
|
|
}
|
2015-01-14 21:08:48 +00:00
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
func initDebugLogger() {
|
2015-01-14 21:08:48 +00:00
|
|
|
debugfile := os.Getenv("DEBUG_LOG")
|
2015-04-30 05:05:58 +00:00
|
|
|
if debugfile == "" {
|
|
|
|
return
|
|
|
|
}
|
2015-02-08 13:46:29 +00:00
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "debug log file %v\n", debugfile)
|
2015-01-14 21:08:48 +00:00
|
|
|
|
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
|
|
|
f, err := fs.OpenFile(debugfile, os.O_WRONLY|os.O_APPEND, 0600)
|
2015-04-30 05:05:58 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
_, err = f.Seek(2, 0)
|
2015-01-14 21:08:48 +00:00
|
|
|
if err != nil {
|
2015-04-30 05:05:58 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "unable to seek to the end of %v: %v\n", debugfile, err)
|
|
|
|
os.Exit(3)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
2015-04-30 05:05:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:18:57 +00:00
|
|
|
if err != nil && os.IsNotExist(errors.Cause(err)) {
|
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
|
|
|
f, err = fs.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE, 0600)
|
2015-04-30 05:05:58 +00:00
|
|
|
}
|
2015-01-14 21:08:48 +00:00
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "unable to open debug log file: %v\n", err)
|
|
|
|
os.Exit(2)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
opts.logger = log.New(f, "", log.LstdFlags)
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
func parseFilter(envname string, pad func(string) string) map[string]bool {
|
|
|
|
filter := make(map[string]bool)
|
2015-01-14 21:08:48 +00:00
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
env := os.Getenv(envname)
|
|
|
|
if env == "" {
|
|
|
|
return filter
|
2015-04-30 05:05:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
for _, fn := range strings.Split(env, ",") {
|
|
|
|
t := pad(strings.TrimSpace(fn))
|
2015-04-30 05:05:58 +00:00
|
|
|
val := true
|
|
|
|
if t[0] == '-' {
|
|
|
|
val = false
|
|
|
|
t = t[1:]
|
|
|
|
} else if t[0] == '+' {
|
|
|
|
val = true
|
|
|
|
t = t[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// test pattern
|
|
|
|
_, err := path.Match(t, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: invalid pattern %q: %v\n", t, err)
|
|
|
|
os.Exit(5)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
filter[t] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
return filter
|
|
|
|
}
|
|
|
|
|
|
|
|
func padFunc(s string) string {
|
|
|
|
if s == "all" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(s, "/") {
|
|
|
|
s = "*/" + s
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func padFile(s string) string {
|
|
|
|
if s == "all" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(s, "/") {
|
|
|
|
s = "*/" + s
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(s, ":") {
|
|
|
|
s = s + ":*"
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDebugTags() {
|
|
|
|
opts.last = make(map[process]time.Time)
|
|
|
|
|
|
|
|
opts.funcs = parseFilter("DEBUG_FUNCS", padFunc)
|
|
|
|
opts.files = parseFilter("DEBUG_FILES", padFile)
|
2015-04-30 05:05:58 +00:00
|
|
|
}
|
2015-01-14 21:08:48 +00:00
|
|
|
|
2015-05-01 22:48:26 +00:00
|
|
|
// taken from https://github.com/VividCortex/trace
|
|
|
|
func goroutineNum() int {
|
|
|
|
b := make([]byte, 20)
|
|
|
|
runtime.Stack(b, false)
|
|
|
|
var num int
|
|
|
|
|
|
|
|
fmt.Sscanf(string(b), "goroutine %d ", &num)
|
|
|
|
return num
|
|
|
|
}
|
|
|
|
|
|
|
|
// taken from https://github.com/VividCortex/trace
|
2016-09-27 20:23:34 +00:00
|
|
|
func getPosition() (fn, dir, file string, line int) {
|
|
|
|
pc, file, line, ok := runtime.Caller(2)
|
2015-05-01 22:48:26 +00:00
|
|
|
if !ok {
|
2016-09-27 20:23:34 +00:00
|
|
|
return "", "", "", 0
|
|
|
|
}
|
|
|
|
|
|
|
|
dirname, filename := filepath.Base(filepath.Dir(file)), filepath.Base(file)
|
|
|
|
|
|
|
|
Func := runtime.FuncForPC(pc)
|
|
|
|
|
|
|
|
return Func.Name(), dirname, filename, line
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkFilter(filter map[string]bool, key string) bool {
|
|
|
|
// check if key is enabled directly
|
|
|
|
if v, ok := filter[key]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for globbing
|
|
|
|
for k, v := range filter {
|
|
|
|
if m, _ := path.Match(k, key); m {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if tag "all" is enabled
|
|
|
|
if v, ok := filter["all"]; ok && v {
|
|
|
|
return true
|
2015-05-01 22:48:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
return false
|
2015-05-01 22:48:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
var maxPosLen = 10
|
2015-05-01 23:13:07 +00:00
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
// Log prints a message to the debug log (if debug is enabled).
|
2016-09-27 20:35:08 +00:00
|
|
|
func Log(f string, args ...interface{}) {
|
2015-01-14 21:08:48 +00:00
|
|
|
opts.m.Lock()
|
|
|
|
defer opts.m.Unlock()
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
fn, dir, file, line := getPosition()
|
2015-10-26 19:10:03 +00:00
|
|
|
goroutine := goroutineNum()
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
last, ok := opts.last[process{fn, goroutine}]
|
2015-10-26 19:10:03 +00:00
|
|
|
if !ok {
|
|
|
|
last = time.Now()
|
|
|
|
}
|
|
|
|
current := time.Now()
|
2016-09-27 20:23:34 +00:00
|
|
|
opts.last[process{fn, goroutine}] = current
|
2015-10-26 19:10:03 +00:00
|
|
|
|
2016-08-28 20:42:46 +00:00
|
|
|
if len(f) == 0 || f[len(f)-1] != '\n' {
|
2015-02-07 16:04:20 +00:00
|
|
|
f += "\n"
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
pos := fmt.Sprintf("%s/%s:%d", dir, file, line)
|
|
|
|
if len(pos) > maxPosLen {
|
|
|
|
maxPosLen = len(pos)
|
2015-05-01 23:13:07 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
formatStringTag := "%2.3f [%" + strconv.FormatInt(int64(maxPosLen), 10) + "s]"
|
|
|
|
formatString := fmt.Sprintf(formatStringTag+" %s", current.Sub(last).Seconds(), pos, f)
|
2015-05-01 22:48:26 +00:00
|
|
|
|
2015-01-14 21:08:48 +00:00
|
|
|
dbgprint := func() {
|
2015-05-01 22:48:26 +00:00
|
|
|
fmt.Fprintf(os.Stderr, formatString, args...)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
if opts.logger != nil {
|
2015-05-01 22:48:26 +00:00
|
|
|
opts.logger.Printf(formatString, args...)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
if checkFilter(opts.files, fmt.Sprintf("%s/%s:%d", dir, file, line)) {
|
|
|
|
dbgprint()
|
2015-01-14 21:08:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:23:34 +00:00
|
|
|
if checkFilter(opts.funcs, fn) {
|
2015-01-14 21:08:48 +00:00
|
|
|
dbgprint()
|
|
|
|
}
|
|
|
|
}
|