removeall.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package removeall
  14. import (
  15. "fmt"
  16. "io"
  17. "os"
  18. "syscall"
  19. "k8s.io/kubernetes/pkg/util/mount"
  20. )
  21. // RemoveAllOneFilesystem removes path and any children it contains.
  22. // It removes everything it can but returns the first error
  23. // it encounters. If the path does not exist, RemoveAll
  24. // returns nil (no error).
  25. // It makes sure it does not cross mount boundary, i.e. it does *not* remove
  26. // files from another filesystems. Like 'rm -rf --one-file-system'.
  27. // It is copied from RemoveAll() sources, with IsLikelyNotMountPoint
  28. func RemoveAllOneFilesystem(mounter mount.Interface, path string) error {
  29. // Simple case: if Remove works, we're done.
  30. err := os.Remove(path)
  31. if err == nil || os.IsNotExist(err) {
  32. return nil
  33. }
  34. // Otherwise, is this a directory we need to recurse into?
  35. dir, serr := os.Lstat(path)
  36. if serr != nil {
  37. if serr, ok := serr.(*os.PathError); ok && (os.IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
  38. return nil
  39. }
  40. return serr
  41. }
  42. if !dir.IsDir() {
  43. // Not a directory; return the error from Remove.
  44. return err
  45. }
  46. // Directory.
  47. isNotMount, err := mounter.IsLikelyNotMountPoint(path)
  48. if err != nil {
  49. return err
  50. }
  51. if !isNotMount {
  52. return fmt.Errorf("cannot delete directory %s: it is a mount point", path)
  53. }
  54. fd, err := os.Open(path)
  55. if err != nil {
  56. if os.IsNotExist(err) {
  57. // Race. It was deleted between the Lstat and Open.
  58. // Return nil per RemoveAll's docs.
  59. return nil
  60. }
  61. return err
  62. }
  63. // Remove contents & return first error.
  64. err = nil
  65. for {
  66. names, err1 := fd.Readdirnames(100)
  67. for _, name := range names {
  68. err1 := RemoveAllOneFilesystem(mounter, path+string(os.PathSeparator)+name)
  69. if err == nil {
  70. err = err1
  71. }
  72. }
  73. if err1 == io.EOF {
  74. break
  75. }
  76. // If Readdirnames returned an error, use it.
  77. if err == nil {
  78. err = err1
  79. }
  80. if len(names) == 0 {
  81. break
  82. }
  83. }
  84. // Close directory, because windows won't remove opened directory.
  85. fd.Close()
  86. // Remove directory.
  87. err1 := os.Remove(path)
  88. if err1 == nil || os.IsNotExist(err1) {
  89. return nil
  90. }
  91. if err == nil {
  92. err = err1
  93. }
  94. return err
  95. }