Share sibling temporary file creation for downloads
This commit is contained in:
@@ -112,7 +112,7 @@ func executeRestoreDownloadAction(
|
||||
return executeRestoreAudioAction(ctx, cfg, safeLocalPath, parent, leaf, action, store)
|
||||
}
|
||||
|
||||
temporaryPath, err := downloadRestoreTemp(safeLocalPath, func(destination io.Writer) error {
|
||||
temporaryPath, err := fileops.WriteSiblingTemp(safeLocalPath, func(destination io.Writer) error {
|
||||
_, err := storage.StreamObject(ctx, store, action.RemoteKey, destination, restoreExpectedSize(action))
|
||||
return err
|
||||
})
|
||||
@@ -202,34 +202,6 @@ func validateRestoreDestinationLeaf(parent *os.Root, leaf string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func downloadRestoreTemp(destination string, download func(io.Writer) error) (string, error) {
|
||||
if strings.TrimSpace(destination) == "" {
|
||||
return "", fmt.Errorf("restore destination path is required")
|
||||
}
|
||||
if download == nil {
|
||||
return "", fmt.Errorf("restore temporary writer is required")
|
||||
}
|
||||
directory := filepath.Dir(destination)
|
||||
if err := fileops.EnsureWorkspaceDirectory(directory); err != nil {
|
||||
return "", fmt.Errorf("create destination directory: %w", err)
|
||||
}
|
||||
temporary, err := os.CreateTemp(directory, "."+filepath.Base(destination)+".tmp-*")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create sibling temporary file: %w", err)
|
||||
}
|
||||
temporaryPath := temporary.Name()
|
||||
if err := download(temporary); err != nil {
|
||||
_ = temporary.Close()
|
||||
_ = os.Remove(temporaryPath)
|
||||
return "", err
|
||||
}
|
||||
if err := temporary.Close(); err != nil {
|
||||
_ = os.Remove(temporaryPath)
|
||||
return "", fmt.Errorf("close sibling temporary file: %w", err)
|
||||
}
|
||||
return temporaryPath, nil
|
||||
}
|
||||
|
||||
func verifyRestoredObject(ctx context.Context, store storage.ObjectStore, action RestoreAction, temporaryPath string) error {
|
||||
if strings.TrimSpace(action.SHA256) == "" && strings.TrimSpace(action.Generation) == "" {
|
||||
return nil
|
||||
@@ -292,15 +264,11 @@ func executeRestoreAudioAction(
|
||||
if cfg == nil || cfg.Pipeline == nil || cfg.Pipeline.Storage.S3 == nil || cfg.Session == nil {
|
||||
return fmt.Errorf("resolved s3 config and session are required")
|
||||
}
|
||||
temporary, err := os.CreateTemp(filepath.Dir(safeLocalPath), "."+filepath.Base(safeLocalPath)+".restore-*")
|
||||
temporaryPath, err := fileops.WriteSiblingTemp(safeLocalPath, func(io.Writer) error { return nil })
|
||||
if err != nil {
|
||||
return fmt.Errorf("create sibling audio staging file: %w", err)
|
||||
}
|
||||
temporaryPath := temporary.Name()
|
||||
if err := temporary.Close(); err != nil {
|
||||
_ = os.Remove(temporaryPath)
|
||||
return fmt.Errorf("close sibling audio staging file: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = os.Remove(temporaryPath) }()
|
||||
|
||||
_, err = audio.MaterializeS3Audio(ctx, audio.S3MaterializeRequest{
|
||||
|
||||
@@ -97,7 +97,7 @@ func validateLocalAudio(path string, expectedSize int64) error {
|
||||
|
||||
func downloadObjectAtomic(ctx context.Context, store storage.ObjectStore, key, destPath string, expected *int64, reporter logging.Reporter) (storage.ObjectInfo, error) {
|
||||
var observed storage.ObjectInfo
|
||||
temporaryPath, err := writeAudioTemp(destPath, func(destination io.Writer) error {
|
||||
temporaryPath, err := fileops.WriteSiblingTemp(destPath, func(destination io.Writer) error {
|
||||
if reporter != nil {
|
||||
reporter.Progress("downloading audio", "audio", filepath.Base(destPath))
|
||||
}
|
||||
@@ -123,31 +123,3 @@ func downloadObjectAtomic(ctx context.Context, store storage.ObjectStore, key, d
|
||||
}
|
||||
return observed, nil
|
||||
}
|
||||
|
||||
func writeAudioTemp(destination string, write func(io.Writer) error) (string, error) {
|
||||
if strings.TrimSpace(destination) == "" {
|
||||
return "", fmt.Errorf("destination path is required")
|
||||
}
|
||||
if write == nil {
|
||||
return "", fmt.Errorf("temporary audio writer is required")
|
||||
}
|
||||
dir := filepath.Dir(destination)
|
||||
if err := fileops.EnsureWorkspaceDirectory(dir); err != nil {
|
||||
return "", fmt.Errorf("create destination directory: %w", err)
|
||||
}
|
||||
temporary, err := os.CreateTemp(dir, "."+filepath.Base(destination)+".tmp-*")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create sibling temporary file: %w", err)
|
||||
}
|
||||
temporaryPath := temporary.Name()
|
||||
if err := write(temporary); err != nil {
|
||||
_ = temporary.Close()
|
||||
_ = os.Remove(temporaryPath)
|
||||
return "", fmt.Errorf("write sibling temporary file: %w", err)
|
||||
}
|
||||
if err := temporary.Close(); err != nil {
|
||||
_ = os.Remove(temporaryPath)
|
||||
return "", fmt.Errorf("close sibling temporary file: %w", err)
|
||||
}
|
||||
return temporaryPath, nil
|
||||
}
|
||||
|
||||
37
internal/fileops/sibling_temp.go
Normal file
37
internal/fileops/sibling_temp.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package fileops
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// WriteSiblingTemp writes and closes a temporary sibling without installing it.
|
||||
// The caller owns the returned path and must remove or install it.
|
||||
func WriteSiblingTemp(destination string, write func(io.Writer) error) (string, error) {
|
||||
if strings.TrimSpace(destination) == "" {
|
||||
return "", fmt.Errorf("destination path is required")
|
||||
}
|
||||
if write == nil {
|
||||
return "", fmt.Errorf("temporary writer is required")
|
||||
}
|
||||
directory := filepath.Dir(destination)
|
||||
if err := EnsureWorkspaceDirectory(directory); err != nil {
|
||||
return "", fmt.Errorf("create destination directory: %w", err)
|
||||
}
|
||||
file, err := os.CreateTemp(directory, "."+filepath.Base(destination)+".tmp-*")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create sibling temporary file: %w", err)
|
||||
}
|
||||
path := file.Name()
|
||||
writeErr := write(file)
|
||||
closeErr := file.Close()
|
||||
if err := errors.Join(writeErr, closeErr); err != nil {
|
||||
removeErr := os.Remove(path)
|
||||
return "", errors.Join(err, removeErr)
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
64
internal/fileops/sibling_temp_test.go
Normal file
64
internal/fileops/sibling_temp_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package fileops
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWriteSiblingTempTransfersClosedFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
target := filepath.Join(dir, "output")
|
||||
var writer io.Writer
|
||||
path, err := WriteSiblingTemp(target, func(w io.Writer) error { writer = w; _, err := io.WriteString(w, "new data"); return err })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(path)
|
||||
if filepath.Dir(path) != dir {
|
||||
t.Fatalf("temporary path = %q", path)
|
||||
}
|
||||
if _, err := writer.Write([]byte("later")); !errors.Is(err, os.ErrClosed) {
|
||||
t.Fatalf("write to closed temporary = %v", err)
|
||||
}
|
||||
body, err := os.ReadFile(path)
|
||||
if err != nil || string(body) != "new data" {
|
||||
t.Fatalf("data = %q, error = %v", body, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteSiblingTempFailurePreservesDestination(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
target := filepath.Join(dir, "output")
|
||||
if err := os.WriteFile(target, []byte("old"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cause := errors.New("download failed")
|
||||
path, err := WriteSiblingTemp(target, func(w io.Writer) error { _, _ = io.WriteString(w, "partial"); return cause })
|
||||
if path != "" || !errors.Is(err, cause) {
|
||||
t.Fatalf("path = %q, error = %v", path, err)
|
||||
}
|
||||
body, err := os.ReadFile(target)
|
||||
if err != nil || string(body) != "old" {
|
||||
t.Fatalf("destination = %q, error = %v", body, err)
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil || len(entries) != 1 || entries[0].Name() != "output" {
|
||||
t.Fatalf("directory = %v, error = %v", entries, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteSiblingTempRetainsWriteAndCloseCauses(t *testing.T) {
|
||||
cause := errors.New("write failed")
|
||||
path, err := WriteSiblingTemp(filepath.Join(t.TempDir(), "output"), func(w io.Writer) error {
|
||||
if err := w.(*os.File).Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return cause
|
||||
})
|
||||
if path != "" || !errors.Is(err, cause) || !errors.Is(err, os.ErrClosed) {
|
||||
t.Fatalf("path = %q, error = %v", path, err)
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func hydratePreviousSessionArtifacts(
|
||||
return nil, fmt.Errorf("create previous-session path directory for %q: %w", record.LocalPath, err)
|
||||
}
|
||||
|
||||
temporaryPath, err := writePreviousTemp(record.LocalPath, func(destination io.Writer) error {
|
||||
temporaryPath, err := fileops.WriteSiblingTemp(record.LocalPath, func(destination io.Writer) error {
|
||||
_, err := storage.StreamObject(ctx, env.ObjectStore, record.RemoteKey, destination, &record.Size)
|
||||
return err
|
||||
})
|
||||
@@ -99,34 +99,6 @@ func hydratePreviousSessionArtifacts(
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func writePreviousTemp(destination string, write func(io.Writer) error) (string, error) {
|
||||
if strings.TrimSpace(destination) == "" {
|
||||
return "", fmt.Errorf("previous-session destination path is required")
|
||||
}
|
||||
if write == nil {
|
||||
return "", fmt.Errorf("previous-session temporary writer is required")
|
||||
}
|
||||
directory := filepath.Dir(destination)
|
||||
if err := fileops.EnsureWorkspaceDirectory(directory); err != nil {
|
||||
return "", fmt.Errorf("create previous-session path directory: %w", err)
|
||||
}
|
||||
temporary, err := os.CreateTemp(directory, "."+filepath.Base(destination)+".tmp-*")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create sibling temporary file: %w", err)
|
||||
}
|
||||
temporaryPath := temporary.Name()
|
||||
if err := write(temporary); err != nil {
|
||||
_ = temporary.Close()
|
||||
_ = os.Remove(temporaryPath)
|
||||
return "", err
|
||||
}
|
||||
if err := temporary.Close(); err != nil {
|
||||
_ = os.Remove(temporaryPath)
|
||||
return "", fmt.Errorf("close sibling temporary file: %w", err)
|
||||
}
|
||||
return temporaryPath, nil
|
||||
}
|
||||
|
||||
func validatePreviousTemp(record previouscache.Record, temporaryPath string) (string, error) {
|
||||
info, err := os.Stat(temporaryPath)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user