-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwriter.go
327 lines (296 loc) · 8.14 KB
/
writer.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package wpk
import (
"encoding/binary"
"io"
"io/fs"
"os"
"strings"
"gopkg.in/djherbis/times.v1"
)
// WriteSeekCloser is typical useful interface for package writing.
type WriteSeekCloser interface {
io.Writer
io.Seeker
io.Closer
}
// Begin writes prebuild header for new empty package.
func (ftt *FTT) Begin(wpt, wpf io.WriteSeeker) (err error) {
ftt.mux.Lock()
defer ftt.mux.Unlock()
// write prebuild header
var offset uint64
if wpf == nil || wpf == wpt {
offset = HeaderSize
}
var hdr = Header{
typesize: TypeSize{ftt.tidsz, ftt.tagsz, ftt.tssize},
fttoffset: offset,
fttsize: 0,
datoffset: offset,
datsize: 0,
}
copy(hdr.signature[:], SignBuild)
if _, err = wpt.Seek(0, io.SeekStart); err != nil {
return
}
if _, err = hdr.WriteTo(wpt); err != nil {
return
}
// update data offset/pos
ftt.datoffset, ftt.datsize = offset, 0
return
}
// Append writes prebuild header for previously opened package to append new files.
func (ftt *FTT) Append(wpt, wpf io.WriteSeeker) (err error) {
ftt.mux.Lock()
defer ftt.mux.Unlock()
// go to file start
if _, err = wpt.Seek(0, io.SeekStart); err != nil {
return
}
// rewrite prebuild signature
if err = binary.Write(wpt, binary.LittleEndian, S2B(SignBuild)); err != nil {
return
}
// go to tags table start to replace it by new data
if wpf != nil && wpf != wpt { // splitted package files
if _, err = wpf.Seek(int64(ftt.datoffset+ftt.datsize), io.SeekStart); err != nil {
return
}
} else { // single package file
if _, err = wpt.Seek(int64(ftt.datoffset+ftt.datsize), io.SeekStart); err != nil {
return
}
}
return
}
// Sync writes actual file tags table and true signature with settings.
func (ftt *FTT) Sync(wpt, wpf io.WriteSeeker) (err error) {
ftt.mux.Lock()
defer ftt.mux.Unlock()
var fftpos, fftend, datpos, datend int64
if wpf != nil && wpf != wpt { // splitted package files
// get tags table offset as actual end of file
datpos = 0
if datend, err = wpf.Seek(0, io.SeekCurrent); err != nil {
return
}
fftpos = HeaderSize
// update package info if it has
if ts, ok := ftt.Info(); ok {
ts.Set(TIDoffset, UintTag(Uint(datpos)))
ts.Set(TIDsize, UintTag(Uint(datend-datpos)))
}
// write file tags table
if _, err = wpt.Seek(fftpos, io.SeekStart); err != nil {
return
}
if _, err = ftt.WriteTo(wpt); err != nil {
return
}
// get writer end marker and setup the file tags table size
if fftend, err = wpt.Seek(0, io.SeekCurrent); err != nil {
return
}
} else { // single package file
// get tags table offset as actual end of file
datpos = HeaderSize
if datend, err = wpt.Seek(0, io.SeekCurrent); err != nil {
return
}
fftpos = datend
// update package info if it has
if ts, ok := ftt.Info(); ok {
ts.Set(TIDoffset, UintTag(Uint(datpos)))
ts.Set(TIDsize, UintTag(Uint(datend-datpos)))
}
// write file tags table
if _, err = ftt.WriteTo(wpt); err != nil {
return
}
// get writer end marker and setup the file tags table size
if fftend, err = wpt.Seek(0, io.SeekCurrent); err != nil {
return
}
}
// rewrite true header
var hdr = Header{
typesize: TypeSize{ftt.tidsz, ftt.tagsz, ftt.tssize},
fttoffset: uint64(fftpos),
fttsize: uint64(fftend - fftpos),
datoffset: uint64(datpos),
datsize: uint64(datend - datpos),
}
copy(hdr.signature[:], SignReady)
if _, err = wpt.Seek(0, io.SeekStart); err != nil {
return
}
if _, err = hdr.WriteTo(wpt); err != nil {
return
}
// update data offset/pos
ftt.datoffset, ftt.datsize = uint64(datpos), uint64(datend-datpos)
return
}
// PackData puts data streamed by given reader into package as a file
// and associate keyname "kpath" with it.
func (pkg *Package) PackData(w io.WriteSeeker, r io.Reader, fpath string) (ts *TagsetRaw, err error) {
if _, ok := pkg.GetTagset(fpath); ok {
err = &fs.PathError{Op: "packdata", Path: fpath, Err: fs.ErrExist}
return
}
var offset, size int64
if func() {
pkg.mux.Lock()
defer pkg.mux.Unlock()
// get offset and put provided data
if offset, err = w.Seek(0, io.SeekCurrent); err != nil {
return
}
if size, err = io.Copy(w, r); err != nil {
return
}
// update actual package data size
pkg.datsize += uint64(size)
}(); err != nil {
return
}
// insert new entry to tags table
ts = pkg.BaseTagset(Uint(offset), Uint(size), fpath)
pkg.SetTagset(fpath, ts)
return
}
// PackFile puts file with given file handle into package and associate keyname "fpath" with it.
func (pkg *Package) PackFile(w io.WriteSeeker, file fs.File, fpath string) (ts *TagsetRaw, err error) {
var fi os.FileInfo
if fi, err = file.Stat(); err != nil {
return
}
if ts, err = pkg.PackData(w, file, fpath); err != nil {
return
}
//ts.Put(TIDmtime, TimeTag(fi.ModTime()))
var tsp = times.Get(fi)
ts.Put(TIDmtime, TimeTag(tsp.ModTime()))
ts.Put(TIDatime, TimeTag(tsp.AccessTime()))
if tsp.HasChangeTime() {
ts.Put(TIDctime, TimeTag(tsp.ChangeTime()))
}
if tsp.HasBirthTime() {
ts.Put(TIDbtime, TimeTag(tsp.BirthTime()))
}
ts.Put(TIDlink, StrTag(fpath))
return
}
// PackDirLogger is function called during PackDir processing after each
// file with OS file object and inserted tagset, that can be modified.
type PackDirLogger func(r io.ReadSeeker, ts *TagsetRaw) error
// PackDir puts all files of given folder and it's subfolders into package.
// Logger function can be nil.
func (pkg *Package) PackDir(w io.WriteSeeker, dirname, prefix string, logger PackDirLogger) (err error) {
var fis []os.FileInfo
if func() {
var dir *os.File
if dir, err = os.Open(dirname); err != nil {
return
}
defer dir.Close()
if fis, err = dir.Readdir(-1); err != nil {
return
}
}(); err != nil {
return
}
for _, fi := range fis {
if fi != nil {
var kpath = prefix + fi.Name()
var fpath = dirname + fi.Name()
if fi.IsDir() {
if err = pkg.PackDir(w, fpath+"/", kpath+"/", logger); err != nil {
return
}
} else if func() {
var file *os.File
var ts *TagsetRaw
if file, err = os.Open(fpath); err != nil {
return
}
defer file.Close()
if ts, err = pkg.PackFile(w, file, kpath); err != nil {
return
}
if err = logger(file, ts); err != nil {
return
}
}(); err != nil {
return
}
}
}
return
}
// Rename tagset with file name 'oldname' to 'newname'.
// Keeps link to original file name.
func (pkg *Package) Rename(oldname, newname string) error {
var ts, ok = pkg.GetTagset(oldname)
if !ok {
return &fs.PathError{Op: "rename", Path: oldname, Err: fs.ErrNotExist}
}
if _, ok = pkg.GetTagset(newname); ok {
return &fs.PathError{Op: "rename", Path: newname, Err: fs.ErrExist}
}
ts.Set(TIDpath, StrTag(ToSlash(pkg.FullPath(newname))))
pkg.DelTagset(oldname)
pkg.SetTagset(newname, ts)
return nil
}
// RenameDir renames all files in package with 'olddir' path to 'newdir' path.
func (pkg *Package) RenameDir(olddir, newdir string, skipexist bool) (count int, err error) {
if len(olddir) > 0 && olddir[len(olddir)-1] != '/' {
olddir += "/"
}
if len(newdir) > 0 && newdir[len(newdir)-1] != '/' {
newdir += "/"
}
pkg.Enum(func(fkey string, ts *TagsetRaw) bool {
if strings.HasPrefix(fkey, olddir) {
var newkey = newdir + fkey[len(olddir):]
if _, ok := pkg.GetTagset(newkey); ok {
err = &fs.PathError{Op: "renamedir", Path: newkey, Err: fs.ErrExist}
return skipexist
}
ts.Set(TIDpath, StrTag(ToSlash(pkg.FullPath(newkey))))
pkg.DelTagset(fkey)
pkg.SetTagset(newkey, ts)
count++
}
return true
})
if skipexist {
err = nil
}
return
}
// PutAlias makes clone tagset with file name 'oldname' and replace name tag
// in it to 'newname'. Keeps link to original file name.
func (pkg *Package) PutAlias(oldname, newname string) error {
var ts1, ok = pkg.GetTagset(oldname)
if !ok {
return &fs.PathError{Op: "putalias", Path: oldname, Err: fs.ErrNotExist}
}
if _, ok = pkg.GetTagset(newname); ok {
return &fs.PathError{Op: "putalias", Path: newname, Err: fs.ErrExist}
}
var tsi = ts1.Iterator()
var ts2 = pkg.NewTagset()
for tsi.Next() {
if tsi.tid != TIDpath {
ts2.Put(tsi.tid, tsi.Tag())
} else {
ts2.Put(TIDpath, StrTag(ToSlash(pkg.FullPath(newname))))
}
}
pkg.SetTagset(newname, ts2)
return nil
}
// The End.