Files
archie/internal/tools/builtin/fsutil.go
T

57 lines
1.1 KiB
Go

package builtin
import (
"context"
"os"
"path/filepath"
)
// runWithContext runs fn and returns its result, or ctx.Err() if ctx ends
// first; fn keeps running in the background. fn is not started if ctx is
// already done.
func runWithContext(ctx context.Context, fn func() error) error {
if err := ctx.Err(); err != nil {
return err
}
done := make(chan error, 1)
go func() { done <- fn() }()
select {
case err := <-done:
return err
case <-ctx.Done():
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)
}