realfs.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 2018 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 fs
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "log"
  18. "os"
  19. "path/filepath"
  20. )
  21. var _ FileSystem = realFS{}
  22. // realFS implements FileSystem using the local filesystem.
  23. type realFS struct{}
  24. // MakeRealFS makes an instance of realFS.
  25. func MakeRealFS() FileSystem {
  26. return realFS{}
  27. }
  28. // Create delegates to os.Create.
  29. func (realFS) Create(name string) (File, error) { return os.Create(name) }
  30. // Mkdir delegates to os.Mkdir.
  31. func (realFS) Mkdir(name string) error {
  32. return os.Mkdir(name, 0777|os.ModeDir)
  33. }
  34. // MkdirAll delegates to os.MkdirAll.
  35. func (realFS) MkdirAll(name string) error {
  36. return os.MkdirAll(name, 0777|os.ModeDir)
  37. }
  38. // RemoveAll delegates to os.RemoveAll.
  39. func (realFS) RemoveAll(name string) error {
  40. return os.RemoveAll(name)
  41. }
  42. // Open delegates to os.Open.
  43. func (realFS) Open(name string) (File, error) { return os.Open(name) }
  44. // CleanedAbs returns a cleaned, absolute path
  45. // with no symbolic links split into directory
  46. // and file components. If the entire path is
  47. // a directory, the file component is an empty
  48. // string.
  49. func (x realFS) CleanedAbs(
  50. path string) (ConfirmedDir, string, error) {
  51. absRoot, err := filepath.Abs(path)
  52. if err != nil {
  53. return "", "", fmt.Errorf(
  54. "abs path error on '%s' : %v", path, err)
  55. }
  56. deLinked, err := filepath.EvalSymlinks(absRoot)
  57. if err != nil {
  58. return "", "", fmt.Errorf(
  59. "evalsymlink failure on '%s' : %v", path, err)
  60. }
  61. if x.IsDir(deLinked) {
  62. return ConfirmedDir(deLinked), "", nil
  63. }
  64. d := filepath.Dir(deLinked)
  65. if !x.IsDir(d) {
  66. // Programmer/assumption error.
  67. log.Fatalf("first part of '%s' not a directory", deLinked)
  68. }
  69. if d == deLinked {
  70. // Programmer/assumption error.
  71. log.Fatalf("d '%s' should be a subset of deLinked", d)
  72. }
  73. f := filepath.Base(deLinked)
  74. if filepath.Join(d, f) != deLinked {
  75. // Programmer/assumption error.
  76. log.Fatalf("these should be equal: '%s', '%s'",
  77. filepath.Join(d, f), deLinked)
  78. }
  79. return ConfirmedDir(d), f, nil
  80. }
  81. // Exists returns true if os.Stat succeeds.
  82. func (realFS) Exists(name string) bool {
  83. _, err := os.Stat(name)
  84. return err == nil
  85. }
  86. // Glob returns the list of matching files
  87. func (realFS) Glob(pattern string) ([]string, error) {
  88. return filepath.Glob(pattern)
  89. }
  90. // IsDir delegates to os.Stat and FileInfo.IsDir
  91. func (realFS) IsDir(name string) bool {
  92. info, err := os.Stat(name)
  93. if err != nil {
  94. return false
  95. }
  96. return info.IsDir()
  97. }
  98. // ReadFile delegates to ioutil.ReadFile.
  99. func (realFS) ReadFile(name string) ([]byte, error) { return ioutil.ReadFile(name) }
  100. // WriteFile delegates to ioutil.WriteFile with read/write permissions.
  101. func (realFS) WriteFile(name string, c []byte) error {
  102. return ioutil.WriteFile(name, c, 0666)
  103. }