afs_impl.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package amntfs
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/spf13/afero"
  6. "os"
  7. "time"
  8. )
  9. func (fs *AMNTFS) Create(name string) (afero.File, error) {
  10. f, r := fs.getMountEntity(name)
  11. if f == nil {
  12. return nil, NewAMNTFSError(
  13. ErrNoAvailableMountPointForThisPath,
  14. fmt.Errorf("path '%' not belonging to any mounted fs", name),
  15. )
  16. }
  17. file, err := f.GetFs().Create(r)
  18. if err != nil {
  19. return nil, NewAMNTFSChildFsError(f, err)
  20. }
  21. return file, nil
  22. }
  23. func (fs *AMNTFS) Mkdir(name string, perm os.FileMode) error {
  24. f, r := fs.getMountEntity(name)
  25. if f == nil {
  26. return NewAMNTFSError(
  27. ErrNoAvailableMountPointForThisPath,
  28. fmt.Errorf("path '%' not belonging to any mounted fs", name),
  29. )
  30. }
  31. return f.GetFs().Mkdir(r, perm)
  32. }
  33. func (fs *AMNTFS) MkdirAll(path string, perm os.FileMode) error {
  34. f, r := fs.getMountEntity(path)
  35. if f == nil {
  36. return NewAMNTFSError(
  37. ErrNoAvailableMountPointForThisPath,
  38. fmt.Errorf("path '%' not belonging to any mounted fs", path),
  39. )
  40. }
  41. return f.GetFs().MkdirAll(r, perm)
  42. }
  43. func (fs *AMNTFS) Open(name string) (afero.File, error) {
  44. f, r := fs.getMountEntity(name)
  45. if f == nil {
  46. return nil, NewAMNTFSError(
  47. ErrNoAvailableMountPointForThisPath,
  48. fmt.Errorf("path '%' not belonging to any mounted fs", name),
  49. )
  50. }
  51. file, err := f.GetFs().Open(r)
  52. if err != nil {
  53. return nil, NewAMNTFSChildFsError(f, err)
  54. }
  55. return file, nil
  56. }
  57. func (fs *AMNTFS) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
  58. f, r := fs.getMountEntity(name)
  59. if f == nil {
  60. return nil, NewAMNTFSError(
  61. ErrNoAvailableMountPointForThisPath,
  62. fmt.Errorf("path '%' not belonging to any mounted fs", name),
  63. )
  64. }
  65. file, err := f.GetFs().OpenFile(r, flag, perm)
  66. if err != nil {
  67. return nil, NewAMNTFSChildFsError(f, err)
  68. }
  69. return file, nil
  70. }
  71. func (fs *AMNTFS) Remove(name string) error {
  72. f, r := fs.getMountEntity(name)
  73. if f == nil {
  74. return NewAMNTFSError(
  75. ErrNoAvailableMountPointForThisPath,
  76. fmt.Errorf("path '%' not belonging to any mounted fs", name),
  77. )
  78. }
  79. return f.GetFs().Remove(r)
  80. }
  81. func (fs *AMNTFS) RemoveAll(path string) error {
  82. f, r := fs.getMountEntity(path)
  83. if f == nil {
  84. return NewAMNTFSError(
  85. ErrNoAvailableMountPointForThisPath,
  86. fmt.Errorf("path '%' not belonging to any mounted fs", path),
  87. )
  88. }
  89. return f.GetFs().RemoveAll(r)
  90. }
  91. func (fs *AMNTFS) Rename(oldname, newname string) error {
  92. oldF, oldR := fs.getMountEntity(oldname)
  93. newF, newR := fs.getMountEntity(newname)
  94. if oldF == nil || newF == nil {
  95. return NewAMNTFSError(
  96. ErrNoAvailableMountPointForThisPath,
  97. fmt.Errorf("path '%' or '%' not belonging to any mounted fs", oldname, newname),
  98. )
  99. }
  100. if oldF != newF {
  101. return errors.New("rename across different file systems is not supported")
  102. }
  103. return oldF.GetFs().Rename(oldR, newR)
  104. }
  105. func (fs *AMNTFS) Stat(name string) (os.FileInfo, error) {
  106. f, r := fs.getMountEntity(name)
  107. if f == nil {
  108. return nil, NewAMNTFSError(
  109. ErrNoAvailableMountPointForThisPath,
  110. fmt.Errorf("path '%' not belonging to any mounted fs", name),
  111. )
  112. }
  113. return f.GetFs().Stat(r)
  114. }
  115. func (fs *AMNTFS) Name() string {
  116. return "AMNTFS"
  117. }
  118. func (fs *AMNTFS) Chmod(name string, mode os.FileMode) error {
  119. f, r := fs.getMountEntity(name)
  120. if f == nil {
  121. return NewAMNTFSError(
  122. ErrNoAvailableMountPointForThisPath,
  123. fmt.Errorf("path '%' not belonging to any mounted fs", name),
  124. )
  125. }
  126. return f.GetFs().Chmod(r, mode)
  127. }
  128. func (fs *AMNTFS) Chown(name string, uid, gid int) error {
  129. f, r := fs.getMountEntity(name)
  130. if f == nil {
  131. return NewAMNTFSError(
  132. ErrNoAvailableMountPointForThisPath,
  133. fmt.Errorf("path '%' not belonging to any mounted fs", name),
  134. )
  135. }
  136. return f.GetFs().Chown(r, uid, gid)
  137. }
  138. func (fs *AMNTFS) Chtimes(name string, atime time.Time, mtime time.Time) error {
  139. f, r := fs.getMountEntity(name)
  140. if f == nil {
  141. return NewAMNTFSError(
  142. ErrNoAvailableMountPointForThisPath,
  143. fmt.Errorf("path '%' not belonging to any mounted fs", name),
  144. )
  145. }
  146. return f.GetFs().Chtimes(r, atime, mtime)
  147. }