diff options
Diffstat (limited to 'internal/file/regular_file.go')
| -rw-r--r-- | internal/file/regular_file.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/internal/file/regular_file.go b/internal/file/regular_file.go new file mode 100644 index 0000000..7747b9e --- /dev/null +++ b/internal/file/regular_file.go @@ -0,0 +1,26 @@ +package file + +import ( + "crypto/sha256" + "log" +) + +// haveRegularFile writes content to f.path idempotently (via a checksum-guarded +// temp file) and enforces mode/ownership. +func (f *File) haveRegularFile(content []byte) error { + log.Printf("processing file: %s", f.path) + existingChecksum := getChecksum(f.path) + newChecksum := sha256.Sum256(content) + log.Printf("computed checksum for new content: %x", newChecksum) + + tmpPath := f.path + ".tmp" + if err := writeTmpFile(tmpPath, content, f.mode); err != nil { + return err + } + + if err := updateFromTmp(tmpPath, f.path, existingChecksum != newChecksum); err != nil { + return err + } + + return f.applyAttributes() +} |
