2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-15 17:47:21 +00:00
restic/cmd/restic/delete.go

67 lines
1.9 KiB
Go
Raw Normal View History

2020-07-17 20:14:46 +00:00
package main
import (
"golang.org/x/sync/errgroup"
"github.com/restic/restic/internal/restic"
)
// DeleteFiles deletes the given fileList of fileType in parallel
// it will print a warning if there is an error, but continue deleting the remaining files
func DeleteFiles(gopts GlobalOptions, repo restic.Repository, fileList restic.IDSet, fileType restic.FileType) {
2021-01-30 16:25:10 +00:00
_ = deleteFiles(gopts, true, repo, fileList, fileType)
2020-07-17 20:14:46 +00:00
}
// DeleteFilesChecked deletes the given fileList of fileType in parallel
2020-07-17 20:14:46 +00:00
// if an error occurs, it will cancel and return this error
func DeleteFilesChecked(gopts GlobalOptions, repo restic.Repository, fileList restic.IDSet, fileType restic.FileType) error {
return deleteFiles(gopts, false, repo, fileList, fileType)
}
const numDeleteWorkers = 8
// 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.
func deleteFiles(gopts GlobalOptions, ignoreError bool, repo restic.Repository, fileList restic.IDSet, fileType restic.FileType) error {
totalCount := len(fileList)
fileChan := make(chan restic.ID)
wg, ctx := errgroup.WithContext(gopts.ctx)
wg.Go(func() error {
defer close(fileChan)
2020-07-17 20:14:46 +00:00
for id := range fileList {
select {
case fileChan <- id:
case <-ctx.Done():
return ctx.Err()
}
2020-07-17 20:14:46 +00:00
}
return nil
})
2020-07-17 20:14:46 +00:00
bar := newProgressMax(!gopts.JSON && !gopts.Quiet, uint64(totalCount), "files deleted")
defer bar.Done()
2020-07-17 20:14:46 +00:00
for i := 0; i < numDeleteWorkers; i++ {
wg.Go(func() error {
for id := range fileChan {
h := restic.Handle{Type: fileType, Name: id.String()}
err := repo.Backend().Remove(ctx, h)
if err != nil {
if !gopts.JSON {
Warnf("unable to remove %v from the repository\n", h)
}
if !ignoreError {
return err
}
}
if !gopts.JSON && gopts.verbosity > 2 {
2020-07-17 20:14:46 +00:00
Verbosef("removed %v\n", h)
}
bar.Add(1)
2020-07-17 20:14:46 +00:00
}
return nil
})
}
err := wg.Wait()
return err
}