Skip to content

Commit 476395c

Browse files
author
Bryan C. Mills
committed
misc: remove use of relative directories in overlayDir functions
It turns out that the relative-path support never worked in the first place. It had been masked by the fact that we ~never invoke overlayDir with an absolute path, which caused filepath.Rel to always return an error, and overlayDir to always fall back to absolute paths. Since the absolute paths seem to be working fine (and are simpler), let's stick with those. As far as I can recall, the relative paths were only a space optimization anyway. Updates #28387 Updates #30316 Change-Id: Ie8cd28f3c41ca6497ace2799f4193d7f5dde7a37 Reviewed-on: https://go-review.googlesource.com/c/go/+/208481 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 01f15b6 commit 476395c

File tree

10 files changed

+39
-68
lines changed

10 files changed

+39
-68
lines changed

misc/cgo/life/overlaydir_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
symBase, err := filepath.Rel(srcRoot, dstRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2525
if err != nil {
26-
symBase, err = filepath.Abs(srcRoot)
27-
if err != nil {
28-
return err
29-
}
26+
return err
3027
}
3128

3229
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
@@ -52,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5249
// Always copy directories (don't symlink them).
5350
// If we add a file in the overlay, we don't want to add it in the original.
5451
if info.IsDir() {
55-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5653
}
5754

5855
// If the OS supports symlinks, use them instead of copying bytes.
59-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
6057
return nil
6158
}
6259

misc/cgo/stdio/overlaydir_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
symBase, err := filepath.Rel(srcRoot, dstRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2525
if err != nil {
26-
symBase, err = filepath.Abs(srcRoot)
27-
if err != nil {
28-
return err
29-
}
26+
return err
3027
}
3128

3229
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
@@ -52,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5249
// Always copy directories (don't symlink them).
5350
// If we add a file in the overlay, we don't want to add it in the original.
5451
if info.IsDir() {
55-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5653
}
5754

5855
// If the OS supports symlinks, use them instead of copying bytes.
59-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
6057
return nil
6158
}
6259

misc/cgo/test/overlaydir_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
symBase, err := filepath.Rel(srcRoot, dstRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2525
if err != nil {
26-
symBase, err = filepath.Abs(srcRoot)
27-
if err != nil {
28-
return err
29-
}
26+
return err
3027
}
3128

3229
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
@@ -52,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5249
// Always copy directories (don't symlink them).
5350
// If we add a file in the overlay, we don't want to add it in the original.
5451
if info.IsDir() {
55-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5653
}
5754

5855
// If the OS supports symlinks, use them instead of copying bytes.
59-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
6057
return nil
6158
}
6259

misc/cgo/testcarchive/overlaydir_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
symBase, err := filepath.Rel(srcRoot, dstRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2525
if err != nil {
26-
symBase, err = filepath.Abs(srcRoot)
27-
if err != nil {
28-
return err
29-
}
26+
return err
3027
}
3128

3229
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
@@ -52,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5249
// Always copy directories (don't symlink them).
5350
// If we add a file in the overlay, we don't want to add it in the original.
5451
if info.IsDir() {
55-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5653
}
5754

5855
// If the OS supports symlinks, use them instead of copying bytes.
59-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
6057
return nil
6158
}
6259

misc/cgo/testcshared/overlaydir_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
symBase, err := filepath.Rel(srcRoot, dstRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2525
if err != nil {
26-
symBase, err = filepath.Abs(srcRoot)
27-
if err != nil {
28-
return err
29-
}
26+
return err
3027
}
3128

3229
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
@@ -52,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5249
// Always copy directories (don't symlink them).
5350
// If we add a file in the overlay, we don't want to add it in the original.
5451
if info.IsDir() {
55-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5653
}
5754

5855
// If the OS supports symlinks, use them instead of copying bytes.
59-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
6057
return nil
6158
}
6259

misc/cgo/testplugin/overlaydir_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
symBase, err := filepath.Rel(srcRoot, dstRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2525
if err != nil {
26-
symBase, err = filepath.Abs(srcRoot)
27-
if err != nil {
28-
return err
29-
}
26+
return err
3027
}
3128

3229
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
@@ -52,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5249
// Always copy directories (don't symlink them).
5350
// If we add a file in the overlay, we don't want to add it in the original.
5451
if info.IsDir() {
55-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5653
}
5754

5855
// If the OS supports symlinks, use them instead of copying bytes.
59-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
6057
return nil
6158
}
6259

misc/cgo/testshared/overlaydir_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
symBase, err := filepath.Rel(srcRoot, dstRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2525
if err != nil {
26-
symBase, err = filepath.Abs(srcRoot)
27-
if err != nil {
28-
return err
29-
}
26+
return err
3027
}
3128

3229
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
@@ -52,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5249
// Always copy directories (don't symlink them).
5350
// If we add a file in the overlay, we don't want to add it in the original.
5451
if info.IsDir() {
55-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5653
}
5754

5855
// If the OS supports symlinks, use them instead of copying bytes.
59-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
6057
return nil
6158
}
6259

misc/cgo/testso/overlaydir_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
symBase, err := filepath.Rel(srcRoot, dstRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2525
if err != nil {
26-
symBase, err = filepath.Abs(srcRoot)
27-
if err != nil {
28-
return err
29-
}
26+
return err
3027
}
3128

3229
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
@@ -52,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5249
// Always copy directories (don't symlink them).
5350
// If we add a file in the overlay, we don't want to add it in the original.
5451
if info.IsDir() {
55-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5653
}
5754

5855
// If the OS supports symlinks, use them instead of copying bytes.
59-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
6057
return nil
6158
}
6259

misc/cgo/testsovar/overlaydir_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
symBase, err := filepath.Rel(srcRoot, dstRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2525
if err != nil {
26-
symBase, err = filepath.Abs(srcRoot)
27-
if err != nil {
28-
return err
29-
}
26+
return err
3027
}
3128

3229
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
@@ -52,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5249
// Always copy directories (don't symlink them).
5350
// If we add a file in the overlay, we don't want to add it in the original.
5451
if info.IsDir() {
55-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5653
}
5754

5855
// If the OS supports symlinks, use them instead of copying bytes.
59-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
6057
return nil
6158
}
6259

misc/reboot/overlaydir_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ func overlayDir(dstRoot, srcRoot string) error {
2121
return err
2222
}
2323

24-
// If we don't use the absolute path here, exec'ing make.bash fails with
25-
// “too many levels of symbolic links”.
26-
symBase, err := filepath.Abs(srcRoot)
24+
srcRoot, err := filepath.Abs(srcRoot)
2725
if err != nil {
2826
return err
2927
}
@@ -51,11 +49,11 @@ func overlayDir(dstRoot, srcRoot string) error {
5149
// Always copy directories (don't symlink them).
5250
// If we add a file in the overlay, we don't want to add it in the original.
5351
if info.IsDir() {
54-
return os.Mkdir(dstPath, perm|0200)
52+
return os.MkdirAll(dstPath, perm|0200)
5553
}
5654

5755
// If the OS supports symlinks, use them instead of copying bytes.
58-
if err := os.Symlink(filepath.Join(symBase, suffix), dstPath); err == nil {
56+
if err := os.Symlink(srcPath, dstPath); err == nil {
5957
return nil
6058
}
6159

0 commit comments

Comments
 (0)