github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fstest/test_all/clean.go (about) 1 // Clean the left over test files 2 3 package main 4 5 import ( 6 "context" 7 "fmt" 8 "log" 9 "regexp" 10 11 "github.com/rclone/rclone/fs" 12 "github.com/rclone/rclone/fs/fspath" 13 "github.com/rclone/rclone/fs/list" 14 "github.com/rclone/rclone/fs/operations" 15 ) 16 17 // MatchTestRemote matches the remote names used for testing (copied 18 // from fstest/fstest.go so we don't have to import that and get all 19 // its flags) 20 var MatchTestRemote = regexp.MustCompile(`^rclone-test-[abcdefghijklmnopqrstuvwxyz0123456789]{24}(_segments)?$`) 21 22 // cleanFs runs a single clean fs for left over directories 23 func cleanFs(ctx context.Context, remote string, cleanup bool) error { 24 f, err := fs.NewFs(context.Background(), remote) 25 if err != nil { 26 return err 27 } 28 var lastErr error 29 if cleanup { 30 log.Printf("%q - running cleanup", remote) 31 err = operations.CleanUp(ctx, f) 32 if err != nil { 33 lastErr = err 34 fs.Errorf(f, "Cleanup failed: %v", err) 35 } 36 } 37 entries, err := list.DirSorted(ctx, f, true, "") 38 if err != nil { 39 return err 40 } 41 err = entries.ForDirError(func(dir fs.Directory) error { 42 dirPath := dir.Remote() 43 fullPath := fspath.JoinRootPath(remote, dirPath) 44 if MatchTestRemote.MatchString(dirPath) { 45 if *dryRun { 46 log.Printf("Not Purging %s - -dry-run", fullPath) 47 return nil 48 } 49 log.Printf("Purging %s", fullPath) 50 dir, err := fs.NewFs(context.Background(), fullPath) 51 if err != nil { 52 err = fmt.Errorf("NewFs failed: %w", err) 53 lastErr = err 54 fs.Errorf(fullPath, "%v", err) 55 return nil 56 } 57 err = operations.Purge(ctx, dir, "") 58 if err != nil { 59 err = fmt.Errorf("purge failed: %w", err) 60 lastErr = err 61 fs.Errorf(dir, "%v", err) 62 return nil 63 } 64 } 65 return nil 66 }) 67 if err != nil { 68 return err 69 } 70 return lastErr 71 } 72 73 // cleanRemotes cleans the list of remotes passed in 74 func cleanRemotes(conf *Config) error { 75 var lastError error 76 for _, backend := range conf.Backends { 77 remote := backend.Remote 78 log.Printf("%q - Cleaning", remote) 79 err := cleanFs(context.Background(), remote, backend.CleanUp) 80 if err != nil { 81 lastError = err 82 log.Printf("Failed to purge %q: %v", remote, err) 83 } 84 } 85 return lastError 86 }