summaryrefslogtreecommitdiff
path: root/internal/file/regular_file.go
blob: 7747b9e8f41e7a1f036fcead04dad18c3ac98ea3 (plain)
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
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()
}