-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.go
287 lines (266 loc) · 6.81 KB
/
backup.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"bufio"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"golang.org/x/term"
. "github.com/JaMo42/spellcheck_comments/common"
"github.com/JaMo42/spellcheck_comments/term_input"
"github.com/JaMo42/spellcheck_comments/tui"
"github.com/JaMo42/spellcheck_comments/util"
)
const (
iconNone = " "
iconWarn = "\x1b[33m⚠\x1b[m"
iconError = "\x1b[31m❌\x1b[m"
iconOk = "\x1b[32m✓\x1b[m"
iconSkipped = "\x1b[2m-\x1b[m"
)
func backupFileName() string {
return fmt.Sprintf("%s.backup", appName)
}
func relPath(pathname string) string {
cwd, _ := os.Getwd()
rel, err := filepath.Rel(cwd, pathname)
if err != nil {
return pathname
}
return rel
}
type backupLine struct {
isFile bool
line int
time int64
text string
}
type Backup struct {
lines []backupLine
builder strings.Builder
lastLine int
file *os.File
}
func (self *Backup) SetFile(filename string) {
pathname, _ := filepath.Abs(filepath.Clean(filename))
stat, _ := os.Stat(pathname)
self.lines = append(self.lines, backupLine{
isFile: true,
time: stat.ModTime().Unix(),
text: pathname,
})
self.lastLine = -1
}
func (self *Backup) AddLine(line int, tb *tui.TextBuffer, replacements map[tui.SliceIndex]string) {
if line == self.lastLine {
return
}
self.builder.Reset()
tb.ForEachInLine(line, func(s string, index tui.SliceIndex) {
replacement, found := replacements[index]
if found {
self.builder.WriteString(replacement)
} else {
self.builder.WriteString(s)
}
})
self.lines = append(self.lines, backupLine{
line: line,
text: self.builder.String(),
})
self.lastLine = line
}
// Create creates the backup file
func (self *Backup) Create() error {
var err error
self.file, err = os.Create(backupFileName())
return err
}
// Write writes the backup file
func (self *Backup) Write() {
w := bufio.NewWriter(self.file)
for _, line := range self.lines {
if line.isFile {
fmt.Fprintf(w, "FILE %d %s\n", line.time, line.text)
} else {
fmt.Fprintf(w, "%d %s\n", line.line, line.text)
}
}
w.Flush()
self.file.Close()
}
// LoadBackup attempts to load a backup file from the current directory.
func LoadBackup() Optional[Backup] {
data, err := os.ReadFile(backupFileName())
if err != nil {
return None[Backup]()
}
backup := Backup{}
for _, line := range strings.Split(string(data), "\n") {
if len(line) == 0 {
continue
}
if strings.HasPrefix(line, "FILE") {
split := strings.SplitN(line, " ", 3)
time, _ := strconv.ParseInt(split[1], 10, 64)
pathname := split[2]
backup.lines = append(backup.lines, backupLine{
isFile: true,
time: time,
text: pathname,
})
} else {
split := strings.SplitN(line, " ", 2)
line, _ := strconv.ParseInt(split[0], 10, 32)
text := split[1]
backup.lines = append(backup.lines, backupLine{
line: int(line),
text: text,
})
}
}
return Some(backup)
}
// statAndCheck stats the given file and checks if it's a writable regular file.
func statAndCheck(pathname string) (os.FileInfo, error) {
stat, err := os.Stat(pathname)
if os.IsNotExist(err) {
return nil, fmt.Errorf("files does not exist")
}
if !stat.Mode().IsRegular() {
return nil, fmt.Errorf("not a regular file")
}
// FIXME: this assumes we are the user that created the file
needPerm := fs.FileMode(0o600)
if stat.Mode().Perm()&needPerm != needPerm {
return nil, fmt.Errorf("file is not writable")
}
return stat, nil
}
type backupForEachCallback = func(pathname string, err error, outdated bool, lineNumbers []int, lines []string) bool
// ForEach calls the given function for each file. This consumes the backup data.
func (self *Backup) ForEach(f backupForEachCallback) {
if !self.lines[0].isFile {
Fatal("invalid backup: first line must be a file header")
}
var line backupLine
lineNumbers := []int{}
lines := []string{}
for len(self.lines) != 0 {
line, self.lines = util.PopFront(self.lines)
pathname := line.text
stat, err := statAndCheck(pathname)
if err != nil {
f(pathname, err, false, lineNumbers, lines)
continue
}
outdated := stat.ModTime().Unix() != line.time
lineNumbers = lineNumbers[:0]
lines = lines[:0]
for len(self.lines) != 0 && !self.lines[0].isFile {
line, self.lines = util.PopFront(self.lines)
lineNumbers = append(lineNumbers, line.line)
lines = append(lines, line.text)
}
if !f(pathname, nil, outdated, lineNumbers, lines) {
break
}
}
}
// backupApplyFile returns a callback for Backup.ForEach that applies the backup
// for one file with the specified error handling.
func backupApplyFile(pathname string, lineNumbers []int, lines []string) error {
data, _ := os.ReadFile(pathname)
fileLines := strings.Split(string(data), "\n")
for i, line := range lineNumbers {
fileLines[line] = lines[i]
}
file, _ := os.Create(pathname)
w := bufio.NewWriter(file)
for _, line := range fileLines {
w.WriteString(line)
w.WriteByte('\n')
}
err := w.Flush()
file.Close()
return err
}
func restoreAllForEach(pathname string, err error, outdated bool, lineNumbers []int, lines []string) bool {
displayPath := relPath(pathname)
if err != nil {
log.Printf("%s: %s\n", displayPath, err)
}
if outdated {
log.Printf("%s: file changed since backup (skipping)", displayPath)
}
err = backupApplyFile(pathname, lineNumbers, lines)
if err != nil {
log.Printf("%s: %s\n", displayPath, err)
}
return true
}
func BackupRestoreAll() {
LoadBackup().Then(func(backup Backup) {
backup.ForEach(restoreAllForEach)
}).Else(func() {
fmt.Println("No backup file in current directory")
})
}
func runBackupForEach(pathname string, err error, outdated bool, lineNumbers []int, lines []string) bool {
displayPath := relPath(pathname)
var textLen int
if err != nil {
fmt.Printf("%s %s: %s\n", iconError, displayPath, err)
return true
} else if outdated {
textLen, _ = fmt.Printf("%s %s (file changed since backup!) (y/N)", iconWarn, displayPath)
} else {
textLen, _ = fmt.Printf("%s %s (Y/n)", iconNone, displayPath)
}
defer fmt.Println("\r")
yes := false
inputLoop:
for {
switch term_input.Read() {
case 'y', 'Y':
yes = true
break inputLoop
case 'n', 'N':
break inputLoop
case '\r', '\n':
if !outdated {
yes = true
}
break inputLoop
case 0:
return false
}
}
if yes {
err = backupApplyFile(pathname, lineNumbers, lines)
if err != nil {
clearOld := strings.Repeat(" ", textLen+1)
fmt.Printf("\r%s\r%s %s: %s", clearOld, iconError, displayPath, err)
} else {
fmt.Printf("\r%s", iconOk)
}
} else {
fmt.Printf("\r%s", iconSkipped)
}
return true
}
func RunBackup() {
LoadBackup().Then(func(backup Backup) {
fd := int(os.Stdin.Fd())
oldState, _ := term.MakeRaw(fd)
term_input.Begin()
backup.ForEach(runBackupForEach)
term_input.Stop()
term.Restore(fd, oldState)
}).Else(func() {
fmt.Println("No backup file in current directory")
})
}