Filter publication record names before sorting directory results
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
@@ -40,16 +41,10 @@ func ListAttempts(ctx context.Context, repositoryDir, override string) (Attempts
|
||||
if err := ctx.Err(); err != nil {
|
||||
return result, &RecoveryError{Kind: "canceled", cause: err}
|
||||
}
|
||||
entries, err := os.ReadDir(directory)
|
||||
paths, err := recordPaths(ctx, directory)
|
||||
if err != nil {
|
||||
return result, recoveryFailure(ctx, "storage_enumeration", fmt.Errorf("enumerate records in %q: %w", directory, err))
|
||||
}
|
||||
paths := make([]string, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".json") {
|
||||
paths = append(paths, filepath.Join(directory, entry.Name()))
|
||||
}
|
||||
}
|
||||
sort.Strings(paths)
|
||||
for _, path := range paths {
|
||||
if err := ctx.Err(); err != nil {
|
||||
@@ -73,6 +68,33 @@ func ListAttempts(ctx context.Context, repositoryDir, override string) (Attempts
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// recordPaths filters unsorted batches, retaining only selected record names.
|
||||
func recordPaths(ctx context.Context, directory string) ([]string, error) {
|
||||
file, err := os.Open(directory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
var paths []string
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return paths, err
|
||||
}
|
||||
entries, err := file.ReadDir(128)
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".json") {
|
||||
paths = append(paths, filepath.Join(directory, entry.Name()))
|
||||
}
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
return paths, nil
|
||||
}
|
||||
if err != nil {
|
||||
return paths, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func recordDirectoryFor(ctx context.Context, repositoryDir, override string) (string, error) {
|
||||
if filepath.IsAbs(override) {
|
||||
return filepath.Clean(override), nil
|
||||
|
||||
Reference in New Issue
Block a user