-
Notifications
You must be signed in to change notification settings - Fork 13
/
file.go
167 lines (142 loc) · 3.83 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
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
package ioz
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"github.com/google/renameio/v2/maybe"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/ioz/contextio"
)
// CopyFile copies the contents from src to dst atomically.
// If dst does not exist, CopyFile creates it with src's perms.
// If the copy fails, CopyFile aborts and dst is preserved.
// If mkdir is true, the directory for dst is created if it
// doesn't exist.
func CopyFile(dst, src string, mkdir bool) error {
if mkdir {
if err := RequireDir(filepath.Dir(dst)); err != nil {
return err
}
}
fi, err := os.Stat(src)
if err != nil {
return errz.Err(err)
}
in, err := os.Open(src)
if err != nil {
return errz.Err(err)
}
defer in.Close()
tmp, err := os.CreateTemp(filepath.Dir(dst), "*_"+filepath.Base(src))
if err != nil {
return errz.Err(err)
}
_, err = io.Copy(tmp, in)
if err != nil {
_ = tmp.Close()
_ = os.Remove(tmp.Name())
return errz.Err(err)
}
if err = tmp.Close(); err != nil {
_ = os.Remove(tmp.Name())
return errz.Err(err)
}
if err = os.Chmod(tmp.Name(), fi.Mode()); err != nil {
_ = os.Remove(tmp.Name())
return errz.Err(err)
}
return errz.Err(os.Rename(tmp.Name(), dst))
}
// PrintFile reads file from name and writes it to stdout.
func PrintFile(name string) error {
return FPrintFile(os.Stdout, name)
}
// FPrintFile reads file from name and writes it to w.
func FPrintFile(w io.Writer, name string) error {
b, err := os.ReadFile(name)
if err != nil {
return errz.Err(err)
}
_, err = io.Copy(w, bytes.NewReader(b))
return errz.Err(err)
}
// IsPathToRegularFile return true if path is a regular file or
// a symlink that resolves to a regular file. False is returned on
// any error.
func IsPathToRegularFile(path string) bool {
dest, err := filepath.EvalSymlinks(path)
if err != nil {
return false
}
fi, err := os.Stat(dest)
if err != nil {
return false
}
return fi.Mode().IsRegular()
}
// FileAccessible returns true if path is a file that can be read.
func FileAccessible(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// ReadFileToString reads the file at name and returns its contents
// as a string.
func ReadFileToString(name string) (string, error) {
b, err := os.ReadFile(name)
if err != nil {
return "", errz.Err(err)
}
return string(b), nil
}
// Filesize returns the size of the file at fp. An error is returned
// if fp doesn't exist or is a directory.
func Filesize(fp string) (int64, error) {
fi, err := os.Stat(fp)
if err != nil {
return 0, errz.Err(err)
}
if fi.IsDir() {
return 0, errz.Errorf("not a file: %s", fp)
}
return fi.Size(), nil
}
// FileInfoEqual returns true if a and b are equal.
// The FileInfo.Sys() field is ignored.
func FileInfoEqual(a, b os.FileInfo) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return a.Name() == b.Name() &&
a.Size() == b.Size() &&
a.ModTime().Equal(b.ModTime()) &&
a.Mode() == b.Mode() &&
a.IsDir() == b.IsDir()
}
// WriteToFile writes the contents of r to fp. If fp doesn't exist,
// the file is created (including any parent dirs). If fp exists, it is
// truncated. The write operation is context-aware.
func WriteToFile(ctx context.Context, fp string, r io.Reader) (written int64, err error) {
if err = RequireDir(filepath.Dir(fp)); err != nil {
return 0, err
}
f, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, RWPerms)
if err != nil {
return 0, errz.Err(err)
}
cr := contextio.NewReader(ctx, r)
written, err = io.Copy(f, cr)
closeErr := f.Close()
if err == nil {
return written, errz.Err(closeErr)
}
return written, errz.Err(err)
}
// WriteFileAtomic writes data to fp atomically, but not on Windows.
func WriteFileAtomic(fp string, data []byte, mode os.FileMode) error {
return errz.Err(maybe.WriteFile(fp, data, mode))
}