Share sibling temporary file creation for downloads
This commit is contained in:
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user