mirror of
https://github.com/restic/restic.git
synced 2025-12-12 05:42:28 +00:00
Refactor debug into debug module
This commit is contained in:
69
cmd/gentestdata/main.go
Normal file
69
cmd/gentestdata/main.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxFiles = 23
|
||||
MaxDepth = 3
|
||||
)
|
||||
|
||||
var urnd *os.File
|
||||
|
||||
func init() {
|
||||
f, err := os.Open("/dev/urandom")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
urnd = f
|
||||
}
|
||||
|
||||
func rndRd(bytes int) io.Reader {
|
||||
return io.LimitReader(urnd, int64(bytes))
|
||||
}
|
||||
|
||||
func create_dir(target string, depth int) {
|
||||
fmt.Printf("create_dir %s, depth %d\n", target, depth)
|
||||
err := os.Mkdir(target, 0755)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for i := 0; i < MaxFiles; i++ {
|
||||
if depth == 0 {
|
||||
filename := filepath.Join(target, fmt.Sprintf("file%d", i))
|
||||
fmt.Printf("create file %v\n", filename)
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(f, rndRd(rand.Intn(1024)))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
create_dir(filepath.Join(target, fmt.Sprintf("dir%d", i)), depth-1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Fprintf(os.Stderr, "USAGE: %s TARGETDIR\n", os.Args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
create_dir(os.Args[1], MaxDepth)
|
||||
}
|
||||
26
cmd/ram/main.go
Normal file
26
cmd/ram/main.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/restic/restic"
|
||||
)
|
||||
|
||||
func main() {
|
||||
max := int(1e6)
|
||||
nodes := make([]*restic.Node, 0, max)
|
||||
|
||||
fi, err := os.Lstat("main.go")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for i := 0; i < max; i++ {
|
||||
node, err := restic.NodeFromFileInfo("main.go", fi)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
nodes = append(nodes, node)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
VERSION = $(shell ./version.sh)
|
||||
VERSION ?= "unknown version"
|
||||
LDFLAGS = -X main.version $(VERSION)
|
||||
LDFLAGS += -X github.com/restic/restic.Version $(VERSION)
|
||||
TAGS =
|
||||
|
||||
.PHONY: all both clean debug
|
||||
@@ -19,7 +20,7 @@ restic: $(wildcard *.go) $(wildcard ../../*.go) $(wildcard ../../*/*.go)
|
||||
go build -tags "$(TAGS)" $(GOFLAGS) -ldflags "$(LDFLAGS)"
|
||||
|
||||
restic.debug: $(wildcard *.go) $(wildcard ../../*.go) $(wildcard ../../*/*.go)
|
||||
go build -o restic.debug -tags "debug" $(GOFLAGS) -ldflags "$(LDFLAGS)"
|
||||
go build -o restic.debug -tags "debug debug_cmd" $(GOFLAGS) -ldflags "$(LDFLAGS)"
|
||||
|
||||
clean:
|
||||
go clean
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/restic/restic"
|
||||
"github.com/restic/restic/backend"
|
||||
"github.com/restic/restic/debug"
|
||||
)
|
||||
|
||||
type findQuery struct {
|
||||
@@ -64,7 +65,7 @@ func parseTime(str string) (time.Time, error) {
|
||||
}
|
||||
|
||||
func (c CmdFind) findInTree(s restic.Server, blob restic.Blob, path string) ([]findResult, error) {
|
||||
debug("checking tree %v\n", blob)
|
||||
debug.Log("restic.find", "checking tree %v\n", blob)
|
||||
tree, err := restic.LoadTree(s, blob)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -72,7 +73,7 @@ func (c CmdFind) findInTree(s restic.Server, blob restic.Blob, path string) ([]f
|
||||
|
||||
results := []findResult{}
|
||||
for _, node := range tree.Nodes {
|
||||
debug(" testing entry %q\n", node.Name)
|
||||
debug.Log("restic.find", " testing entry %q\n", node.Name)
|
||||
|
||||
m, err := filepath.Match(c.pattern, node.Name)
|
||||
if err != nil {
|
||||
@@ -80,20 +81,20 @@ func (c CmdFind) findInTree(s restic.Server, blob restic.Blob, path string) ([]f
|
||||
}
|
||||
|
||||
if m {
|
||||
debug(" pattern matches\n")
|
||||
debug.Log("restic.find", " pattern matches\n")
|
||||
if !c.oldest.IsZero() && node.ModTime.Before(c.oldest) {
|
||||
debug(" ModTime is older than %s\n", c.oldest)
|
||||
debug.Log("restic.find", " ModTime is older than %s\n", c.oldest)
|
||||
continue
|
||||
}
|
||||
|
||||
if !c.newest.IsZero() && node.ModTime.After(c.newest) {
|
||||
debug(" ModTime is newer than %s\n", c.newest)
|
||||
debug.Log("restic.find", " ModTime is newer than %s\n", c.newest)
|
||||
continue
|
||||
}
|
||||
|
||||
results = append(results, findResult{node: node, path: path})
|
||||
} else {
|
||||
debug(" pattern does not match\n")
|
||||
debug.Log("restic.find", " pattern does not match\n")
|
||||
}
|
||||
|
||||
if node.Type == "dir" {
|
||||
@@ -115,7 +116,7 @@ func (c CmdFind) findInTree(s restic.Server, blob restic.Blob, path string) ([]f
|
||||
}
|
||||
|
||||
func (c CmdFind) findInSnapshot(s restic.Server, id backend.ID) error {
|
||||
debug("searching in snapshot %s\n for entries within [%s %s]", id, c.oldest, c.newest)
|
||||
debug.Log("restic.find", "searching in snapshot %s\n for entries within [%s %s]", id, c.oldest, c.newest)
|
||||
|
||||
sn, err := restic.LoadSnapshot(s, id)
|
||||
if err != nil {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/restic/restic"
|
||||
"github.com/restic/restic/backend"
|
||||
"github.com/restic/restic/debug"
|
||||
)
|
||||
|
||||
type CmdFsck struct {
|
||||
@@ -34,7 +35,7 @@ func fsckFile(opts CmdFsck, s restic.Server, m *restic.Map, IDs []backend.ID) (u
|
||||
var bytes uint64
|
||||
|
||||
for _, id := range IDs {
|
||||
debug("checking data blob %v\n", id)
|
||||
debug.Log("restic.fsck", "checking data blob %v\n", id)
|
||||
|
||||
// test if blob is in map
|
||||
blob, err := m.FindID(id)
|
||||
@@ -72,7 +73,7 @@ func fsckFile(opts CmdFsck, s restic.Server, m *restic.Map, IDs []backend.ID) (u
|
||||
}
|
||||
|
||||
func fsckTree(opts CmdFsck, s restic.Server, blob restic.Blob) error {
|
||||
debug("checking tree %v\n", blob.ID)
|
||||
debug.Log("restic.fsck", "checking tree %v\n", blob.ID)
|
||||
|
||||
tree, err := restic.LoadTree(s, blob)
|
||||
if err != nil {
|
||||
@@ -138,7 +139,7 @@ func fsckTree(opts CmdFsck, s restic.Server, blob restic.Blob) error {
|
||||
}
|
||||
|
||||
func fsck_snapshot(opts CmdFsck, s restic.Server, id backend.ID) error {
|
||||
debug("checking snapshot %v\n", id)
|
||||
debug.Log("restic.fsck", "checking snapshot %v\n", id)
|
||||
|
||||
sn, err := restic.LoadSnapshot(s, id)
|
||||
if err != nil {
|
||||
@@ -151,7 +152,7 @@ func fsck_snapshot(opts CmdFsck, s restic.Server, id backend.ID) error {
|
||||
|
||||
err = fsckTree(opts, s, sn.Tree)
|
||||
if err != nil {
|
||||
debug(" checking tree %v for snapshot %v\n", sn.Tree, id)
|
||||
debug.Log("restic.fsck", " checking tree %v for snapshot %v\n", sn.Tree, id)
|
||||
fmt.Fprintf(os.Stderr, "snapshot %v:\n error for tree %v:\n %v\n", id, sn.Tree, err)
|
||||
}
|
||||
|
||||
@@ -196,7 +197,7 @@ func (cmd CmdFsck) Execute(args []string) error {
|
||||
}
|
||||
|
||||
list, err := s.List(backend.Snapshot)
|
||||
debug("checking %d snapshots\n", len(list))
|
||||
debug.Log("restic.fsck", "checking %d snapshots\n", len(list))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -214,7 +215,7 @@ func (cmd CmdFsck) Execute(args []string) error {
|
||||
return firstErr
|
||||
}
|
||||
|
||||
debug("starting orphaned check\n")
|
||||
debug.Log("restic.fsck", "starting orphaned check\n")
|
||||
|
||||
l := []struct {
|
||||
desc string
|
||||
@@ -226,7 +227,7 @@ func (cmd CmdFsck) Execute(args []string) error {
|
||||
}
|
||||
|
||||
for _, d := range l {
|
||||
debug("checking for orphaned %v\n", d.desc)
|
||||
debug.Log("restic.fsck", "checking for orphaned %v\n", d.desc)
|
||||
|
||||
blobs, err := s.List(d.tpe)
|
||||
if err != nil {
|
||||
|
||||
@@ -3,8 +3,6 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/restic/restic"
|
||||
)
|
||||
|
||||
type CmdVersion struct{}
|
||||
@@ -20,13 +18,7 @@ func init() {
|
||||
}
|
||||
|
||||
func (cmd CmdVersion) Execute(args []string) error {
|
||||
fmt.Printf("restic version %s, lib %v on %v\n", version, restic.Version, runtime.Version())
|
||||
for _, s := range features {
|
||||
fmt.Printf(" %s\n", s)
|
||||
}
|
||||
for _, s := range restic.Features {
|
||||
fmt.Printf(" %s\n", s)
|
||||
}
|
||||
fmt.Printf("restic %s on %v\n", version, runtime.Version())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
// +build debug_cmd
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
var debugLogger = initDebugLogger()
|
||||
|
||||
func initDebugLogger() *log.Logger {
|
||||
// create new log file
|
||||
filename := fmt.Sprintf("restic-debug-%d-%s",
|
||||
os.Getpid(), time.Now().Format("20060201-150405"))
|
||||
path := filepath.Join(os.TempDir(), filename)
|
||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "unable to create debug log file: %v", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
// open logger
|
||||
l := log.New(io.MultiWriter(os.Stderr, f), "DEBUG: ", log.LstdFlags)
|
||||
fmt.Fprintf(os.Stderr, "debug log for restic command activated, writing log file %s\n", path)
|
||||
l.Printf("restic %s", version)
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func debug(fmt string, args ...interface{}) {
|
||||
debugLogger.Printf(fmt, args...)
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
// +build !debug_cmd
|
||||
|
||||
package main
|
||||
|
||||
func debug(fmt string, args ...interface{}) {}
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/restic/restic"
|
||||
"github.com/restic/restic/backend"
|
||||
"github.com/restic/restic/debug"
|
||||
)
|
||||
|
||||
var version = "compiled manually"
|
||||
@@ -166,6 +167,8 @@ func main() {
|
||||
// defer profile.Start(profile.MemProfileRate(100000), profile.ProfilePath(".")).Stop()
|
||||
opts.Repo = os.Getenv("RESTIC_REPOSITORY")
|
||||
|
||||
debug.Log("restic", "main %#v", os.Args)
|
||||
|
||||
_, err := parser.Parse()
|
||||
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
||||
os.Exit(0)
|
||||
|
||||
Reference in New Issue
Block a user