-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
62 lines (54 loc) · 1.33 KB
/
write.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
//spellchecker:words umaskfree
package umaskfree
//spellchecker:words errors time
import (
"errors"
"io/fs"
"os"
"time"
)
// Create is like [os.Create] with an additional mode argument.
func Create(path string, mode fs.FileMode) (*os.File, error) {
m.Lock()
defer m.Unlock()
return os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
}
// WriteFile is like [os.WriteFile].
func WriteFile(path string, data []byte, mode fs.FileMode) error {
handle, err := Create(path, mode)
if err != nil {
return err
}
defer handle.Close()
if _, err := handle.Write(data); err != nil {
return err
}
return nil
}
// DefaultFilePerm should be used by callers to use a consistent file mode for new files.
const DefaultFilePerm fs.FileMode = 0666
// Touch touches a file.
// It is similar to the unix 'touch' command.
//
// If the file does not exist, it is created using [Create].
// If the file does exist, its' access and modification times are updated to the current time.
func Touch(path string, perm fs.FileMode) error {
if perm == 0 {
perm = DefaultFilePerm
}
_, err := os.Stat(path)
switch {
case errors.Is(err, fs.ErrNotExist):
f, err := Create(path, perm)
if err != nil {
return err
}
defer f.Close()
return nil
case err != nil:
return err
default:
now := time.Now().Local()
return os.Chtimes(path, now, now)
}
}