mirror of
https://github.com/octoleo/restic.git
synced 2024-11-22 04:45:15 +00:00
split deleteFiles into UI and logic parts
This commit is contained in:
parent
25ac1549e7
commit
bedff1ed6d
@ -3,9 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"golang.org/x/sync/errgroup"
|
|
||||||
|
|
||||||
"github.com/restic/restic/internal/backend"
|
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,46 +21,21 @@ func DeleteFilesChecked(ctx context.Context, gopts GlobalOptions, repo restic.Re
|
|||||||
// deleteFiles deletes the given fileList of fileType in parallel
|
// deleteFiles deletes the given fileList of fileType in parallel
|
||||||
// if ignoreError=true, it will print a warning if there was an error, else it will abort.
|
// if ignoreError=true, it will print a warning if there was an error, else it will abort.
|
||||||
func deleteFiles(ctx context.Context, gopts GlobalOptions, ignoreError bool, repo restic.Repository, fileList restic.IDSet, fileType restic.FileType) error {
|
func deleteFiles(ctx context.Context, gopts GlobalOptions, ignoreError bool, repo restic.Repository, fileList restic.IDSet, fileType restic.FileType) error {
|
||||||
totalCount := len(fileList)
|
bar := newProgressMax(!gopts.JSON && !gopts.Quiet, 0, "files deleted")
|
||||||
fileChan := make(chan restic.ID)
|
|
||||||
wg, ctx := errgroup.WithContext(ctx)
|
|
||||||
wg.Go(func() error {
|
|
||||||
defer close(fileChan)
|
|
||||||
for id := range fileList {
|
|
||||||
select {
|
|
||||||
case fileChan <- id:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
bar := newProgressMax(!gopts.JSON && !gopts.Quiet, uint64(totalCount), "files deleted")
|
|
||||||
defer bar.Done()
|
defer bar.Done()
|
||||||
// deleting files is IO-bound
|
|
||||||
workerCount := repo.Connections()
|
return restic.ParallelRemove(ctx, repo, fileList, fileType, func(id restic.ID, err error) error {
|
||||||
for i := 0; i < int(workerCount); i++ {
|
|
||||||
wg.Go(func() error {
|
|
||||||
for id := range fileChan {
|
|
||||||
h := backend.Handle{Type: fileType, Name: id.String()}
|
|
||||||
err := repo.Backend().Remove(ctx, h)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !gopts.JSON {
|
if !gopts.JSON {
|
||||||
Warnf("unable to remove %v from the repository\n", h)
|
Warnf("unable to remove %v/%v from the repository\n", fileType, id)
|
||||||
}
|
}
|
||||||
if !ignoreError {
|
if !ignoreError {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !gopts.JSON && gopts.verbosity > 2 {
|
if !gopts.JSON && gopts.verbosity > 2 {
|
||||||
Verbosef("removed %v\n", h)
|
Verbosef("removed %v/%v\n", fileType, id)
|
||||||
}
|
|
||||||
bar.Add(1)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
}, bar)
|
||||||
}
|
|
||||||
err := wg.Wait()
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,9 @@ package restic
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/restic/restic/internal/backend"
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
|
"github.com/restic/restic/internal/ui/progress"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -50,3 +52,43 @@ func ParallelList(ctx context.Context, r Lister, t FileType, parallelism uint, f
|
|||||||
|
|
||||||
return wg.Wait()
|
return wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParallelRemove deletes the given fileList of fileType in parallel
|
||||||
|
// if callback returns an error, then it will abort.
|
||||||
|
func ParallelRemove(ctx context.Context, repo Repository, fileList IDSet, fileType FileType, report func(id ID, err error) error, bar *progress.Counter) error {
|
||||||
|
fileChan := make(chan ID)
|
||||||
|
wg, ctx := errgroup.WithContext(ctx)
|
||||||
|
wg.Go(func() error {
|
||||||
|
defer close(fileChan)
|
||||||
|
for id := range fileList {
|
||||||
|
select {
|
||||||
|
case fileChan <- id:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
bar.SetMax(uint64(len(fileList)))
|
||||||
|
|
||||||
|
// deleting files is IO-bound
|
||||||
|
workerCount := repo.Connections()
|
||||||
|
for i := 0; i < int(workerCount); i++ {
|
||||||
|
wg.Go(func() error {
|
||||||
|
for id := range fileChan {
|
||||||
|
h := backend.Handle{Type: fileType, Name: id.String()}
|
||||||
|
err := repo.Backend().Remove(ctx, h)
|
||||||
|
if report != nil {
|
||||||
|
err = report(id, err)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
bar.Add(1)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return wg.Wait()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user