-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifile.go
executable file
·207 lines (181 loc) · 3.97 KB
/
ifile.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
package ifile
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
pathspec "github.com/tomruk/go-pathspec"
"go.uber.org/zap"
)
type (
Ifile struct {
log *zap.Logger
logS *zap.SugaredLogger
mode Mode
buf bytes.Buffer
bufMu sync.Mutex
end []byte
filePath string
file *os.File
once sync.Once
}
entry struct {
path string
isDir bool
}
entries []*entry
ignorefile struct {
p *pathspec.PathSpec
dir string
}
Mode int
)
const (
// Include file. Paths are absolute.
ModeRestic Mode = iota
// Ignore file. Paths are relative to the .gitignore/.csignore.
ModeSyncthing
)
func (m Mode) String() string {
switch m {
case ModeRestic:
return "restic"
case ModeSyncthing:
return "syncthing"
default:
return "<invalid mode>"
}
}
const (
generatedBy = "# Generated by kopyaship. DO NOT TOUCH THE LINES BETWEEN I_BEGIN AND I_END."
beginIndicator = "# I_BEGIN"
endIndicator = "# I_END"
)
func New(filePath string, mode Mode, appendToExisting bool, log *zap.Logger) (ifile *Ifile, err error) {
ifile = &Ifile{
log: log,
logS: log.Sugar(),
mode: mode,
filePath: filePath,
}
flags := os.O_CREATE | os.O_RDWR
if !appendToExisting {
flags |= os.O_TRUNC
}
ifile.file, err = os.OpenFile(ifile.filePath, flags, 0660)
if err != nil {
return nil, err
}
if appendToExisting {
err = ifile.seekToEnd()
if err != nil {
return nil, err
}
} else {
ifile.buf.WriteString(generatedBy + "\n")
ifile.buf.WriteString(beginIndicator + "\n")
ifile.end = append(ifile.end, []byte(endIndicator+"\n")...)
}
return
}
func (i *Ifile) seekToEnd() error {
content, err := io.ReadAll(i.file)
if err != nil {
return err
}
content = bytes.ReplaceAll(content, []byte{'\r', '\n'}, []byte{'\n'})
splitted := bytes.Split(content, []byte{'\n'})
begin := -1
end := -1
c := 0
for _, line := range splitted {
if bytes.Equal(line, []byte(endIndicator)) {
end = c
break
} else if bytes.Equal(line, []byte(beginIndicator)) {
begin = c
}
c += len(line) + 1
}
if begin == -1 && end != -1 {
return fmt.Errorf("ifile begin indicator ('%s') not found", beginIndicator)
} else if end == -1 && begin != -1 {
return fmt.Errorf("ifile end indicator ('%s') not found", endIndicator)
}
i.logS.Debugf("ifile: %s: begin %d, end %d", i.filePath, begin, end)
// To apply potential newline seperator change.
_, err = i.file.Seek(0, io.SeekStart)
if err != nil {
return err
}
err = i.file.Truncate(0)
if err != nil {
return err
}
_, err = i.file.Write(content)
if err != nil {
return err
}
if begin == -1 && end == -1 {
_, err := i.file.Seek(0, io.SeekEnd)
if err != nil {
return err
}
i.buf.WriteString(generatedBy + "\n")
i.buf.WriteString(beginIndicator + "\n")
i.end = append(i.end, []byte(endIndicator+"\n")...)
} else {
seek := int64(begin + len(beginIndicator) + 1)
err = i.file.Truncate(seek)
if err != nil {
return err
}
_, err = i.file.Seek(seek, io.SeekStart)
if err != nil {
return err
}
i.end = content[end:]
}
return nil
}
func (i *Ifile) Close() (err error) {
var (
err1 error
err2 error
)
i.once.Do(func() {
i.logS.Debugf("ifile: %s: writing i.buf", i.filePath)
_, err1 = i.file.Write(i.buf.Bytes())
if len(i.end) != 0 {
i.logS.Debugf("ifile: %s: writing i.end", i.filePath)
_, err2 = i.file.Write(i.end)
} else {
i.logS.Debugf("ifile: %s: writing newline", i.filePath)
_, err2 = i.file.WriteString("\n")
}
i.file.Close()
})
// Prioritize err1
if err1 != nil {
i.logS.Debugf("ifile: %s: err1 occured", i.filePath)
err = err1
} else if err2 != nil {
i.logS.Debugf("ifile: %s: err2 occured", i.filePath)
err = err2
}
return
}
func (e *entry) Len() int {
// + 1 is for newline; + 10 is for potential '\[' and '\]'
return len(e.path) + 1 + 10
}
func (e *entry) String() string {
s := e.path + "\n"
s = strings.ReplaceAll(s, "[", "\\[")
s = strings.ReplaceAll(s, "]", "\\]")
s = filepath.ToSlash(s)
return s
}