Complete safety and transfer remediation verification

This commit is contained in:
2026-12-01 20:47:59 +00:00
parent f59a53e31b
commit 75eddf8adc
27 changed files with 309 additions and 328 deletions

View File

@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"sync"
@@ -21,19 +20,17 @@ type FakeBackend struct {
Objects map[string]FakeObject
Uploads []FakeUploadCall
Downloads []FakeDownloadCall
Reads []FakeReadCall
Deletes []string
ExistsKeys []string
ListErr error
DownloadErr error
UploadErr error
DeleteErr error
ExistsErr error
UploadHook func(FakeUploadCall) error
DownloadHook func(FakeDownloadCall) error
ReadHook func(FakeReadCall) error
ListErr error
ReadErr error
UploadErr error
DeleteErr error
ExistsErr error
UploadHook func(FakeUploadCall) error
ReadHook func(FakeReadCall) error
}
// FakeUploadCall captures one upload invocation in call order.
@@ -43,13 +40,6 @@ type FakeUploadCall struct {
Options UploadOptions
}
// FakeDownloadCall captures one download invocation in call order.
type FakeDownloadCall struct {
Key string
LocalPath string
Bytes int64
}
// FakeReadCall captures one opened object in call order.
type FakeReadCall struct {
Key string
@@ -123,8 +113,8 @@ func (f *FakeBackend) Read(ctx context.Context, key string) (ObjectInfo, io.Read
if err := ctx.Err(); err != nil {
return ObjectInfo{}, nil, err
}
if f.DownloadErr != nil {
return ObjectInfo{}, nil, f.DownloadErr
if f.ReadErr != nil {
return ObjectInfo{}, nil, f.ReadErr
}
normalizedKey := normalizeObjectKey(key)
if f.ReadHook != nil {
@@ -148,54 +138,6 @@ func (f *FakeBackend) Read(ctx context.Context, key string) (ObjectInfo, io.Read
return ObjectInfo{Key: obj.Key, Size: int64(len(obj.Data)), SizeKnown: true, ETag: obj.ETag, LastModified: obj.LastModified}, io.NopCloser(bytes.NewReader(obj.Data)), nil
}
// DownloadTo writes one object to a caller-owned destination writer.
func (f *FakeBackend) DownloadTo(ctx context.Context, key string, destination io.Writer) error {
if err := ctx.Err(); err != nil {
return err
}
if f.DownloadErr != nil {
return f.DownloadErr
}
if destination == nil {
return fmt.Errorf("download object: destination writer is required")
}
if f.DownloadHook != nil {
if err := f.DownloadHook(FakeDownloadCall{Key: normalizeObjectKey(key)}); err != nil {
return err
}
}
_, source, err := f.Read(ctx, key)
if err != nil {
return err
}
defer source.Close()
count, err := io.Copy(destination, source)
if err != nil {
return fmt.Errorf("download object %q: write destination: %w", key, err)
}
f.mu.Lock()
f.Downloads = append(f.Downloads, FakeDownloadCall{Key: normalizeObjectKey(key), Bytes: count})
f.mu.Unlock()
return nil
}
// Download writes one object to a local path.
func (f *FakeBackend) Download(ctx context.Context, key, localPath string) error {
if strings.TrimSpace(localPath) == "" {
return fmt.Errorf("download object: local path is required")
}
if err := os.MkdirAll(filepath.Dir(localPath), 0o755); err != nil {
return fmt.Errorf("download object %q: create parent directory: %w", key, err)
}
destination, err := os.Create(localPath)
if err != nil {
return fmt.Errorf("download object %q: create local file: %w", key, err)
}
defer destination.Close()
return f.DownloadTo(ctx, key, destination)
}
// Upload reads a local file and stores it under key.
func (f *FakeBackend) Upload(ctx context.Context, localPath, key string, opts UploadOptions) (ObjectInfo, error) {
if err := ctx.Err(); err != nil {