mirror of
https://github.com/restic/restic.git
synced 2025-08-12 11:17:42 +00:00
Moves files
This commit is contained in:
202
internal/debug/debug.go
Normal file
202
internal/debug/debug.go
Normal file
@@ -0,0 +1,202 @@
|
||||
// +build debug
|
||||
|
||||
package debug
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"restic/fs"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"restic/errors"
|
||||
)
|
||||
|
||||
var opts struct {
|
||||
logger *log.Logger
|
||||
funcs map[string]bool
|
||||
files map[string]bool
|
||||
}
|
||||
|
||||
// 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 {
|
||||
initDebugLogger()
|
||||
initDebugTags()
|
||||
|
||||
fmt.Fprintf(os.Stderr, "debug enabled\n")
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func initDebugLogger() {
|
||||
debugfile := os.Getenv("DEBUG_LOG")
|
||||
if debugfile == "" {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "debug log file %v\n", debugfile)
|
||||
|
||||
f, err := fs.OpenFile(debugfile, os.O_WRONLY|os.O_APPEND, 0600)
|
||||
|
||||
if err == nil {
|
||||
_, err = f.Seek(2, 0)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "unable to seek to the end of %v: %v\n", debugfile, err)
|
||||
os.Exit(3)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil && os.IsNotExist(errors.Cause(err)) {
|
||||
f, err = fs.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "unable to open debug log file: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
opts.logger = log.New(f, "", log.LstdFlags)
|
||||
}
|
||||
|
||||
func parseFilter(envname string, pad func(string) string) map[string]bool {
|
||||
filter := make(map[string]bool)
|
||||
|
||||
env := os.Getenv(envname)
|
||||
if env == "" {
|
||||
return filter
|
||||
}
|
||||
|
||||
for _, fn := range strings.Split(env, ",") {
|
||||
t := pad(strings.TrimSpace(fn))
|
||||
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)
|
||||
}
|
||||
|
||||
filter[t] = val
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
func padFunc(s string) string {
|
||||
if s == "all" {
|
||||
return s
|
||||
}
|
||||
|
||||
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.funcs = parseFilter("DEBUG_FUNCS", padFunc)
|
||||
opts.files = parseFilter("DEBUG_FILES", padFile)
|
||||
}
|
||||
|
||||
// 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
|
||||
func getPosition() (fn, dir, file string, line int) {
|
||||
pc, file, line, ok := runtime.Caller(2)
|
||||
if !ok {
|
||||
return "", "", "", 0
|
||||
}
|
||||
|
||||
dirname, filename := filepath.Base(filepath.Dir(file)), filepath.Base(file)
|
||||
|
||||
Func := runtime.FuncForPC(pc)
|
||||
|
||||
return path.Base(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
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Log prints a message to the debug log (if debug is enabled).
|
||||
func Log(f string, args ...interface{}) {
|
||||
fn, dir, file, line := getPosition()
|
||||
goroutine := goroutineNum()
|
||||
|
||||
if len(f) == 0 || f[len(f)-1] != '\n' {
|
||||
f += "\n"
|
||||
}
|
||||
|
||||
pos := fmt.Sprintf("%s/%s:%d", dir, file, line)
|
||||
|
||||
formatString := fmt.Sprintf("%s\t%s\t%d\t%s", pos, fn, goroutine, f)
|
||||
|
||||
dbgprint := func() {
|
||||
fmt.Fprintf(os.Stderr, formatString, args...)
|
||||
}
|
||||
|
||||
if opts.logger != nil {
|
||||
opts.logger.Printf(formatString, args...)
|
||||
}
|
||||
|
||||
if checkFilter(opts.files, fmt.Sprintf("%s/%s:%d", dir, file, line)) {
|
||||
dbgprint()
|
||||
return
|
||||
}
|
||||
|
||||
if checkFilter(opts.funcs, fn) {
|
||||
dbgprint()
|
||||
}
|
||||
}
|
6
internal/debug/debug_release.go
Normal file
6
internal/debug/debug_release.go
Normal file
@@ -0,0 +1,6 @@
|
||||
// +build !debug
|
||||
|
||||
package debug
|
||||
|
||||
// Log prints a message to the debug log (if debug is enabled).
|
||||
func Log(fmt string, args ...interface{}) {}
|
2
internal/debug/doc.go
Normal file
2
internal/debug/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package debug provides an infrastructure for logging debug information and breakpoints.
|
||||
package debug
|
28
internal/debug/hooks.go
Normal file
28
internal/debug/hooks.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// +build !release
|
||||
|
||||
package debug
|
||||
|
||||
var (
|
||||
hooks map[string]func(interface{})
|
||||
)
|
||||
|
||||
func init() {
|
||||
hooks = make(map[string]func(interface{}))
|
||||
}
|
||||
|
||||
func Hook(name string, f func(interface{})) {
|
||||
hooks[name] = f
|
||||
}
|
||||
|
||||
func RunHook(name string, context interface{}) {
|
||||
f, ok := hooks[name]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
f(context)
|
||||
}
|
||||
|
||||
func RemoveHook(name string) {
|
||||
delete(hooks, name)
|
||||
}
|
9
internal/debug/hooks_release.go
Normal file
9
internal/debug/hooks_release.go
Normal file
@@ -0,0 +1,9 @@
|
||||
// +build release
|
||||
|
||||
package debug
|
||||
|
||||
func Hook(name string, f func(interface{})) {}
|
||||
|
||||
func RunHook(name string, context interface{}) {}
|
||||
|
||||
func RemoveHook(name string) {}
|
94
internal/debug/round_tripper_debug.go
Normal file
94
internal/debug/round_tripper_debug.go
Normal file
@@ -0,0 +1,94 @@
|
||||
// +build debug
|
||||
|
||||
package debug
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
"restic/errors"
|
||||
)
|
||||
|
||||
type eofDetectRoundTripper struct {
|
||||
http.RoundTripper
|
||||
}
|
||||
|
||||
type eofDetectReader struct {
|
||||
eofSeen bool
|
||||
rd io.ReadCloser
|
||||
}
|
||||
|
||||
func (rd *eofDetectReader) Read(p []byte) (n int, err error) {
|
||||
n, err = rd.rd.Read(p)
|
||||
if err == io.EOF {
|
||||
rd.eofSeen = true
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (rd *eofDetectReader) Close() error {
|
||||
if !rd.eofSeen {
|
||||
buf, err := ioutil.ReadAll(rd)
|
||||
msg := fmt.Sprintf("body not drained, %d bytes not read", len(buf))
|
||||
if err != nil {
|
||||
msg += fmt.Sprintf(", error: %v", err)
|
||||
}
|
||||
|
||||
if len(buf) > 0 {
|
||||
if len(buf) > 20 {
|
||||
buf = append(buf[:20], []byte("...")...)
|
||||
}
|
||||
msg += fmt.Sprintf(", body: %q", buf)
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stderr, msg)
|
||||
Log("%s: %+v", msg, errors.New("Close()"))
|
||||
}
|
||||
return rd.rd.Close()
|
||||
}
|
||||
|
||||
func (tr eofDetectRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
|
||||
res, err = tr.RoundTripper.RoundTrip(req)
|
||||
if res != nil && res.Body != nil {
|
||||
res.Body = &eofDetectReader{rd: res.Body}
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
type loggingRoundTripper struct {
|
||||
http.RoundTripper
|
||||
}
|
||||
|
||||
// RoundTripper returns a new http.RoundTripper which logs all requests (if
|
||||
// debug is enabled). When debug is not enabled, upstream is returned.
|
||||
func RoundTripper(upstream http.RoundTripper) http.RoundTripper {
|
||||
return loggingRoundTripper{eofDetectRoundTripper{upstream}}
|
||||
}
|
||||
|
||||
func (tr loggingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
|
||||
trace, err := httputil.DumpRequestOut(req, false)
|
||||
if err != nil {
|
||||
Log("DumpRequestOut() error: %v\n", err)
|
||||
} else {
|
||||
Log("------------ HTTP REQUEST -----------\n%s", trace)
|
||||
}
|
||||
|
||||
res, err = tr.RoundTripper.RoundTrip(req)
|
||||
if err != nil {
|
||||
Log("RoundTrip() returned error: %v", err)
|
||||
}
|
||||
|
||||
if res != nil {
|
||||
trace, err := httputil.DumpResponse(res, false)
|
||||
if err != nil {
|
||||
Log("DumpResponse() error: %v\n", err)
|
||||
} else {
|
||||
Log("------------ HTTP RESPONSE ----------\n%s", trace)
|
||||
}
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
11
internal/debug/round_tripper_release.go
Normal file
11
internal/debug/round_tripper_release.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// +build !debug
|
||||
|
||||
package debug
|
||||
|
||||
import "net/http"
|
||||
|
||||
// RoundTripper returns a new http.RoundTripper which logs all requests (if
|
||||
// debug is enabled). When debug is not enabled, upstream is returned.
|
||||
func RoundTripper(upstream http.RoundTripper) http.RoundTripper {
|
||||
return upstream
|
||||
}
|
Reference in New Issue
Block a user