defaultfs.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 filesystem
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "path/filepath"
  18. "time"
  19. )
  20. // DefaultFs implements Filesystem using same-named functions from "os" and "io/ioutil"
  21. type DefaultFs struct{}
  22. var _ Filesystem = DefaultFs{}
  23. // Stat via os.Stat
  24. func (DefaultFs) Stat(name string) (os.FileInfo, error) {
  25. return os.Stat(name)
  26. }
  27. // Create via os.Create
  28. func (DefaultFs) Create(name string) (File, error) {
  29. file, err := os.Create(name)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &defaultFile{file}, nil
  34. }
  35. // Rename via os.Rename
  36. func (DefaultFs) Rename(oldpath, newpath string) error {
  37. return os.Rename(oldpath, newpath)
  38. }
  39. // MkdirAll via os.MkdirAll
  40. func (DefaultFs) MkdirAll(path string, perm os.FileMode) error {
  41. return os.MkdirAll(path, perm)
  42. }
  43. // Chtimes via os.Chtimes
  44. func (DefaultFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
  45. return os.Chtimes(name, atime, mtime)
  46. }
  47. // RemoveAll via os.RemoveAll
  48. func (DefaultFs) RemoveAll(path string) error {
  49. return os.RemoveAll(path)
  50. }
  51. // Remove via os.RemoveAll
  52. func (DefaultFs) Remove(name string) error {
  53. return os.Remove(name)
  54. }
  55. // ReadFile via ioutil.ReadFile
  56. func (DefaultFs) ReadFile(filename string) ([]byte, error) {
  57. return ioutil.ReadFile(filename)
  58. }
  59. // TempDir via ioutil.TempDir
  60. func (DefaultFs) TempDir(dir, prefix string) (string, error) {
  61. return ioutil.TempDir(dir, prefix)
  62. }
  63. // TempFile via ioutil.TempFile
  64. func (DefaultFs) TempFile(dir, prefix string) (File, error) {
  65. file, err := ioutil.TempFile(dir, prefix)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &defaultFile{file}, nil
  70. }
  71. // ReadDir via ioutil.ReadDir
  72. func (DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) {
  73. return ioutil.ReadDir(dirname)
  74. }
  75. // Walk via filepath.Walk
  76. func (DefaultFs) Walk(root string, walkFn filepath.WalkFunc) error {
  77. return filepath.Walk(root, walkFn)
  78. }
  79. // defaultFile implements File using same-named functions from "os"
  80. type defaultFile struct {
  81. file *os.File
  82. }
  83. // Name via os.File.Name
  84. func (file *defaultFile) Name() string {
  85. return file.file.Name()
  86. }
  87. // Write via os.File.Write
  88. func (file *defaultFile) Write(b []byte) (n int, err error) {
  89. return file.file.Write(b)
  90. }
  91. // Sync via os.File.Sync
  92. func (file *defaultFile) Sync() error {
  93. return file.file.Sync()
  94. }
  95. // Close via os.File.Close
  96. func (file *defaultFile) Close() error {
  97. return file.file.Close()
  98. }