2018-09-29 15:48:29 +02:00
|
|
|
// Clean the left over test files
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"fmt"
|
2018-09-29 15:48:29 +02:00
|
|
|
"regexp"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2022-06-18 16:05:12 +02:00
|
|
|
"github.com/rclone/rclone/fs/fspath"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/list"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2018-09-29 15:48:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// MatchTestRemote matches the remote names used for testing (copied
|
|
|
|
// from fstest/fstest.go so we don't have to import that and get all
|
|
|
|
// its flags)
|
2024-04-23 01:34:20 +02:00
|
|
|
var MatchTestRemote = regexp.MustCompile(`^rclone-test-[abcdefghijklmnopqrstuvwxyz0123456789]{12,24}(_segments)?$`)
|
2018-09-29 15:48:29 +02:00
|
|
|
|
|
|
|
// cleanFs runs a single clean fs for left over directories
|
2020-03-31 16:56:58 +02:00
|
|
|
func cleanFs(ctx context.Context, remote string, cleanup bool) error {
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := fs.NewFs(context.Background(), remote)
|
2018-09-29 15:48:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-31 16:56:58 +02:00
|
|
|
var lastErr error
|
|
|
|
if cleanup {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Logf(nil, "%q - running cleanup", remote)
|
2020-03-31 16:56:58 +02:00
|
|
|
err = operations.CleanUp(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
|
|
|
fs.Errorf(f, "Cleanup failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries, err := list.DirSorted(ctx, f, true, "")
|
2018-09-29 15:48:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-07 12:29:52 +01:00
|
|
|
err = entries.ForDirError(func(dir fs.Directory) error {
|
2018-09-29 15:48:29 +02:00
|
|
|
dirPath := dir.Remote()
|
2022-06-18 16:05:12 +02:00
|
|
|
fullPath := fspath.JoinRootPath(remote, dirPath)
|
2018-09-29 15:48:29 +02:00
|
|
|
if MatchTestRemote.MatchString(dirPath) {
|
|
|
|
if *dryRun {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Logf(nil, "Not Purging %s - -dry-run", fullPath)
|
2018-09-29 15:48:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Logf(nil, "Purging %s", fullPath)
|
2020-11-05 16:18:51 +01:00
|
|
|
dir, err := fs.NewFs(context.Background(), fullPath)
|
2018-09-29 15:48:29 +02:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
err = fmt.Errorf("NewFs failed: %w", err)
|
2019-02-07 12:29:52 +01:00
|
|
|
lastErr = err
|
|
|
|
fs.Errorf(fullPath, "%v", err)
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-31 16:56:58 +02:00
|
|
|
err = operations.Purge(ctx, dir, "")
|
2019-02-07 12:29:52 +01:00
|
|
|
if err != nil {
|
2022-06-08 22:54:39 +02:00
|
|
|
err = fmt.Errorf("purge failed: %w", err)
|
2019-02-07 12:29:52 +01:00
|
|
|
lastErr = err
|
|
|
|
fs.Errorf(dir, "%v", err)
|
|
|
|
return nil
|
2018-09-29 15:48:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-02-07 12:29:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return lastErr
|
2018-09-29 15:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanRemotes cleans the list of remotes passed in
|
2020-03-31 16:56:58 +02:00
|
|
|
func cleanRemotes(conf *Config) error {
|
2018-09-29 15:48:29 +02:00
|
|
|
var lastError error
|
2020-03-31 16:56:58 +02:00
|
|
|
for _, backend := range conf.Backends {
|
|
|
|
remote := backend.Remote
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Logf(nil, "%q - Cleaning", remote)
|
2020-03-31 16:56:58 +02:00
|
|
|
err := cleanFs(context.Background(), remote, backend.CleanUp)
|
2018-09-29 15:48:29 +02:00
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Logf(nil, "Failed to purge %q: %v", remote, err)
|
2018-09-29 15:48:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return lastError
|
|
|
|
}
|