-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
78 lines (74 loc) · 1.57 KB
/
file.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
package pgbackup
import (
"crypto/sha512"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// File is a wrapper around a filepath to expose functionality.
type File string
// SHA512 computes a sha512 hash and writes it to the same path by replacing the
// existing extension with .sha512.
func (f File) SHA512() error {
var dfd, sfd *os.File
var err error
//
fstr := string(f)
//
if sfd, err = os.Open(fstr); err != nil {
return err
}
defer sfd.Close()
//
h := sha512.New()
if _, err = io.Copy(h, sfd); err != nil {
return err
}
//
hfile := strings.Replace(fstr, filepath.Ext(fstr), ".sha512", 1)
if dfd, err = os.Create(hfile); err != nil {
return err
}
defer dfd.Close()
if _, err = fmt.Fprintf(dfd, "%x", h.Sum(nil)); err != nil {
return err
}
//
return nil
}
// Split splits a file into sequentially numbered chunks of equal size until the last chunk which
// will hold whatever remains.
//
// If dst is empty string then the chunk file names are derived from the filename. Otherwise
// the part numbers are appended to dst as needed.
func (f File) Split(basepath string, size int, suffixLen int) error {
fstr := string(f)
if basepath == "" {
ext := filepath.Ext(fstr)
basepath = strings.TrimSuffix(fstr, ext)
}
//
split := &SplitWriter{
Basepath: basepath,
SplitSize: size,
SuffixLength: suffixLen,
}
defer split.Close()
//
sfd, err := os.Open(fstr)
if err != nil {
return err
}
defer sfd.Close()
//
if _, err = io.Copy(split, sfd); err != nil {
return err
}
//
if err = split.Close(); err != nil {
return err
}
return nil
}