| 1 | package fileutil |
| 2 | |
| 3 | import "os" |
| 4 | |
| 5 | // StageAtomicWrite writes data to a temp file beside path, fsynced and with |
| 6 | // perm applied, and returns its name for PublishStagedWrite. A caller that |
| 7 | // abandons the staged file removes it. |
| 8 | func StageAtomicWrite(path string, data []byte, perm os.FileMode) (string, error) { |
| 9 | return writeAtomicTemp(path, data, perm) |
| 10 | } |
| 11 | |
| 12 | // PublishStagedWrite renames a staged file onto path under the strict rules |
| 13 | // of AtomicWriteFileStrict: atomic rename only, retried through transient |
| 14 | // locks, then a best-effort parent-directory fsync. A failed publish removes |
| 15 | // the staged file. |
| 16 | func PublishStagedWrite(tmpPath, path string) error { |
| 17 | if err := replaceFile(tmpPath, path, false); err != nil { |
| 18 | _ = os.Remove(tmpPath) |
| 19 | return err |
| 20 | } |
| 21 | _ = syncParentDirFn(path) |
| 22 | return nil |
| 23 | } |
| 24 |