Merge "Handle readonly directories in tmpdir"

This commit is contained in:
Treehugger Robot 2018-05-15 04:38:19 +00:00 committed by Gerrit Code Review
commit 2eb7e2135c
2 changed files with 53 additions and 4 deletions

View File

@ -62,9 +62,24 @@ func ensureDirectoriesExist(ctx Context, dirs ...string) {
func ensureEmptyDirectoriesExist(ctx Context, dirs ...string) {
// remove all the directories
for _, dir := range dirs {
err := os.RemoveAll(dir)
if err != nil {
ctx.Fatalf("Error removing %s: %q\n", dir, err)
seenErr := map[string]bool{}
for {
err := os.RemoveAll(dir)
if err == nil {
break
}
if pathErr, ok := err.(*os.PathError); !ok ||
dir == pathErr.Path || seenErr[pathErr.Path] {
ctx.Fatalf("Error removing %s: %q\n", dir, err)
} else {
seenErr[pathErr.Path] = true
err = os.Chmod(filepath.Dir(pathErr.Path), 0700)
if err != nil {
ctx.Fatal(err)
}
}
}
}
// recreate all the directories

View File

@ -14,7 +14,41 @@
package build
import "testing"
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"android/soong/ui/logger"
)
func TestEnsureEmptyDirs(t *testing.T) {
ctx := testContext()
defer logger.Recover(func(err error) {
t.Error(err)
})
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer func() {
err := os.RemoveAll(tmpDir)
if err != nil {
t.Errorf("Error removing tmpDir: %v", err)
}
}()
ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a/b"))
err = os.Chmod(filepath.Join(tmpDir, "a"), 0555)
if err != nil {
t.Fatalf("Failed to chown: %v", err)
}
ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a"))
}
func TestStripAnsiEscapes(t *testing.T) {
testcases := []struct {