-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwrite.go
More file actions
119 lines (106 loc) · 3.31 KB
/
Copy pathwrite.go
File metadata and controls
119 lines (106 loc) · 3.31 KB
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
// Package safeio provides race-resistant, no-follow file writes for agent-notify.
//
// The patterns mirror Brigade's Python helpers in localio.write_text_atomic /
// write_text_exclusive and run_journal._open_nofollow / _fsync_directory:
// same-directory temp, fsync the file, exclusive link or replace publish, then
// fsync the parent directory without following a symlinked final component.
package safeio
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
)
// ErrExists is returned when an exclusive (non-force) write finds the
// destination already occupied by a regular file, symlink, or other inode.
var ErrExists = errors.New("destination already exists")
var chmodTemp = func(tmp *os.File, mode os.FileMode) error {
return tmp.Chmod(mode)
}
// WriteFile publishes data at path with mode.
//
// Without force, publication is exclusive: a same-directory temp is fsynced
// and hard-linked into place so a raced-in symlink or file at path cannot
// redirect the write (link fails with ErrExists). With force, the temp is
// published via Rename, which replaces a symlink at the destination rather
// than following it. The parent directory is fsynced on POSIX after publish.
func WriteFile(path string, data []byte, mode os.FileMode, force bool) error {
if path == "" {
return errors.New("path is empty")
}
dir := filepath.Dir(path)
base := filepath.Base(path)
// Refuse a symlinked parent before creating or publishing a temp file.
// The post-publish fsync below still makes the name durable.
if err := fsyncParent(dir); err != nil {
return fmt.Errorf("refuse parent of %s: %w", base, err)
}
if !force {
if err := refuseExisting(path); err != nil {
return err
}
}
tmp, err := os.CreateTemp(dir, "."+base+".*.tmp")
if err != nil {
return fmt.Errorf("create temp for %s: %w", base, err)
}
tmpName := tmp.Name()
cleanup := true
defer func() {
if cleanup {
_ = os.Remove(tmpName)
}
}()
if _, err := tmp.Write(data); err != nil {
_ = tmp.Close()
return fmt.Errorf("write temp for %s: %w", base, err)
}
if err := chmodTemp(tmp, mode); err != nil {
_ = tmp.Close()
return fmt.Errorf("chmod temp for %s: %w", base, err)
}
if err := tmp.Sync(); err != nil {
_ = tmp.Close()
return fmt.Errorf("sync temp for %s: %w", base, err)
}
if err := tmp.Close(); err != nil {
return fmt.Errorf("close temp for %s: %w", base, err)
}
if force {
if err := os.Rename(tmpName, path); err != nil {
return fmt.Errorf("replace %s: %w", base, err)
}
cleanup = false
} else {
if err := os.Link(tmpName, path); err != nil {
if isExist(err) {
return fmt.Errorf("%w: %s", ErrExists, path)
}
return fmt.Errorf("publish %s: %w", base, err)
}
// Temp remains until defer removes it; the hard link is the durable name.
}
if err := fsyncParent(dir); err != nil {
return fmt.Errorf("fsync parent of %s: %w", base, err)
}
return nil
}
func refuseExisting(path string) error {
fi, err := os.Lstat(path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return fmt.Errorf("stat %s: %w", filepath.Base(path), err)
}
_ = fi
return fmt.Errorf("%w: %s", ErrExists, path)
}
func isExist(err error) bool {
return errors.Is(err, fs.ErrExist)
}
func supportsDirectoryFsync() bool {
return runtime.GOOS != "windows" && runtime.GOOS != "js" && runtime.GOOS != "plan9"
}