62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// removeRunTree preserves the owned-tree cleanup behavior of RemoveAll while
|
|
// honoring maintenance cancellation between directory chunks and entries.
|
|
// A canceled pass leaves staged data measured/reported, never claimed freed.
|
|
func removeRunTree(ctx context.Context, path string) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
info, err := os.Lstat(path)
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !info.IsDir() {
|
|
return os.Remove(path)
|
|
}
|
|
directory, err := os.Open(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer directory.Close()
|
|
actual, err := directory.Stat()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !os.SameFile(info, actual) {
|
|
return fmt.Errorf("cleanup directory changed: %s", path)
|
|
}
|
|
for {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
entries, err := directory.ReadDir(256)
|
|
if err != nil && err != io.EOF {
|
|
return err
|
|
}
|
|
for _, entry := range entries {
|
|
if err := removeRunTree(ctx, filepath.Join(path, entry.Name())); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
return os.Remove(path)
|
|
}
|