Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
feat(backups): add selective system restores and harden recovery (#3708)
Neureka874134cfedc2
diff --git a/backend/internal/backup/engine.go b/backend/internal/backup/engine.goindex 046fb7329..fba3175cb 100644--- a/backend/internal/backup/engine.go+++ b/backend/internal/backup/engine.go@@ -9,6 +9,8 @@ import ("fmt""io""log/slog"+ "path"+ "slices""strings""time"@@ -134,7 +136,7 @@ func (e *Engine) CreateSnapshot(ctx context.Context, dockerClient *client.Client// RestoreSnapshot restores a snapshot (or one path inside it) onto the target mount.func (e *Engine) RestoreSnapshot(ctx context.Context, dockerClient *client.Client, repository Repository, password, snapshotID string, target mount.Mount, options RestoreOptions) error {- command := []string{"restore"}+ command := []string{"restore", "--verify-existing"}if options.DeleteExtra {command = append(command, "--delete")}@@ -151,9 +153,20 @@ func (e *Engine) RestoreSnapshot(ctx context.Context, dockerClient *client.Clienreturn err}-// ListSnapshotFiles returns every path recorded in the snapshot.-func (e *Engine) ListSnapshotFiles(ctx context.Context, dockerClient *client.Client, repository Repository, password, snapshotID string) ([]string, error) {- output, err := e.runInternal(ctx, dockerClient, repository, password, []string{"ls", "--json", "--recursive", "--", snapshotID + ":/"})+// ListSnapshotFiles lists a snapshot path and returns paths relative to the+// snapshot root. A supplied path is nonrecursive unless recursive is true.+func (e *Engine) ListSnapshotFiles(ctx context.Context, dockerClient *client.Client, repository Repository, password, snapshotID, filePath string, recursive bool) ([]string, error) {+ command := []string{"ls", "--json"}+ if recursive {+ command = append(command, "--recursive")+ }+ cleanedPath := strings.Trim(strings.TrimSpace(filePath), "/")+ source := snapshotID + ":/" + cleanedPath+ if cleanedPath != "" && strings.HasSuffix(strings.TrimSpace(filePath), "/") {+ source += "/"+ }+ command = append(command, "--", source)+ output, err := e.runInternal(ctx, dockerClient, repository, password, command)if err != nil {return nil, fmt.Errorf("failed to list Rustic snapshot: %w", err)}@@ -161,7 +174,71 @@ func (e *Engine) ListSnapshotFiles(ctx context.Context, dockerClient *client.Cliif err := json.Unmarshal([]byte(output), &files); err != nil {return nil, fmt.Errorf("failed to decode Rustic file list: %w", err)}- return files, nil+ if !recursive && len(files) > 0 {+ // Rustic's JSON listing omits node types; the stable long listing restores them.+ longCommand := slices.Clone(command)+ longCommand[1] = "--long"+ longOutput, err := e.runInternal(ctx, dockerClient, repository, password, longCommand)+ if err != nil {+ return nil, fmt.Errorf("failed to list Rustic snapshot metadata: %w", err)+ }+ files, err = markSnapshotDirectoriesInternal(files, longOutput)+ if err != nil {+ return nil, err+ }+ }+ return qualifySnapshotListingInternal(files, cleanedPath), nil+}++func qualifySnapshotListingInternal(files []string, snapshotPath string) []string {+ qualified := slices.Clone(files)+ prefix := strings.Trim(strings.TrimSpace(snapshotPath), "/")+ if prefix == "" {+ return qualified+ }+ for index, file := range qualified {+ directory := strings.HasSuffix(file, "/")+ relative := strings.TrimPrefix(strings.TrimPrefix(file, "./"), "/")+ qualified[index] = path.Join(prefix, relative)+ if directory {+ qualified[index] += "/"+ }+ }+ return qualified+}++func markSnapshotDirectoriesInternal(files []string, longOutput string) ([]string, error) {+ if len(files) == 0 {+ if strings.TrimSpace(longOutput) != "" {+ return nil, errors.New("rustic file and metadata listings have different lengths")+ }+ return []string{}, nil+ }+ lines := strings.Split(strings.ReplaceAll(longOutput, "\r\n", "\n"), "\n")+ if len(lines) != len(files) {+ return nil, errors.New("rustic file and metadata listings have different lengths")+ }+ marked := slices.Clone(files)+ for index, line := range lines {+ if line == "" {+ return nil, errors.New("rustic returned empty file metadata")+ }+ if line[0] == 'd' && !strings.HasSuffix(marked[index], "/") {+ marked[index] += "/"+ }+ }+ return marked, nil+}++// ReadSnapshotTextFile returns one text file from a snapshot.+func (e *Engine) ReadSnapshotTextFile(ctx context.Context, dockerClient *client.Client, repository Repository, password, snapshotID, filePath string) (string, error) {+ output, err := e.runInternal(ctx, dockerClient, repository, password, []string{+ "dump", "--archive", "content", "--", snapshotID + ":/" + strings.TrimPrefix(filePath, "/"),+ })+ if err != nil {+ return "", fmt.Errorf("failed to read Rustic snapshot file: %w", err)+ }+ return output, nil}// DiscoveredSnapshot describes one snapshot found in a repository.diff --git a/backend/internal/backup/engine_test.go b/backend/internal/backup/engine_test.gonew file mode 100644index 000000000..678215148--- /dev/null+++ b/backend/internal/backup/engine_test.go@@ -0,0 +1,68 @@+package backup++import (+ "testing"++ "github.com/stretchr/testify/require"+)++func TestMarkSnapshotDirectoriesInternal(t *testing.T) {+ files := []string{"/volume", "/volume/folder", "/volume/file.txt", "/volume/link"}+ longOutput := "drwxr-xr-x root root 0 1 Jan 2026 00:00 \"/volume\"\r\n" ++ "drwxr-xr-x root root 0 1 Jan 2026 00:00 \"/volume/folder\"\r\n" ++ "-rw-r--r-- root root 5 1 Jan 2026 00:00 \"/volume/file.txt\"\r\n" ++ "lrwxrwxrwx root root 4 1 Jan 2026 00:00 \"/volume/link\" -> \"file.txt\""++ marked, err := markSnapshotDirectoriesInternal(files, longOutput)+ require.NoError(t, err)+ require.Equal(t, []string{"/volume/", "/volume/folder/", "/volume/file.txt", "/volume/link"}, marked)+ require.Equal(t, []string{"/volume", "/volume/folder", "/volume/file.txt", "/volume/link"}, files)+}++func TestMarkSnapshotDirectoriesRejectsMismatchedListingsInternal(t *testing.T) {+ _, err := markSnapshotDirectoriesInternal([]string{"/volume", "/volume/file.txt"}, "drwxr-xr-x root root 0 1 Jan 2026 00:00 \"/volume\"")+ require.ErrorContains(t, err, "different lengths")+}++func TestQualifySnapshotListingInternal(t *testing.T) {+ tests := []struct {+ name string+ files []string+ snapshotPath string+ expected []string+ }{+ {+ name: "root listing",+ files: []string{"folder/", "file.txt"},+ snapshotPath: "",+ expected: []string{"folder/", "file.txt"},+ },+ {+ name: "empty listing",+ files: []string{},+ snapshotPath: "folder",+ expected: []string{},+ },+ {+ name: "nested listing",+ files: []string{"nested/", "file.txt", "./link"},+ snapshotPath: "folder/",+ expected: []string{"folder/nested/", "folder/file.txt", "folder/link"},+ },+ {+ name: "legacy system project listing",+ files: []string{"demo/compose.yaml"},+ snapshotPath: "/app/data/projects/",+ expected: []string{"app/data/projects/demo/compose.yaml"},+ },+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ qualified := qualifySnapshotListingInternal(test.files, test.snapshotPath)+ require.Equal(t, test.expected, qualified)+ if len(test.files) > 0 {+ require.NotSame(t, &test.files[0], &qualified[0])+ }+ })+ }+}diff --git a/backend/internal/common/errors.go b/backend/internal/common/errors.goindex a2d868873..f09350438 100644--- a/backend/internal/common/errors.go+++ b/backend/internal/common/errors.go@@ -73,6 +73,7 @@ var (ErrGlobalVariableSecretValueRequired = Classify(ErrValidation, errors.Sentinel("A new value is required when making a secret variable readable"))ErrContainerComposeManaged = Classify(ErrConflict, errors.Sentinel("container is managed by a compose project; edit it via the project editor"))ErrContainerNameTaken = Classify(ErrConflict, errors.Sentinel("a container with this name already exists"))+ ErrInvalidBackupSelection = Classify(ErrBadRequest, errors.Sentinel("invalid backup file selection"))ErrSwarmNotEnabled = Classify(ErrBadRequest, errors.Sentinel("Swarm mode is not enabled"))ErrSwarmManagerRequired = Classify(ErrForbidden, errors.Sentinel("Swarm manager access required"))ErrRoleNotFound = Classify(ErrNotFound, errors.Sentinel("Role not found"))diff --git a/backend/internal/database/database.go b/backend/internal/database/database.goindex 5ad07492b..56030816a 100644--- a/backend/internal/database/database.go+++ b/backend/internal/database/database.go@@ -23,6 +23,7 @@ import ("gorm.io/gorm""gorm.io/gorm/logger"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils""github.com/getarcaneapp/arcane/backend/v2/resources")@@ -683,16 +684,10 @@ func ensureSQLiteDirectoryInternal(connString string) error {if !strings.HasPrefix(connString, "file:") {return nil}- u, err := url.Parse(connString)+ pathPart, err := utils.SQLitePathFromDSN(connString)if err != nil {return errors.WrapIf(err, "failed to parse SQLite DSN")}-- // For "file:data/arcane.db?...", path is in Opaque; for "file:/abs/path.db", it's in Path.- pathPart := u.Opaque- if pathPart == "" {- pathPart = u.Path- }if pathPart == "" || strings.HasPrefix(pathPart, ":memory:") {return nil}diff --git a/backend/internal/di/providers.go b/backend/internal/di/providers.goindex f846db47d..e29639c16 100644--- a/backend/internal/di/providers.go+++ b/backend/internal/di/providers.go@@ -259,8 +259,8 @@ func provideVolumeServiceInternal(module *volume.Module) *volume.VolumeService {return module.Service()}-func provideSystemBackupServiceInternal(db *database.DB, dockerService *docker.DockerClientService, volumeModule *volume.Module, engine *backup.Engine, s3Service *s3domain.S3DestinationService, activityService *activity.ActivityService, cfg *config.Config) *systembackup.SystemBackupService {- return systembackup.NewSystemBackupService(db, dockerService, volumeModule.Service(), engine, s3Service, activityService, cfg)+func provideSystemBackupServiceInternal(db *database.DB, dockerService *docker.DockerClientService, volumeModule *volume.Module, engine *backup.Engine, s3Service *s3domain.S3DestinationService, activityService *activity.ActivityService, settingsService *settings.SettingsService, cfg *config.Config) *systembackup.SystemBackupService {+ return systembackup.NewSystemBackupService(db, dockerService, volumeModule.Service(), engine, s3Service, activityService, settingsService, cfg)}func provideContainerModuleInternal(ctx context.Context, event *event.EventService, docker *docker.DockerClientService, image *image.ImageService, settings *settings.SettingsService, project *project.ProjectService, activity *activity.ActivityService) *container.Module {diff --git a/backend/internal/systembackup/handler.go b/backend/internal/systembackup/handler.goindex 7e009e431..729afca59 100644--- a/backend/internal/systembackup/handler.go+++ b/backend/internal/systembackup/handler.go@@ -9,6 +9,7 @@ import ("github.com/danielgtaylor/huma/v2""github.com/getarcaneapp/arcane/backend/v2/internal/activity"+ "github.com/getarcaneapp/arcane/backend/v2/internal/common""github.com/getarcaneapp/arcane/backend/v2/internal/database""github.com/getarcaneapp/arcane/backend/v2/internal/middleware""github.com/getarcaneapp/arcane/backend/v2/pkg/authz"@@ -65,6 +66,27 @@ type RestoreSystemBackupInput struct {ID string `path:"id"`Body backuptypes.RestoreSystemBackupRequest}++// BrowseSystemBackupFilesInput selects one page of a system backup tree.+type BrowseSystemBackupFilesInput struct {+ ID string `path:"id"`+ Path string `query:"path" doc:"Folder path relative to the backup root"`+ Search string `query:"search" doc:"Case-insensitive full-path search"`+ Start int `query:"start" default:"0" doc:"Start index for the page"`+ Limit int `query:"limit" default:"20" doc:"Requested page size"`+ Body backuptypes.SystemBackupRecoveryKey+}++// BrowseSystemBackupFilesOutput contains a page of project files and folders.+type BrowseSystemBackupFilesOutput struct {+ Body base.Paginated[backuptypes.BackupFileEntry]+}++// RestoreSystemBackupFilesInput selects project files to restore from a system backup.+type RestoreSystemBackupFilesInput struct {+ ID string `path:"id"`+ Body backuptypes.RestoreSystemBackupFilesRequest+}type UploadSystemBackupInput struct {ID string `path:"id"`Body backuptypes.UploadSystemBackupRequest@@ -92,6 +114,8 @@ func RegisterSystemBackups(api huma.API, service *SystemBackupService, activitySmiddleware.RegisterWithPermission(api, huma.Operation{OperationID: "create-system-backup", Method: http.MethodPost, Path: "/backups", Summary: "Create Arcane system backup", Tags: []string{"System Backups"}, Middlewares: adminOnly}, authz.PermSystemBackupsManage, h.Create)middleware.RegisterWithPermission(api, huma.Operation{OperationID: "discover-system-backups", Method: http.MethodPost, Path: "/backups/discover", Summary: "Discover Arcane system backups in S3", Tags: []string{"System Backups"}, Middlewares: adminOnly}, authz.PermSystemBackupsManage, h.Discover)middleware.RegisterWithPermission(api, huma.Operation{OperationID: "restore-system-backup", Method: http.MethodPost, Path: "/backups/{id}/restore", Summary: "Restore Arcane system backup", Tags: []string{"System Backups"}, Middlewares: adminOnly}, authz.PermSystemBackupsRestore, h.Restore)+ middleware.RegisterWithPermission(api, huma.Operation{OperationID: "browse-system-backup-files", Method: http.MethodPost, Path: "/backups/{id}/files/browse", Summary: "Browse project files in an Arcane system backup", Tags: []string{"System Backups"}, Middlewares: adminOnly}, authz.PermSystemBackupsRead, h.BrowseFiles)+ middleware.RegisterWithPermission(api, huma.Operation{OperationID: "restore-system-backup-files", Method: http.MethodPost, Path: "/backups/{id}/restore-files", Summary: "Restore project files from an Arcane system backup", Tags: []string{"System Backups"}, Middlewares: adminOnly}, authz.PermSystemBackupsRestore, h.RestoreFiles)middleware.RegisterWithPermission(api, huma.Operation{OperationID: "upload-system-backup", Method: http.MethodPost, Path: "/backups/{id}/upload", Summary: "Upload Arcane system backup", Tags: []string{"System Backups"}, Middlewares: adminOnly}, authz.PermSystemBackupsManage, h.Upload)middleware.RegisterWithPermission(api, huma.Operation{OperationID: "delete-system-backup", Method: http.MethodDelete, Path: "/backups/{id}", Summary: "Delete Arcane system backup", Tags: []string{"System Backups"}, Middlewares: adminOnly}, authz.PermSystemBackupsManage, h.Delete)}@@ -229,6 +253,43 @@ func (h *SystemBackupHandler) Restore(ctx context.Context, input *RestoreSystemBreturn messageOutputInternal("Arcane system restore started", activityID), nil}+// BrowseFiles returns one lazy-loaded project tree page.+func (h *SystemBackupHandler) BrowseFiles(ctx context.Context, input *BrowseSystemBackupFilesInput) (*BrowseSystemBackupFilesOutput, error) {+ params := handlerutil.PaginationParams(input.Start, input.Limit, "", "", input.Search)+ items, page, err := h.service.BrowseBackupFiles(ctx, input.ID, input.Body.RecoveryKey, input.Path, params)+ if err != nil {+ return nil, huma.Error400BadRequest(err.Error())+ }+ return &BrowseSystemBackupFilesOutput{Body: base.Paginated[backuptypes.BackupFileEntry]{+ Success: true, Data: items, Pagination: handlerutil.PaginationResponse(page),+ }}, nil+}++// RestoreFiles restores selected project files from a system backup.+func (h *SystemBackupHandler) RestoreFiles(ctx context.Context, input *RestoreSystemBackupFilesInput) (*SystemBackupMessageOutput, error) {+ user, err := handlerutil.RequireUser(ctx)+ if err != nil {+ return nil, err+ }+ activityID, err := activitylib.RunHandlerActivity(utils.ActivityRuntimeContext(ctx, h.appCtx), h.activity, activitylib.HandlerOptions{+ EnvironmentID: "0", Type: activitytypes.TypeResourceAction, ResourceType: "system_backup", ResourceID: input.ID, ResourceName: "Arcane", User: user,+ Step: "Restoring project files", Message: "Restoring project files from Arcane system backup", SuccessMessage: "Arcane project files restored successfully",+ Metadata: database.JSON{"action": "restore_system_backup_files", "backupId": input.ID, "pathCount": len(input.Body.Paths), "selectAll": input.Body.SelectAll, "search": input.Body.Search},+ }, func(activityCtx context.Context) error {+ return h.service.RestoreBackupFiles(activityCtx, input.ID, input.Body, *user)+ })+ if errors.Is(err, ErrSystemBackupAlreadyRunning) {+ return nil, huma.Error409Conflict(err.Error())+ }+ if errors.Is(err, common.ErrBadRequest) {+ return nil, huma.Error400BadRequest(err.Error())+ }+ if err != nil {+ return nil, huma.Error500InternalServerError(err.Error())+ }+ return messageOutputInternal("Arcane project files restored successfully", activityID), nil+}+func (h *SystemBackupHandler) Upload(ctx context.Context, input *UploadSystemBackupInput) (*SystemBackupOutput, error) {user, err := handlerutil.RequireUser(ctx)if err != nil {diff --git a/backend/internal/systembackup/service.go b/backend/internal/systembackup/service.goindex b525285c6..0448e9bb9 100644--- a/backend/internal/systembackup/service.go+++ b/backend/internal/systembackup/service.go@@ -8,8 +8,8 @@ import ("fmt""log/slog""maps"- "net/url""os"+ "path""path/filepath""reflect""regexp"@@ -28,6 +28,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/docker"recoverytypes "github.com/getarcaneapp/arcane/backend/v2/internal/recovery"s3domain "github.com/getarcaneapp/arcane/backend/v2/internal/s3"+ "github.com/getarcaneapp/arcane/backend/v2/internal/settings""github.com/getarcaneapp/arcane/backend/v2/internal/system""github.com/getarcaneapp/arcane/backend/v2/internal/volume"dockerutil "github.com/getarcaneapp/arcane/backend/v2/pkg/dockerutil"@@ -36,6 +37,8 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/pagination""github.com/getarcaneapp/arcane/backend/v2/pkg/projects""github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/entityjobs"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/backupbrowser"activitytypes "github.com/getarcaneapp/arcane/types/v2/activity"backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler"@@ -43,6 +46,7 @@ import (containertypes "github.com/moby/moby/api/types/container""github.com/moby/moby/api/types/mount""github.com/moby/moby/client"+ "go.getarcane.app/acfs""go.getarcane.app/sys/cgroup""go.getarcane.app/sys/crypto""gorm.io/gorm"@@ -51,6 +55,7 @@ import (const (defaultSystemBackupSchedule = "0 0 3 * * *"systemRecoveryManifestName = ".arcane-recovery.json"+ systemRecoveryRequestName = ".arcane-recovery-request.json"systemRecoveryHelperPath = "/app/arcane-recovery-helper"systemAdmissionID = "system")@@ -64,14 +69,15 @@ type SystemBackupService struct {engine *backup.Engines3Destinations *s3domain.S3DestinationServiceactivityService *activity.ActivityService+ settingsService *settings.SettingsServiceconfig *config.Configjobs *entityjobs.Registry}-func NewSystemBackupService(db *database.DB, dockerService *docker.DockerClientService, volumeService *volume.VolumeService, engine *backup.Engine, s3Destinations *s3domain.S3DestinationService, activityService *activity.ActivityService, cfg *config.Config) *SystemBackupService {+func NewSystemBackupService(db *database.DB, dockerService *docker.DockerClientService, volumeService *volume.VolumeService, engine *backup.Engine, s3Destinations *s3domain.S3DestinationService, activityService *activity.ActivityService, settingsService *settings.SettingsService, cfg *config.Config) *SystemBackupService {return &SystemBackupService{db: db, dockerService: dockerService, volumeService: volumeService, engine: engine,- s3Destinations: s3Destinations, activityService: activityService, config: cfg,+ s3Destinations: s3Destinations, activityService: activityService, settingsService: settingsService, config: cfg,jobs: entityjobs.New("system-backup:", backup.SystemAdmissionScope),}}@@ -111,7 +117,7 @@ func recoveryHelperExecutableInternal(mounts []containertypes.MountPoint, executreturn systemRecoveryHelperPath, executableMount, nil}-func (s *SystemBackupService) recoveryEnvironmentInternal() map[string]string {+func (s *SystemBackupService) recoveryEnvironmentInternal(ctx context.Context) map[string]string {result := make(map[string]string)var visit func(reflect.Value)visit = func(value reflect.Value) {@@ -147,6 +153,9 @@ func (s *SystemBackupService) recoveryEnvironmentInternal() map[string]string {}}visit(reflect.ValueOf(s.config))+ if s.settingsService != nil {+ result["PROJECTS_DIRECTORY"] = s.settingsService.GetStringSetting(ctx, "projectsDirectory", result["PROJECTS_DIRECTORY"])+ }return result}@@ -154,14 +163,10 @@ func (s *SystemBackupService) recoveryEnvironmentInternal() map[string]string {// /app/data in the container, the local data directory in host development.// The recovery manifest must live inside it so every snapshot includes it.func (s *SystemBackupService) dataDirectoryInternal() (string, error) {- parsed, err := url.Parse(s.config.DatabaseURL)+ databasePath, err := utils.SQLitePathFromDSN(s.config.DatabaseURL)if err != nil {return "", fmt.Errorf("failed to parse database URL: %w", err)}- databasePath := parsed.Opaque- if databasePath == "" {- databasePath = parsed.Path- }if strings.TrimSpace(databasePath) == "" {return "", errors.New("cannot resolve the Arcane data directory from the database URL")}@@ -181,7 +186,7 @@ func (s *SystemBackupService) manifestPathInternal() (string, error) {}func (s *SystemBackupService) writeManifestInternal(ctx context.Context, backupID string) error {- manifest := recoverytypes.Manifest{FormatVersion: 1, ArcaneVersion: config.Version, BackupID: backupID, ActivityID: activitylib.IDFromContext(ctx), CreatedAt: time.Now().UTC(), Environment: s.recoveryEnvironmentInternal()}+ manifest := recoverytypes.Manifest{FormatVersion: 1, ArcaneVersion: config.Version, BackupID: backupID, ActivityID: activitylib.IDFromContext(ctx), CreatedAt: time.Now().UTC(), Environment: s.recoveryEnvironmentInternal(ctx)}data, err := json.MarshalIndent(manifest, "", " ")if err != nil {return fmt.Errorf("failed to encode recovery manifest: %w", err)@@ -262,14 +267,6 @@ func systemSnapshotPathFromFilesInternal(files []string) (string, error) {return "", errors.New("system recovery snapshot does not contain an Arcane recovery manifest")}-func (s *SystemBackupService) snapshotPathInternal(ctx context.Context, dockerClient *client.Client, repository backup.Repository, recoveryKey, snapshotID string) (string, error) {- files, err := s.engine.ListSnapshotFiles(ctx, dockerClient, repository, recoveryKey, snapshotID)- if err != nil {- return "", fmt.Errorf("failed to inspect system recovery snapshot: %w", err)- }- return systemSnapshotPathFromFilesInternal(files)-}-func (s *SystemBackupService) recoveryKeyInternal(ctx context.Context, supplied string) (string, error) {if strings.TrimSpace(supplied) != "" {if err := validateRecoveryKeyInternal(supplied); err != nil {@@ -332,7 +329,46 @@ func (s *SystemBackupService) resolveBackupPlanInternal(ctx context.Context, reqreturn localEnabled, s3Enabled, destinationID, destination, nil}-func (s *SystemBackupService) CreateBackup(ctx context.Context, user common.User, trigger SystemBackupTrigger, request backuptypes.CreateSystemBackupRequest) (_ *SystemBackupRun, err error) {+func (s *SystemBackupService) CreateBackup(ctx context.Context, user common.User, trigger SystemBackupTrigger, request backuptypes.CreateSystemBackupRequest) (*SystemBackupRun, error) {+ lease, admitted, err := s.engine.TryAcquireRun(ctx, backup.SystemAdmissionScope, systemAdmissionID)+ if err != nil {+ return nil, err+ }+ if !admitted {+ return nil, ErrSystemBackupAlreadyRunning+ }+ defer lease.Release()+ return s.createBackupInternal(ctx, trigger, request)+}++func (s *SystemBackupService) createStagedSystemRecoverySnapshotInternal(ctx context.Context, dockerClient *client.Client, recoveryKey, backupID, destinationID string, localEnabled, s3Enabled bool) (backup.Snapshot, backup.Repository, bool, error) {+ localRepository, err := s.localRepositoryInternal(ctx, dockerClient, false)+ var snapshot backup.Snapshot+ if err == nil {+ snapshot, err = s.snapshotWithDatabaseLockInternal(ctx, dockerClient, localRepository, recoveryKey, backupID)+ }+ if err == nil {+ return snapshot, localRepository, false, nil+ }+ localStagingErr := fmt.Errorf("failed to create local system recovery staging snapshot: %w", err)+ if localEnabled || !s3Enabled {+ return backup.Snapshot{}, backup.Repository{}, false, localStagingErr+ }+ // S3-only backups normally use local staging so the database lock does+ // not span an upload. If local storage is unusable, write directly to+ // S3 so remote backups and restore safety snapshots remain available.+ remoteRepository, repoErr := s.remoteRepositoryInternal(ctx, destinationID)+ if repoErr != nil {+ return backup.Snapshot{}, backup.Repository{}, false, errors.Combine(localStagingErr, repoErr)+ }+ remoteSnapshot, snapshotErr := s.snapshotWithDatabaseLockInternal(ctx, dockerClient, remoteRepository, recoveryKey, backupID)+ if snapshotErr != nil {+ return backup.Snapshot{}, backup.Repository{}, false, errors.Combine(localStagingErr, fmt.Errorf("failed to create S3 system recovery snapshot: %w", snapshotErr))+ }+ return remoteSnapshot, remoteRepository, true, nil+}++func (s *SystemBackupService) createBackupInternal(ctx context.Context, trigger SystemBackupTrigger, request backuptypes.CreateSystemBackupRequest) (_ *SystemBackupRun, err error) {if !s.SupportedDatabaseProvider() {return nil, errors.New("arcane system recovery currently requires the SQLite database provider")}@@ -344,14 +380,6 @@ func (s *SystemBackupService) CreateBackup(ctx context.Context, user common.Userif err != nil {return nil, err}- lease, admitted, leaseErr := s.engine.TryAcquireRun(ctx, backup.SystemAdmissionScope, systemAdmissionID)- if leaseErr != nil {- return nil, leaseErr- }- if !admitted {- return nil, ErrSystemBackupAlreadyRunning- }- defer lease.Release()run := &SystemBackupRun{CreatedAt: time.Now().UTC(), Status: SystemBackupStatusRunning, Trigger: trigger, Destination: destination, S3DestinationID: destinationID, PolicyID: request.PolicyID}run.ID = "system-" + uuid.NewString()if err := s.db.WithContext(ctx).Create(run).Error; err != nil {@@ -371,17 +399,20 @@ func (s *SystemBackupService) CreateBackup(ctx context.Context, user common.Userif err != nil {return run, err}- localRepository, err := s.localRepositoryInternal(ctx, dockerClient, false)+ stagedSnapshot, stagingRepository, directRemote, err := s.createStagedSystemRecoverySnapshotInternal(+ ctx, dockerClient, recoveryKey, run.ID, destinationID, localEnabled, s3Enabled,+ )if err != nil {return run, err}+ if directRemote {+ run.RemoteSnapshotID, run.Size = stagedSnapshot.ID, stagedSnapshot.Size+ return run, nil+ }+ localRepository := stagingRepository// The database is locked only while the local staging snapshot is taken.// An S3 copy replicates that immutable snapshot afterwards, so the// exclusive lock never spans a network upload and the source is read once.- stagedSnapshot, err := s.snapshotWithDatabaseLockInternal(ctx, dockerClient, localRepository, recoveryKey, run.ID)- if err != nil {- return run, err- }// The staged snapshot is kept only for local-enabled runs. Forgetting it in// a defer covers the failure paths too: a failed replication otherwise// orphans one unreferenced snapshot in the staging repository per run.@@ -414,9 +445,8 @@ func (s *SystemBackupService) CreateBackup(ctx context.Context, user common.Userreturn run, nil}-// snapshotWithDatabaseLockInternal takes the local staging snapshot while the-// SQLite database is checkpointed and exclusively locked, and releases the-// lock before returning.+// snapshotWithDatabaseLockInternal takes a snapshot while the SQLite database+// is checkpointed and exclusively locked, and releases the lock before returning.func (s *SystemBackupService) snapshotWithDatabaseLockInternal(ctx context.Context, dockerClient *client.Client, repository backup.Repository, recoveryKey, backupID string) (backup.Snapshot, error) {if err := s.writeManifestInternal(ctx, backupID); err != nil {return backup.Snapshot{}, err@@ -444,7 +474,7 @@ func (s *SystemBackupService) snapshotWithDatabaseLockInternal(ctx context.Contedefer func() { _, _ = connection.ExecContext(context.WithoutCancel(ctx), "ROLLBACK") }()snapshot, err := s.createSnapshotInternal(ctx, dockerClient, repository, recoveryKey)if err != nil {- return backup.Snapshot{}, fmt.Errorf("failed to create local system recovery snapshot: %w", err)+ return backup.Snapshot{}, fmt.Errorf("failed to create system recovery snapshot: %w", err)}return snapshot, nil}@@ -484,6 +514,534 @@ func (s *SystemBackupService) backupInternal(ctx context.Context, id string) (*Sreturn &run, nil}+type systemBackupSnapshotLocationInternal struct {+ name string+ destination backuptypes.SystemBackupDestination+ s3DestinationID string+ repository backup.Repository+ snapshotID string+}++type systemBackupSnapshotInternal struct {+ systemBackupSnapshotLocationInternal++ files []string+ entries []backuptypes.BackupFileEntry+ snapshotPath string+ projectsPath string+}++type systemBackupSafetySnapshotInternal struct {+ systemBackupSnapshotLocationInternal++ snapshotPath string+ dataPaths map[string]struct{}+}++// systemBackupSnapshotSessionInternal carries the Docker client and resolved+// recovery key through one browse or restore operation, so the snapshot steps+// below don't each thread them through their signatures.+type systemBackupSnapshotSessionInternal struct {+ service *SystemBackupService+ dockerClient *client.Client+ recoveryKey string+}++func (s *SystemBackupService) snapshotSessionInternal(dockerClient *client.Client, recoveryKey string) systemBackupSnapshotSessionInternal {+ return systemBackupSnapshotSessionInternal{service: s, dockerClient: dockerClient, recoveryKey: recoveryKey}+}++func (s *SystemBackupService) backupSnapshotLocationsInternal(ctx context.Context, dockerClient *client.Client, run *SystemBackupRun) ([]systemBackupSnapshotLocationInternal, error) {+ locations := make([]systemBackupSnapshotLocationInternal, 0, 2)+ var setupErr error+ if run.LocalSnapshotID != "" {+ repository, err := s.localRepositoryInternal(ctx, dockerClient, true)+ if err != nil {+ setupErr = errors.Combine(setupErr, fmt.Errorf("open local system backup repository: %w", err))+ } else {+ locations = append(locations, systemBackupSnapshotLocationInternal{+ name: "local", destination: backuptypes.SystemBackupDestinationLocal,+ repository: repository, snapshotID: run.LocalSnapshotID,+ })+ }+ }+ if run.RemoteSnapshotID != "" {+ repository, err := s.remoteRepositoryInternal(ctx, run.S3DestinationID)+ if err != nil {+ setupErr = errors.Combine(setupErr, fmt.Errorf("open S3 system backup repository: %w", err))+ } else {+ locations = append(locations, systemBackupSnapshotLocationInternal{+ name: "S3", destination: backuptypes.SystemBackupDestinationS3, s3DestinationID: run.S3DestinationID,+ repository: repository, snapshotID: run.RemoteSnapshotID,+ })+ }+ }+ if len(locations) == 0 && setupErr == nil {+ setupErr = errors.New("system backup has no Rustic snapshot")+ }+ return locations, setupErr+}++func (session systemBackupSnapshotSessionInternal) inspectReadableSnapshotInternal(ctx context.Context, location systemBackupSnapshotLocationInternal) (systemBackupSnapshotInternal, error) {+ files, err := session.service.engine.ListSnapshotFiles(ctx, session.dockerClient, location.repository, session.recoveryKey, location.snapshotID, "", true)+ if err != nil {+ return systemBackupSnapshotInternal{}, fmt.Errorf("inspect %s system recovery snapshot: %w", location.name, err)+ }+ snapshotPath, err := systemSnapshotPathFromFilesInternal(files)+ if err != nil {+ return systemBackupSnapshotInternal{}, fmt.Errorf("inspect %s system recovery snapshot: %w", location.name, err)+ }+ return systemBackupSnapshotInternal{systemBackupSnapshotLocationInternal: location, files: files, snapshotPath: snapshotPath}, nil+}++func (session systemBackupSnapshotSessionInternal) firstReadableSnapshotInternal(ctx context.Context, locations []systemBackupSnapshotLocationInternal, setupErr error) (systemBackupSnapshotInternal, error) {+ inspectErr := setupErr+ for _, location := range locations {+ snapshot, err := session.inspectReadableSnapshotInternal(ctx, location)+ if err == nil {+ return snapshot, nil+ }+ inspectErr = errors.Combine(inspectErr, err)+ }+ if inspectErr == nil {+ inspectErr = errors.New("system backup has no Rustic snapshot")+ }+ return systemBackupSnapshotInternal{}, fmt.Errorf("failed to open system recovery snapshot: %w", inspectErr)+}++func recoveryManifestDatabasePathInternal(manifest recoverytypes.Manifest) (string, error) {+ databaseURL := strings.TrimSpace(manifest.Environment["DATABASE_URL"])+ if databaseURL == "" {+ return "", errors.New("system recovery manifest does not record the database path")+ }+ databasePath, err := utils.SQLitePathFromDSN(databaseURL)+ if err != nil {+ return "", fmt.Errorf("parse database URL from system recovery manifest: %w", err)+ }+ databasePath = strings.ReplaceAll(strings.TrimSpace(databasePath), `\`, "/")+ if databasePath == "" {+ return "", errors.New("system recovery manifest contains an empty database path")+ }+ return path.Clean(databasePath), nil+}++func portablePathIsAbsInternal(filePath string) bool {+ return path.IsAbs(filePath) || (len(filePath) >= 3 && filePath[1] == ':' && filePath[2] == '/')+}++func projectsRelativePathFromManifestInternal(manifest recoverytypes.Manifest) (string, error) {+ configured := strings.TrimSpace(manifest.Environment["PROJECTS_DIRECTORY"])+ if configured == "" {+ return "", errors.New("system recovery manifest does not record the projects directory")+ }+ if strings.HasPrefix(configured, "/") {+ if separator := strings.Index(configured, ":"); separator > 0 {+ configured = configured[:separator]+ }+ }+ projectsPath := path.Clean(strings.ReplaceAll(configured, `\`, "/"))+ databasePath, err := recoveryManifestDatabasePathInternal(manifest)+ if err != nil {+ return "", err+ }+ dataPath := path.Dir(databasePath)+ dataAbsolute := portablePathIsAbsInternal(dataPath)+ projectsAbsolute := portablePathIsAbsInternal(projectsPath)+ if !dataAbsolute && projectsAbsolute {+ relativeDataPath := strings.Trim(dataPath, "/")+ if relativeDataPath == "." {+ return "", errors.New("cannot relate the absolute projects directory to the relative database path in the system recovery manifest")+ }+ marker := "/" + relativeDataPath+ index := strings.LastIndex(projectsPath, marker)+ if index < 0 || (len(projectsPath) > index+len(marker) && projectsPath[index+len(marker)] != '/') {+ return "", errors.New("cannot relate the projects directory to the database path in the system recovery manifest")+ }+ dataPath = projectsPath[:index+len(marker)]+ dataAbsolute = true+ } else if dataAbsolute && !projectsAbsolute {+ projectsPath = path.Join(path.Dir(dataPath), projectsPath)+ projectsAbsolute = true+ }+ if dataAbsolute != projectsAbsolute {+ return "", errors.New("projects and database paths in the system recovery manifest use incompatible roots")+ }+ if projectsPath == dataPath {+ return "", nil+ }+ if !utils.FilePathMatches(projectsPath, dataPath) {+ return "", errors.New("the backup-time projects directory is outside Arcane's system backup data")+ }+ return strings.TrimPrefix(projectsPath, dataPath+"/"), nil+}++func (session systemBackupSnapshotSessionInternal) inspectProjectSnapshotInternal(ctx context.Context, location systemBackupSnapshotLocationInternal) (systemBackupSnapshotInternal, error) {+ snapshot, databasePath, err := session.inspectProjectManifestInternal(ctx, location)+ if err != nil {+ return systemBackupSnapshotInternal{}, err+ }+ root := path.Join(snapshot.snapshotPath, snapshot.projectsPath)+ listed, err := session.service.engine.ListSnapshotFiles(ctx, session.dockerClient, location.repository, session.recoveryKey, location.snapshotID, root+"/", true)+ if err != nil {+ return systemBackupSnapshotInternal{}, fmt.Errorf("inspect %s project files: %w", location.name, err)+ }+ snapshot.entries = projectEntriesFromSnapshotInternal(listed, snapshot.snapshotPath, snapshot.projectsPath, databasePath, "", true)+ return snapshot, nil+}++func (session systemBackupSnapshotSessionInternal) inspectProjectManifestInternal(ctx context.Context, location systemBackupSnapshotLocationInternal) (systemBackupSnapshotInternal, string, error) {+ var manifestData string+ var snapshotPath string+ var readErr error+ found := false+ for _, candidate := range []string{"/", "/app/data"} {+ data, err := session.service.engine.ReadSnapshotTextFile(ctx, session.dockerClient, location.repository, session.recoveryKey, location.snapshotID, path.Join(candidate, systemRecoveryManifestName))+ if err != nil {+ readErr = errors.Combine(readErr, err)+ continue+ }+ manifestData, snapshotPath = data, candidate+ found = true+ break+ }+ if !found {+ return systemBackupSnapshotInternal{}, "", fmt.Errorf("read %s system recovery manifest: %w", location.name, readErr)+ }+ var manifest recoverytypes.Manifest+ if err := json.Unmarshal([]byte(manifestData), &manifest); err != nil {+ return systemBackupSnapshotInternal{}, "", fmt.Errorf("decode %s system recovery manifest: %w", location.name, err)+ }+ if manifest.FormatVersion != 1 {+ return systemBackupSnapshotInternal{}, "", fmt.Errorf("%s system recovery manifest uses unsupported format %d", location.name, manifest.FormatVersion)+ }+ projectsPath, err := projectsRelativePathFromManifestInternal(manifest)+ if err != nil {+ return systemBackupSnapshotInternal{}, "", fmt.Errorf("resolve projects directory from %s system recovery manifest: %w", location.name, err)+ }+ fullDatabasePath, err := recoveryManifestDatabasePathInternal(manifest)+ if err != nil {+ return systemBackupSnapshotInternal{}, "", fmt.Errorf("resolve database path from %s system recovery manifest: %w", location.name, err)+ }+ return systemBackupSnapshotInternal{+ systemBackupSnapshotLocationInternal: location,+ snapshotPath: snapshotPath,+ projectsPath: projectsPath,+ }, path.Base(fullDatabasePath), nil+}++func (session systemBackupSnapshotSessionInternal) availableProjectSnapshotsInternal(ctx context.Context, locations []systemBackupSnapshotLocationInternal, setupErr error, firstOnly bool) ([]systemBackupSnapshotInternal, error) {+ snapshots := make([]systemBackupSnapshotInternal, 0, len(locations))+ inspectErr := setupErr+ for _, location := range locations {+ snapshot, err := session.inspectProjectSnapshotInternal(ctx, location)+ if err != nil {+ inspectErr = errors.Combine(inspectErr, err)+ continue+ }+ snapshots = append(snapshots, snapshot)+ if firstOnly {+ break+ }+ }+ if len(snapshots) == 0 {+ if inspectErr == nil {+ inspectErr = errors.New("system backup has no Rustic snapshot")+ }+ return nil, fmt.Errorf("failed to open project files in system recovery snapshot: %w", inspectErr)+ }+ return snapshots, nil+}++func snapshotRelativePathInternal(filePath, snapshotPath string) (string, bool) {+ cleanedFile := path.Clean("/" + strings.TrimPrefix(strings.TrimSpace(filePath), "/"))+ cleanedRoot := path.Clean("/" + strings.TrimPrefix(strings.TrimSpace(snapshotPath), "/"))+ if cleanedFile == "/" {+ return "", false+ }+ if cleanedRoot == "/" {+ return strings.TrimPrefix(cleanedFile, "/"), true+ }+ prefix := cleanedRoot + "/"+ if !strings.HasPrefix(cleanedFile, prefix) {+ return "", false+ }+ return strings.TrimPrefix(cleanedFile, prefix), true+}++func protectedSystemBackupPathInternal(candidate, databasePath string) bool {+ if candidate == systemRecoveryManifestName || candidate == systemRecoveryRequestName {+ return true+ }+ return candidate == databasePath || candidate == databasePath+"-wal" || candidate == databasePath+"-shm" || candidate == databasePath+"-journal"+}++func projectEntriesFromSnapshotInternal(files []string, snapshotPath, projectsPath, databasePath, browsePath string, recursive bool) []backuptypes.BackupFileEntry {+ eligible := make([]string, 0, len(files))+ for _, file := range files {+ dataRelative, ok := snapshotRelativePathInternal(file, snapshotPath)+ if !ok || protectedSystemBackupPathInternal(dataRelative, databasePath) {+ continue+ }+ projectRelative := dataRelative+ if projectsPath != "" {+ if dataRelative == projectsPath || !utils.FilePathMatches(dataRelative, projectsPath) {+ continue+ }+ projectRelative = strings.TrimPrefix(dataRelative, projectsPath+"/")+ }+ normalized, err := utils.NormalizeRelativePath(projectRelative)+ if err != nil {+ continue+ }+ if strings.HasSuffix(strings.TrimSpace(file), "/") {+ normalized += "/"+ }+ eligible = append(eligible, normalized)+ }+ return backupbrowser.BuildEntries(eligible, browsePath, recursive)+}++// BrowseBackupFiles returns one page from the backup's logical project tree.+func (s *SystemBackupService) BrowseBackupFiles(ctx context.Context, id, recoveryKey, requestedPath string, params pagination.QueryParams) ([]backuptypes.BackupFileEntry, pagination.Response, error) {+ listPath, recursive, err := backupbrowser.ListScope(requestedPath, params)+ if err != nil {+ return nil, pagination.Response{}, fmt.Errorf("%w: %w", common.ErrInvalidBackupSelection, err)+ }+ run, err := s.backupInternal(ctx, id)+ if err != nil {+ return nil, pagination.Response{}, err+ }+ if run.Status != SystemBackupStatusSucceeded {+ return nil, pagination.Response{}, errors.New("only successful system backups can be opened")+ }+ key, err := s.recoveryKeyInternal(ctx, recoveryKey)+ if err != nil {+ return nil, pagination.Response{}, err+ }+ dockerClient, err := s.dockerService.GetClient(ctx)+ if err != nil {+ return nil, pagination.Response{}, err+ }+ session := s.snapshotSessionInternal(dockerClient, key)+ locations, setupErr := s.backupSnapshotLocationsInternal(ctx, dockerClient, run)+ browseErr := setupErr+ for _, location := range locations {+ snapshot, databasePath, inspectErr := session.inspectProjectManifestInternal(ctx, location)+ if inspectErr != nil {+ browseErr = errors.Combine(browseErr, inspectErr)+ continue+ }+ snapshotRoot := path.Join(snapshot.snapshotPath, snapshot.projectsPath, listPath)+ listed, listErr := s.engine.ListSnapshotFiles(ctx, dockerClient, location.repository, key, location.snapshotID, snapshotRoot+"/", recursive)+ if listErr != nil {+ browseErr = errors.Combine(browseErr, fmt.Errorf("browse %s system recovery snapshot: %w", location.name, listErr))+ continue+ }+ entries := projectEntriesFromSnapshotInternal(listed, snapshot.snapshotPath, snapshot.projectsPath, databasePath, listPath, recursive)+ items, page := backupbrowser.Browse(entries, params)+ return items, page, nil+ }+ return nil, pagination.Response{}, fmt.Errorf("failed to browse project files in system recovery snapshot: %w", browseErr)+}++// openSafetySnapshotInternal resolves the safety run's snapshot in the same+// repository the restore reads from and indexes the data paths it holds.+func (session systemBackupSnapshotSessionInternal) openSafetySnapshotInternal(ctx context.Context, source systemBackupSnapshotInternal, safetyRun *SystemBackupRun) (systemBackupSafetySnapshotInternal, error) {+ location := source.systemBackupSnapshotLocationInternal+ location.name = "safety"+ if source.destination == backuptypes.SystemBackupDestinationS3 {+ location.snapshotID = safetyRun.RemoteSnapshotID+ } else {+ location.snapshotID = safetyRun.LocalSnapshotID+ }+ if location.snapshotID == "" {+ return systemBackupSafetySnapshotInternal{}, errors.New("pre-restore system backup has no snapshot in the selected repository")+ }+ snapshot, err := session.inspectReadableSnapshotInternal(ctx, location)+ if err != nil {+ return systemBackupSafetySnapshotInternal{}, fmt.Errorf("open pre-restore system backup: %w", err)+ }+ dataPaths := make(map[string]struct{}, len(snapshot.files))+ for _, file := range snapshot.files {+ if relative, ok := snapshotRelativePathInternal(file, snapshot.snapshotPath); ok {+ dataPaths[relative] = struct{}{}+ }+ }+ return systemBackupSafetySnapshotInternal{+ systemBackupSnapshotLocationInternal: location,+ snapshotPath: snapshot.snapshotPath,+ dataPaths: dataPaths,+ }, nil+}++func (s *SystemBackupService) removeProjectFileInternal(ctx context.Context, projectsPath, selectedPath string) error {+ dataDirectory, err := s.dataDirectoryInternal()+ if err != nil {+ return err+ }+ return acfs.RemoveAll(ctx, dataDirectory, path.Join(projectsPath, selectedPath))+}++func (session systemBackupSnapshotSessionInternal) restoreEntryInternal(ctx context.Context, snapshots []systemBackupSnapshotInternal, projectsPath string, selected backuptypes.BackupFileEntry, dataMount mount.Mount) error {+ var restoreErr error+ for _, snapshot := range snapshots {+ if snapshot.projectsPath != projectsPath || !snapshotContainsProjectEntryInternal(snapshot, selected) {+ continue+ }+ sourcePath := path.Join(snapshot.snapshotPath, projectsPath, selected.Path)+ if selected.IsDirectory {+ sourcePath += "/"+ }+ err := session.service.engine.RestoreSnapshot(ctx, session.dockerClient, snapshot.repository, session.recoveryKey, snapshot.snapshotID, dataMount, backup.RestoreOptions{+ DeleteExtra: selected.IsDirectory,+ SourcePath: sourcePath,+ DestinationPath: path.Join(dataMount.Target, projectsPath, selected.Path),+ })+ if err == nil {+ return nil+ }+ restoreErr = errors.Combine(restoreErr, fmt.Errorf("restore from %s snapshot: %w", snapshot.name, err))+ }+ if restoreErr == nil {+ restoreErr = errors.New("file is unavailable in every readable system recovery snapshot")+ }+ return restoreErr+}++func snapshotContainsProjectEntryInternal(snapshot systemBackupSnapshotInternal, selected backuptypes.BackupFileEntry) bool {+ if selected.Path == "" && selected.IsDirectory {+ return true+ }+ for _, entry := range snapshot.entries {+ if entry.Path == selected.Path && entry.IsDirectory == selected.IsDirectory {+ return true+ }+ }+ return false+}++func safetySnapshotContainsPathInternal(safety systemBackupSafetySnapshotInternal, dataRelative string) bool {+ if _, exists := safety.dataPaths[dataRelative]; exists {+ return true+ }+ prefix := strings.TrimSuffix(dataRelative, "/") + "/"+ for candidate := range safety.dataPaths {+ if strings.HasPrefix(candidate, prefix) {+ return true+ }+ }+ return false+}++func (session systemBackupSnapshotSessionInternal) rollbackInternal(ctx context.Context, safety systemBackupSafetySnapshotInternal, projectsPath string, selected []backuptypes.BackupFileEntry, dataMount mount.Mount) error {+ var rollbackErr error+ for _, selectedEntry := range slices.Backward(selected) {+ dataRelative := path.Join(projectsPath, selectedEntry.Path)+ if !safetySnapshotContainsPathInternal(safety, dataRelative) {+ if err := session.service.removeProjectFileInternal(ctx, projectsPath, selectedEntry.Path); err != nil {+ rollbackErr = errors.Combine(rollbackErr, fmt.Errorf("remove newly restored project path %s: %w", selectedEntry.Path, err))+ }+ continue+ }+ sourcePath := path.Join(safety.snapshotPath, dataRelative)+ if selectedEntry.IsDirectory {+ sourcePath += "/"+ }+ if err := session.service.engine.RestoreSnapshot(ctx, session.dockerClient, safety.repository, session.recoveryKey, safety.snapshotID, dataMount, backup.RestoreOptions{+ DeleteExtra: selectedEntry.IsDirectory,+ SourcePath: sourcePath,+ DestinationPath: path.Join(dataMount.Target, dataRelative),+ }); err != nil {+ rollbackErr = errors.Combine(rollbackErr, fmt.Errorf("restore project path %s from safety backup: %w", selectedEntry.Path, err))+ }+ }+ return rollbackErr+}++func (session systemBackupSnapshotSessionInternal) restoreSelectedInternal(ctx context.Context, snapshots []systemBackupSnapshotInternal, safety systemBackupSafetySnapshotInternal, selected []backuptypes.BackupFileEntry, dataMount mount.Mount) error {+ projectsPath := snapshots[0].projectsPath+ restored := make([]backuptypes.BackupFileEntry, 0, len(selected))+ for _, selectedEntry := range selected {+ if err := session.restoreEntryInternal(ctx, snapshots, projectsPath, selectedEntry, dataMount); err != nil {+ affected := slices.Clone(restored)+ affected = append(affected, selectedEntry)+ rollbackErr := session.rollbackInternal(context.WithoutCancel(ctx), safety, projectsPath, affected, dataMount)+ restoreErr := fmt.Errorf("failed to restore project path %s from system backup: %w", selectedEntry.Path, err)+ if rollbackErr != nil {+ return errors.Combine(restoreErr, fmt.Errorf("failed to roll back project files from pre-restore system backup: %w", rollbackErr))+ }+ return fmt.Errorf("%w; affected project files were rolled back", restoreErr)+ }+ restored = append(restored, selectedEntry)+ }+ return nil+}++// RestoreBackupFiles restores selected project files after creating a safety snapshot.+func (s *SystemBackupService) RestoreBackupFiles(ctx context.Context, id string, request backuptypes.RestoreSystemBackupFilesRequest, user common.User) error {+ lease, admitted, err := s.engine.TryAcquireRun(ctx, backup.SystemAdmissionScope, systemAdmissionID)+ if err != nil {+ return err+ }+ if !admitted {+ return ErrSystemBackupAlreadyRunning+ }+ defer lease.Release()+ run, err := s.backupInternal(ctx, id)+ if err != nil {+ return err+ }+ if run.Status != SystemBackupStatusSucceeded {+ return errors.New("only successful system backups can be restored")+ }+ key, err := s.recoveryKeyInternal(ctx, request.RecoveryKey)+ if err != nil {+ return err+ }+ dockerClient, err := s.dockerService.GetClient(ctx)+ if err != nil {+ return err+ }+ session := s.snapshotSessionInternal(dockerClient, key)+ locations, setupErr := s.backupSnapshotLocationsInternal(ctx, dockerClient, run)+ snapshots, err := session.availableProjectSnapshotsInternal(ctx, locations, setupErr, false)+ if err != nil {+ return err+ }+ selected, err := normalizeSystemBackupSelectionInternal(request.RestoreSelection, snapshots[0])+ if err != nil {+ return fmt.Errorf("%w: %w", common.ErrInvalidBackupSelection, err)+ }+ dataMount, err := s.appDataMountInternal(ctx, dockerClient, "/arcane-data", false)+ if err != nil {+ return err+ }+ // The safety backup mirrors the restore source so the rollback snapshot+ // lives in the repository the restore is already reading from.+ source := snapshots[0]+ safetyRequest := backuptypes.CreateSystemBackupRequest{Destination: backuptypes.SystemBackupDestinationLocal, RecoveryKey: key}+ if source.destination == backuptypes.SystemBackupDestinationS3 {+ safetyRequest.Destination = backuptypes.SystemBackupDestinationS3+ safetyRequest.S3DestinationID = source.s3DestinationID+ }+ safetyRun, err := s.createBackupInternal(ctx, SystemBackupTriggerSafety, safetyRequest)+ if err != nil {+ return fmt.Errorf("failed to create pre-restore system backup: %w", err)+ }+ safety, err := session.openSafetySnapshotInternal(ctx, source, safetyRun)+ if err != nil {+ return err+ }+ return session.restoreSelectedInternal(ctx, snapshots, safety, selected, dataMount)+}++func normalizeSystemBackupSelectionInternal(selection backuptypes.RestoreSelection, snapshot systemBackupSnapshotInternal) ([]backuptypes.BackupFileEntry, error) {+ if selection.SelectAll && strings.TrimSpace(selection.Search) == "" && snapshot.projectsPath != "" {+ return backupbrowser.NormalizeSelection(selection, []backuptypes.BackupFileEntry{{Path: "", Name: path.Base(snapshot.projectsPath), IsDirectory: true}})+ }+ return backupbrowser.NormalizeSelection(selection, snapshot.entries)+}+func (s *SystemBackupService) DeleteBackup(ctx context.Context, id, recoveryKey string) error {// Deletes contend with create/restore/upload on the system run lease so a// slow operation cannot resurrect or double-forget snapshots.@@ -683,40 +1241,26 @@ func (s *SystemBackupService) RestoreBackup(ctx context.Context, id, recoveryKeyif appDataMount == nil {return errors.New("arcane system restore requires /app/data to be mounted")}- var repository backup.Repository- var snapshotID string- switch {- case run.LocalSnapshotID != "":- repository, err = s.localRepositoryInternal(ctx, dockerClient, true)- snapshotID = run.LocalSnapshotID- case run.RemoteSnapshotID != "":- repository, err = s.remoteRepositoryInternal(ctx, run.S3DestinationID)- snapshotID = run.RemoteSnapshotID- default:- return errors.New("system backup has no Rustic snapshot")- }- if err != nil {- return err- }- snapshotPath, err := s.snapshotPathInternal(ctx, dockerClient, repository, key, snapshotID)+ locations, setupErr := s.backupSnapshotLocationsInternal(ctx, dockerClient, run)+ snapshot, err := s.snapshotSessionInternal(dockerClient, key).firstReadableSnapshotInternal(ctx, locations, setupErr)if err != nil {return err}request := recoverytypes.RestoreRequest{- BackupID: run.ID, ContainerID: current.ID, ContainerImage: current.Config.Image, SnapshotID: snapshotID, SnapshotPath: snapshotPath, RecoveryKey: key,+ BackupID: run.ID, ContainerID: current.ID, ContainerImage: current.Config.Image, SnapshotID: snapshot.snapshotID, SnapshotPath: snapshot.snapshotPath, RecoveryKey: key,LocalSnapshotID: run.LocalSnapshotID, RemoteSnapshotID: run.RemoteSnapshotID, S3DestinationID: run.S3DestinationID,Size: run.Size, NetworkMode: rusticRestoreNetworkModeInternal(¤t),SafetyBackup: &recoverytypes.SafetyBackup{ID: safetyBackup.ID, LocalSnapshotID: safetyBackup.LocalSnapshotID,Size: safetyBackup.Size, CreatedAt: safetyBackup.CreatedAt,},- RepositoryEnvironment: repository.Environment, RepositoryMounts: repository.Mounts, AppDataMount: *appDataMount,+ RepositoryEnvironment: snapshot.repository.Environment, RepositoryMounts: snapshot.repository.Mounts, AppDataMount: *appDataMount,}requestData, err := json.Marshal(request)if err != nil {return fmt.Errorf("encode recovery request: %w", err)}- requestFile := "/app/data/.arcane-recovery-request.json"+ requestFile := path.Join("/app/data", systemRecoveryRequestName)if err := os.WriteFile(requestFile, requestData, 0o600); err != nil {return fmt.Errorf("write recovery request: %w", err)}diff --git a/backend/internal/systembackup/service_test.go b/backend/internal/systembackup/service_test.goindex 124ea7963..e09ee32c7 100644--- a/backend/internal/systembackup/service_test.go+++ b/backend/internal/systembackup/service_test.go@@ -2,6 +2,7 @@ package systembackupimport ("context"+ "slices""testing""time"@@ -9,6 +10,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/backup""github.com/getarcaneapp/arcane/backend/v2/internal/config""github.com/getarcaneapp/arcane/backend/v2/internal/database"+ recoverytypes "github.com/getarcaneapp/arcane/backend/v2/internal/recovery""github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/entityjobs"backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler"@@ -135,7 +137,7 @@ func TestRecoveryEnvironmentInternalIncludesRuntimeSecrets(t *testing.T) {OidcClientSecret: "oidc-secret",FilePerm: 0o640,}- environment := (&SystemBackupService{config: cfg}).recoveryEnvironmentInternal()+ environment := (&SystemBackupService{config: cfg}).recoveryEnvironmentInternal(context.Background())require.Equal(t, "jwt-secret", environment["JWT_SECRET"])require.Equal(t, "encryption-secret", environment["ENCRYPTION_KEY"])require.Equal(t, "admin-key", environment["ADMIN_STATIC_API_KEY"])@@ -155,3 +157,139 @@ func TestSystemSnapshotPathFromFilesInternal(t *testing.T) {_, err = systemSnapshotPathFromFilesInternal([]string{"/arcane.db"})require.ErrorContains(t, err, "does not contain an Arcane recovery manifest")}++func TestProjectFilesFromSnapshotInternal(t *testing.T) {+ tests := []struct {+ name string+ files []string+ snapshotPath string+ projectsPath string+ databasePath string+ expected []string+ }{+ {+ name: "current snapshot root",+ files: []string{+ "/.arcane-recovery.json", "/arcane.db", "/arcane.db-wal", "/templates/demo.yaml",+ "/projects/demo/docker-compose.yaml", "/projects/demo/.env", "/projects/demo/data/", "/projects/demo/.env",+ },+ snapshotPath: "/", projectsPath: "projects", databasePath: "arcane.db",+ expected: []string{"demo/.env", "demo/docker-compose.yaml"},+ },+ {+ name: "legacy snapshot and custom projects root",+ files: []string{+ "/app/data/.arcane-recovery.json", "/app/data/arcane.db", "/app/data/custom/projects/nested/app/compose.yaml",+ "/app/data/projects/ignored/compose.yaml",+ },+ snapshotPath: "/app/data", projectsPath: "custom/projects", databasePath: "arcane.db",+ expected: []string{"nested/app/compose.yaml"},+ },+ {+ name: "projects directory is data root",+ files: []string{+ "/.arcane-recovery.json", "/.arcane-recovery-request.json", "/custom.db", "/custom.db-shm", "/demo/config.yaml",+ },+ snapshotPath: "/", projectsPath: "", databasePath: "custom.db",+ expected: []string{"demo/config.yaml"},+ },+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ entries := projectEntriesFromSnapshotInternal(test.files, test.snapshotPath, test.projectsPath, test.databasePath, "", true)+ actual := make([]string, 0, len(entries))+ for _, entry := range entries {+ if !entry.IsDirectory {+ actual = append(actual, entry.Path)+ }+ }+ slices.Sort(actual)+ require.Equal(t, test.expected, actual)+ })+ }+}++func TestProjectsRelativePathFromManifestInternal(t *testing.T) {+ tests := []struct {+ name string+ databaseURL string+ projectsDirectory string+ expected string+ errorContains string+ }{+ {name: "absolute database path", databaseURL: "file:/app/data/arcane.db", projectsDirectory: "/app/data/historical", expected: "historical"},+ {name: "relative default database path", databaseURL: "file:data/arcane.db", projectsDirectory: "/app/data/historical", expected: "historical"},+ {name: "projects mapping", databaseURL: "file:/app/data/arcane.db", projectsDirectory: "/app/data/historical:/host/projects", expected: "historical"},+ {name: "data root", databaseURL: "file:/app/data/arcane.db", projectsDirectory: "/app/data", expected: ""},+ {name: "outside data", databaseURL: "file:/app/data/arcane.db", projectsDirectory: "/srv/projects", errorContains: "outside Arcane's system backup data"},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ manifest := recoverytypes.Manifest{Environment: map[string]string{+ "DATABASE_URL": test.databaseURL,+ "PROJECTS_DIRECTORY": test.projectsDirectory,+ }}+ relative, err := projectsRelativePathFromManifestInternal(manifest)+ if test.errorContains != "" {+ require.ErrorContains(t, err, test.errorContains)+ return+ }+ require.NoError(t, err)+ require.Equal(t, test.expected, relative)+ })+ }+}++func TestProjectEntriesFromSnapshotSynthesizesFoldersAndExcludesProtectedDataInternal(t *testing.T) {+ entries := projectEntriesFromSnapshotInternal([]string{+ "/.arcane-recovery.json",+ "/.arcane-recovery-request.json",+ "/arcane.db",+ "/arcane.db-wal",+ "/arcane.db-shm",+ "/arcane.db-journal",+ "/demo/nested/compose.yaml",+ "/z.txt",+ }, "/", "", "arcane.db", "", false)+ require.Equal(t, []backuptypes.BackupFileEntry{+ {Path: "demo", Name: "demo", IsDirectory: true},+ {Path: "z.txt", Name: "z.txt"},+ }, entries)+}++func TestProjectEntriesFromSnapshotNestedBrowseInternal(t *testing.T) {+ entries := projectEntriesFromSnapshotInternal([]string{+ "/app/data/custom/projects/demo/nested/",+ "/app/data/custom/projects/demo/compose.yaml",+ }, "/app/data", "custom/projects", "arcane.db", "demo", false)+ require.Equal(t, []backuptypes.BackupFileEntry{+ {Path: "demo/nested", Name: "nested", IsDirectory: true},+ {Path: "demo/compose.yaml", Name: "compose.yaml"},+ }, entries)+}++func TestNormalizeSystemBackupSelectionInternal(t *testing.T) {+ snapshot := systemBackupSnapshotInternal{+ projectsPath: "historical",+ entries: []backuptypes.BackupFileEntry{+ {Path: "demo", Name: "demo", IsDirectory: true},+ {Path: "demo/compose.yaml", Name: "compose.yaml"},+ },+ }+ selected, err := normalizeSystemBackupSelectionInternal(backuptypes.RestoreSelection{SelectAll: true}, snapshot)+ require.NoError(t, err)+ require.Equal(t, []backuptypes.BackupFileEntry{{Path: "", Name: "historical", IsDirectory: true}}, selected)++ _, err = normalizeSystemBackupSelectionInternal(+ backuptypes.RestoreSelection{SelectAll: true, Paths: []string{"demo"}},+ snapshot,+ )+ require.ErrorContains(t, err, "cannot be combined")++ selected, err = normalizeSystemBackupSelectionInternal(+ backuptypes.RestoreSelection{Paths: []string{"demo/compose.yaml", "demo"}},+ snapshot,+ )+ require.NoError(t, err)+ require.Equal(t, []backuptypes.BackupFileEntry{{Path: "demo", Name: "demo", IsDirectory: true}}, selected)+}diff --git a/backend/internal/volume/backup.go b/backend/internal/volume/backup.goindex 61479710b..c8e56a3a2 100644--- a/backend/internal/volume/backup.go+++ b/backend/internal/volume/backup.go@@ -10,6 +10,7 @@ import ("io""log/slog""path"+ "slices""strings""time""uuid"@@ -27,7 +28,9 @@ import (activitylib "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/activity""github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/volumehelper""github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/backupbrowser"activitytypes "github.com/getarcaneapp/arcane/types/v2/activity"+ backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"volumetypes "github.com/getarcaneapp/arcane/types/v2/volume""github.com/moby/moby/api/types/container""github.com/moby/moby/api/types/mount"@@ -901,7 +904,48 @@ func (s *VolumeService) ListBackupFiles(ctx context.Context, backupID string) ([if err != nil {return nil, err}- return s.engine.ListSnapshotFiles(ctx, dockerClient, repository, s.rusticPasswordInternal(), snapshotID)+ return s.engine.ListSnapshotFiles(ctx, dockerClient, repository, s.rusticPasswordInternal(), snapshotID, "", true)+}++// BrowseBackupFiles returns one lazy-loaded page from a volume backup tree.+func (s *VolumeService) BrowseBackupFiles(ctx context.Context, backupID, requestedPath string, params pagination.QueryParams) ([]backuptypes.BackupFileEntry, pagination.Response, error) {+ listPath, recursive, err := backupbrowser.ListScope(requestedPath, params)+ if err != nil {+ return nil, pagination.Response{}, fmt.Errorf("%w: %w", common.ErrInvalidBackupSelection, err)+ }+ var entry VolumeBackup+ if err := s.db.WithContext(ctx).Where("id = ?", backupID).First(&entry).Error; err != nil {+ return nil, pagination.Response{}, err+ }+ entries, err := s.backupFileEntriesInternal(ctx, &entry, listPath, recursive)+ if err != nil {+ return nil, pagination.Response{}, err+ }+ items, page := backupbrowser.Browse(entries, params)+ return items, page, nil+}++func (s *VolumeService) backupFileEntriesInternal(ctx context.Context, entry *VolumeBackup, browsePath string, recursive bool) ([]backuptypes.BackupFileEntry, error) {+ if entry.Format == VolumeBackupFormatArchive {+ paths, err := s.listArchiveBackupPathsInternal(ctx, entry.ID)+ if err != nil {+ return nil, err+ }+ return backupbrowser.BuildEntries(paths, browsePath, recursive), nil+ }+ dockerClient, err := s.dockerService.GetClient(ctx)+ if err != nil {+ return nil, err+ }+ repository, snapshotID, err := s.rusticRepositoryForBackupInternal(ctx, dockerClient, entry)+ if err != nil {+ return nil, err+ }+ listed, err := s.engine.ListSnapshotFiles(ctx, dockerClient, repository, s.rusticPasswordInternal(), snapshotID, browsePath+"/", recursive)+ if err != nil {+ return nil, err+ }+ return backupbrowser.BuildEntries(listed, browsePath, recursive), nil}func (s *VolumeService) BackupHasPath(ctx context.Context, backupID, filePath string) (bool, error) {@@ -921,10 +965,69 @@ func (s *VolumeService) BackupHasPath(ctx context.Context, backupID, filePath streturn false, nil}-func (s *VolumeService) RestoreBackupFiles(ctx context.Context, volumeName, backupID string, paths []string, user common.User) (err error) {- if len(paths) == 0 {- return errors.New("no paths provided")+type volumeBackupRestoreSelectionInternal struct {+ entries []backuptypes.BackupFileEntry+ archivePaths []string+ globalRoot bool+}++func (s *VolumeService) resolveVolumeBackupRestoreSelectionInternal(ctx context.Context, dockerClient *client.Client, entry *VolumeBackup, repository backup.Repository, snapshotID string, selection backuptypes.RestoreSelection) (volumeBackupRestoreSelectionInternal, error) {+ if selection.SelectAll && strings.TrimSpace(selection.Search) == "" {+ if _, err := backupbrowser.NormalizeSelection(selection, []backuptypes.BackupFileEntry{{Path: "", IsDirectory: true}}); err != nil {+ return volumeBackupRestoreSelectionInternal{}, fmt.Errorf("%w: %w", common.ErrInvalidBackupSelection, err)+ }+ return volumeBackupRestoreSelectionInternal{globalRoot: true}, nil+ }+ resolved := volumeBackupRestoreSelectionInternal{}+ var eligible []backuptypes.BackupFileEntry+ var err error+ if entry.Format == VolumeBackupFormatArchive {+ resolved.archivePaths, err = s.listArchiveBackupPathsInternal(ctx, entry.ID)+ eligible = backupbrowser.BuildEntries(resolved.archivePaths, "", true)+ } else {+ var listed []string+ listed, err = s.engine.ListSnapshotFiles(ctx, dockerClient, repository, s.rusticPasswordInternal(), snapshotID, "", true)+ eligible = backupbrowser.BuildEntries(listed, "", true)+ }+ if err != nil {+ return volumeBackupRestoreSelectionInternal{}, err}+ resolved.entries, err = backupbrowser.NormalizeSelection(selection, eligible)+ if err != nil {+ return volumeBackupRestoreSelectionInternal{}, fmt.Errorf("%w: %w", common.ErrInvalidBackupSelection, err)+ }+ return resolved, nil+}++func (s *VolumeService) restoreVolumeBackupSelectionInternal(ctx context.Context, dockerClient *client.Client, volumeName, backupID string, entry *VolumeBackup, repository backup.Repository, snapshotID string, selection volumeBackupRestoreSelectionInternal) error {+ if entry.Format == VolumeBackupFormatArchive {+ if selection.globalRoot {+ return s.restoreArchiveBackupInternal(ctx, dockerClient, volumeName, backupID)+ }+ members := archiveMembersForSelectionInternal(selection.archivePaths, selection.entries)+ return s.restoreArchiveBackupFilesInternal(ctx, dockerClient, volumeName, backupID, members)+ }+ target := mount.Mount{Type: mount.TypeVolume, Source: volumeName, Target: "/volume"}+ if selection.globalRoot {+ if err := s.engine.RestoreSnapshot(ctx, dockerClient, repository, s.rusticPasswordInternal(), snapshotID, target, backup.RestoreOptions{DeleteExtra: true}); err != nil {+ return fmt.Errorf("failed to restore Rustic snapshot: %w", err)+ }+ return nil+ }+ for _, selectedEntry := range selection.entries {+ sourcePath := selectedEntry.Path+ if selectedEntry.IsDirectory {+ sourcePath += "/"+ }+ options := backup.RestoreOptions{DeleteExtra: selectedEntry.IsDirectory, SourcePath: sourcePath, DestinationPath: path.Join(target.Target, selectedEntry.Path)}+ if err := s.engine.RestoreSnapshot(ctx, dockerClient, repository, s.rusticPasswordInternal(), snapshotID, target, options); err != nil {+ return fmt.Errorf("failed to restore %s from Rustic snapshot: %w", selectedEntry.Path, err)+ }+ }+ return nil+}++func (s *VolumeService) RestoreBackupFiles(ctx context.Context, volumeName, backupID string, selection backuptypes.RestoreSelection, user common.User) (err error) {var entry VolumeBackupif err := s.db.WithContext(ctx).Where("id = ?", backupID).First(&entry).Error; err != nil {return err@@ -938,14 +1041,6 @@ func (s *VolumeService) RestoreBackupFiles(ctx context.Context, volumeName, backservice: s,volumeName: volumeName,})- cleanedPaths := make([]string, 0, len(paths))- for _, requestedPath := range paths {- cleaned, cleanErr := s.sanitizeBackupPathInternal(requestedPath)- if cleanErr != nil {- return cleanErr- }- cleanedPaths = append(cleanedPaths, cleaned)- }dockerClient, err := s.dockerService.GetClient(ctx)if err != nil {return err@@ -958,6 +1053,10 @@ func (s *VolumeService) RestoreBackupFiles(ctx context.Context, volumeName, backreturn err}}+ resolvedSelection, err := s.resolveVolumeBackupRestoreSelectionInternal(ctx, dockerClient, &entry, repository, snapshotID, selection)+ if err != nil {+ return err+ }stopped, err := s.stopRunningContainersForBackupInternal(ctx, dockerClient, volumeName, user, true)containersStopped := len(stopped) > 0defer func() {@@ -973,16 +1072,8 @@ func (s *VolumeService) RestoreBackupFiles(ctx context.Context, volumeName, backif err != nil {return fmt.Errorf("failed to create pre-restore backup: %w", err)}- if entry.Format == VolumeBackupFormatArchive {- if err := s.restoreArchiveBackupFilesInternal(ctx, dockerClient, volumeName, backupID, cleanedPaths); err != nil {- return err- }- } else {- for _, cleaned := range cleanedPaths {- if err := s.engine.RestoreSnapshot(ctx, dockerClient, repository, s.rusticPasswordInternal(), snapshotID, mount.Mount{Type: mount.TypeVolume, Source: volumeName, Target: "/volume"}, backup.RestoreOptions{SourcePath: cleaned, DestinationPath: path.Join("/volume", cleaned)}); err != nil {- return fmt.Errorf("failed to restore %s from Rustic snapshot: %w", cleaned, err)- }- }+ if err := s.restoreVolumeBackupSelectionInternal(ctx, dockerClient, volumeName, backupID, &entry, repository, snapshotID, resolvedSelection); err != nil {+ return err}if containersStopped {stopped, err = s.startContainersAfterBackupInternal(context.WithoutCancel(ctx), dockerClient, stopped, user)@@ -991,13 +1082,35 @@ func (s *VolumeService) RestoreBackupFiles(ctx context.Context, volumeName, backreturn err}}- metadata := database.JSON{"action": "backup_restore_files", "backup_id": backupID, "pre_restore_backupId": preBackup.ID, "paths_count": len(cleanedPaths)}+ metadata := database.JSON{"action": "backup_restore_files", "backup_id": backupID, "pre_restore_backupId": preBackup.ID, "paths_count": len(resolvedSelection.entries), "select_all": selection.SelectAll, "search": selection.Search}if logErr := s.eventService.LogVolumeEvent(ctx, event.EventTypeVolumeBackupRestoreFiles, volumeName, volumeName, user.ID, user.Username, "0", metadata); logErr != nil {slog.WarnContext(ctx, "could not log volume backup restore files event", "volume", volumeName, "error", logErr)}return nil}+func archiveMembersForSelectionInternal(paths []string, selected []backuptypes.BackupFileEntry) []string {+ files := make([]string, 0, len(paths))+ for _, raw := range paths {+ directory := strings.HasSuffix(strings.TrimSpace(raw), "/")+ cleaned, err := backupbrowser.NormalizePath(strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(raw), "./"), "/"), false)+ if err != nil {+ continue+ }+ for _, entry := range selected {+ if cleaned == entry.Path || (entry.IsDirectory && strings.HasPrefix(cleaned, entry.Path+"/")) {+ if directory {+ cleaned += "/"+ }+ files = append(files, cleaned)+ break+ }+ }+ }+ slices.Sort(files)+ return slices.Compact(files)+}+// DownloadBackup streams a backup as a tar.gz archive. Legacy archive rows// stream their stored file; local Rustic rows are restored into a scratch// volume and packaged on the fly.@@ -1232,6 +1345,20 @@ func (s *VolumeService) restoreArchiveBackupInternal(ctx context.Context, docker}func (s *VolumeService) listArchiveBackupFilesInternal(ctx context.Context, backupID string) ([]string, error) {+ paths, err := s.listArchiveBackupPathsInternal(ctx, backupID)+ if err != nil {+ return nil, err+ }+ files := make([]string, 0, len(paths))+ for _, candidate := range paths {+ if !strings.HasSuffix(candidate, "/") {+ files = append(files, candidate)+ }+ }+ return files, nil+}++func (s *VolumeService) listArchiveBackupPathsInternal(ctx context.Context, backupID string) ([]string, error) {filename, err := s.backupArchiveFilenameInternal(backupID)if err != nil {return nil, err@@ -1262,7 +1389,7 @@ func (s *VolumeService) listArchiveBackupFilesInternal(ctx context.Context, backcontinue}clean = strings.TrimPrefix(clean, "./")- if strings.HasSuffix(clean, "/") {+ if clean == "" {continue}if _, ok := seen[clean]; ok {diff --git a/backend/internal/volume/backup_file_browser_test.go b/backend/internal/volume/backup_file_browser_test.gonew file mode 100644index 000000000..6c26f1a95--- /dev/null+++ b/backend/internal/volume/backup_file_browser_test.go@@ -0,0 +1,18 @@+package volume++import (+ "testing"++ backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"+ "github.com/stretchr/testify/require"+)++func TestArchiveMembersForSelectionExpandsFoldersInternal(t *testing.T) {+ paths := []string{"./folder/", "./folder/a.txt", "./folder/nested/b.txt", "./other.txt"}+ selected := []backuptypes.BackupFileEntry{{Path: "folder", Name: "folder", IsDirectory: true}}+ require.Equal(+ t,+ []string{"folder/", "folder/a.txt", "folder/nested/b.txt"},+ archiveMembersForSelectionInternal(paths, selected),+ )+}diff --git a/backend/internal/volume/handler.go b/backend/internal/volume/handler.goindex 00ca31a14..e426a31f6 100644--- a/backend/internal/volume/handler.go+++ b/backend/internal/volume/handler.go@@ -27,6 +27,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/utils""github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"activitytypes "github.com/getarcaneapp/arcane/types/v2/activity"+ backuptypes "github.com/getarcaneapp/arcane/types/v2/backup""github.com/getarcaneapp/arcane/types/v2/base"uploadtypes "github.com/getarcaneapp/arcane/types/v2/upload"volumetypes "github.com/getarcaneapp/arcane/types/v2/volume"@@ -228,9 +229,7 @@ type RestoreBackupFilesInput struct {EnvironmentID string `path:"id" doc:"Environment ID"`VolumeName string `path:"volumeName" doc:"Volume name"`BackupID string `path:"backupId" doc:"Backup ID"`- Body struct {- Paths []string `json:"paths" doc:"Paths to restore from backup"`- }+ Body backuptypes.RestoreSelection}type RestoreBackupFilesOutput struct {@@ -260,6 +259,19 @@ type ListBackupFilesOutput struct {Body base.ApiResponse[[]string]}+type BrowseBackupFilesInput struct {+ EnvironmentID string `path:"id" doc:"Environment ID"`+ BackupID string `path:"backupId" doc:"Backup ID"`+ Path string `query:"path" doc:"Folder path relative to the backup root"`+ Search string `query:"search" doc:"Case-insensitive full-path search"`+ Start int `query:"start" default:"0" doc:"Start index for the page"`+ Limit int `query:"limit" default:"20" doc:"Requested page size"`+}++type BrowseBackupFilesOutput struct {+ Body base.Paginated[backuptypes.BackupFileEntry]+}+type DeleteBackupInput struct {EnvironmentID string `path:"id" doc:"Environment ID"`BackupID string `path:"backupId" doc:"Backup ID"`@@ -501,6 +513,15 @@ func RegisterVolumes(api huma.API, dockerService *docker.DockerClientService, voSecurity: handlerutil.DefaultOperationSecurity(),}, authz.PermVolumesRead, h.ListBackupFiles)+ middleware.RegisterWithPermission(api, huma.Operation{+ OperationID: "browse-volume-backup-files",+ Method: http.MethodGet,+ Path: "/environments/{id}/volumes/backups/{backupId}/files/browse",+ Summary: "Browse files in a volume backup",+ Tags: []string{"Volume Backup"},+ Security: handlerutil.DefaultOperationSecurity(),+ }, authz.PermVolumesRead, h.BrowseBackupFiles)+middleware.RegisterWithPermission(api, huma.Operation{OperationID: "upload-volume-backup",Method: http.MethodPost,@@ -1046,10 +1067,6 @@ func (h *VolumeHandler) RestoreBackupFiles(ctx context.Context, input *RestoreBareturn nil, err}- if len(input.Body.Paths) == 0 {- return nil, huma.Error400BadRequest("paths are required")- }-runtimeCtx := utils.ActivityRuntimeContext(ctx, h.appCtx)activityID, err := activitylib.RunHandlerActivity(runtimeCtx, h.activityService, activitylib.HandlerOptions{EnvironmentID: input.EnvironmentID,@@ -1062,13 +1079,18 @@ func (h *VolumeHandler) RestoreBackupFiles(ctx context.Context, input *RestoreBaMessage: "Restoring files from volume backup",SuccessMessage: "Restore initiated successfully",Metadata: database.JSON{- "action": "restore_volume_backup_files",- "backupId": input.BackupID,- "paths": input.Body.Paths,+ "action": "restore_volume_backup_files",+ "backupId": input.BackupID,+ "paths": input.Body.Paths,+ "selectAll": input.Body.SelectAll,+ "search": input.Body.Search,},}, func(runtimeCtx context.Context) error {- return h.volumeService.RestoreBackupFiles(runtimeCtx, input.VolumeName, input.BackupID, input.Body.Paths, *user)+ return h.volumeService.RestoreBackupFiles(runtimeCtx, input.VolumeName, input.BackupID, input.Body, *user)})+ if errors.Is(err, common.ErrBadRequest) {+ return nil, huma.Error400BadRequest(err.Error())+ }if err != nil {return nil, huma.Error500InternalServerError(err.Error())}@@ -1113,6 +1135,20 @@ func (h *VolumeHandler) ListBackupFiles(ctx context.Context, input *ListBackupFi}, nil}+func (h *VolumeHandler) BrowseBackupFiles(ctx context.Context, input *BrowseBackupFilesInput) (*BrowseBackupFilesOutput, error) {+ params := handlerutil.PaginationParams(input.Start, input.Limit, "", "", input.Search)+ items, page, err := h.volumeService.BrowseBackupFiles(ctx, input.BackupID, input.Path, params)+ if errors.Is(err, common.ErrBadRequest) {+ return nil, huma.Error400BadRequest(err.Error())+ }+ if err != nil {+ return nil, huma.Error500InternalServerError(err.Error())+ }+ return &BrowseBackupFilesOutput{Body: base.Paginated[backuptypes.BackupFileEntry]{+ Success: true, Data: items, Pagination: handlerutil.PaginationResponse(page),+ }}, nil+}+func (h *VolumeHandler) DeleteBackup(ctx context.Context, input *DeleteBackupInput) (*DeleteBackupOutput, error) {user, _ := common.CurrentUserFromContext(ctx)runtimeCtx := utils.ActivityRuntimeContext(ctx, h.appCtx)diff --git a/backend/pkg/libarcane/edge/commands.go b/backend/pkg/libarcane/edge/commands.goindex 04c3c9b84..7661b7841 100644--- a/backend/pkg/libarcane/edge/commands.go+++ b/backend/pkg/libarcane/edge/commands.go@@ -128,6 +128,7 @@ var commandRoutes = []commandRoute{{Method: http.MethodPut, PathPattern: "/api/environments/{id}/volumes/{volumeName}/backup-policy", CommandName: "volume.backup_policy.update"},{Method: http.MethodGet, PathPattern: "/api/environments/{id}/volumes/backups/{backupId}/has-path", CommandName: "volume.backup.has_path"},{Method: http.MethodGet, PathPattern: "/api/environments/{id}/volumes/backups/{backupId}/files", CommandName: "volume.backup.files"},+ {Method: http.MethodGet, PathPattern: "/api/environments/{id}/volumes/backups/{backupId}/files/browse", CommandName: "volume.backup.files_browse"},{Method: http.MethodPost, PathPattern: "/api/environments/{id}/volumes/backups/{backupId}/upload", CommandName: "volume.backup.upload"},{Method: http.MethodGet, PathPattern: "/api/environments/{id}/builds/browse", CommandName: "build_workspace.browse.list"},diff --git a/backend/pkg/utils/backupbrowser/browser.go b/backend/pkg/utils/backupbrowser/browser.gonew file mode 100644index 000000000..696c6b7d3--- /dev/null+++ b/backend/pkg/utils/backupbrowser/browser.go@@ -0,0 +1,244 @@+// Package backupbrowser provides deterministic backup-tree construction,+// paging, and restore-selection validation.+package backupbrowser++import (+ "cmp"+ "fmt"+ "path"+ "slices"+ "sort"+ "strings"++ "emperror.dev/errors"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils"+ backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"+)++// NormalizePath validates and normalizes a path relative to a backup root.+func NormalizePath(value string, allowEmpty bool) (string, error) {+ trimmed := strings.TrimSpace(strings.ReplaceAll(value, `\`, "/"))+ if trimmed == "" || path.Clean(trimmed) == "." {+ if allowEmpty {+ return "", nil+ }+ return "", errors.New("backup path is required")+ }+ // Reject ".." segments before cleaning: "a/../b" must not sneak through as "b".+ if slices.Contains(strings.Split(trimmed, "/"), "..") {+ return "", errors.New("backup path must stay within the restore root")+ }+ cleaned, err := utils.NormalizeRelativePath(trimmed)+ if err != nil {+ return "", errors.New("backup path must stay within the restore root")+ }+ return cleaned, nil+}++// ListScope resolves the snapshot listing scope for one browse request: the+// whole tree when searching, otherwise just the requested folder.+func ListScope(requestedPath string, params pagination.QueryParams) (listPath string, recursive bool, err error) {+ browsePath, err := NormalizePath(requestedPath, true)+ if err != nil || params.Start < 0 {+ return "", false, errors.New("invalid browse path or start")+ }+ if strings.TrimSpace(params.Search) != "" {+ return "", true, nil+ }+ return browsePath, false, nil+}++// BuildEntries converts archive or Rustic path output into a synthesized tree.+// Paths are returned relative to the supplied logical root.+func BuildEntries(paths []string, browsePath string, recursive bool) []backuptypes.BackupFileEntry {+ root, err := NormalizePath(browsePath, true)+ if err != nil {+ return nil+ }+ entries := make(map[string]backuptypes.BackupFileEntry)+ for _, raw := range paths {+ directory := strings.HasSuffix(strings.TrimSpace(raw), "/")+ candidate, ok := normalizeListedPathInternal(raw)+ if !ok {+ continue+ }+ relative := candidate+ if root != "" {+ if candidate == root || !utils.FilePathMatches(candidate, root) {+ continue+ }+ relative = strings.TrimPrefix(candidate, root+"/")+ }+ segments := strings.Split(relative, "/")+ if len(segments) == 0 || segments[0] == "" {+ continue+ }+ if !recursive && len(segments) > 1 {+ addEntryInternal(entries, path.Join(root, segments[0]), true)+ continue+ }+ if recursive {+ for index := 1; index < len(segments); index++ {+ addEntryInternal(entries, path.Join(root, path.Join(segments[:index]...)), true)+ }+ }+ addEntryInternal(entries, candidate, directory)+ }+ result := make([]backuptypes.BackupFileEntry, 0, len(entries))+ for _, entry := range entries {+ result = append(result, entry)+ }+ slices.SortStableFunc(result, compareEntriesInternal)+ return result+}++func compareEntriesInternal(left, right backuptypes.BackupFileEntry) int {+ if left.IsDirectory != right.IsDirectory {+ if left.IsDirectory {+ return -1+ }+ return 1+ }+ if result := cmp.Compare(strings.ToLower(left.Name), strings.ToLower(right.Name)); result != 0 {+ return result+ }+ if result := cmp.Compare(strings.ToLower(left.Path), strings.ToLower(right.Path)); result != 0 {+ return result+ }+ return cmp.Compare(left.Path, right.Path)+}++// Browse searches, orders, and paginates backup entries using the standard pagination contract.+func Browse(entries []backuptypes.BackupFileEntry, params pagination.QueryParams) ([]backuptypes.BackupFileEntry, pagination.Response) {+ result := pagination.SearchOrderAndPaginate(entries, params, pagination.Config[backuptypes.BackupFileEntry]{+ SearchAccessors: []pagination.SearchAccessor[backuptypes.BackupFileEntry]{+ func(entry backuptypes.BackupFileEntry) (string, error) { return entry.Path, nil },+ },+ SortBindings: []pagination.SortBinding[backuptypes.BackupFileEntry]{+ {Key: "path", Fn: compareEntriesInternal},+ },+ })+ return result.Items, pagination.BuildResponse(result.TotalCount, result.TotalAvailable, params)+}++// NormalizeSelection validates explicit selections against eligible entries,+// deduplicates them, and removes descendants covered by selected directories.+func NormalizeSelection(selection backuptypes.RestoreSelection, eligible []backuptypes.BackupFileEntry) ([]backuptypes.BackupFileEntry, error) {+ if selection.SelectAll && len(selection.Paths) > 0 {+ return nil, errors.New("selectAll cannot be combined with explicit paths")+ }+ if !selection.SelectAll && len(selection.Paths) == 0 {+ return nil, errors.New("paths are required")+ }+ candidates, err := selectionCandidatesInternal(selection, eligible)+ if err != nil {+ return nil, err+ }+ return collapseSelectionCandidatesInternal(candidates), nil+}++func selectionCandidatesInternal(selection backuptypes.RestoreSelection, eligible []backuptypes.BackupFileEntry) ([]backuptypes.BackupFileEntry, error) {+ if selection.SelectAll {+ query := strings.ToLower(strings.TrimSpace(selection.Search))+ candidates := make([]backuptypes.BackupFileEntry, 0, len(eligible))+ for _, entry := range eligible {+ if query == "" || strings.Contains(strings.ToLower(entry.Path), query) {+ candidates = append(candidates, entry)+ }+ }+ if len(candidates) == 0 {+ return nil, errors.New("no backup paths match the selection")+ }+ return candidates, nil+ }+ return explicitSelectionCandidatesInternal(selection.Paths, eligible)+}++func explicitSelectionCandidatesInternal(requestedPaths []string, eligible []backuptypes.BackupFileEntry) ([]backuptypes.BackupFileEntry, error) {+ byPath := make(map[string]backuptypes.BackupFileEntry, len(eligible))+ for _, entry := range eligible {+ byPath[entry.Path] = entry+ }+ candidates := make([]backuptypes.BackupFileEntry, 0, len(requestedPaths))+ seen := make(map[string]struct{}, len(requestedPaths))+ for _, requested := range requestedPaths {+ expectsDirectory := strings.HasSuffix(strings.TrimSpace(strings.ReplaceAll(requested, `\`, "/")), "/")+ normalized, err := NormalizePath(requested, false)+ if err != nil {+ return nil, err+ }+ entry, exists := byPath[normalized]+ if !exists {+ return nil, fmt.Errorf("%s does not exist in this backup", normalized)+ }+ if expectsDirectory && !entry.IsDirectory {+ return nil, fmt.Errorf("%s is a file, not a folder", normalized)+ }+ if _, exists := seen[normalized]; exists {+ continue+ }+ seen[normalized] = struct{}{}+ candidates = append(candidates, entry)+ }+ return candidates, nil+}++func collapseSelectionCandidatesInternal(candidates []backuptypes.BackupFileEntry) []backuptypes.BackupFileEntry {+ sort.SliceStable(candidates, func(i, j int) bool {+ leftDepth := strings.Count(candidates[i].Path, "/")+ rightDepth := strings.Count(candidates[j].Path, "/")+ if leftDepth != rightDepth {+ return leftDepth < rightDepth+ }+ leftPath := strings.ToLower(candidates[i].Path)+ rightPath := strings.ToLower(candidates[j].Path)+ if leftPath != rightPath {+ return leftPath < rightPath+ }+ return candidates[i].Path < candidates[j].Path+ })+ result := make([]backuptypes.BackupFileEntry, 0, len(candidates))+ selectedDirectories := make([]string, 0, len(candidates))+ for _, candidate := range candidates {+ covered := false+ for _, directory := range selectedDirectories {+ if strings.HasPrefix(candidate.Path, directory+"/") {+ covered = true+ break+ }+ }+ if covered {+ continue+ }+ result = append(result, candidate)+ if candidate.IsDirectory {+ selectedDirectories = append(selectedDirectories, candidate.Path)+ }+ }+ return result+}++func normalizeListedPathInternal(raw string) (string, bool) {+ trimmed := strings.TrimSpace(strings.ReplaceAll(raw, `\`, "/"))+ cleaned, err := NormalizePath(strings.TrimPrefix(strings.TrimSuffix(trimmed, "/"), "/"), false)+ if err != nil {+ return "", false+ }+ return cleaned, true+}++func addEntryInternal(entries map[string]backuptypes.BackupFileEntry, entryPath string, directory bool) {+ if entryPath == "" || entryPath == "." {+ return+ }+ current, exists := entries[entryPath]+ if exists && (current.IsDirectory || !directory) {+ return+ }+ entries[entryPath] = backuptypes.BackupFileEntry{+ Path: entryPath,+ Name: path.Base(entryPath),+ IsDirectory: directory,+ }+}diff --git a/backend/pkg/utils/backupbrowser/browser_test.go b/backend/pkg/utils/backupbrowser/browser_test.gonew file mode 100644index 000000000..aa1f8fac6--- /dev/null+++ b/backend/pkg/utils/backupbrowser/browser_test.go@@ -0,0 +1,90 @@+package backupbrowser++import (+ "testing"++ "github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"+ backuptypes "github.com/getarcaneapp/arcane/types/v2/backup"+ "github.com/stretchr/testify/require"+)++func TestBuildEntriesSynthesizesAndSortsDirectoriesInternal(t *testing.T) {+ entries := BuildEntries([]string{"z.txt", "alpha/nested/file.txt", "Beta/", "a.txt"}, "", false)+ require.Equal(t, []backuptypes.BackupFileEntry{+ {Path: "alpha", Name: "alpha", IsDirectory: true},+ {Path: "Beta", Name: "Beta", IsDirectory: true},+ {Path: "a.txt", Name: "a.txt"},+ {Path: "z.txt", Name: "z.txt"},+ }, entries)+}++func TestBuildEntriesNestedBrowseInternal(t *testing.T) {+ entries := BuildEntries([]string{"folder/nested/", "folder/file.txt"}, "folder", false)+ require.Equal(t, []backuptypes.BackupFileEntry{+ {Path: "folder/nested", Name: "nested", IsDirectory: true},+ {Path: "folder/file.txt", Name: "file.txt"},+ }, entries)+}++func TestBrowseWalkReachesEveryEntryInternal(t *testing.T) {+ entries := BuildEntries([]string{"d/", "a.txt", "b.txt", "c.txt", "d/e.txt"}, "", true)+ var walked []backuptypes.BackupFileEntry+ start := 0+ for {+ items, page := Browse(entries, pagination.QueryParams{Params: pagination.Params{Start: start, Limit: 2}})+ walked = append(walked, items...)+ start += len(items)+ if int64(start) >= page.TotalItems {+ break+ }+ }+ require.Equal(t, entries, walked)+}++func TestBrowseSearchOrderingAndPaginationInternal(t *testing.T) {+ entries := []backuptypes.BackupFileEntry{+ {Path: "zeta/file.txt", Name: "file.txt"},+ {Path: "Alpha", Name: "Alpha", IsDirectory: true},+ {Path: "alpha/Second.txt", Name: "Second.txt"},+ {Path: "alpha/first.txt", Name: "first.txt"},+ }+ params := pagination.QueryParams{+ SearchQuery: pagination.SearchQuery{Search: "ALPHA"},+ Params: pagination.Params{Start: 1, Limit: 1},+ }+ items, page := Browse(entries, params)+ require.Equal(t, []backuptypes.BackupFileEntry{{Path: "alpha/first.txt", Name: "first.txt"}}, items)+ require.Equal(t, int64(3), page.TotalItems)+ require.Equal(t, int64(4), page.GrandTotalItems)+ require.Equal(t, 2, page.CurrentPage)+ require.Equal(t, 1, page.ItemsPerPage)++ items, page = Browse(entries, pagination.QueryParams{Params: pagination.Params{Start: 10, Limit: 2}})+ require.Empty(t, items)+ require.Equal(t, int64(4), page.TotalItems)+}++func TestNormalizeSelectionInternal(t *testing.T) {+ eligible := BuildEntries([]string{"folder/a.txt", "folder/nested/b.txt", "other.txt"}, "", true)+ selected, err := NormalizeSelection(backuptypes.RestoreSelection{Paths: []string{"folder/nested/b.txt", "folder", "folder/a.txt", "folder"}}, eligible)+ require.NoError(t, err)+ require.Equal(t, []backuptypes.BackupFileEntry{{Path: "folder", Name: "folder", IsDirectory: true}}, selected)++ _, err = NormalizeSelection(backuptypes.RestoreSelection{Paths: []string{"../arcane.db"}}, eligible)+ require.ErrorContains(t, err, "restore root")+ _, err = NormalizeSelection(backuptypes.RestoreSelection{Paths: []string{"missing"}}, eligible)+ require.ErrorContains(t, err, "does not exist")+ _, err = NormalizeSelection(backuptypes.RestoreSelection{Paths: []string{"other.txt/"}}, eligible)+ require.ErrorContains(t, err, "not a folder")+ _, err = NormalizeSelection(backuptypes.RestoreSelection{Paths: []string{"other.txt"}, SelectAll: true}, eligible)+ require.ErrorContains(t, err, "cannot be combined")+ _, err = NormalizeSelection(backuptypes.RestoreSelection{}, eligible)+ require.ErrorContains(t, err, "paths are required")+}++func TestNormalizeSearchSelectionCoversDescendantsInternal(t *testing.T) {+ eligible := BuildEntries([]string{"match/a.txt", "match/nested/b.txt", "other.txt"}, "", true)+ selected, err := NormalizeSelection(backuptypes.RestoreSelection{SelectAll: true, Search: "match"}, eligible)+ require.NoError(t, err)+ require.Equal(t, []backuptypes.BackupFileEntry{{Path: "match", Name: "match", IsDirectory: true}}, selected)+}diff --git a/backend/pkg/utils/path_util.go b/backend/pkg/utils/path_util.goindex e2aba30cf..505ec6c8a 100644--- a/backend/pkg/utils/path_util.go+++ b/backend/pkg/utils/path_util.go@@ -3,6 +3,7 @@ package utilsimport ("hash""io"+ "net/url""path""path/filepath""strconv"@@ -11,6 +12,20 @@ import ("emperror.dev/errors")+// SQLitePathFromDSN returns the filesystem portion of a SQLite file DSN.+// For "file:data/arcane.db?..." the path is carried in Opaque; for+// "file:/abs/path.db" it is in Path. In-memory DSNs yield ":memory:" or "".+func SQLitePathFromDSN(dsn string) (string, error) {+ parsed, err := url.Parse(dsn)+ if err != nil {+ return "", err+ }+ if parsed.Opaque != "" {+ return parsed.Opaque, nil+ }+ return parsed.Path, nil+}+// NormalizeRelativePath validates and normalizes a slash-delimited path rooted// at a managed file tree. The returned path never begins with a slash.func NormalizeRelativePath(input string) (string, error) {diff --git a/frontend/messages/en.json b/frontend/messages/en.jsonindex 78635c364..b837a74d8 100644--- a/frontend/messages/en.json+++ b/frontend/messages/en.json@@ -955,6 +955,10 @@"volumes_backup_restore_in_use_warning": "\n\nThis volume is currently in use by {count} container(s). Arcane will stop them before creating the safety backup and restart them after the restore.","volumes_backup_restore_success": "Volume restored successfully. A safety backup was created before restoring.","volumes_backup_restore_files_success": "{count} file(s) restored successfully. A safety backup was created before restoring.",+ "volumes_backup_restore_selection_success": "The selected backup content was restored successfully. A safety backup was created before restoring.",+ "volumes_backup_restore_discard_changes": "You have unsaved workspace changes. Continuing will discard all unsaved text and staged file operations after the restore succeeds.",+ "backup_file_browser_select_all_matches": "Select all search matches",+ "backup_file_browser_load_remaining_failed": "The remaining backup files could not be loaded.","volumes_backup_safety_info": "A safety backup will be created automatically before restoring, so you can undo this operation if needed.","volume_backup_restore_files_lifecycle_info": "Arcane will stop containers using this volume, create a safety backup, restore the selected files, and then restart the containers.","volumes_backup_overwrite_warning": "Selected files will overwrite existing files at those paths.",@@ -3265,6 +3269,12 @@"system_backups_restore_description": "Arcane will create a safety backup, stop itself, restore all data and runtime secrets, recreate its container, and start the recovered instance.","system_backups_restore_started": "Arcane restore started. This page will disconnect while Arcane restarts.","system_backups_restore_failed": "Failed to start the Arcane restore.",+ "system_backups_restore_files_description": "Select project files from this backup to restore.",+ "system_backups_restore_files_lifecycle_info": "Arcane will create a complete local safety backup and then overwrite the selected project files. Arcane and project containers will keep running.",+ "system_backups_load_files": "Load files",+ "system_backups_restore_files_success": "{count} project file(s) restored successfully. A safety backup was created before restoring.",+ "system_backups_restore_selection_success": "The selected project content was restored successfully. A safety backup was created before restoring.",+ "system_backups_restore_files_failed": "Failed to restore project files from the system backup.","system_backups_upload_title": "Upload system backup","system_backups_delete_title": "Delete system backup?","system_backups_delete_description": "The Rustic snapshot will be removed from every reachable storage destination.",diff --git a/frontend/src/lib/components/arcane-table/arcane-table-desktop-view.svelte b/frontend/src/lib/components/arcane-table/arcane-table-desktop-view.svelteindex 10286b7c8..0b79aa9da 100644--- a/frontend/src/lib/components/arcane-table/arcane-table-desktop-view.svelte+++ b/frontend/src/lib/components/arcane-table/arcane-table-desktop-view.svelte@@ -1,7 +1,7 @@<script lang="ts" generics="TData extends Record<string, any> & { id: string }">import type { ArcaneCell, ArcaneFeatures, ArcaneRow, ArcaneSvelteTable } from './table-features';import { FlexRender as FlexRenderBase } from '@tanstack/svelte-table';- import { createVirtualizer } from './virtualizer.svelte';+ import { createVirtualizer } from '../ui/virtualizer.svelte';import Skeleton from '#lib/components/ui/skeleton/skeleton.svelte';import * as Table from '#lib/components/ui/table/index.js';import { ArrowRightIcon, ArrowDownIcon } from '#lib/icons';diff --git a/frontend/src/lib/components/backup-file-picker.svelte b/frontend/src/lib/components/backup-file-picker.sveltenew file mode 100644index 000000000..8308ad1db--- /dev/null+++ b/frontend/src/lib/components/backup-file-picker.svelte@@ -0,0 +1,322 @@+<script lang="ts">+ import { ArcaneButton } from '#lib/components/arcane-button';+ import FileTreeRow from '#lib/components/file-tree-row.svelte';+ import { Input } from '#lib/components/ui/input';+ import { Spinner } from '#lib/components/ui/spinner';+ import { createVirtualizer } from '#lib/components/ui/virtualizer.svelte';+ import type { BackupFileEntry, BackupFileProvider } from '#lib/types/backup';+ import * as m from '#lib/paraglide/messages.js';++ type FolderPageState = {+ entries: BackupFileEntry[];+ continuationStart?: number;+ loading: boolean;+ error: boolean;+ requestID: number;+ };++ type PickerRow =+ | { kind: 'entry'; entry: BackupFileEntry; depth: number }+ | { kind: 'continuation'; folder: string; depth: number };++ let {+ provider,+ selectedPaths = $bindable([]),+ selectAll = $bindable(false),+ search = $bindable('')+ }: {+ provider: BackupFileProvider;+ selectedPaths?: string[];+ selectAll?: boolean;+ search?: string;+ } = $props();++ let pages = $state<Record<string, FolderPageState>>({});+ let expandedFolders = $state<Set<string>>(new Set());+ let selectedDirectoryPaths = $state<Set<string>>(new Set());+ let debouncedSearch = $state('');+ let selectionSearch = $state('');+ let generation = $state(0);+ let nextRequestID = 0;+ let initializedProvider: BackupFileProvider | null = null;+ let scrollElement = $state<HTMLElement | null>(null);++ const selectedSet = $derived(new Set(selectedPaths));+ const searchActive = $derived(debouncedSearch.length > 0);+ const rows = $derived.by<PickerRow[]>(() => {+ const result: PickerRow[] = [];+ appendFolderRowsInternal(result, '', 0);+ return result;+ });+ const rootLoading = $derived(pages['']?.loading === true && pages['']?.entries.length === 0);+ const rootEmpty = $derived(+ pages[''] !== undefined && !pages['']?.loading && !pages['']?.error && pages['']?.entries.length === 0+ );++ const rowVirtualizer = createVirtualizer<HTMLElement, HTMLDivElement>(() => ({+ count: rows.length,+ getScrollElement: () => scrollElement,+ getItemKey: (index) => {+ const row = rows[index];+ return row?.kind === 'entry' ? `entry:${row.entry.path}` : `continuation:${row?.folder ?? ''}`;+ },+ estimateSize: () => 34,+ overscan: 8+ }));++ function pageStateInternal(folder: string): FolderPageState {+ return pages[folder] ?? { entries: [], loading: false, error: false, requestID: 0 };+ }++ function updatePageInternal(folder: string, state: FolderPageState) {+ pages = { ...pages, [folder]: state };+ }++ function appendFolderRowsInternal(result: PickerRow[], folder: string, depth: number) {+ const state = pages[folder];+ if (!state) return;+ for (const entry of state.entries) {+ result.push({ kind: 'entry', entry, depth });+ if (!searchActive && entry.isDirectory && expandedFolders.has(entry.path)) {+ appendFolderRowsInternal(result, entry.path, depth + 1);+ }+ }+ const initialNestedFolderLoad = folder !== '' && state.loading && state.entries.length === 0;+ if (!initialNestedFolderLoad && (state.loading || state.error || state.continuationStart !== undefined)) {+ result.push({ kind: 'continuation', folder, depth });+ }+ }++ function folderLoadingInternal(folder: string): boolean {+ const state = pages[folder];+ return state?.loading === true && state.entries.length === 0;+ }++ function resetPagesInternal() {+ generation += 1;+ pages = {};+ expandedFolders = new Set();+ void loadPageInternal('', 0, true, generation);+ }++ async function loadPageInternal(folder: string, start: number, replace: boolean, requestGeneration = generation) {+ const current = pageStateInternal(folder);+ if (current.loading) return;+ const requestID = ++nextRequestID;+ updatePageInternal(folder, { ...current, loading: true, error: false, requestID });+ try {+ const page = await provider.browse({+ path: searchActive ? undefined : folder,+ search: searchActive ? debouncedSearch : undefined,+ start: start > 0 ? start : undefined+ });+ const latest = pages[folder];+ if (generation !== requestGeneration || latest?.requestID !== requestID) return;+ const merged = replace ? page.data : [...latest.entries, ...page.data];+ const unique = Array.from(new Map(merged.map((entry) => [entry.path, entry])).values());+ const continuationStart = start + page.data.length;+ updatePageInternal(folder, {+ entries: unique,+ continuationStart: continuationStart < page.pagination.totalItems ? continuationStart : undefined,+ loading: false,+ error: false,+ requestID+ });+ } catch {+ const latest = pages[folder];+ if (generation !== requestGeneration || latest?.requestID !== requestID) return;+ updatePageInternal(folder, { ...latest, loading: false, error: true });+ }+ }++ function toggleFolderInternal(folder: string) {+ if (searchActive) return;+ const next = new Set(expandedFolders);+ if (next.has(folder)) {+ next.delete(folder);+ expandedFolders = next;+ return;+ }+ next.add(folder);+ expandedFolders = next;+ if (!pages[folder]) void loadPageInternal(folder, 0, true);+ }++ function coveringAncestorInternal(entryPath: string): string | undefined {+ for (const candidate of selectedSet) {+ if (selectedDirectoryPaths.has(candidate) && entryPath.startsWith(candidate + '/')) return candidate;+ }+ return undefined;+ }++ function entryCheckedInternal(entry: BackupFileEntry): boolean {+ return selectAll || selectedSet.has(entry.path) || coveringAncestorInternal(entry.path) !== undefined;+ }++ function entryIndeterminateInternal(entry: BackupFileEntry): boolean {+ if (!entry.isDirectory || entryCheckedInternal(entry)) return false;+ return selectedPaths.some((candidate) => candidate.startsWith(entry.path + '/'));+ }++ function toggleEntryInternal(entry: BackupFileEntry, checked: boolean) {+ if (selectAll || coveringAncestorInternal(entry.path)) return;+ const next = new Set(selectedPaths);+ if (!checked) {+ next.delete(entry.path);+ selectedPaths = [...next];+ return;+ }+ if (entry.isDirectory) {+ for (const candidate of next) {+ if (candidate.startsWith(entry.path + '/')) next.delete(candidate);+ }+ const directories = new Set(selectedDirectoryPaths);+ directories.add(entry.path);+ selectedDirectoryPaths = directories;+ }+ next.add(entry.path);+ selectedPaths = [...next];+ }++ function selectAllInternal() {+ selectedPaths = [];+ selectAll = true;+ selectionSearch = search.trim();+ }++ function clearSelectionInternal() {+ selectedPaths = [];+ selectAll = false;+ selectionSearch = '';+ }++ // A new provider means a new backup tree: the picker owns clearing its+ // selection and search state so every parent dialog doesn't have to.+ $effect(() => {+ const currentProvider = provider;+ if (initializedProvider === currentProvider) return;+ initializedProvider = currentProvider;+ selectedPaths = [];+ selectAll = false;+ selectionSearch = '';+ selectedDirectoryPaths = new Set();+ search = '';+ debouncedSearch = '';+ resetPagesInternal();+ });++ $effect(() => {+ const query = search.trim();+ if (selectAll && query !== selectionSearch) {+ selectAll = false;+ selectionSearch = '';+ }+ if (query === debouncedSearch) return;+ const timeout = window.setTimeout(() => {+ debouncedSearch = query;+ resetPagesInternal();+ }, 250);+ return () => window.clearTimeout(timeout);+ });++ $effect(() => {+ for (const virtualItem of rowVirtualizer.virtualItems) {+ const row = rows[virtualItem.index];+ if (row?.kind !== 'continuation') continue;+ const state = pages[row.folder];+ if (state?.continuationStart !== undefined && !state.loading && !state.error) {+ void loadPageInternal(row.folder, state.continuationStart, false);+ }+ }+ });+</script>++<div class="space-y-3">+ <div class="flex items-center justify-between gap-2">+ <Input class="h-9" placeholder={m.volume_search_files()} bind:value={search} />+ <div class="flex items-center gap-2">+ <ArcaneButton+ action="base"+ tone="ghost"+ size="sm"+ onclick={selectAllInternal}+ customLabel={search.trim() ? m.backup_file_browser_select_all_matches() : m.common_select_all()}+ />+ <ArcaneButton action="base" tone="ghost" size="sm" onclick={clearSelectionInternal} customLabel={m.common_clear()} />+ </div>+ </div>++ <div bind:this={scrollElement} class="h-64 overflow-auto rounded-md border p-1" data-backup-file-tree>+ {#if rootLoading}+ <div class="flex items-center justify-center py-8">+ <Spinner class="size-5 text-muted-foreground" />+ </div>+ {:else if rootEmpty}+ <div class="flex items-center justify-center py-8 text-sm text-muted-foreground">+ {m.volume_backup_no_files()}+ </div>+ {:else}+ <div class="relative min-w-max" style={`height: ${rowVirtualizer.totalSize}px`}>+ {#each rowVirtualizer.virtualItems as virtualItem (virtualItem.key)}+ {@const row = rows[virtualItem.index]}+ {#if row}+ <div+ class="absolute top-0 left-0 w-full"+ style={`transform: translateY(${virtualItem.start}px)`}+ data-index={virtualItem.index}+ use:rowVirtualizer.measureElement+ >+ {#if row.kind === 'entry'}+ <FileTreeRow+ name={searchActive ? row.entry.path : row.entry.name}+ path={row.entry.path}+ depth={searchActive ? 0 : row.depth}+ isDirectory={row.entry.isDirectory}+ expanded={expandedFolders.has(row.entry.path)}+ showDisclosure={!searchActive}+ selectable+ checked={entryCheckedInternal(row.entry)}+ indeterminate={entryIndeterminateInternal(row.entry)}+ disabled={selectAll || coveringAncestorInternal(row.entry.path) !== undefined}+ loading={row.entry.isDirectory && expandedFolders.has(row.entry.path) && folderLoadingInternal(row.entry.path)}+ expandLabel={m.workspace_file_expand_folder({ name: row.entry.name })}+ collapseLabel={m.workspace_file_collapse_folder({ name: row.entry.name })}+ onToggle={() => toggleFolderInternal(row.entry.path)}+ onActivate={() =>+ row.entry.isDirectory+ ? toggleFolderInternal(row.entry.path)+ : toggleEntryInternal(row.entry, !entryCheckedInternal(row.entry))}+ onCheckedChange={(checked) => toggleEntryInternal(row.entry, checked)}+ />+ {:else}+ {@const state = pages[row.folder]}+ <div+ class="flex min-h-8 items-center gap-1.5 pr-2 text-xs text-muted-foreground"+ style={`padding-left: ${0.5 + row.depth * 1}rem`}+ >+ {#if state?.error}+ <span>{m.backup_file_browser_load_remaining_failed()}</span>+ <ArcaneButton+ action="base"+ tone="ghost"+ size="sm"+ customLabel={m.common_retry()}+ onclick={() => loadPageInternal(row.folder, state.continuationStart ?? 0, state.entries.length === 0)}+ />+ {:else}+ {#if !searchActive}+ <span class="inline-flex size-4 shrink-0"></span>+ {/if}+ <span class="inline-flex size-4 shrink-0 items-center justify-center">+ <Spinner class="size-4" />+ </span>+ {/if}+ </div>+ {/if}+ </div>+ {/if}+ {/each}+ </div>+ {/if}+ </div>+</div>diff --git a/frontend/src/lib/components/file-tree-row.svelte b/frontend/src/lib/components/file-tree-row.sveltenew file mode 100644index 000000000..2dee7a50a--- /dev/null+++ b/frontend/src/lib/components/file-tree-row.svelte@@ -0,0 +1,120 @@+<script lang="ts">+ import type { Snippet } from 'svelte';+ import * as Checkbox from '#lib/components/ui/checkbox';+ import { Spinner } from '#lib/components/ui/spinner';+ import { ArrowDownIcon, ArrowRightIcon, FileTextIcon, FolderOpenIcon, LockIcon } from '#lib/icons';+ import { cn } from '#lib/utils';++ let {+ name,+ path,+ depth,+ isDirectory,+ expanded = false,+ showDisclosure = true,+ selected = false,+ selectable = false,+ checked = false,+ indeterminate = false,+ disabled = false,+ loading = false,+ pending = false,+ locked = false,+ expandLabel,+ collapseLabel,+ lockedLabel,+ pendingLabel,+ onToggle,+ onActivate,+ onCheckedChange,+ trailing+ }: {+ name: string;+ path: string;+ depth: number;+ isDirectory: boolean;+ expanded?: boolean;+ showDisclosure?: boolean;+ selected?: boolean;+ selectable?: boolean;+ checked?: boolean;+ indeterminate?: boolean;+ disabled?: boolean;+ loading?: boolean;+ pending?: boolean;+ locked?: boolean;+ expandLabel: string;+ collapseLabel: string;+ lockedLabel?: string;+ pendingLabel?: string;+ onToggle?: () => void;+ onActivate?: () => void;+ onCheckedChange?: (checked: boolean) => void;+ trailing?: Snippet;+ } = $props();+</script>++<div+ class={cn(+ 'group flex min-h-8 w-full items-center gap-1.5 rounded-md pr-2 text-[13px] hover:bg-accent',+ selected && 'bg-accent'+ )}+ style={`padding-left: ${0.5 + depth * 1}rem`}+ data-path={path}+ data-directory={isDirectory}+>+ {#if isDirectory && showDisclosure}+ <button+ type="button"+ class="inline-flex size-4 shrink-0 items-center justify-center rounded hover:bg-muted"+ aria-label={expanded ? collapseLabel : expandLabel}+ onclick={onToggle}+ >+ {#if expanded}+ <ArrowDownIcon class="size-3.5" />+ {:else}+ <ArrowRightIcon class="size-3.5" />+ {/if}+ </button>+ {:else if showDisclosure}+ <span class="inline-flex size-4 shrink-0 items-center justify-center"></span>+ {/if}++ {#if selectable}+ <Checkbox.Root+ {checked}+ {indeterminate}+ {disabled}+ aria-label={name}+ onCheckedChange={(value) => onCheckedChange?.(!!value)}+ />+ {/if}++ <button+ type="button"+ class="flex min-w-0 flex-1 items-center gap-1.5 py-1 text-left disabled:cursor-default"+ disabled={disabled && !isDirectory}+ title={path}+ onclick={onActivate}+ >+ {#if isDirectory}+ <span class="relative size-4 shrink-0">+ <FolderOpenIcon class={cn('size-4 text-amber-500', loading && 'opacity-40')} />+ {#if loading}+ <Spinner aria-hidden="true" class="absolute inset-0 size-4 text-foreground" />+ {/if}+ </span>+ {:else}+ <FileTextIcon class="size-4 shrink-0 text-muted-foreground" />+ {/if}+ <span class="min-w-0 flex-1 truncate">{name}</span>+ {#if pending}+ <span class="size-1.5 shrink-0 rounded-full bg-primary" role="img" aria-label={pendingLabel} title={pendingLabel}></span>+ {/if}+ </button>++ {#if locked}+ <LockIcon class="mx-1 size-3.5 shrink-0 text-muted-foreground" aria-label={lockedLabel} />+ {/if}+ {@render trailing?.()}+</div>diff --git a/frontend/src/lib/components/arcane-table/virtualizer.svelte.ts b/frontend/src/lib/components/ui/virtualizer.svelte.tssimilarity index 88%rename from frontend/src/lib/components/arcane-table/virtualizer.svelte.tsrename to frontend/src/lib/components/ui/virtualizer.svelte.tsindex d294c54c1..36236e2ec 100644--- a/frontend/src/lib/components/arcane-table/virtualizer.svelte.ts+++ b/frontend/src/lib/components/ui/virtualizer.svelte.ts@@ -9,7 +9,7 @@ import {// The three platform adapters (`observeElementRect` / `observeElementOffset` / `scrollToFn`) are// supplied for the caller, so they only provide the data-driven options (count, getScrollElement,-// estimateSize, …).+// estimateSize, and so on).type SuppliedKeys = 'observeElementRect' | 'observeElementOffset' | 'scrollToFn';type CreateVirtualizerOptions<TScroll extends Element, TItem extends Element> = Omit<VirtualizerOptions<TScroll, TItem>,@@ -21,10 +21,10 @@ type CreateVirtualizerOptions<TScroll extends Element, TItem extends Element> =* A Svelte 5 runes wrapper around `@tanstack/virtual-core`.** We wrap the framework-agnostic core directly (rather than `@tanstack/svelte-virtual`, whose v3- * adapter still uses Svelte 4 stores and mis-tracks the scroll-element binding under runes) — the- * same approach the repo took for `@tanstack/table-core`. `onChange` pushes the recomputed window+ * adapter still uses Svelte 4 stores and mis-tracks the scroll-element binding under runes), using+ * the same approach the repo took for `@tanstack/table-core`. `onChange` pushes the recomputed window* into `$state`, and `$effect.pre` re-applies options whenever the reactive inputs (row count,- * scroll element, …) change.+ * scroll element, and related values) change.** Pass `options` as a thunk so its reactive reads are tracked.*/@@ -71,7 +71,7 @@ export function createVirtualizer<TScroll extends Element, TItem extends Elementget totalSize() {return totalSize;},- /** `use:` action target — measures real row heights for accurate offsets. */+ /** `use:` action target that measures real row heights for accurate offsets. */measureElement: (node: TItem) => instance.measureElement(node)};}diff --git a/frontend/src/lib/components/workspace-file-tree-panel.svelte b/frontend/src/lib/components/workspace-file-tree-panel.svelteindex 7d648c426..ba6926586 100644--- a/frontend/src/lib/components/workspace-file-tree-panel.svelte+++ b/frontend/src/lib/components/workspace-file-tree-panel.svelte@@ -1,11 +1,13 @@<script lang="ts">import { ArcaneButton } from '#lib/components/arcane-button/index.js';+ import FileTreeRow from '#lib/components/file-tree-row.svelte';import { openConfirmDialog } from '#lib/components/confirm-dialog';import * as Dialog from '#lib/components/ui/dialog';import { Input } from '#lib/components/ui/input';import { Label } from '#lib/components/ui/label';import * as Tooltip from '#lib/components/ui/tooltip/index.js';import * as TreeView from '#lib/components/ui/tree-view/index.js';+ import { createVirtualizer } from '#lib/components/ui/virtualizer.svelte';import {ArrowDownIcon,ArrowRightIcon,@@ -122,6 +124,7 @@let uploadInputKey = $state(0);let dialogSubmitting = $state(false);let dialogError = $state<string | null>(null);+ let treeScrollElement = $state<HTMLElement | null>(null);const entryByPath = $derived.by(() => new Map(entries.map((entry) => [entry.relativePath, entry])));const selectedWorkspacePath = $derived(selectedFile.startsWith('file:') ? selectedFile.slice(5) : '');@@ -163,6 +166,13 @@allDestinationOptions.filter((option) => option.relativePath === '' || isDestinationVisible(option.relativePath)));const hasValidDestination = $derived(allDestinationOptions.some((option) => !option.disabled));+ const rowVirtualizer = createVirtualizer<HTMLElement, HTMLDivElement>(() => ({+ count: rows.length,+ getScrollElement: () => treeScrollElement,+ getItemKey: (index) => rows[index]?.relativePath ?? index,+ estimateSize: () => 32,+ overscan: 8+ }));function toggleFolder(relativePath: string) {activeFolderPath = relativePath;@@ -568,7 +578,7 @@<div class="border-b border-border px-3 py-2 text-xs text-muted-foreground">{readOnlyMessage}</div>{/if}- <div class="min-h-0 flex-1 overflow-auto">+ <div bind:this={treeScrollElement} class="min-h-0 flex-1 overflow-auto"><TreeView.Root class="min-w-max p-2 whitespace-nowrap">{#each leadingRows as leadingRow (leadingRow.key)}<button@@ -600,142 +610,124 @@{#if rows.length === 0}<div class="px-7 py-3 text-xs text-muted-foreground">{emptyMessage}</div>{:else}- {#each rows as row (row.relativePath)}- <div- class={cn(- 'group flex w-full items-center gap-1.5 rounded-md px-2 py-0.5 text-[13px] hover:bg-accent',- selectedFile === `file:${row.relativePath}` && 'bg-accent'- )}- style={`padding-left: ${0.5 + row.depth * 1}rem`}- >- {#if row.isDirectory}- <button- type="button"- class="inline-flex size-4 shrink-0 items-center justify-center rounded hover:bg-muted"- aria-label={openFolders[row.relativePath]- ? m.workspace_file_collapse_folder({ name: row.name })- : m.workspace_file_expand_folder({ name: row.name })}- onclick={() => toggleFolder(row.relativePath)}+ <div class="relative" style={`height: ${rowVirtualizer.totalSize}px`}>+ {#each rowVirtualizer.virtualItems as virtualItem (virtualItem.key)}+ {@const row = rows[virtualItem.index]}+ {#if row}+ <div+ class="absolute top-0 left-0 w-full"+ style={`transform: translateY(${virtualItem.start}px)`}+ data-index={virtualItem.index}+ use:rowVirtualizer.measureElement>- {#if openFolders[row.relativePath] === true}- <ArrowDownIcon class="size-3.5" />- {:else}- <ArrowRightIcon class="size-3.5" />- {/if}- </button>- {:else if hasDirectories}- <span class="inline-flex size-4 shrink-0 items-center justify-center"></span>- {/if}-- <button- type="button"- class="flex min-w-0 flex-1 items-center gap-1.5 py-1 text-left"- onclick={() => (row.isDirectory ? toggleFolder(row.relativePath) : onSelect(`file:${row.relativePath}`))}- >- {#if row.isDirectory}- <FolderOpenIcon class="size-4 shrink-0 text-amber-500" />- {:else}- <FileTextIcon class="size-4 shrink-0 text-muted-foreground" />- {/if}- <span class="min-w-0 truncate">{row.name}</span>- {#if row.pending}- <span- class="size-1.5 shrink-0 rounded-full bg-primary"- role="img"- aria-label={m.common_unsaved_changes()}- title={m.common_unsaved_changes()}- ></span>- {/if}- </button>-- {#if row.locked || row.isSymlink || onRename || onMove || onDelete || onDownload || onRestore}- <div class="flex shrink-0 items-center gap-0.5">- {#if onDownload && !row.isDirectory && !row.pending}- <Tooltip.Root>- <Tooltip.Trigger>- <button- type="button"- class="inline-flex size-6 items-center justify-center rounded text-foreground hover:bg-foreground/10"- aria-label={m.templates_download()}- onclick={() => onDownload?.(row.relativePath)}- >- <DownloadIcon class="size-3.5" />- </button>- </Tooltip.Trigger>- <Tooltip.Content>{m.templates_download()}</Tooltip.Content>- </Tooltip.Root>- {/if}- {#if row.locked || row.isSymlink}- <LockIcon class="mx-1 size-3.5 shrink-0 text-muted-foreground" aria-label={lockedLabel} />- {:else}- {#if onRestore && !row.isDirectory && !row.pending}- <Tooltip.Root>- <Tooltip.Trigger>- <button- type="button"- class="inline-flex size-6 items-center justify-center rounded text-foreground hover:bg-foreground/10"- aria-label={m.workspace_restore()}- onclick={() => onRestore?.(row.relativePath)}- >- <RefreshIcon class="size-3.5" />- </button>- </Tooltip.Trigger>- <Tooltip.Content>{m.workspace_restore()}</Tooltip.Content>- </Tooltip.Root>- {/if}- {#if onRename}- <Tooltip.Root>- <Tooltip.Trigger>- <button- type="button"- class="inline-flex size-6 items-center justify-center rounded text-foreground hover:bg-foreground/10"- aria-label={m.workspace_file_rename_label({ name: row.relativePath })}- {disabled}- onclick={() => openRenameDialog(row.relativePath)}- >- <EditIcon class="size-3.5" />- </button>- </Tooltip.Trigger>- <Tooltip.Content>{m.rename()}</Tooltip.Content>- </Tooltip.Root>- {/if}- {#if onMove}- <Tooltip.Root>- <Tooltip.Trigger>- <button- type="button"- class="inline-flex size-6 items-center justify-center rounded text-foreground hover:bg-foreground/10"- aria-label={m.workspace_file_move_label({ name: row.relativePath })}- {disabled}- onclick={() => openMoveDialog(row.relativePath)}- >- <FolderMoveIcon class="size-3.5" />- </button>- </Tooltip.Trigger>- <Tooltip.Content>{m.move()}</Tooltip.Content>- </Tooltip.Root>- {/if}- {#if onDelete}- <Tooltip.Root>- <Tooltip.Trigger>- <button- type="button"- class="inline-flex size-6 items-center justify-center rounded text-destructive hover:bg-destructive/10"- aria-label={m.delete_name({ name: row.relativePath })}- {disabled}- onclick={() => handleDelete(row)}- >- <TrashIcon class="size-3.5" />- </button>- </Tooltip.Trigger>- <Tooltip.Content>{m.common_delete()}</Tooltip.Content>- </Tooltip.Root>- {/if}- {/if}+ <FileTreeRow+ name={row.name}+ path={row.relativePath}+ depth={row.depth}+ isDirectory={row.isDirectory}+ expanded={openFolders[row.relativePath] === true}+ showDisclosure={hasDirectories}+ selected={selectedFile === `file:${row.relativePath}`}+ pending={row.pending}+ pendingLabel={m.common_unsaved_changes()}+ expandLabel={m.workspace_file_expand_folder({ name: row.name })}+ collapseLabel={m.workspace_file_collapse_folder({ name: row.name })}+ onToggle={() => toggleFolder(row.relativePath)}+ onActivate={() => (row.isDirectory ? toggleFolder(row.relativePath) : onSelect(`file:${row.relativePath}`))}+ >+ {#snippet trailing()}+ {#if row.locked || row.isSymlink || onRename || onMove || onDelete || onDownload || onRestore}+ <div class="flex shrink-0 items-center gap-0.5">+ {#if onDownload && !row.isDirectory && !row.pending}+ <Tooltip.Root>+ <Tooltip.Trigger>+ <button+ type="button"+ class="inline-flex size-6 items-center justify-center rounded text-foreground hover:bg-foreground/10"+ aria-label={m.templates_download()}+ onclick={() => onDownload?.(row.relativePath)}+ >+ <DownloadIcon class="size-3.5" />+ </button>+ </Tooltip.Trigger>+ <Tooltip.Content>{m.templates_download()}</Tooltip.Content>+ </Tooltip.Root>+ {/if}+ {#if row.locked || row.isSymlink}+ <LockIcon class="mx-1 size-3.5 shrink-0 text-muted-foreground" aria-label={lockedLabel} />+ {:else}+ {#if onRestore && !row.isDirectory && !row.pending}+ <Tooltip.Root>+ <Tooltip.Trigger>+ <button+ type="button"+ class="inline-flex size-6 items-center justify-center rounded text-foreground hover:bg-foreground/10"+ aria-label={m.workspace_restore()}+ onclick={() => onRestore?.(row.relativePath)}+ >+ <RefreshIcon class="size-3.5" />+ </button>+ </Tooltip.Trigger>+ <Tooltip.Content>{m.workspace_restore()}</Tooltip.Content>+ </Tooltip.Root>+ {/if}+ {#if onRename}+ <Tooltip.Root>+ <Tooltip.Trigger>+ <button+ type="button"+ class="inline-flex size-6 items-center justify-center rounded text-foreground hover:bg-foreground/10"+ aria-label={m.workspace_file_rename_label({ name: row.relativePath })}+ {disabled}+ onclick={() => openRenameDialog(row.relativePath)}+ >+ <EditIcon class="size-3.5" />+ </button>+ </Tooltip.Trigger>+ <Tooltip.Content>{m.rename()}</Tooltip.Content>+ </Tooltip.Root>+ {/if}+ {#if onMove}+ <Tooltip.Root>+ <Tooltip.Trigger>+ <button+ type="button"+ class="inline-flex size-6 items-center justify-center rounded text-foreground hover:bg-foreground/10"+ aria-label={m.workspace_file_move_label({ name: row.relativePath })}+ {disabled}+ onclick={() => openMoveDialog(row.relativePath)}+ >+ <FolderMoveIcon class="size-3.5" />+ </button>+ </Tooltip.Trigger>+ <Tooltip.Content>{m.move()}</Tooltip.Content>+ </Tooltip.Root>+ {/if}+ {#if onDelete}+ <Tooltip.Root>+ <Tooltip.Trigger>+ <button+ type="button"+ class="inline-flex size-6 items-center justify-center rounded text-destructive hover:bg-destructive/10"+ aria-label={m.delete_name({ name: row.relativePath })}+ {disabled}+ onclick={() => handleDelete(row)}+ >+ <TrashIcon class="size-3.5" />+ </button>+ </Tooltip.Trigger>+ <Tooltip.Content>{m.common_delete()}</Tooltip.Content>+ </Tooltip.Root>+ {/if}+ {/if}+ </div>+ {/if}+ {/snippet}+ </FileTreeRow></div>{/if}- </div>- {/each}+ {/each}+ </div>{/if}</TreeView.Root></div>diff --git a/frontend/src/lib/query/query-keys.ts b/frontend/src/lib/query/query-keys.tsindex b0c6aa904..ef34dc1ab 100644--- a/frontend/src/lib/query/query-keys.ts+++ b/frontend/src/lib/query/query-keys.ts@@ -110,6 +110,7 @@ export const queryKeys = {},projects: {all: ['projects'] as const,+ environment: (environmentId: string) => ['project', environmentId] as const,list: (environmentId: string, options: SearchPaginationSortRequest) =>['projects', environmentId, stableSerialize(options)] as const,checkUpdates: (environmentId: string) => ['projects', 'check-updates', environmentId] as const,diff --git a/frontend/src/lib/services/api-service.ts b/frontend/src/lib/services/api-service.tsindex cc99778c6..d506c9c25 100644--- a/frontend/src/lib/services/api-service.ts+++ b/frontend/src/lib/services/api-service.ts@@ -4,6 +4,7 @@ import { toast } from 'svelte-sonner';export interface APIRequestConfig {baseURL?: string;+ cache?: RequestCache;data?: unknown;headers?: HeadersInit;params?: SearchParamsOption;@@ -299,6 +300,7 @@ class APIClient {headers.set('X-Arcane-Batch-Id', activeActivityBatchIdInternal);}const options: KyOptions = {+ cache: config.cache,method,headers,retry: config.retry ?? 0,diff --git a/frontend/src/lib/services/project-service.ts b/frontend/src/lib/services/project-service.tsindex d2eb4ad14..19ed2a745 100644--- a/frontend/src/lib/services/project-service.ts+++ b/frontend/src/lib/services/project-service.ts@@ -110,7 +110,9 @@ class ProjectService extends BaseAPIService {}private async getProjectSection(path: string): Promise<Project> {- const response = await this.handleResponse<{ project?: Project; success?: boolean } | Project>(this.api.get(path));+ const response = await this.handleResponse<{ project?: Project; success?: boolean } | Project>(+ this.api.get(path, { cache: 'no-store' })+ );return 'project' in response && response.project ? response.project : (response as Project);}diff --git a/frontend/src/lib/services/project-workspace-service.ts b/frontend/src/lib/services/project-workspace-service.tsindex 7d6cda3ee..5fcf3b6e2 100644--- a/frontend/src/lib/services/project-workspace-service.ts+++ b/frontend/src/lib/services/project-workspace-service.ts@@ -10,13 +10,16 @@ class ProjectWorkspaceService extends BaseAPIService {async getWorkspace(projectId: string, environmentId?: string): Promise<ProjectWorkspace> {const envId = await this.resolveEnvironmentId(environmentId);- return this.handleResponse(this.api.get(`/environments/${envId}/projects/${projectId}/workspace`));+ return this.handleResponse(this.api.get(`/environments/${envId}/projects/${projectId}/workspace`, { cache: 'no-store' }));}async getWorkspaceFile(projectId: string, relativePath: string, environmentId?: string): Promise<ProjectWorkspaceFileContent> {const envId = await this.resolveEnvironmentId(environmentId);return this.handleResponse(- this.api.get(`/environments/${envId}/projects/${projectId}/workspace/file`, { params: { relativePath } })+ this.api.get(`/environments/${envId}/projects/${projectId}/workspace/file`, {+ cache: 'no-store',+ params: { relativePath }+ }));}diff --git a/frontend/src/lib/services/system-backup-service.ts b/frontend/src/lib/services/system-backup-service.tsindex 5359b03a2..eb2aa7930 100644--- a/frontend/src/lib/services/system-backup-service.ts+++ b/frontend/src/lib/services/system-backup-service.ts@@ -8,6 +8,7 @@ import type {UpdateSystemBackupPolicy} from '#lib/types/system-backup';import { transformPaginationParams } from '#lib/utils/tables';+import type { BackupFileBrowseRequest, BackupFileEntry, BackupRestoreSelection } from '#lib/types/backup';class SystemBackupService extends BaseAPIService {async list(options?: SearchPaginationSortRequest): Promise<Paginated<SystemBackupRun>> {@@ -39,6 +40,15 @@ class SystemBackupService extends BaseAPIService {await this.handleResponse(this.api.post(`/backups/${id}/restore`, { recoveryKey }));}+ async browseFiles(id: string, recoveryKey: string, request: BackupFileBrowseRequest): Promise<Paginated<BackupFileEntry>> {+ const response = await this.api.post(`/backups/${id}/files/browse`, { recoveryKey }, { params: request });+ return response.data;+ }++ async restoreFiles(id: string, recoveryKey: string, selection: BackupRestoreSelection): Promise<unknown> {+ return this.handleResponse(this.api.post(`/backups/${id}/restore-files`, { recoveryKey, ...selection }));+ }+async upload(id: string, s3DestinationId: string, recoveryKey: string): Promise<SystemBackupRun> {return this.handleResponse(this.api.post(`/backups/${id}/upload`, { s3DestinationId, recoveryKey }));}diff --git a/frontend/src/lib/services/volume-backup-service.ts b/frontend/src/lib/services/volume-backup-service.tsindex 3607a65cd..ec7afc84d 100644--- a/frontend/src/lib/services/volume-backup-service.ts+++ b/frontend/src/lib/services/volume-backup-service.ts@@ -9,6 +9,7 @@ import type {} from '#lib/types/shared';import type { SearchPaginationSortRequest, Paginated } from '#lib/types/shared';import { transformPaginationParams } from '#lib/utils/tables';+import type { BackupFileBrowseRequest, BackupFileEntry, BackupRestoreSelection } from '#lib/types/backup';export type VolumeBackupListResponse = Paginated<BackupEntry> & { warnings?: string[] };@@ -36,20 +37,28 @@ class VolumeBackupService extends BaseAPIService {return res.data;}- async restoreBackup(volumeName: string, backupId: string): Promise<any> {+ async restoreBackup(volumeName: string, backupId: string): Promise<unknown> {const envId = await environmentStore.getCurrentEnvironmentId();return this.handleResponse(this.api.post(`/environments/${envId}/volumes/${volumeName}/backups/${backupId}/restore`));}- async restoreBackupFiles(volumeName: string, backupId: string, paths: string[]): Promise<any> {+ async restoreBackupFiles(volumeName: string, backupId: string, selection: BackupRestoreSelection): Promise<unknown> {const envId = await environmentStore.getCurrentEnvironmentId();return this.handleResponse(this.api.post(`/environments/${envId}/volumes/${volumeName}/backups/${backupId}/restore-files`, {- paths+ ...selection}));}+ async browseBackupFiles(backupId: string, request: BackupFileBrowseRequest): Promise<Paginated<BackupFileEntry>> {+ const envId = await environmentStore.getCurrentEnvironmentId();+ const response = await this.api.get(`/environments/${envId}/volumes/backups/${backupId}/files/browse`, {+ params: request+ });+ return response.data;+ }+async backupHasPath(backupId: string, filePath: string): Promise<boolean> {const envId = await environmentStore.getCurrentEnvironmentId();const res = await this.api.get(`/environments/${envId}/volumes/backups/${backupId}/has-path`, {@@ -79,7 +88,7 @@ class VolumeBackupService extends BaseAPIService {link.remove();}- async deleteBackup(backupId: string): Promise<any> {+ async deleteBackup(backupId: string): Promise<unknown> {const envId = await environmentStore.getCurrentEnvironmentId();return this.handleResponse(this.api.delete(`/environments/${envId}/volumes/backups/${backupId}`));}@@ -89,7 +98,7 @@ class VolumeBackupService extends BaseAPIService {return this.handleResponse(this.api.post(`/environments/${envId}/volumes/backups/${backupId}/upload`, { s3DestinationId }));}- async uploadAndRestore(volumeName: string, file: File, onProgress?: UploadProgressCallback): Promise<any> {+ async uploadAndRestore(volumeName: string, file: File, onProgress?: UploadProgressCallback): Promise<unknown> {const envId = await environmentStore.getCurrentEnvironmentId();const uploadId = await uploadService.uploadFile(envId, 'volume-backup', file, onProgress);return this.handleResponse(this.api.post(`/environments/${envId}/volumes/${volumeName}/backups/upload`, { uploadId }));diff --git a/frontend/src/lib/services/volume-workspace-service.ts b/frontend/src/lib/services/volume-workspace-service.tsindex e27b128ce..12d702707 100644--- a/frontend/src/lib/services/volume-workspace-service.ts+++ b/frontend/src/lib/services/volume-workspace-service.ts@@ -10,13 +10,16 @@ class VolumeWorkspaceService extends BaseAPIService {async getWorkspace(volumeName: string, environmentId?: string): Promise<VolumeWorkspace> {const envId = await this.resolveEnvironmentId(environmentId);- return this.handleResponse(this.api.get(`/environments/${envId}/volumes/${volumeName}/workspace`));+ return this.handleResponse(this.api.get(`/environments/${envId}/volumes/${volumeName}/workspace`, { cache: 'no-store' }));}async getWorkspaceFile(volumeName: string, relativePath: string, environmentId?: string): Promise<VolumeWorkspaceFileContent> {const envId = await this.resolveEnvironmentId(environmentId);return this.handleResponse(- this.api.get(`/environments/${envId}/volumes/${volumeName}/workspace/file`, { params: { relativePath } })+ this.api.get(`/environments/${envId}/volumes/${volumeName}/workspace/file`, {+ cache: 'no-store',+ params: { relativePath }+ }));}diff --git a/frontend/src/lib/types/backup.ts b/frontend/src/lib/types/backup.tsindex d99d22f02..b27df011b 100644--- a/frontend/src/lib/types/backup.ts+++ b/frontend/src/lib/types/backup.ts@@ -1,3 +1,5 @@+import type { Paginated } from './shared';+export type BackupDestination = 'local' | 's3' | 'local_s3';export type BackupStatus = 'running' | 'succeeded' | 'failed';export type BackupTrigger = 'manual' | 'scheduled' | 'safety';@@ -48,3 +50,26 @@ export type BackupPolicyUpdate = {s3DestinationId: string;stopContainers?: boolean;};++export type BackupFileEntry = {+ path: string;+ name: string;+ isDirectory: boolean;+};++export type BackupFileBrowseRequest = {+ path?: string;+ search?: string;+ start?: number;+ limit?: number;+};++export type BackupRestoreSelection = {+ paths: string[];+ selectAll: boolean;+ search?: string;+};++export type BackupFileProvider = {+ browse(request: BackupFileBrowseRequest): Promise<Paginated<BackupFileEntry>>;+};diff --git a/frontend/src/routes/(app)/projects/[projectId]/+page.svelte b/frontend/src/routes/(app)/projects/[projectId]/+page.svelteindex b161a7d2d..c50a23427 100644--- a/frontend/src/routes/(app)/projects/[projectId]/+page.svelte+++ b/frontend/src/routes/(app)/projects/[projectId]/+page.svelte@@ -127,7 +127,7 @@queryKey: queryKeys.projects.detail(envId, projectId),queryFn: () => projectService.getProjectForEnvironment(envId, projectId),initialData: data.project,- refetchOnMount: false+ refetchOnMount: (query) => query.state.isInvalidated}));const projectTagsQuery = createQuery(() => ({queryKey: queryKeys.projects.tags(envId),diff --git a/frontend/src/routes/(app)/settings/backups/+page.svelte b/frontend/src/routes/(app)/settings/backups/+page.svelteindex c989aa489..a3027ca48 100644--- a/frontend/src/routes/(app)/settings/backups/+page.svelte+++ b/frontend/src/routes/(app)/settings/backups/+page.svelte@@ -4,7 +4,7 @@import { toast } from 'svelte-sonner';import settingsStore from '#lib/stores/config-store';import { SettingsPageLayout, type SettingsActionButton } from '#lib/layouts';- import { AlertIcon, BackupIcon, CloudStorageIcon, LockIcon, ResetIcon } from '#lib/icons';+ import { AlertIcon, BackupIcon, CloudStorageIcon, InfoIcon, LockIcon, ResetIcon } from '#lib/icons';import * as Alert from '#lib/components/ui/alert';import { CopyButton } from '#lib/components/ui/copy-button';import { Input } from '#lib/components/ui/input';@@ -21,8 +21,14 @@import SystemBackupTable from './system-backup-table.svelte';import BackupPolicyDialog from '#lib/components/backup-policy-dialog.svelte';import BackupPolicyCard from '#lib/components/backup-policy-card.svelte';+ import BackupFilePicker from '#lib/components/backup-file-picker.svelte';+ import { activityToastOptions, extractActivityId } from '#lib/utils/activity-toast';+ import type { BackupFileProvider } from '#lib/types/backup';+ import { queryKeys } from '#lib/query/query-keys';+ import { useQueryClient } from '@tanstack/svelte-query';let { data } = $props();+ const queryClient = useQueryClient();let backups = $state(untrack(() => data.backups));let policyCollection = $state(untrack(() => data.policyCollection));let requestOptions = $state<SearchPaginationSortRequest>(untrack(() => data.requestOptions));@@ -39,6 +45,15 @@let newRecoveryKey = $state('');let loading = $state(false);let generatingKey = $state(false);+ let restoreFilesOpen = $state(false);+ let restoreFilesTarget = $state<SystemBackupRun | null>(null);+ let restoreFilesRecoveryKey = $state('');+ let restoreFilesProvider = $state<BackupFileProvider | null>(null);+ let restoreFilesSelectedPaths = $state<string[]>([]);+ let restoreFilesSelectAll = $state(false);+ let restoreFilesSearch = $state('');+ let restoreFilesLoaded = $state(false);+ let restoringFiles = $state(false);const isReadOnly = $derived.by(() => $settingsStore.uiConfigDisabled);const canManageRecoveryKey = $derived(hasPermission('system-backups:recovery-key'));const destinationOptions = $derived(backupDestinationOptions(data.destinations.length > 0));@@ -60,6 +75,15 @@// ever uses this instance's stored key.const actionNeedsTypedKey = $derived(action === 'restore' || action === 'discover');const recoveryKeyPattern = /^[A-Z2-7]{6}(-[A-Z2-7]{6}){7}$/;+ const restoreFilesKeyError = $derived(+ restoreFilesRecoveryKey.length > 0 && !recoveryKeyPattern.test(restoreFilesRecoveryKey.trim())+ ? m.system_backups_recovery_key_required()+ : ''+ );+ const restoreFilesKeyInvalid = $derived(+ Boolean(restoreFilesKeyError) ||+ (!policyCollection.recoveryKeyStored && !recoveryKeyPattern.test(restoreFilesRecoveryKey.trim()))+ );const keyError = $derived(actionNeedsTypedKey && recoveryKey.length > 0 && !recoveryKeyPattern.test(recoveryKey.trim())? m.system_backups_recovery_key_required()@@ -117,6 +141,70 @@recoveryKey = '';actionOpen = true;}++ // The picker resets its own selection and search whenever the provider+ // changes, so the dialog only manages target, key, and provider.+ async function openRestoreFiles(backup: SystemBackupRun) {+ restoreFilesTarget = backup;+ restoreFilesRecoveryKey = '';+ restoreFilesProvider = null;+ restoreFilesLoaded = false;+ restoreFilesOpen = true;+ if (policyCollection.recoveryKeyStored) loadRestoreFiles();+ }++ function closeRestoreFiles() {+ restoreFilesOpen = false;+ restoreFilesTarget = null;+ restoreFilesRecoveryKey = '';+ restoreFilesProvider = null;+ restoreFilesLoaded = false;+ }++ function updateRestoreFilesRecoveryKey(value: string) {+ restoreFilesRecoveryKey = value;+ restoreFilesProvider = null;+ restoreFilesLoaded = false;+ }++ function loadRestoreFiles() {+ if (!restoreFilesTarget || restoreFilesKeyInvalid) return;+ const backupID = restoreFilesTarget.id;+ const recoveryKey = restoreFilesRecoveryKey.trim();+ restoreFilesProvider = {+ browse: (request) => systemBackupService.browseFiles(backupID, recoveryKey, request)+ };+ restoreFilesLoaded = true;+ }++ async function restoreSelectedFiles() {+ if (+ !restoreFilesTarget ||+ !restoreFilesLoaded ||+ (!restoreFilesSelectAll && restoreFilesSelectedPaths.length === 0) ||+ restoringFiles+ )+ return;+ restoringFiles = true;+ try {+ const result = await systemBackupService.restoreFiles(restoreFilesTarget.id, restoreFilesRecoveryKey.trim(), {+ paths: restoreFilesSelectedPaths,+ selectAll: restoreFilesSelectAll,+ search: restoreFilesSelectAll ? restoreFilesSearch.trim() : undefined+ });+ const projectQueryKey = queryKeys.projects.environment('0');+ await queryClient.cancelQueries({ queryKey: projectQueryKey });+ queryClient.removeQueries({ queryKey: projectQueryKey });+ toast.success(m.system_backups_restore_selection_success(), activityToastOptions(extractActivityId(result)));+ closeRestoreFiles();+ await refresh();+ } catch (error) {+ toast.error(error instanceof Error ? error.message : m.system_backups_restore_files_failed());+ } finally {+ restoringFiles = false;+ }+ }+function dialogTitle() {if (action === 'restore') return m.system_backups_restore_title();if (action === 'upload') return m.system_backups_upload_title();@@ -371,6 +459,78 @@</ResponsiveDialog>{/snippet}+{#snippet restoreFilesDialog()}+ <ResponsiveDialog+ bind:open={restoreFilesOpen}+ onOpenChange={(open) => {+ if (!open) closeRestoreFiles();+ }}+ title={m.volume_restore_files()}+ description={m.system_backups_restore_files_description()}+ contentClass="sm:max-w-[640px]"+ >+ {#snippet children()}+ <div class="space-y-3 py-2">+ <Alert.Root class="py-2 [&>svg]:top-2">+ <InfoIcon class="size-4" />+ <Alert.Description class="text-xs">+ {m.system_backups_restore_files_lifecycle_info()}+ </Alert.Description>+ </Alert.Root>++ <div class="flex items-end gap-2">+ <div class="min-w-0 flex-1">+ <TextInputWithLabel+ value={restoreFilesRecoveryKey}+ onChange={updateRestoreFilesRecoveryKey}+ error={restoreFilesKeyError || null}+ label={m.system_backups_recovery_key()}+ description={policyCollection.recoveryKeyStored+ ? m.system_backups_recovery_key_saved_description()+ : m.system_backups_recovery_key_enter_description()}+ type="password"+ autocomplete="current-password"+ />+ </div>+ <ArcaneButton+ action="inspect"+ customLabel={m.system_backups_load_files()}+ onclick={loadRestoreFiles}+ disabled={restoreFilesKeyInvalid}+ />+ </div>++ {#if restoreFilesProvider}+ <BackupFilePicker+ provider={restoreFilesProvider}+ bind:selectedPaths={restoreFilesSelectedPaths}+ bind:selectAll={restoreFilesSelectAll}+ bind:search={restoreFilesSearch}+ />++ <Alert.Root variant="warning" class="py-2 [&>svg]:top-2">+ <AlertIcon class="size-4" />+ <Alert.Description class="text-xs">+ {m.volumes_backup_overwrite_warning()}+ </Alert.Description>+ </Alert.Root>+ {/if}+ </div>+ {/snippet}++ {#snippet footer()}+ <ArcaneButton action="cancel" onclick={closeRestoreFiles} disabled={restoringFiles} />+ <ArcaneButton+ action="confirm"+ customLabel={m.volume_restore_files()}+ onclick={restoreSelectedFiles}+ loading={restoringFiles}+ disabled={restoringFiles || !restoreFilesLoaded || (!restoreFilesSelectAll && restoreFilesSelectedPaths.length === 0)}+ />+ {/snippet}+ </ResponsiveDialog>+{/snippet}+<SettingsPageLayouttitle={m.system_backups_title()}description={m.system_backups_description()}@@ -401,6 +561,7 @@bind:requestOptionsonChanged={(options) => systemBackupService.list(options)}onRestore={(item) => openAction('restore', item)}+ onRestoreFiles={openRestoreFiles}onUpload={(item) => openAction('upload', item)}onDelete={(item) => openAction('delete', item)}/>@@ -430,5 +591,6 @@{@render recoveryKeyDialog()}{@render actionDialog()}+ {@render restoreFilesDialog()}{/snippet}</SettingsPageLayout>diff --git a/frontend/src/routes/(app)/settings/backups/system-backup-table.svelte b/frontend/src/routes/(app)/settings/backups/system-backup-table.svelteindex df82d1578..4549b8c95 100644--- a/frontend/src/routes/(app)/settings/backups/system-backup-table.svelte+++ b/frontend/src/routes/(app)/settings/backups/system-backup-table.svelte@@ -10,7 +10,7 @@import CreatedAtCell from '#lib/components/arcane-table/cells/created-at-cell.svelte';import type { Paginated, SearchPaginationSortRequest } from '#lib/types/shared';import type { SystemBackupRun } from '#lib/types/system-backup';- import { BackupIcon, RestartIcon, TrashIcon, UploadIcon, ClockIcon } from '#lib/icons';+ import { BackupIcon, RestartIcon, TrashIcon, UploadIcon, ClockIcon, FileTextIcon } from '#lib/icons';import { bytes, formatDateTimeShort } from '#lib/utils/formatting';import { backupDestinationDisplay, backupStatusLabel, backupStatusVariant, backupTriggerLabel } from '#lib/utils/backups';import * as m from '#lib/paraglide/messages.js';@@ -20,6 +20,7 @@requestOptions = $bindable(),onChanged,onRestore,+ onRestoreFiles,onUpload,onDelete}: {@@ -27,6 +28,7 @@requestOptions: SearchPaginationSortRequest;onChanged: (options: SearchPaginationSortRequest) => Promise<Paginated<SystemBackupRun>>;onRestore: (backup: SystemBackupRun) => void;+ onRestoreFiles: (backup: SystemBackupRun) => void;onUpload: (backup: SystemBackupRun) => void;onDelete: (backup: SystemBackupRun) => void;} = $props();@@ -63,6 +65,9 @@<DropdownMenu.Item onclick={() => onRestore(item)} disabled={item.status !== 'succeeded'}><RestartIcon class="size-4" />{m.volumes_backups_restore()}</DropdownMenu.Item>+ <DropdownMenu.Item onclick={() => onRestoreFiles(item)} disabled={item.status !== 'succeeded'}+ ><FileTextIcon class="size-4" />{m.volume_restore_files()}</DropdownMenu.Item+ >{#if item.localSnapshotId && !item.remoteSnapshotId}<DropdownMenu.Item onclick={() => onUpload(item)}><UploadIcon class="size-4" />{m.backups_upload_s3()}</DropdownMenu.Item>{/if}diff --git a/frontend/src/routes/(app)/volumes/[volumeName]/+page.svelte b/frontend/src/routes/(app)/volumes/[volumeName]/+page.svelteindex fd2e47cdf..35059334d 100644--- a/frontend/src/routes/(app)/volumes/[volumeName]/+page.svelte+++ b/frontend/src/routes/(app)/volumes/[volumeName]/+page.svelte@@ -106,6 +106,7 @@let workspaceFileLoadErrors = $state<Record<string, string>>({});let workspaceFileLoading = $state<Record<string, boolean>>({});let workspaceRestoreRecords = $state<Record<string, true>>({});+ const workspaceFileLoadVersions = new Map<string, number>();let selectedWorkspaceFile = $state('');let openWorkspaceTabs = $state<string[]>([]);let workspaceTreePaneWidth = $state(420);@@ -293,8 +294,10 @@workspaceFileLoading = { ...workspaceFileLoading, [relativePath]: true };workspaceFileLoadErrors = removeWorkspaceFileRecord(workspaceFileLoadErrors, relativePath);+ const loadVersion = workspaceFileLoadVersions.get(relativePath) ?? 0;try {const file = await volumeWorkspaceService.getWorkspaceFile(volume.name, relativePath, currentEnvId);+ if (loadVersion !== (workspaceFileLoadVersions.get(relativePath) ?? 0)) return;workspaceFileMetadata = { ...workspaceFileMetadata, [relativePath]: file };if (file.editable) {const content = file.content ?? '';@@ -304,12 +307,15 @@}}} catch (error) {+ if (loadVersion !== (workspaceFileLoadVersions.get(relativePath) ?? 0)) return;workspaceFileLoadErrors = {...workspaceFileLoadErrors,[relativePath]: error instanceof Error ? error.message : String(error)};} finally {- workspaceFileLoading = removeWorkspaceFileRecord(workspaceFileLoading, relativePath);+ if (loadVersion === (workspaceFileLoadVersions.get(relativePath) ?? 0)) {+ workspaceFileLoading = removeWorkspaceFileRecord(workspaceFileLoading, relativePath);+ }}}@@ -480,6 +486,7 @@}function clearVolumeWorkspaceDrafts() {+ for (const relativePath of Object.keys(workspaceFileLoading)) expireWorkspaceFileLoad(relativePath);workspaceFileChanges = [];workspaceFileContents = {};loadedWorkspaceFileContents = {};@@ -489,6 +496,16 @@workspaceRestoreRecords = {};}+ function expireWorkspaceFileLoad(relativePath: string) {+ workspaceFileLoadVersions.set(relativePath, (workspaceFileLoadVersions.get(relativePath) ?? 0) + 1);+ }++ async function refreshRestoredVolumeWorkspace() {+ await queryClient.cancelQueries({ queryKey: queryKeys.volumes.workspace(currentEnvId, volume.name) });+ clearVolumeWorkspaceDrafts();+ await workspaceQuery.refetch();+ }+async function handleSaveVolumeWorkspace() {if (!workspaceQuery.data || !canSaveWorkspace) return;const update = buildWorkspaceMultipartUpdate(workspaceFileChanges, workspaceFileContents, loadedWorkspaceFileContents);@@ -873,7 +890,7 @@</div>{/if}{:else if tab === 'backups'}- <BackupList volumeName={volume.name} />+ <BackupList volumeName={volume.name} {hasWorkspaceChanges} onWorkspaceRestored={refreshRestoredVolumeWorkspace} />{/if}</div>{/snippet}diff --git a/frontend/src/routes/(app)/volumes/components/volume-backup-table.svelte b/frontend/src/routes/(app)/volumes/components/volume-backup-table.svelteindex 7fee1b6f6..142af2d5a 100644--- a/frontend/src/routes/(app)/volumes/components/volume-backup-table.svelte+++ b/frontend/src/routes/(app)/volumes/components/volume-backup-table.svelte@@ -7,7 +7,6 @@import type { S3Destination } from '#lib/types/s3-destination';import { onMount } from 'svelte';import {- LoadingSpinnerIcon,TrashIcon,AddIcon,ClockIcon,@@ -36,10 +35,8 @@import RowActionsMenu from '#lib/components/file-browser/row-actions-menu.svelte';import { openConfirmDialog } from '#lib/components/confirm-dialog';import { ResponsiveDialog } from '#lib/components/ui/responsive-dialog';- import { Input } from '#lib/components/ui/input';- import { ScrollArea } from '#lib/components/ui/scroll-area';- import * as Checkbox from '#lib/components/ui/checkbox';import * as Alert from '#lib/components/ui/alert';+ import BackupFilePicker from '#lib/components/backup-file-picker.svelte';import { environmentStore } from '#lib/stores/environment.store.svelte';import { hasPermission } from '#lib/utils/auth';import IfPermitted from '#lib/components/if-permitted.svelte';@@ -60,8 +57,17 @@s3DestinationOptions as buildS3DestinationOptions} from '#lib/utils/backups';import SelectWithLabel from '#lib/components/form/select-with-label.svelte';-- let { volumeName }: { volumeName: string } = $props();+ import type { BackupFileProvider } from '#lib/types/backup';++ let {+ volumeName,+ hasWorkspaceChanges = false,+ onWorkspaceRestored+ }: {+ volumeName: string;+ hasWorkspaceChanges?: boolean;+ onWorkspaceRestored?: () => void | Promise<void>;+ } = $props();const currentEnvId = $derived(environmentStore.selected?.id || '0');const canBackupVolume = $derived(hasPermission('volumes:backup', currentEnvId));@@ -98,16 +104,10 @@let restoringFiles = $state(false);let showRestoreFiles = $state(false);let restoreTarget = $state<BackupEntry | null>(null);- let backupFiles = $state<string[]>([]);- let backupFilesLoading = $state(false);+ let backupFileProvider = $state<BackupFileProvider | null>(null);let backupFilesSearch = $state('');let selectedPaths = $state<string[]>([]);- const filteredBackupFiles = $derived.by(() => {- const q = backupFilesSearch.trim().toLowerCase();- if (!q) return backupFiles;- return backupFiles.filter((p) => p.toLowerCase().includes(q));- });-+ let selectAllBackupFiles = $state(false);async function loadData(options: SearchPaginationSortRequest): Promise<VolumeBackupListResponse> {try {const result = await volumeBackupService.listBackups(volumeName, options);@@ -211,42 +211,13 @@}}- async function openRestoreFilesDialog(backup: BackupEntry) {+ function openRestoreFilesDialog(backup: BackupEntry) {restoreTarget = backup;- selectedPaths = [];- backupFiles = [];- backupFilesSearch = '';+ // The picker clears its selection and search when it sees the new provider.+ backupFileProvider = {+ browse: (request) => volumeBackupService.browseBackupFiles(backup.id, request)+ };showRestoreFiles = true;- backupFilesLoading = true;- try {- backupFiles = await volumeBackupService.listBackupFiles(backup.id);- } catch (error) {- toast.error(error instanceof Error ? error.message : m.common_failed());- } finally {- backupFilesLoading = false;- }- }-- function togglePath(path: string, checked: boolean) {- if (checked) {- if (!selectedPaths.includes(path)) {- selectedPaths = [...selectedPaths, path];- }- return;- }- selectedPaths = selectedPaths.filter((p) => p !== path);- }-- function selectAllVisible() {- const next = new Set(selectedPaths);- for (const p of filteredBackupFiles) {- next.add(p);- }- selectedPaths = Array.from(next);- }-- function clearSelection() {- selectedPaths = [];}async function handleRestore(backup: BackupEntry) {@@ -263,13 +234,17 @@openConfirmDialog({title: m.volumes_backup_restore_title(),- message: m.volumes_backup_restore_message({ volumeName }) + usageWarning,+ message:+ m.volumes_backup_restore_message({ volumeName }) ++ usageWarning ++ (hasWorkspaceChanges ? `\n\n${m.volumes_backup_restore_discard_changes()}` : ''),confirm: {label: m.volumes_backups_restore(),- destructive: !!usageWarning,+ destructive: !!usageWarning || hasWorkspaceChanges,action: async () => {try {const result = await volumeBackupService.restoreBackup(volumeName, backup.id);+ await onWorkspaceRestored?.();toast.success(m.volumes_backup_restore_success(), activityToastOptions(extractActivityId(result)));await loadData(requestOptions);} catch (error) {@@ -282,15 +257,17 @@async function handleRestoreFiles() {if (!restoreTarget) return;- if (!selectedPaths.length) return;+ if (!selectAllBackupFiles && !selectedPaths.length) return;+ const selection = { paths: [...selectedPaths], selectAll: selectAllBackupFiles };restoringFiles = true;try {- const result = await volumeBackupService.restoreBackupFiles(volumeName, restoreTarget.id, selectedPaths);- toast.success(- m.volumes_backup_restore_files_success({ count: selectedPaths.length }),- activityToastOptions(extractActivityId(result))- );+ const result = await volumeBackupService.restoreBackupFiles(volumeName, restoreTarget.id, {+ ...selection,+ search: selection.selectAll ? backupFilesSearch.trim() : undefined+ });+ await onWorkspaceRestored?.();+ toast.success(m.volumes_backup_restore_selection_success(), activityToastOptions(extractActivityId(result)));showRestoreFiles = false;} catch (error) {toast.error(error instanceof Error ? error.message : m.common_failed());@@ -576,6 +553,9 @@<ResponsiveDialogbind:open={showRestoreFiles}+ onOpenChange={(open) => {+ if (!open) backupFileProvider = null;+ }}title={m.volume_restore_files()}description={m.volumes_backup_restore_desc()}contentClass="sm:max-w-[640px]"@@ -589,35 +569,14 @@</Alert.Description></Alert.Root>- <div class="flex items-center justify-between gap-2">- <Input class="h-9" placeholder={m.volume_search_files()} bind:value={backupFilesSearch} />- <div class="flex items-center gap-2">- <ArcaneButton action="base" tone="ghost" size="sm" onclick={selectAllVisible} customLabel={m.common_select_all()} />- <ArcaneButton action="base" tone="ghost" size="sm" onclick={clearSelection} customLabel={m.common_clear()} />- </div>- </div>-- <ScrollArea class="h-64 rounded-md border">- {#if backupFilesLoading}- <div class="flex items-center justify-center py-8">- <LoadingSpinnerIcon class="size-5 text-muted-foreground" />- </div>- {:else if filteredBackupFiles.length === 0}- <div class="flex items-center justify-center py-8 text-sm text-muted-foreground">{m.volume_backup_no_files()}</div>- {:else}- <div class="divide-y divide-border/40">- {#each filteredBackupFiles as filePath (filePath)}- <div class="flex items-center gap-3 px-3 py-2">- <Checkbox.Root- checked={selectedPaths.includes(filePath)}- onCheckedChange={(value) => togglePath(filePath, !!value)}- />- <code class="font-mono text-xs break-all">{filePath}</code>- </div>- {/each}- </div>- {/if}- </ScrollArea>+ {#if backupFileProvider}+ <BackupFilePicker+ provider={backupFileProvider}+ bind:selectedPaths+ bind:selectAll={selectAllBackupFiles}+ bind:search={backupFilesSearch}+ />+ {/if}<Alert.Root variant="warning" class="py-2 [&>svg]:top-2"><AlertIcon class="size-4" />@@ -625,6 +584,15 @@{m.volumes_backup_overwrite_warning()}</Alert.Description></Alert.Root>++ {#if hasWorkspaceChanges}+ <Alert.Root variant="warning" class="py-2 [&>svg]:top-2">+ <AlertIcon class="size-4" />+ <Alert.Description class="text-xs">+ {m.volumes_backup_restore_discard_changes()}+ </Alert.Description>+ </Alert.Root>+ {/if}</div>{/snippet}@@ -633,8 +601,7 @@action="cancel"onclick={() => {showRestoreFiles = false;- selectedPaths = [];- backupFilesSearch = '';+ backupFileProvider = null;}}/>{#if canBackupVolume}@@ -643,7 +610,7 @@customLabel={m.volume_restore_files()}onclick={handleRestoreFiles}loading={restoringFiles}- disabled={restoringFiles || selectedPaths.length === 0}+ disabled={restoringFiles || (!selectAllBackupFiles && selectedPaths.length === 0)}/>{/if}{/snippet}diff --git a/tests/spec/backup-file-picker.spec.ts b/tests/spec/backup-file-picker.spec.tsnew file mode 100644index 000000000..6a16abb44--- /dev/null+++ b/tests/spec/backup-file-picker.spec.ts@@ -0,0 +1,877 @@+import { expect, test, type Locator, type Page, type Route } from '@playwright/test';++const VOLUME_NAME = 'backup-picker-e2e';+const BACKUP_ID = 'backup-picker-snapshot';+const SYSTEM_BACKUP_ID = 'system-picker-id';+const PROJECT_ID = 'backup-picker-project';++type BrowseRequest = {+ path: string;+ search: string;+ start: number;+ limit: string | null;+};++type BrowseHandler = (request: BrowseRequest, route: Route) => Promise<void>;++type BackupFileEntry = {+ path: string;+ name: string;+ isDirectory: boolean;+};++function response(data: unknown) {+ return { success: true, data };+}++function browseResponse(data: BackupFileEntry[], totalItems = data.length, start = 0, limit = 20) {+ return {+ success: true,+ data,+ pagination: {+ totalPages: Math.max(1, Math.ceil(totalItems / limit)),+ totalItems,+ currentPage: Math.floor(start / limit) + 1,+ itemsPerPage: limit,+ grandTotalItems: totalItems+ }+ };+}++async function mockAppShell(page: Page) {+ await page.addInitScript(() => localStorage.removeItem('selectedEnvironmentId'));+ await page.route(/\/api\/auth\/me$/, async (route) => {+ await route.fulfill({+ json: response({+ id: 'backup-picker-admin',+ username: 'arcane',+ roleAssignments: [],+ permissionsByEnv: { global: ['*'] },+ isGlobalAdmin: true,+ createdAt: new Date().toISOString()+ })+ });+ });+ await page.route(/\/api\/auth\/auto-login-config$/, async (route) => {+ await route.fulfill({ json: response({ enabled: false }) });+ });+ await page.route(/\/api\/environments(?:\?.*)?$/, async (route) => {+ await route.fulfill({+ json: {+ success: true,+ data: [+ { id: '0', name: 'Local', apiUrl: '', status: 'online', enabled: true, isEdge: false }+ ],+ pagination: { currentPage: 1, totalPages: 1, totalItems: 1, itemsPerPage: 1000 }+ }+ });+ });+ await page.route(/\/api\/environments\/0\/settings$/, async (route) => {+ await route.fulfill({ json: {} });+ });+ await page.route(/\/api\/environments\/0\/swarm\/status$/, async (route) => {+ await route.fulfill({ json: response({ enabled: false }) });+ });+ await page.route(/\/api\/oidc\/status$/, async (route) => {+ await route.fulfill({+ json: response({+ envForced: false,+ envConfigured: false,+ mergeAccounts: false,+ providerName: '',+ providerLogoUrl: ''+ })+ });+ });+ await page.route(/\/api\/roles\/available-permissions$/, async (route) => {+ await route.fulfill({ status: 503, json: { message: 'not needed for global admin' } });+ });+ await page.route(/\/api\/app-version$/, async (route) => {+ await route.fulfill({+ json: { currentVersion: 'e2e', displayVersion: 'e2e', enabledFeatures: [] }+ });+ });+ await page.route(/\/api\/stream(?:\?.*)?$/, async (route) => {+ await route.fulfill({ contentType: 'application/x-json-stream', body: '' });+ });+ await page.route(/\/api\/backups\/s3(?:\?.*)?$/, async (route) => {+ await route.fulfill({ json: response([]) });+ });+ await page.route(/\/api\/backups\/s3\/options$/, async (route) => {+ await route.fulfill({ json: response([]) });+ });+}++async function mockVolumeBackupPage(page: Page, browse: BrowseHandler) {+ const encodedVolume = encodeURIComponent(VOLUME_NAME);+ await mockAppShell(page);+ await page.route(new RegExp(`/api/environments/0/volumes/${encodedVolume}$`), async (route) => {+ await route.fulfill({+ json: response({+ id: VOLUME_NAME,+ name: VOLUME_NAME,+ driver: 'local',+ mountpoint: `/var/lib/docker/volumes/${VOLUME_NAME}/_data`,+ scope: 'local',+ options: null,+ labels: {},+ createdAt: new Date().toISOString(),+ inUse: false,+ containers: [],+ size: 0+ })+ });+ });+ await page.route(+ new RegExp(`/api/environments/0/volumes/${encodedVolume}/backup-policy$`),+ async (route) => {+ await route.fulfill({ json: response({ policies: [], s3Available: false }) });+ }+ );+ await page.route(+ new RegExp(`/api/environments/0/volumes/${encodedVolume}/backups(?:\\?.*)?$`),+ async (route) => {+ await route.fulfill({+ json: {+ success: true,+ data: [+ {+ id: BACKUP_ID,+ volumeName: VOLUME_NAME,+ size: 1024,+ createdAt: new Date().toISOString(),+ status: 'succeeded',+ trigger: 'manual',+ destination: 'local',+ format: 'rustic',+ localSnapshotId: 'snapshot'+ }+ ],+ pagination: { currentPage: 1, totalPages: 1, totalItems: 1, itemsPerPage: 10 }+ }+ });+ }+ );+ await page.route(+ new RegExp(`/api/environments/0/volumes/backups/${BACKUP_ID}/files/browse(?:\\?.*)?$`),+ async (route) => {+ const url = new URL(route.request().url());+ await browse(+ {+ path: url.searchParams.get('path') ?? '',+ search: url.searchParams.get('search') ?? '',+ start: Number(url.searchParams.get('start') ?? 0),+ limit: url.searchParams.get('limit')+ },+ route+ );+ }+ );++ await page.goto(`/volumes/${encodedVolume}?tab=backups`);+ await openVolumeRestoreFilesDialog(page);+}++type VolumeWorkspaceRestoreState = {+ content: string;+ restoredContent: string;+ revision: number;+ failRestore: boolean;+ workspaceRequests: number;+ fileRequests: number;+ restoreBodies: Array<Record<string, unknown>>;+ fullRestoreRequests: number;+};++async function mockVolumeWorkspaceRestore(page: Page, state: VolumeWorkspaceRestoreState) {+ const encodedVolume = encodeURIComponent(VOLUME_NAME);+ await page.route(+ new RegExp(`/api/environments/0/volumes/${encodedVolume}/workspace$`),+ async (route) => {+ state.workspaceRequests += 1;+ await route.fulfill({+ json: response({+ files: [+ {+ path: '/workspace.txt',+ relativePath: 'workspace.txt',+ name: 'workspace.txt',+ isDirectory: false,+ size: state.content.length,+ mode: '-rw-r--r--',+ isSymlink: false,+ editable: true+ }+ ],+ fileTreeRevision: `revision-${state.revision}`,+ fileTreeTruncated: false+ })+ });+ }+ );+ await page.route(+ new RegExp(`/api/environments/0/volumes/${encodedVolume}/workspace/file(?:\\?.*)?$`),+ async (route) => {+ state.fileRequests += 1;+ await route.fulfill({+ json: response({+ path: '/workspace.txt',+ relativePath: 'workspace.txt',+ name: 'workspace.txt',+ size: state.content.length,+ mimeType: 'text/plain',+ content: state.content,+ editable: true+ })+ });+ }+ );+ await page.route(+ new RegExp(`/api/environments/0/volumes/${encodedVolume}/backups/${BACKUP_ID}/restore-files$`),+ async (route) => {+ state.restoreBodies.push(route.request().postDataJSON() as Record<string, unknown>);+ if (state.failRestore) {+ await route.fulfill({ status: 500, json: { success: false, message: 'restore failed' } });+ return;+ }+ state.content = state.restoredContent;+ state.revision += 1;+ await route.fulfill({ json: response({ message: 'restored' }) });+ }+ );+ await page.route(+ new RegExp(`/api/environments/0/volumes/${encodedVolume}/backups/${BACKUP_ID}/restore$`),+ async (route) => {+ state.fullRestoreRequests += 1;+ state.content = state.restoredContent;+ state.revision += 1;+ await route.fulfill({ json: response({ message: 'restored' }) });+ }+ );+ await page.route(+ new RegExp(`/api/environments/0/volumes/${encodedVolume}/usage$`),+ async (route) => {+ await route.fulfill({ json: response({ inUse: false, containers: [] }) });+ }+ );+}++async function openVolumeRestoreFilesDialog(page: Page) {+ await page.getByRole('tab', { name: 'Backups', exact: true }).click();+ await expect(page.getByText(BACKUP_ID, { exact: true }).first()).toBeVisible();+ const backupRow = page.getByRole('row').filter({ hasText: BACKUP_ID }).first();+ await backupRow.getByRole('button', { name: 'Open menu' }).click();+ await page.getByRole('menuitem', { name: 'Restore files' }).click();+ await expect(page.getByRole('dialog', { name: 'Restore files' })).toBeVisible();+}++async function selectVolumeRestoreFile(page: Page) {+ const dialog = page.getByRole('dialog', { name: 'Restore files' });+ await dialog.locator('[data-path="workspace.txt"]').getByRole('checkbox').click();+ return dialog;+}++async function openVolumeWorkspaceEditor(page: Page) {+ await page.getByRole('tab', { name: 'Workspace', exact: true }).click();+ await page+ .locator('[data-path="workspace.txt"]')+ .getByRole('button', { name: 'workspace.txt', exact: true })+ .first()+ .click();+ const editor = page.locator('.arcane-code-editor .cm-content').first();+ await expect(editor).toBeVisible();+ return editor;+}++async function replaceEditorText(page: Page, editor: Locator, content: string) {+ await editor.click({ position: { x: 10, y: 10 } });+ await editor.press('ControlOrMeta+A');+ await page.keyboard.insertText(content);+}++async function mockProjectWorkspacePage(+ page: Page,+ workspaceState: () => { content: string; revision: number }+) {+ const encodedProject = encodeURIComponent(PROJECT_ID);+ await mockAppShell(page);+ await page.route(/\/api\/variables(?:\?.*)?$/, async (route) => {+ await route.fulfill({ json: response([]) });+ });+ await page.route(/\/api\/environments\/0\/projects\/tags$/, async (route) => {+ await route.fulfill({ json: response([]) });+ });+ await page.route(+ new RegExp(`/api/environments/0/projects/${encodedProject}/workspace$`),+ async (route) => {+ const state = workspaceState();+ await route.fulfill({+ json: response({+ files: [+ {+ path: `/projects/${PROJECT_ID}/workspace.txt`,+ relativePath: 'workspace.txt',+ name: 'workspace.txt',+ isDirectory: false,+ size: state.content.length,+ mode: '-rw-r--r--',+ isSymlink: false,+ editable: true+ }+ ],+ fileTreeRevision: `revision-${state.revision}`,+ fileTreeTruncated: false+ })+ });+ }+ );+ await page.route(+ new RegExp(`/api/environments/0/projects/${encodedProject}/workspace/file(?:\\?.*)?$`),+ async (route) => {+ const state = workspaceState();+ await route.fulfill({+ json: response({+ path: `/projects/${PROJECT_ID}/workspace.txt`,+ relativePath: 'workspace.txt',+ name: 'workspace.txt',+ size: state.content.length,+ mimeType: 'text/plain',+ content: state.content,+ editable: true+ })+ });+ }+ );+ await page.route(+ new RegExp(`/api/environments/0/projects/${encodedProject}(?:/(compose|runtime|updates))?$`),+ async (route) => {+ const section = new URL(route.request().url()).pathname.split('/').at(-1);+ const project = {+ id: PROJECT_ID,+ name: PROJECT_ID,+ path: `/projects/${PROJECT_ID}`,+ dirName: PROJECT_ID,+ status: 'stopped',+ serviceCount: 0,+ runningCount: 0,+ isArchived: false,+ hasBuildDirective: false,+ createdAt: new Date().toISOString(),+ updatedAt: new Date().toISOString(),+ tags: [],+ services: [],+ runtimeServices: [],+ includeFiles: []+ };++ if (section === 'compose') {+ await route.fulfill({+ json: response({+ ...project,+ composeContent: 'services: {}\n',+ envContent: '',+ overrideContent: '',+ composeFileName: 'compose.yaml'+ })+ });+ return;+ }++ await route.fulfill({ json: response(project) });+ }+ );+}++async function mockSystemBackupPage(+ page: Page,+ browse: (request: BrowseRequest, body: Record<string, unknown>, route: Route) => Promise<void>,+ restore: (body: Record<string, unknown>, route: Route) => Promise<void>,+ openPage = true+) {+ await mockAppShell(page);+ await page.route(/\/api\/backups(?:\?.*)?$/, async (route) => {+ await route.fulfill({+ json: {+ success: true,+ data: [+ {+ id: SYSTEM_BACKUP_ID,+ size: 2048,+ createdAt: new Date().toISOString(),+ status: 'succeeded',+ trigger: 'manual',+ destination: 'local',+ localSnapshotId: 'system-snapshot'+ }+ ],+ pagination: { currentPage: 1, totalPages: 1, totalItems: 1, itemsPerPage: 20 }+ }+ });+ });+ await page.route(/\/api\/backups\/policies$/, async (route) => {+ await route.fulfill({ json: response({ policies: [], recoveryKeyStored: true }) });+ });+ await page.route(+ new RegExp(`/api/backups/${SYSTEM_BACKUP_ID}/files/browse(?:\\?.*)?$`),+ async (route) => {+ const url = new URL(route.request().url());+ await browse(+ {+ path: url.searchParams.get('path') ?? '',+ search: url.searchParams.get('search') ?? '',+ start: Number(url.searchParams.get('start') ?? 0),+ limit: url.searchParams.get('limit')+ },+ route.request().postDataJSON() as Record<string, unknown>,+ route+ );+ }+ );+ await page.route(new RegExp(`/api/backups/${SYSTEM_BACKUP_ID}/restore-files$`), async (route) => {+ await restore(route.request().postDataJSON() as Record<string, unknown>, route);+ });+ if (!openPage) return;++ await page.goto('/settings/backups');+ await openSystemRestoreDialog(page);+}++async function openSystemRestoreDialog(page: Page) {+ await expect(page.getByText(SYSTEM_BACKUP_ID, { exact: false }).first()).toBeVisible();+ const backupRow = page.getByRole('row').filter({ hasText: SYSTEM_BACKUP_ID }).first();+ await backupRow.getByRole('button', { name: 'Open menu' }).click();+ await page.getByRole('menuitem', { name: 'Restore files' }).click();+ await expect(page.getByRole('dialog', { name: 'Restore files' })).toBeVisible();+}++async function navigateInApp(page: Page, path: string) {+ await page.evaluate((targetPath) => {+ const link = document.createElement('a');+ link.href = targetPath;+ document.body.append(link);+ link.click();+ link.remove();+ }, path);+ await page.waitForURL((url) => url.pathname === path.split('?')[0]);+}++function rootEntries(count: number) {+ return Array.from({ length: count }, (_, index) =>+ index === 0+ ? { path: 'folder', name: 'folder', isDirectory: true }+ : {+ path: `file-${index.toString().padStart(5, '0')}.txt`,+ name: `file-${index.toString().padStart(5, '0')}.txt`,+ isDirectory: false+ }+ );+}++test.describe('Backup file picker', () => {+ test('refreshes clean volume workspace files after restore', async ({ page }) => {+ const state: VolumeWorkspaceRestoreState = {+ content: 'before restore',+ restoredContent: 'after restore',+ revision: 1,+ failRestore: false,+ workspaceRequests: 0,+ fileRequests: 0,+ restoreBodies: [],+ fullRestoreRequests: 0+ };+ await mockVolumeWorkspaceRestore(page, state);++ await mockVolumeBackupPage(page, async (_request, route) => {+ await route.fulfill({+ json: browseResponse([{ path: 'workspace.txt', name: 'workspace.txt', isDirectory: false }])+ });+ });++ await page.keyboard.press('Escape');+ const editor = await openVolumeWorkspaceEditor(page);+ await expect(editor).toContainText('before restore');++ await openVolumeRestoreFilesDialog(page);+ const dialog = await selectVolumeRestoreFile(page);+ await expect(dialog).not.toContainText('discard all unsaved text and staged file operations');+ await dialog.getByRole('button', { name: 'Restore files' }).click();+ await expect.poll(() => state.restoreBodies.length).toBe(1);+ expect(state.restoreBodies[0]).toEqual({+ paths: ['workspace.txt'],+ selectAll: false+ });+ await expect(dialog).not.toBeVisible();+ const workspaceRequestsAfterRestore = state.workspaceRequests;+ await page.getByRole('tab', { name: 'Workspace', exact: true }).click();++ await expect(editor).toContainText('after restore');+ await expect(page.getByRole('img', { name: 'Unsaved changes' })).toHaveCount(0);+ expect(workspaceRequestsAfterRestore).toBeGreaterThanOrEqual(2);+ expect(state.workspaceRequests).toBe(workspaceRequestsAfterRestore);+ expect(state.fileRequests).toBeGreaterThanOrEqual(2);+ });++ test('warns before discarding volume workspace changes', async ({ page }) => {+ const state: VolumeWorkspaceRestoreState = {+ content: 'before restore',+ restoredContent: 'after restore',+ revision: 1,+ failRestore: false,+ workspaceRequests: 0,+ fileRequests: 0,+ restoreBodies: [],+ fullRestoreRequests: 0+ };+ await mockVolumeWorkspaceRestore(page, state);+ await mockVolumeBackupPage(page, async (_request, route) => {+ await route.fulfill({+ json: browseResponse([{ path: 'workspace.txt', name: 'workspace.txt', isDirectory: false }])+ });+ });++ await page.keyboard.press('Escape');+ const editor = await openVolumeWorkspaceEditor(page);+ await replaceEditorText(page, editor, 'local draft');+ await expect(page.getByRole('img', { name: 'Unsaved changes' })).toHaveCount(1);++ await openVolumeRestoreFilesDialog(page);+ let restoreDialog = await selectVolumeRestoreFile(page);+ await expect(restoreDialog).toContainText(+ 'discard all unsaved text and staged file operations'+ );+ await restoreDialog.getByRole('button', { name: 'Cancel', exact: true }).click();+ await expect(restoreDialog).not.toBeVisible();+ expect(state.restoreBodies).toHaveLength(0);++ await page.getByRole('tab', { name: 'Workspace', exact: true }).click();+ await expect(editor).toContainText('local draft');+ await expect(page.getByRole('img', { name: 'Unsaved changes' })).toHaveCount(1);++ state.failRestore = true;+ await openVolumeRestoreFilesDialog(page);+ restoreDialog = await selectVolumeRestoreFile(page);+ await expect(restoreDialog).toContainText(+ 'discard all unsaved text and staged file operations'+ );+ await restoreDialog.getByRole('button', { name: 'Restore files' }).click();+ await expect.poll(() => state.restoreBodies.length).toBe(1);+ await restoreDialog.getByRole('button', { name: 'Cancel', exact: true }).click();+ await expect(restoreDialog).not.toBeVisible();++ await page.getByRole('tab', { name: 'Workspace', exact: true }).click();+ await expect(editor).toContainText('local draft');+ await expect(page.getByRole('img', { name: 'Unsaved changes' })).toHaveCount(1);++ state.failRestore = false;+ await openVolumeRestoreFilesDialog(page);+ restoreDialog = await selectVolumeRestoreFile(page);+ await expect(restoreDialog).toContainText(+ 'discard all unsaved text and staged file operations'+ );+ await restoreDialog.getByRole('button', { name: 'Restore files' }).click();+ await expect.poll(() => state.restoreBodies.length).toBe(2);+ await expect(restoreDialog).not.toBeVisible();++ await page.getByRole('tab', { name: 'Workspace', exact: true }).click();+ await expect(editor).toContainText('after restore');+ await expect(page.getByRole('img', { name: 'Unsaved changes' })).toHaveCount(0);+ });++ test('warns before a full volume restore discards workspace changes', async ({ page }) => {+ const state: VolumeWorkspaceRestoreState = {+ content: 'before restore',+ restoredContent: 'after restore',+ revision: 1,+ failRestore: false,+ workspaceRequests: 0,+ fileRequests: 0,+ restoreBodies: [],+ fullRestoreRequests: 0+ };+ await mockVolumeWorkspaceRestore(page, state);+ await mockVolumeBackupPage(page, async (_request, route) => {+ await route.fulfill({+ json: browseResponse([{ path: 'workspace.txt', name: 'workspace.txt', isDirectory: false }])+ });+ });++ await page.keyboard.press('Escape');+ const editor = await openVolumeWorkspaceEditor(page);+ await replaceEditorText(page, editor, 'local draft');+ await page.getByRole('tab', { name: 'Backups', exact: true }).click();+ const backupRow = page.getByRole('row').filter({ hasText: BACKUP_ID }).first();+ await backupRow.getByRole('button', { name: 'Open menu' }).click();+ await page.getByRole('menuitem', { name: 'Restore', exact: true }).click();++ const confirmDialog = page.getByRole('dialog', { name: 'Restore Volume' });+ await expect(confirmDialog).toContainText(+ 'discard all unsaved text and staged file operations'+ );+ await confirmDialog.getByRole('button', { name: 'Cancel', exact: true }).click();+ await expect(confirmDialog).not.toBeVisible();+ expect(state.fullRestoreRequests).toBe(0);++ await page.getByRole('tab', { name: 'Workspace', exact: true }).click();+ await expect(editor).toContainText('local draft');+ await expect(page.getByRole('img', { name: 'Unsaved changes' })).toHaveCount(1);+ });++ test('refreshes clean project workspace files after a system restore', async ({ page }) => {+ const workspaceState = { content: 'before restore', revision: 1 };+ const restoreBodies: Array<Record<string, unknown>> = [];+ await mockProjectWorkspacePage(page, () => workspaceState);++ await page.goto(`/projects/${PROJECT_ID}?tab=compose`);+ await page+ .locator('[data-path="workspace.txt"]')+ .getByRole('button', { name: 'workspace.txt', exact: true })+ .first()+ .click();+ const editor = page.locator('.arcane-code-editor .cm-content').first();+ await expect(editor).toContainText('before restore');++ await mockSystemBackupPage(+ page,+ async (_request, _body, route) => {+ await route.fulfill({+ json: browseResponse([+ {+ path: `${PROJECT_ID}/workspace.txt`,+ name: 'workspace.txt',+ isDirectory: false+ }+ ])+ });+ },+ async (body, route) => {+ restoreBodies.push(body);+ workspaceState.content = 'after restore';+ workspaceState.revision += 1;+ await route.fulfill({ json: response({ message: 'restored' }) });+ },+ false+ );+ await navigateInApp(page, '/settings/backups');+ await openSystemRestoreDialog(page);++ const dialog = page.getByRole('dialog', { name: 'Restore files' });+ await dialog.locator(`[data-path="${PROJECT_ID}/workspace.txt"]`).getByRole('checkbox').click();+ await dialog.getByRole('button', { name: 'Restore files' }).click();+ await expect.poll(() => restoreBodies.length).toBe(1);++ await navigateInApp(page, `/projects/${PROJECT_ID}?tab=compose`);+ await page+ .locator('[data-path="workspace.txt"]')+ .getByRole('button', { name: 'workspace.txt', exact: true })+ .first()+ .click();+ await expect(editor).toContainText('after restore');+ await expect(page.getByRole('img', { name: 'Unsaved changes' })).toHaveCount(0);+ });++ test('loads folders lazily, walks continuation pages, retains rows on retry, and bounds mounted rows', async ({+ page+ }) => {+ const requests: BrowseRequest[] = [];+ let continuationAttempts = 0;+ await mockVolumeBackupPage(page, async (request, route) => {+ requests.push(request);+ if (request.path === 'folder/nested') {+ await route.fulfill({+ json: browseResponse([+ { path: 'folder/nested/grandchild.txt', name: 'grandchild.txt', isDirectory: false }+ ])+ });+ return;+ }+ if (request.path === 'folder') {+ await route.fulfill({+ json: browseResponse([+ { path: 'folder/nested', name: 'nested', isDirectory: true },+ { path: 'folder/child.txt', name: 'child.txt', isDirectory: false }+ ])+ });+ return;+ }+ if (request.start === 20) {+ continuationAttempts += 1;+ if (continuationAttempts === 1) {+ await route.fulfill({ status: 500, json: { message: 'interrupted' } });+ return;+ }+ await route.fulfill({+ json: browseResponse([{ path: 'last.txt', name: 'last.txt', isDirectory: false }], 21, 20)+ });+ return;+ }+ await route.fulfill({ json: browseResponse(rootEntries(20), 21) });+ });++ await expect.poll(() => requests.length).toBe(1);+ expect(requests[0]).toMatchObject({ path: '', search: '', start: 0, limit: null });+ const tree = page.locator('[data-backup-file-tree]');+ await expect(tree.locator('[data-path="folder"]')).toBeVisible();+ expect(await tree.locator('[data-path]').count()).toBeLessThan(40);++ await tree+ .locator('[data-path="folder"]')+ .getByRole('button', { name: 'Expand folder' })+ .click();+ await expect.poll(() => requests.filter((request) => request.path === 'folder').length).toBe(1);+ await expect(tree.locator('[data-path="folder/child.txt"]')).toBeVisible();+ await tree+ .locator('[data-path="folder/nested"]')+ .getByRole('button', { name: 'Expand nested' })+ .click();+ await expect+ .poll(() => requests.filter((request) => request.path === 'folder/nested').length)+ .toBe(1);+ await expect(tree.locator('[data-path="folder/nested/grandchild.txt"]')).toBeVisible();++ await tree.evaluate((element) => {+ element.scrollTop = element.scrollHeight;+ element.dispatchEvent(new Event('scroll'));+ });+ await expect(page.getByText('The remaining backup files could not be loaded.')).toBeVisible();+ expect(await tree.locator('[data-path]').count()).toBeLessThan(40);+ await tree.evaluate((element) => {+ element.scrollTop = 0;+ element.dispatchEvent(new Event('scroll'));+ });+ await expect(tree.locator('[data-path="folder"]')).toBeVisible();+ await tree.evaluate((element) => {+ element.scrollTop = element.scrollHeight;+ element.dispatchEvent(new Event('scroll'));+ });+ await expect(page.getByText('The remaining backup files could not be loaded.')).toBeVisible();+ await page.getByRole('button', { name: 'Retry' }).click();+ await expect.poll(() => continuationAttempts).toBe(2);+ await expect(tree.locator('[data-path="last.txt"]')).toBeVisible();+ });++ test('supports folder coverage, indeterminate state, global selection, and search-scoped selection', async ({+ page+ }) => {+ const restoreBodies: Array<Record<string, unknown>> = [];+ await page.route(+ new RegExp(`/api/environments/0/volumes/${VOLUME_NAME}/backups/${BACKUP_ID}/restore-files$`),+ async (route) => {+ restoreBodies.push(route.request().postDataJSON() as Record<string, unknown>);+ await route.fulfill({ json: response({ message: 'restored' }) });+ }+ );+ await mockVolumeBackupPage(page, async (request, route) => {+ if (request.search) {+ await route.fulfill({+ json: browseResponse([+ {+ path: `folder/${request.search}.txt`,+ name: `${request.search}.txt`,+ isDirectory: false+ }+ ])+ });+ return;+ }+ if (request.path === 'folder') {+ await route.fulfill({+ json: browseResponse([+ { path: 'folder/a.txt', name: 'a.txt', isDirectory: false },+ { path: 'folder/b.txt', name: 'b.txt', isDirectory: false }+ ])+ });+ return;+ }+ await route.fulfill({+ json: browseResponse([+ { path: 'folder', name: 'folder', isDirectory: true },+ { path: 'root.txt', name: 'root.txt', isDirectory: false }+ ])+ });+ });++ const dialog = page.getByRole('dialog', { name: 'Restore files' });+ const folder = dialog.locator('[data-path="folder"]');+ await folder.getByRole('button', { name: 'Expand folder' }).click();+ const child = dialog.locator('[data-path="folder/a.txt"]');+ await child.getByRole('checkbox').click();+ await expect(folder.getByRole('checkbox')).toHaveAttribute('data-state', 'indeterminate');+ await folder.getByRole('checkbox').click();+ await expect(child.getByRole('checkbox')).toBeDisabled();+ await expect(child.getByRole('checkbox')).toBeChecked();++ await dialog.getByRole('button', { name: 'Clear' }).click();+ await dialog.getByRole('button', { name: 'Select all' }).click();+ await expect(dialog.locator('[data-path="root.txt"]').getByRole('checkbox')).toBeDisabled();+ await dialog.getByRole('button', { name: 'Restore files' }).click();+ await expect.poll(() => restoreBodies.length).toBe(1);+ expect(restoreBodies[0]).toEqual({ paths: [], selectAll: true, search: '' });++ await mockVolumeBackupPage(page, async (request, route) => {+ if (request.search) {+ await route.fulfill({+ json: browseResponse([+ {+ path: `folder/${request.search}.txt`,+ name: `${request.search}.txt`,+ isDirectory: false+ }+ ])+ });+ return;+ }+ await route.fulfill({ json: browseResponse(rootEntries(2)) });+ });+ const reopened = page.getByRole('dialog', { name: 'Restore files' });+ await reopened.getByPlaceholder('Search files').fill('match');+ await expect(reopened.locator('[data-path="folder/match.txt"]')).toBeVisible();+ await reopened.getByRole('button', { name: 'Select all search matches' }).click();+ await reopened.getByPlaceholder('Search files').fill('changed');+ await expect(reopened.getByRole('button', { name: 'Restore files' })).toBeDisabled();+ });++ test('keeps a synthetic 100,000-entry result bounded to the viewport', async ({ page }) => {+ await mockVolumeBackupPage(page, async (_request, route) => {+ await route.fulfill({ json: browseResponse(rootEntries(100_000), 100_000, 0, 100_000) });+ });+ const tree = page.locator('[data-backup-file-tree]');+ await expect(tree.locator('[data-path="folder"]')).toBeVisible();+ expect(await tree.locator('[data-path]').count()).toBeLessThan(40);+ await tree.evaluate((element) => {+ element.scrollTop = element.scrollHeight / 2;+ element.dispatchEvent(new Event('scroll'));+ });+ await expect.poll(() => tree.locator('[data-path]').count()).toBeLessThan(40);+ });++ test('uses the same provider selection contract in the system backup dialog', async ({+ page+ }) => {+ const browseRequests: BrowseRequest[] = [];+ const browseBodies: Array<Record<string, unknown>> = [];+ const restoreBodies: Array<Record<string, unknown>> = [];+ await mockSystemBackupPage(+ page,+ async (request, body, route) => {+ browseRequests.push(request);+ browseBodies.push(body);+ await route.fulfill({+ json: browseResponse([{ path: 'project', name: 'project', isDirectory: true }])+ });+ },+ async (body, route) => {+ restoreBodies.push(body);+ await route.fulfill({ json: response({ message: 'restored' }) });+ }+ );++ await expect.poll(() => browseBodies.length).toBe(1);+ expect(browseBodies[0]).toEqual({ recoveryKey: '' });+ expect(browseRequests[0]).toMatchObject({ path: '', search: '', start: 0, limit: null });+ const dialog = page.getByRole('dialog', { name: 'Restore files' });+ await dialog.getByRole('button', { name: 'Select all' }).click();+ await dialog.getByRole('button', { name: 'Restore files' }).click();+ await expect.poll(() => restoreBodies.length).toBe(1);+ expect(restoreBodies[0]).toEqual({ recoveryKey: '', paths: [], selectAll: true, search: '' });+ });+});diff --git a/tests/spec/s3-backups.spec.ts b/tests/spec/s3-backups.spec.tsindex 2af32afaa..1295a7425 100644--- a/tests/spec/s3-backups.spec.ts+++ b/tests/spec/s3-backups.spec.ts@@ -80,7 +80,13 @@ async function getVolumeWorkspaceRevision(page: Page, volumeName: string) {return body.data.fileTreeRevision as string;}-async function writeVolumeFile(page: Page, volumeName: string, fileName: string, content: string) {+async function writeVolumeFile(+ page: Page,+ volumeName: string,+ fileName: string,+ content: string,+ operation: 'create_file' | 'update_file' = 'create_file'+) {const fileTreeRevision = await getVolumeWorkspaceRevision(page, volumeName);const response = await page.request.put(`/api/environments/0/volumes/${encodeURIComponent(volumeName)}/workspace`,@@ -88,7 +94,7 @@ async function writeVolumeFile(page: Page, volumeName: string, fileName: string,multipart: {manifest: JSON.stringify({fileTreeRevision,- fileChanges: [{ operation: 'create_file', relativePath: fileName, uploadIndex: 0 }]+ fileChanges: [{ operation, relativePath: fileName, uploadIndex: 0 }]}),files: { name: fileName, mimeType: 'text/plain', buffer: Buffer.from(content) }}@@ -112,25 +118,6 @@ async function readVolumeFile(page: Page, volumeName: string, filePath: string)return typeof content === 'string' ? content : null;}-async function deleteVolumeFile(page: Page, volumeName: string, filePath: string) {- const relativePath = filePath.replace(/^\/+/, '');- const fileTreeRevision = await getVolumeWorkspaceRevision(page, volumeName);- const response = await page.request.put(- `/api/environments/0/volumes/${encodeURIComponent(volumeName)}/workspace`,- {- multipart: {- manifest: JSON.stringify({- fileTreeRevision,- fileChanges: [{ operation: 'delete', relativePath }]- })- }- }- );- if (!response.ok()) {- throw new Error(`Failed to delete ${filePath}: ${response.status()} ${await response.text()}`);- }-}-async function listBackups(page: Page, volumeName: string): Promise<BackupEntry[]> {const response = await page.request.get(`/api/environments/0/volumes/${encodeURIComponent(volumeName)}/backups`@@ -259,8 +246,9 @@ test.describe('S3 Backups', () => {expect(files.status(), await files.text()).toBe(200);expect(JSON.stringify((await files.json())?.data ?? [])).toContain('payload.txt');- await deleteVolumeFile(page, volumeName, '/payload.txt');- expect(await readVolumeFile(page, volumeName, '/payload.txt')).toBeNull();+ const replacementContent = 'x'.repeat(fileContent.length);+ await writeVolumeFile(page, volumeName, 'payload.txt', replacementContent, 'update_file');+ expect(await readVolumeFile(page, volumeName, '/payload.txt')).toBe(replacementContent);const restore = await page.request.post(`/api/environments/0/volumes/${encodeURIComponent(volumeName)}/backups/${created.id}/restore`,diff --git a/types/backup/file_browser.go b/types/backup/file_browser.gonew file mode 100644index 000000000..2a2b4daf1--- /dev/null+++ b/types/backup/file_browser.go@@ -0,0 +1,16 @@+package backup++// BackupFileEntry describes one file or directory under a backup's logical+// restore root.+type BackupFileEntry struct {+ Path string `json:"path"`+ Name string `json:"name"`+ IsDirectory bool `json:"isDirectory"`+}++// RestoreSelection selects explicit paths or every eligible backup entry.+type RestoreSelection struct {+ Paths []string `json:"paths,omitempty"`+ SelectAll bool `json:"selectAll,omitempty"`+ Search string `json:"search,omitempty"`+}diff --git a/types/backup/system_backup.go b/types/backup/system_backup.goindex 2a563f7f0..c9a53a7b4 100644--- a/types/backup/system_backup.go+++ b/types/backup/system_backup.go@@ -80,6 +80,13 @@ type RestoreSystemBackupRequest struct {RecoveryKey string `json:"recoveryKey"`}+// RestoreSystemBackupFilesRequest selects project files to restore from a system backup.+type RestoreSystemBackupFilesRequest struct {+ RestoreSelection++ RecoveryKey string `json:"recoveryKey"`+}+type UploadSystemBackupRequest struct {S3DestinationID string `json:"s3DestinationId"`RecoveryKey string `json:"recoveryKey"`