/
io.go
711 lines (564 loc) · 12.7 KB
/
io.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
package common
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"
)
var (
ReadOnlyFileMode = FileMode(true, true, false)
DefaultFileMode = FileMode(true, true, false)
DefaultDirMode = FileMode(true, true, true)
FlagCountBackups *int
)
type ErrFileNotFound struct {
FileName string
}
func (e *ErrFileNotFound) Error() string {
return fmt.Sprintf("file or path not found: %s", e.FileName)
}
type debugWriter struct {
Name string
Action string
}
func (this *debugWriter) Write(p []byte) (n int, err error) {
Debug("%s %s %d bytes: %+q", this.Name, this.Action, len(p), string(p))
return len(p), nil
}
// +-----+---+--------------------------+
// | rwx | 7 | Read, write and execute |
// | rw- | 6 | Read, write |
// | r-x | 5 | Read, and execute |
// | r-- | 4 | Read, |
// | -wx | 3 | Write and execute |
// | -w- | 2 | Write |
// | --x | 1 | Execute |
// | --- | 0 | no permissions |
// +------------------------------------+
// +------------+------+-------+
// | Permission | Octal| Field |
// +------------+------+-------+
// | rwx------ | 0700 | User |
// | ---rwx--- | 0070 | Group |
// | ------rwx | 0007 | Other |
// +------------+------+-------+
var tempDir string
func init() {
var err error
tempDir, err = ioutil.TempDir("", Title())
if err != nil {
panic(err)
}
AddShutdownHook(func() {
Error(deleteTempDir())
})
FlagCountBackups = flag.Int("backup.count", 3, "amount of file backups")
}
// AppCleanup cleans up all remaining objects
func deleteTempDir() error {
b, err := FileExists(tempDir)
if err != nil {
return err
}
if !b {
return nil
}
DebugFunc(tempDir)
err = filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
b, err := IsFileReadOnly(path)
if err != nil {
return err
}
if !b {
err := SetFileReadOnly(path, false)
if err != nil {
return err
}
}
}
return nil
})
if err != nil {
return err
}
err = os.RemoveAll(tempDir)
if err != nil {
return err
}
return nil
}
// TempDir returns the private temporary directory of the app
func TempDir() string {
return tempDir
}
// CreateTempFile creates a temporary file
func CreateTempFile() (file *os.File, err error) {
tempDir := TempDir()
file, err = ioutil.TempFile(tempDir, GetRuntimeInfo(1).Filename()+"-")
if err != nil {
return nil, err
}
defer func() {
Ignore(file.Close())
}()
Debug(fmt.Sprintf("CreateTempFile : %s", file.Name()))
return file, err
}
// CreateTempDir creates a temporary file
func CreateTempDir() (string, error) {
rootTempDir := TempDir()
tempdir, err := ioutil.TempDir(rootTempDir, GetRuntimeInfo(1).Filename()+"-")
if err != nil {
return "", err
}
Debug(fmt.Sprintf("CreateTempDir : %s", tempdir))
return tempdir, err
}
func fileExists(filename string) (bool, error) {
var b bool
_, err := os.Stat(filename)
if os.IsNotExist(err) {
b = false
err = nil
} else {
b = err == nil
}
return b, err
}
// FileExists does ... guess what :-)
func FileExists(filename string) (bool, error) {
b, err := fileExists(filename)
Debug(fmt.Sprintf("FileExists %s: %v", filename, b))
return b, err
}
// FileDelete does ... guess what :-)
func FileDelete(filename string) error {
b, err := FileExists(filename)
if err != nil {
return err
}
if b {
Debug(fmt.Sprintf("FileRemove %s: %v", filename, b))
return os.Remove(filename)
}
return nil
}
// FileDate does ... guess what :-)
func FileDate(filename string) (time.Time, error) {
f, err := os.Stat(filename)
if err != nil {
return time.Time{}, err
}
t, err := f.ModTime().MarshalText()
if err != nil {
return time.Time{}, err
}
Debug(fmt.Sprintf("FileDate %s: %s", filename, string(t)))
return f.ModTime(), nil
}
// FileSize does ... guess what :-)
func FileSize(filename string) (int64, error) {
file, err := os.Stat(filename)
if err != nil {
return -1, err
}
return file.Size(), nil
}
// FileCopy does ... guess what :-)
func FileCopy(src string, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer func() {
Ignore(srcFile.Close())
}()
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer func() {
Ignore(destFile.Close())
}()
_, err = io.Copy(destFile, srcFile)
if err != nil {
return err
}
err = destFile.Sync()
if err != nil {
return err
}
return nil
}
// FileStore creates backup of files
func FileStore(filename string, r io.Reader) error {
// create the file
out, err := os.Create(filename)
if err != nil {
return err
}
// care about final cleanup of open file
defer func() {
Ignore(out.Close())
}()
// download the remote resource to the file
_, err = io.Copy(out, r)
if err != nil {
return err
}
return nil
}
// FileBackup creates backup of files
func FileBackup(filename string) error {
if *FlagCountBackups < 1 {
return nil
}
for i := *FlagCountBackups - 1; i >= 0; i-- {
src := filename
if i > 0 {
src = src + "." + strconv.Itoa(i)
}
dst := ""
if *FlagCountBackups == 1 {
dst = filename + ".bak"
} else {
dst = filename + "." + strconv.Itoa(i+1)
}
b, err := fileExists(src)
if err != nil {
return err
}
if b {
b, err = fileExists(dst)
if err != nil {
return err
}
if b {
err = os.Remove(dst)
if err != nil {
return err
}
}
err := os.Rename(src, dst)
if err != nil {
return err
}
}
}
return nil
}
// IsFileReadOnly checks if a file is read only
func IsFileReadOnly(path string) (result bool, err error) {
result = false
file, err := os.OpenFile(path, os.O_WRONLY, DefaultFileMode)
if err != nil {
if !os.IsPermission(err) {
result = true
} else {
return false, err
}
}
Ignore(file.Close())
return result, nil
}
// IsDirectory checks if the path leads to a directory
func IsDirectory(path string) (bool, error) {
b, err := FileExists(path)
if err != nil {
return false, err
}
if b {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.IsDir(), nil
} else {
return false, nil
}
}
// IsDirectory checks if the path leads to a directory
func IsFile(path string) (bool, error) {
b, err := IsDirectory(path)
if err != nil {
return false, err
}
return !b, nil
}
// IsSymbolicLink checks if the path leads to symbolic link
func IsSymbolicLink(path string) bool {
file, err := os.Lstat(path)
if err != nil {
return false
}
return file.Mode()&os.ModeSymlink != 0
}
// SetFileReadOnly sets file READ-ONLY yes or false
func SetFileReadOnly(path string, readonly bool) (err error) {
if readonly {
err = os.Chmod(path, ReadOnlyFileMode)
} else {
err = os.Chmod(path, DefaultFileMode)
}
return err
}
// Returns the complete filename "test.txt"
func FileName(filename string) string {
_, filename = filepath.Split(filename)
return filename
}
// Returns the filename part without extension "test.txt" -> "test"
func FileNamePart(filename string) string {
_, filename = filepath.Split(filename)
return filename[0 : len(filename)-len(FileNameExt(filename))]
}
// Returns the filename extension without part "test.txt" -> ".txt"
func FileNameExt(filename string) string {
return filepath.Ext(filename)
}
// CleanPath cleans the given path and also replace to OS specific separators
func CleanPath(path string) string {
result := path
if IsWindowsOS() {
result = strings.Replace(result, "/", string(filepath.Separator), -1)
} else {
result = strings.Replace(result, "\\", string(filepath.Separator), -1)
}
p := strings.Index(result, "~")
if p != -1 {
userHomeDir := ""
usr, err := user.Current()
if !Error(err) {
userHomeDir = usr.HomeDir
}
result = strings.Replace(result, "~", userHomeDir, -1)
}
result = filepath.Clean(result)
if !filepath.IsAbs(result) && !strings.HasPrefix(result, string(filepath.Separator)) {
var dir string
var err error
if IsRunningInteractive() {
dir, err = os.Getwd()
} else {
dir, err = os.Executable()
if err == nil {
dir = filepath.Dir(dir)
}
}
if err != nil {
Error(err)
} else {
result = filepath.Join(dir, result)
}
}
DebugFunc("%s -> %s", path, result)
return result
}
func ScanLinesWithLF(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, data[0 : i+1], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
func CopyWithContext(ctx context.Context, cancel context.CancelFunc, name string, writer io.Writer, reader io.Reader, bufferSize int) (int64, error) {
Debug("%s copyWithContext: start", name)
var written int64
var err error
go func() {
if bufferSize <= 0 {
bufferSize = 32 * 1024
}
buf := make([]byte, bufferSize)
defer func() {
Debug("%s cancel!", name)
cancel()
}()
if *FlagLogIO {
written, err = io.CopyBuffer(io.MultiWriter(writer, &debugWriter{name, "WRITE"}), io.TeeReader(reader, &debugWriter{name, "READ"}), buf)
} else {
written, err = io.CopyBuffer(writer, reader, buf)
}
if err != nil {
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
err = fmt.Errorf("Timeout error")
}
if IsErrNetClosing(err) || IsErrUnexpectedEOF(err) {
err = nil
} else {
DebugError(err)
}
}
}()
<-ctx.Done()
Debug("%s copyWithContext: stop", name)
return written, err
}
type FilePermission struct {
Read bool
Write bool
Execute bool
}
func CalcFileMode(owner FilePermission, group FilePermission, public FilePermission) os.FileMode {
txt := "0"
for _, p := range []FilePermission{owner, group, public} {
var value int
if p.Execute {
value += 1
}
if p.Write {
value += 2
}
if p.Read {
value += 4
}
txt += strconv.Itoa(value)
}
result, _ := strconv.ParseInt(txt, 8, 64)
DebugFunc("%s, %d: owner: %+v group: %+v public: %+v", txt, result, owner, group, public)
return os.FileMode(result)
}
func FileMode(read, write, execute bool) os.FileMode {
return CalcFileMode(
FilePermission{
Read: read,
Write: write,
Execute: execute,
},
FilePermission{
Read: read,
Write: false,
Execute: execute,
},
FilePermission{
Read: read,
Write: false,
Execute: execute,
},
)
}
type lineBuffer struct {
buf bytes.Buffer
count int
lines []string
f func(string) string
ba io.Reader
}
func NewLineBuffer(count int, f func(string) string) *lineBuffer {
return &lineBuffer{
buf: bytes.Buffer{},
count: count,
lines: make([]string, 0),
f: f,
}
}
func (this *lineBuffer) Read(p []byte) (n int, err error) {
if this.ba == nil {
this.ba = strings.NewReader(strings.Join(this.lines, ""))
}
return this.ba.Read(p)
}
func (this *lineBuffer) Write(p []byte) (int, error) {
for _, b := range p {
err := this.buf.WriteByte(b)
if Error(err) {
return -1, err
}
if b == '\n' {
line := this.buf.String()
if this.f != nil {
line = this.f(line)
}
if len(this.lines) < this.count {
this.lines = append(this.lines, line)
} else {
copy(this.lines, this.lines[1:])
this.lines[len(this.lines)-1] = line
}
this.buf.Reset()
}
}
return len(p), nil
}
func (this *lineBuffer) Lines() []string {
return this.lines
}
func URLGet(url string) ([]byte, error) {
h := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if Error(err) {
return nil, err
}
var r *http.Response
r, err = h.Do(req)
if Error(err) {
return nil, err
}
ba, err := ioutil.ReadAll(r.Body)
defer func() {
Error(r.Body.Close())
}()
if Error(err) {
return nil, err
}
return ba, nil
}
func WriteJsonFile(filename string, v interface{}, fileMode os.FileMode) error {
ba, err := json.MarshalIndent(v, "", " ")
if Error(err) {
return err
}
return ioutil.WriteFile(filename, ba, fileMode)
}
func ReadJsonFile(filename string, v interface{}) error {
ba, err := ioutil.ReadFile(filename)
if Error(err) {
return err
}
return json.Unmarshal(ba, v)
}
type ZeroReader struct {
}
func NewZeroReader() *ZeroReader {
return &ZeroReader{}
}
func (this ZeroReader) Read(p []byte) (n int, err error) {
for i := range p {
p[i] = 0
}
return len(p), nil
}
type RandomReader struct {
template [256]byte
}
func NewRandomReader() *RandomReader {
r := RandomReader{}
for i := range r.template {
r.template[i] = byte(Rnd(256))
}
return &r
}
func (this RandomReader) Read(p []byte) (n int, err error) {
copy(p, this.template[:])
return len(p), nil
}