This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
debug.go
165 lines (144 loc) · 4.09 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2016, Gareth Watts
// All rights reserved.
package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
)
func addSysinfoToZip(zf *zip.Writer) error {
info := fmt.Sprintf(`OS: %s
Arch: %s
CPU Count: %d
`, runtime.GOOS, runtime.GOARCH, runtime.NumCPU())
return addStringToZip(zf, "sysinfo.txt", info)
}
var captureFilenames = []string{restrictionsPlistName, "Status.plist"}
// addBackupInfoToZip retrieves information about the supplied backup
// and adds some information about it to the zip file including:
// * some human readable text information such as pathname, parsed pin information, etc
// * A list of all the on-disk files in the backup (but not the contents or the unhashed filenames)
// * The contents of the Status.plist and the restrictions information plist files.
// No other information is included.
func addBackupInfoToZip(zf *zip.Writer, b *backup) error {
fn := filepath.Base(b.Path)
if err := addStringToZip(zf, path.Join("backups", fn, "info.txt"), b.debugInfo()); err != nil {
return err
}
// Enumerate the files the backup contains
var filelist bytes.Buffer
filepath.Walk(b.Path, func(fpath string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.IsDir() {
fmt.Fprintf(&filelist, "%-10d %s\n", info.Size(), fpath[len(b.Path)+1:])
if oneOf(path.Base(fpath), captureFilenames) {
addFileToZip(zf, fpath, path.Join("backups", fn, fpath[len(b.Path)+1:]))
}
}
return nil
})
if err := addStringToZip(zf, path.Join("backups", fn, "filelist.txt"), filelist.String()); err != nil {
return err
}
return nil
}
// addFileToZip copies a single file into the supplied zip using the given filename.
func addFileToZip(zf *zip.Writer, path, fn string) error {
f, err := os.Open(path)
if err != nil {
return addStringToZip(zf, fn, fmt.Sprintf("failed to open file %s: %v", path, err))
}
defer f.Close()
g, err := zf.Create(fn)
if err != nil {
return err
}
_, err = io.Copy(g, f)
return err
}
// buildDebug constructs a .zip file containing debugging information in the given target
// directory. If targetDir is empty then it will use the user's home or desktop directory.
func buildDebug(targetDir string, backupResult string, allBackups *backups) (fn string, err error) {
if targetDir == "" {
targetDir, err = getDefaultDir()
if err != nil {
return "", err
}
}
fn = filepath.Join(targetDir, "pinfinder-debug.zip")
debugFile, err := os.Create(fn)
if err != nil {
return "", fmt.Errorf("Failed to open %s for write: %v", fn, err)
}
defer debugFile.Close()
zf := zip.NewWriter(debugFile)
defer zf.Close()
if err := addStringToZip(zf, "output.txt", backupResult); err != nil {
return "", err
}
// Capture system info
if err := addSysinfoToZip(zf); err != nil {
return "", err
}
for _, backup := range allBackups.backups {
if err := addBackupInfoToZip(zf, backup); err != nil {
return "", err
}
}
return fn, nil
}
// addStringToZip adds a string as a new file to a zip file with the given filename
func addStringToZip(zf *zip.Writer, filename, content string) error {
f, err := zf.Create(filename)
if err != nil {
return err
}
_, err = f.Write([]byte(content))
return err
}
// getDefaultDir returns the directory the executable is in,
// or the user's home directory, or the Windows Desktop directory if that
// is not writable
func getDefaultDir() (string, error) {
dir := filepath.Dir(os.Args[0])
if dir != "" && isWritable(dir) {
return dir, nil
}
user, err := user.Current()
if err != nil {
return "", fmt.Errorf("failed to fetch user information: %v", err)
}
switch runtime.GOOS {
case "windows":
return filepath.Join(user.HomeDir, "Desktop"), nil
default:
return user.HomeDir, nil
}
}
func isWritable(dir string) bool {
tf, err := ioutil.TempFile(dir, "pinfinder")
if err != nil {
return false
}
defer os.Remove(tf.Name())
tf.Close()
return true
}
// oneOf returns true if s matches one of choices.
func oneOf(s string, choices []string) bool {
for _, ch := range choices {
if s == ch {
return true
}
}
return false
}