-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
81 lines (71 loc) · 1.51 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
79
80
81
package service
import (
"bufio"
"errors"
"fmt"
"io"
"os"
)
type unit int64
const (
GiB unit = 1024
GB unit = 1000
)
// TODO
// I would like to have better handle of data instead of it being in here
func CreateEmptyFile(path string, size int64, u unit) (err error) {
file, err := os.Create(path)
if err != nil {
return err
}
defer func() {
file.Close()
if err != nil {
os.Remove(path)
}
}()
switch u {
case GiB:
size = size * 1024 * 1024 * 1024
case GB:
// The image size will be reduced to fit commercial drives that are
// smaller than what they claim, 975 comes from 97.5% of the total size
// but we want to be a multiple of 512 (and size is an int) we divide by
// 512 and multiply it again
size = size * 1000 * 1000 * 975 / 512 * 512
default:
panic("improper sizing unit used")
}
if err := file.Truncate(size); err != nil {
return errors.New(fmt.Sprintf("Error creating %s of size %d to stage image onto", path, size))
}
return nil
}
func CopyFile(src, dst string) error {
srcStat, err := os.Stat(src)
if err != nil {
return err
}
dstFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, srcStat.Mode())
if err != nil {
return err
}
defer dstFile.Close()
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
reader := bufio.NewReader(srcFile)
writer := bufio.NewWriter(dstFile)
defer func() {
if err != nil {
writer.Flush()
}
}()
if _, err = io.Copy(writer, reader); err != nil {
return err
}
writer.Flush()
return nil
}