diff --git a/internal/app/archied/setup.go b/internal/app/archied/setup.go index 8cf9e30d1..4a3825d39 100644 --- a/internal/app/archied/setup.go +++ b/internal/app/archied/setup.go @@ -13,6 +13,7 @@ import ( configtemplate "github.com/samcharles93/archie-core" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/fsutil" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/setup" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite" @@ -259,7 +260,7 @@ func installValidatedConfig(loader *configuration.Loader, path string, sink setu if err := proveLoadable(loader, rendered); err != nil { return err } - if err := writeFileAtomicMode(path, rendered, 0o600); err != nil { + if err := fsutil.WriteFileAtomic(path, rendered, 0o600); err != nil { return fmt.Errorf("write config: %w", err) } if err := sink.Commit(); err != nil { @@ -291,30 +292,3 @@ func proveLoadable(loader *configuration.Loader, rendered []byte) error { } return nil } - -// writeFileAtomicMode writes data to path via a same-directory temp file and -// rename, so a concurrent reader never observes a truncated config. -func writeFileAtomicMode(path string, data []byte, mode os.FileMode) error { - tmp, err := os.CreateTemp(filepath.Dir(path), ".config-*.toml") - if err != nil { - return err - } - name := tmp.Name() - defer os.Remove(name) // no-op after a successful rename - if err := tmp.Chmod(mode); err != nil { - _ = tmp.Close() - return err - } - if _, err := tmp.Write(data); err != nil { - _ = tmp.Close() - return err - } - if err := tmp.Sync(); err != nil { - _ = tmp.Close() - return err - } - if err := tmp.Close(); err != nil { - return err - } - return os.Rename(name, path) -} diff --git a/internal/app/servicekit/nats.go b/internal/app/servicekit/nats.go index 642a81a44..e02322bb0 100644 --- a/internal/app/servicekit/nats.go +++ b/internal/app/servicekit/nats.go @@ -7,6 +7,7 @@ import ( "path/filepath" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/fsutil" ) type NATSEndpoint struct { @@ -24,29 +25,9 @@ func WriteNATSEndpoint(stateDir, url, token string) error { if err != nil { return fmt.Errorf("marshal embedded NATS endpoint: %w", err) } - if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { - return fmt.Errorf("create embedded NATS directory: %w", err) - } - tmp, err := os.CreateTemp(filepath.Dir(path), ".endpoint-*") - if err != nil { - return fmt.Errorf("create embedded NATS endpoint: %w", err) - } - tmpName := tmp.Name() - defer os.Remove(tmpName) - if err := tmp.Chmod(0o600); err != nil { - _ = tmp.Close() - return fmt.Errorf("protect embedded NATS endpoint: %w", err) - } - if _, err := tmp.Write(data); err != nil { - _ = tmp.Close() + if err := fsutil.WriteFileAtomic(path, data, 0o600); err != nil { return fmt.Errorf("write embedded NATS endpoint: %w", err) } - if err := tmp.Close(); err != nil { - return fmt.Errorf("close embedded NATS endpoint: %w", err) - } - if err := os.Rename(tmpName, path); err != nil { - return fmt.Errorf("publish embedded NATS endpoint: %w", err) - } return nil } diff --git a/internal/fsutil/fsutil.go b/internal/fsutil/fsutil.go new file mode 100644 index 000000000..b00ae4e14 --- /dev/null +++ b/internal/fsutil/fsutil.go @@ -0,0 +1,39 @@ +// Package fsutil provides filesystem helpers shared across services. +package fsutil + +import ( + "os" + "path/filepath" +) + +// WriteFileAtomic writes data to path atomically: a temp file in the same +// directory is written, synced and renamed into place, so a crash mid-write +// never leaves a truncated file. The parent directory is created if needed. +func WriteFileAtomic(path string, data []byte, mode os.FileMode) error { + dir := filepath.Dir(path) + if err := os.MkdirAll(dir, 0o755); err != nil { + return err + } + tmp, err := os.CreateTemp(dir, "."+filepath.Base(path)+".*.tmp") + if err != nil { + return err + } + tmpPath := tmp.Name() + defer os.Remove(tmpPath) // no-op after a successful rename + if err := tmp.Chmod(mode); err != nil { + _ = tmp.Close() + return err + } + if _, err := tmp.Write(data); err != nil { + _ = tmp.Close() + return err + } + if err := tmp.Sync(); err != nil { + _ = tmp.Close() + return err + } + if err := tmp.Close(); err != nil { + return err + } + return os.Rename(tmpPath, path) +} diff --git a/internal/infrastructure/configuration/setup/envsink.go b/internal/infrastructure/configuration/setup/envsink.go index 183ce28c5..3889ce737 100644 --- a/internal/infrastructure/configuration/setup/envsink.go +++ b/internal/infrastructure/configuration/setup/envsink.go @@ -5,9 +5,10 @@ import ( "fmt" "maps" "os" - "path/filepath" "slices" "strings" + + "github.com/samcharles93/archie-core/internal/fsutil" ) // EnvFileSink stores secrets as KEY='value' lines in the env file beside @@ -66,10 +67,7 @@ func (s *EnvFileSink) Commit() error { out = append(out, key+"='"+escapeEnvValue(s.entries[key])+"'") } - if err := os.MkdirAll(filepath.Dir(s.path), 0o700); err != nil { - return fmt.Errorf("setup: create env directory: %w", err) - } - if err := writeEnvFileAtomic(s.path, []byte(strings.Join(out, "\n")+"\n")); err != nil { + if err := fsutil.WriteFileAtomic(s.path, []byte(strings.Join(out, "\n")+"\n"), 0o600); err != nil { return fmt.Errorf("setup: write env file: %w", err) } return nil @@ -97,31 +95,3 @@ func readEnvLines(path string) ([]string, error) { func escapeEnvValue(v string) string { return strings.ReplaceAll(v, "'", `'\''`) } - -// writeEnvFileAtomic writes data to path via a same-directory temp file and -// rename, mirroring set_env_key's mktemp-then-mv so a reader never observes -// a half-written env file. -func writeEnvFileAtomic(path string, data []byte) error { - tmp, err := os.CreateTemp(filepath.Dir(path), ".env.XXXXXX") - if err != nil { - return err - } - name := tmp.Name() - defer os.Remove(name) // no-op after a successful rename - if err := tmp.Chmod(0o600); err != nil { - _ = tmp.Close() - return err - } - if _, err := tmp.Write(data); err != nil { - _ = tmp.Close() - return err - } - if err := tmp.Sync(); err != nil { - _ = tmp.Close() - return err - } - if err := tmp.Close(); err != nil { - return err - } - return os.Rename(name, path) -} diff --git a/internal/infrastructure/extension/source.go b/internal/infrastructure/extension/source.go index d9f0ea887..c0b8cf607 100644 --- a/internal/infrastructure/extension/source.go +++ b/internal/infrastructure/extension/source.go @@ -11,6 +11,7 @@ import ( "path/filepath" "github.com/samcharles93/archie-core/internal/domain/storepkg" + "github.com/samcharles93/archie-core/internal/fsutil" "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" ) @@ -70,26 +71,7 @@ func Materialize(ctx context.Context, packages Packages, cacheDir string, pkg st if existing, err := os.ReadFile(target); err == nil && sha256.Sum256(existing) == sum { return target, hex.EncodeToString(sum[:]), nil } - if err := os.MkdirAll(dir, 0o700); err != nil { - return "", "", err - } - tmp, err := os.CreateTemp(dir, ".extract-*") - if err != nil { - return "", "", err - } - defer func() { _ = os.Remove(tmp.Name()) }() - if _, err := tmp.Write(content); err != nil { - _ = tmp.Close() - return "", "", err - } - if err := tmp.Chmod(0o700); err != nil { - _ = tmp.Close() - return "", "", err - } - if err := tmp.Close(); err != nil { - return "", "", err - } - if err := os.Rename(tmp.Name(), target); err != nil { + if err := fsutil.WriteFileAtomic(target, content, 0o700); err != nil { return "", "", err } return target, hex.EncodeToString(sum[:]), nil diff --git a/internal/infrastructure/memory/builtin/store.go b/internal/infrastructure/memory/builtin/store.go index 1e303e47c..21f02b010 100644 --- a/internal/infrastructure/memory/builtin/store.go +++ b/internal/infrastructure/memory/builtin/store.go @@ -6,10 +6,11 @@ import ( "errors" "fmt" "os" - "path/filepath" "regexp" "strings" "sync" + + "github.com/samcharles93/archie-core/internal/fsutil" ) // defaultMaxFileBytes is the default per-file size bound (100KB). @@ -157,7 +158,7 @@ func (s *Store) Add(sectionName, content string) (AddResult, error) { if len(rendered) > s.maxBytes { return AddResult{}, fmt.Errorf("builtin: add to section %q: %w (%d > %d bytes)", sectionName, ErrTooLarge, len(rendered), s.maxBytes) } - if err := writeFileAtomic(s.path, rendered); err != nil { + if err := fsutil.WriteFileAtomic(s.path, []byte(rendered), 0o644); err != nil { return AddResult{}, err } @@ -213,7 +214,7 @@ func (s *Store) Replace(sectionName, substring, replacement string, caseSensitiv if len(rendered) > s.maxBytes { return ReplaceResult{}, fmt.Errorf("builtin: replace in section %q would exceed max file size (%d > %d bytes)", sectionName, len(rendered), s.maxBytes) } - if err := writeFileAtomic(s.path, rendered); err != nil { + if err := fsutil.WriteFileAtomic(s.path, []byte(rendered), 0o644); err != nil { return ReplaceResult{}, err } @@ -270,7 +271,7 @@ func (s *Store) Remove(sectionName, substring string, caseSensitive bool) (Remov if len(rendered) > s.maxBytes { return RemoveResult{}, fmt.Errorf("builtin: remove from section %q would exceed max file size (%d > %d bytes)", sectionName, len(rendered), s.maxBytes) } - if err := writeFileAtomic(s.path, rendered); err != nil { + if err := fsutil.WriteFileAtomic(s.path, []byte(rendered), 0o644); err != nil { return RemoveResult{}, err } @@ -455,38 +456,3 @@ func indexFold(haystack, needle string) (int, int) { } return -1, 0 } - -// writeFileAtomic writes content to path by writing to a temp file in the -// same directory and renaming it into place, so a crash mid-write cannot -// leave a truncated file behind. -func writeFileAtomic(path, content string) error { - dir := filepath.Dir(path) - if err := os.MkdirAll(dir, 0o755); err != nil { - return fmt.Errorf("builtin: creating directory %s: %w", dir, err) - } - - tmp, err := os.CreateTemp(dir, ".tmp-*") - if err != nil { - return fmt.Errorf("builtin: creating temp file in %s: %w", dir, err) - } - tmpPath := tmp.Name() - - if _, err := tmp.WriteString(content); err != nil { - _ = tmp.Close() - _ = os.Remove(tmpPath) - return fmt.Errorf("builtin: writing %s: %w", path, err) - } - if err := tmp.Close(); err != nil { - _ = os.Remove(tmpPath) - return fmt.Errorf("builtin: closing %s: %w", path, err) - } - if err := os.Chmod(tmpPath, 0o644); err != nil { - _ = os.Remove(tmpPath) - return fmt.Errorf("builtin: setting permissions on %s: %w", path, err) - } - if err := os.Rename(tmpPath, path); err != nil { - _ = os.Remove(tmpPath) - return fmt.Errorf("builtin: renaming into %s: %w", path, err) - } - return nil -} diff --git a/internal/infrastructure/modelcatalog/catalog.go b/internal/infrastructure/modelcatalog/catalog.go index c42ad9c56..6e39b2594 100644 --- a/internal/infrastructure/modelcatalog/catalog.go +++ b/internal/infrastructure/modelcatalog/catalog.go @@ -8,7 +8,6 @@ import ( "io" "net/http" "os" - "path/filepath" "slices" "strings" "time" @@ -16,6 +15,7 @@ import ( "github.com/samcharles93/ai-sdk/catalog" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/fsutil" ) type Options struct { @@ -116,27 +116,7 @@ func fetch(ctx context.Context, opts Options) ([]byte, error) { } func writeCache(path string, raw []byte) error { - if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { - return err - } - tmp, err := os.CreateTemp(filepath.Dir(path), ".models-*.json") - if err != nil { - return err - } - tmpName := tmp.Name() - defer func() { _ = os.Remove(tmpName) }() - if err := tmp.Chmod(0o600); err != nil { - _ = tmp.Close() - return err - } - if _, err := tmp.Write(raw); err != nil { - _ = tmp.Close() - return err - } - if err := tmp.Close(); err != nil { - return err - } - return os.Rename(tmpName, path) + return fsutil.WriteFileAtomic(path, raw, 0o600) } func parse(raw []byte, opts Options) (Snapshot, error) { diff --git a/internal/releaseannounce/announcer.go b/internal/releaseannounce/announcer.go index e0120863e..42246712d 100644 --- a/internal/releaseannounce/announcer.go +++ b/internal/releaseannounce/announcer.go @@ -9,9 +9,10 @@ import ( "errors" "fmt" "os" - "path/filepath" "strconv" "strings" + + "github.com/samcharles93/archie-core/internal/fsutil" ) type Sender = func(ctx context.Context, recipient int64, message string) error @@ -244,34 +245,9 @@ func loadState(path string) (map[string]string, error) { } func saveState(path string, state map[string]string) error { - dir := filepath.Dir(path) - if err := os.MkdirAll(dir, 0o700); err != nil { - return fmt.Errorf("create state directory: %w", err) - } - file, err := os.CreateTemp(dir, ".release-announcements-*") - if err != nil { - return fmt.Errorf("create temporary state: %w", err) - } - tempPath := file.Name() - defer func() { _ = os.Remove(tempPath) }() - - if err := file.Chmod(0o600); err != nil { - _ = file.Close() - return fmt.Errorf("protect temporary state: %w", err) - } - if err := json.NewEncoder(file).Encode(state); err != nil { - _ = file.Close() + var buf strings.Builder + if err := json.NewEncoder(&buf).Encode(state); err != nil { return fmt.Errorf("encode release announcement state: %w", err) } - if err := file.Sync(); err != nil { - _ = file.Close() - return fmt.Errorf("sync release announcement state: %w", err) - } - if err := file.Close(); err != nil { - return fmt.Errorf("close release announcement state: %w", err) - } - if err := os.Rename(tempPath, path); err != nil { - return fmt.Errorf("replace release announcement state: %w", err) - } - return nil + return fsutil.WriteFileAtomic(path, []byte(buf.String()), 0o600) } diff --git a/internal/releaseupdate/pending_report.go b/internal/releaseupdate/pending_report.go index 898e35661..cb4c34833 100644 --- a/internal/releaseupdate/pending_report.go +++ b/internal/releaseupdate/pending_report.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "os" + + "github.com/samcharles93/archie-core/internal/fsutil" ) // Report is an update's post-restart outcome, written by the watchdog and @@ -39,7 +41,7 @@ func WritePendingReport(path string, r Report) error { if err != nil { return fmt.Errorf("encode update report: %w", err) } - if err := writeFileAtomic(path, data); err != nil { + if err := fsutil.WriteFileAtomic(path, data, 0o600); err != nil { return fmt.Errorf("write update report: %w", err) } return nil diff --git a/internal/releaseupdate/service.go b/internal/releaseupdate/service.go index 0b361dba9..23b1b0b97 100644 --- a/internal/releaseupdate/service.go +++ b/internal/releaseupdate/service.go @@ -8,11 +8,11 @@ import ( "errors" "fmt" "os" - "path/filepath" "strconv" "sync" "time" + "github.com/samcharles93/archie-core/internal/fsutil" "github.com/samcharles93/archie-core/internal/installtype" ) @@ -238,38 +238,5 @@ func saveDeferrals(path string, state deferrals) error { if err != nil { return fmt.Errorf("encode update deferrals: %w", err) } - return writeFileAtomic(path, data) -} - -// writeFileAtomic writes data to path through a synced temp file and rename. -func writeFileAtomic(path string, data []byte) error { - dir := filepath.Dir(path) - if err := os.MkdirAll(dir, 0o700); err != nil { - return fmt.Errorf("create state directory: %w", err) - } - file, err := os.CreateTemp(dir, ".update-state-*") - if err != nil { - return fmt.Errorf("create temp state file: %w", err) - } - tempPath := file.Name() - defer func() { _ = os.Remove(tempPath) }() - if err := file.Chmod(0o600); err != nil { - _ = file.Close() - return fmt.Errorf("protect state file: %w", err) - } - if _, err := file.Write(data); err != nil { - _ = file.Close() - return fmt.Errorf("write state file: %w", err) - } - if err := file.Sync(); err != nil { - _ = file.Close() - return fmt.Errorf("sync state file: %w", err) - } - if err := file.Close(); err != nil { - return fmt.Errorf("close state file: %w", err) - } - if err := os.Rename(tempPath, path); err != nil { - return fmt.Errorf("replace state file: %w", err) - } - return nil + return fsutil.WriteFileAtomic(path, data, 0o600) } diff --git a/internal/releaseupdate/transaction.go b/internal/releaseupdate/transaction.go index 1c6ed61f4..279a045c8 100644 --- a/internal/releaseupdate/transaction.go +++ b/internal/releaseupdate/transaction.go @@ -11,6 +11,8 @@ import ( "time" "golang.org/x/sys/unix" + + "github.com/samcharles93/archie-core/internal/fsutil" ) // A transaction's journal moves through these phases. prepared means nothing @@ -186,7 +188,7 @@ func (t *Transaction) switchOver(ctx context.Context, j *journal) error { return fmt.Errorf("switch %s: %w", unit, err) } } - if err := writeFileAtomic(filepath.Join(t.BinDir, agentVersionFile), []byte(j.Version+"\n")); err != nil { + if err := fsutil.WriteFileAtomic(filepath.Join(t.BinDir, agentVersionFile), []byte(j.Version+"\n"), 0o600); err != nil { return err } if err := syncDir(t.BinDir); err != nil { @@ -468,7 +470,7 @@ func writeJournal(dir string, j journal) error { if err != nil { return err } - if err := writeFileAtomic(filepath.Join(dir, journalFile), data); err != nil { + if err := fsutil.WriteFileAtomic(filepath.Join(dir, journalFile), data, 0o600); err != nil { return err } return syncDir(dir) diff --git a/internal/tools/builtin/edit.go b/internal/tools/builtin/edit.go index 9a5f00e99..daeead780 100644 --- a/internal/tools/builtin/edit.go +++ b/internal/tools/builtin/edit.go @@ -8,6 +8,8 @@ import ( "os" "sort" "strings" + + "github.com/samcharles93/archie-core/internal/fsutil" ) // EditParams are the parameters for the edit tool. @@ -208,7 +210,7 @@ func performEdit(path string, p EditParams, mq *MutationQueue) Result { newContent = strings.ReplaceAll(newContent, "\n", "\r\n") } - if err := writeFileAtomic(path, []byte(bom+newContent), 0o644); err != nil { + if err := fsutil.WriteFileAtomic(path, []byte(bom+newContent), 0o644); err != nil { return Result{Content: fmt.Sprintf("error writing file: %v", err), IsError: true} } diff --git a/internal/tools/builtin/fsutil.go b/internal/tools/builtin/fsutil.go index 86d1a7eee..078486cdb 100644 --- a/internal/tools/builtin/fsutil.go +++ b/internal/tools/builtin/fsutil.go @@ -2,8 +2,6 @@ package builtin import ( "context" - "os" - "path/filepath" ) // runWithContext runs fn and returns its result, or ctx.Err() if ctx ends @@ -23,34 +21,3 @@ func runWithContext(ctx context.Context, fn func() error) error { return ctx.Err() } } - -// writeFileAtomic replaces path atomically using a temp file in the same directory. -func writeFileAtomic(path string, data []byte, mode os.FileMode) error { - dir := filepath.Dir(path) - if err := os.MkdirAll(dir, 0o755); err != nil { - return err - } - - tmp, err := os.CreateTemp(dir, filepath.Base(path)+".*.tmp") - if err != nil { - return err - } - tmpPath := tmp.Name() - cleanup := func() { - _ = os.Remove(tmpPath) - } - defer cleanup() - - if err := tmp.Chmod(mode); err != nil { - _ = tmp.Close() - return err - } - if _, err := tmp.Write(data); err != nil { - _ = tmp.Close() - return err - } - if err := tmp.Close(); err != nil { - return err - } - return os.Rename(tmpPath, path) -} diff --git a/internal/tools/builtin/write.go b/internal/tools/builtin/write.go index 65c4f4b5d..886e15d66 100644 --- a/internal/tools/builtin/write.go +++ b/internal/tools/builtin/write.go @@ -7,6 +7,8 @@ import ( "fmt" "os" "path/filepath" + + "github.com/samcharles93/archie-core/internal/fsutil" ) // WriteParams are the parameters for the write tool. @@ -89,7 +91,7 @@ func makeWriteExecutor(cwd string, mq *MutationQueue, rt *ReadTracker) Executor return nil } - if err := writeFileAtomic(path, []byte(p.Content), 0o644); err != nil { + if err := fsutil.WriteFileAtomic(path, []byte(p.Content), 0o644); err != nil { result = Result{Content: fmt.Sprintf("error writing file: %v", err), IsError: true} return nil }