file.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Copyright © 2015 Steve Francia <spf@spf13.com>.
  2. // Copyright 2013 tsuru authors. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mem
  15. import (
  16. "bytes"
  17. "errors"
  18. "io"
  19. "io/fs"
  20. "os"
  21. "path/filepath"
  22. "sync"
  23. "sync/atomic"
  24. "time"
  25. "github.com/spf13/afero/internal/common"
  26. )
  27. const FilePathSeparator = string(filepath.Separator)
  28. var _ fs.ReadDirFile = &File{}
  29. type File struct {
  30. // atomic requires 64-bit alignment for struct field access
  31. at int64
  32. readDirCount int64
  33. closed bool
  34. readOnly bool
  35. fileData *FileData
  36. }
  37. func NewFileHandle(data *FileData) *File {
  38. return &File{fileData: data}
  39. }
  40. func NewReadOnlyFileHandle(data *FileData) *File {
  41. return &File{fileData: data, readOnly: true}
  42. }
  43. func (f File) Data() *FileData {
  44. return f.fileData
  45. }
  46. type FileData struct {
  47. sync.Mutex
  48. name string
  49. data []byte
  50. memDir Dir
  51. dir bool
  52. mode os.FileMode
  53. modtime time.Time
  54. uid int
  55. gid int
  56. }
  57. func (d *FileData) Name() string {
  58. d.Lock()
  59. defer d.Unlock()
  60. return d.name
  61. }
  62. func CreateFile(name string) *FileData {
  63. return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}
  64. }
  65. func CreateDir(name string) *FileData {
  66. return &FileData{name: name, memDir: &DirMap{}, dir: true, modtime: time.Now()}
  67. }
  68. func ChangeFileName(f *FileData, newname string) {
  69. f.Lock()
  70. f.name = newname
  71. f.Unlock()
  72. }
  73. func SetMode(f *FileData, mode os.FileMode) {
  74. f.Lock()
  75. f.mode = mode
  76. f.Unlock()
  77. }
  78. func SetModTime(f *FileData, mtime time.Time) {
  79. f.Lock()
  80. setModTime(f, mtime)
  81. f.Unlock()
  82. }
  83. func setModTime(f *FileData, mtime time.Time) {
  84. f.modtime = mtime
  85. }
  86. func SetUID(f *FileData, uid int) {
  87. f.Lock()
  88. f.uid = uid
  89. f.Unlock()
  90. }
  91. func SetGID(f *FileData, gid int) {
  92. f.Lock()
  93. f.gid = gid
  94. f.Unlock()
  95. }
  96. func GetFileInfo(f *FileData) *FileInfo {
  97. return &FileInfo{f}
  98. }
  99. func (f *File) Open() error {
  100. atomic.StoreInt64(&f.at, 0)
  101. atomic.StoreInt64(&f.readDirCount, 0)
  102. f.fileData.Lock()
  103. f.closed = false
  104. f.fileData.Unlock()
  105. return nil
  106. }
  107. func (f *File) Close() error {
  108. f.fileData.Lock()
  109. f.closed = true
  110. if !f.readOnly {
  111. setModTime(f.fileData, time.Now())
  112. }
  113. f.fileData.Unlock()
  114. return nil
  115. }
  116. func (f *File) Name() string {
  117. return f.fileData.Name()
  118. }
  119. func (f *File) Stat() (os.FileInfo, error) {
  120. return &FileInfo{f.fileData}, nil
  121. }
  122. func (f *File) Sync() error {
  123. return nil
  124. }
  125. func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
  126. if !f.fileData.dir {
  127. return nil, &os.PathError{Op: "readdir", Path: f.fileData.name, Err: errors.New("not a dir")}
  128. }
  129. var outLength int64
  130. f.fileData.Lock()
  131. files := f.fileData.memDir.Files()[f.readDirCount:]
  132. if count > 0 {
  133. if len(files) < count {
  134. outLength = int64(len(files))
  135. } else {
  136. outLength = int64(count)
  137. }
  138. if len(files) == 0 {
  139. err = io.EOF
  140. }
  141. } else {
  142. outLength = int64(len(files))
  143. }
  144. f.readDirCount += outLength
  145. f.fileData.Unlock()
  146. res = make([]os.FileInfo, outLength)
  147. for i := range res {
  148. res[i] = &FileInfo{files[i]}
  149. }
  150. return res, err
  151. }
  152. func (f *File) Readdirnames(n int) (names []string, err error) {
  153. fi, err := f.Readdir(n)
  154. names = make([]string, len(fi))
  155. for i, f := range fi {
  156. _, names[i] = filepath.Split(f.Name())
  157. }
  158. return names, err
  159. }
  160. // Implements fs.ReadDirFile
  161. func (f *File) ReadDir(n int) ([]fs.DirEntry, error) {
  162. fi, err := f.Readdir(n)
  163. if err != nil {
  164. return nil, err
  165. }
  166. di := make([]fs.DirEntry, len(fi))
  167. for i, f := range fi {
  168. di[i] = common.FileInfoDirEntry{FileInfo: f}
  169. }
  170. return di, nil
  171. }
  172. func (f *File) Read(b []byte) (n int, err error) {
  173. f.fileData.Lock()
  174. defer f.fileData.Unlock()
  175. if f.closed {
  176. return 0, ErrFileClosed
  177. }
  178. if len(b) > 0 && int(f.at) == len(f.fileData.data) {
  179. return 0, io.EOF
  180. }
  181. if int(f.at) > len(f.fileData.data) {
  182. return 0, io.ErrUnexpectedEOF
  183. }
  184. if len(f.fileData.data)-int(f.at) >= len(b) {
  185. n = len(b)
  186. } else {
  187. n = len(f.fileData.data) - int(f.at)
  188. }
  189. copy(b, f.fileData.data[f.at:f.at+int64(n)])
  190. atomic.AddInt64(&f.at, int64(n))
  191. return
  192. }
  193. func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
  194. prev := atomic.LoadInt64(&f.at)
  195. atomic.StoreInt64(&f.at, off)
  196. n, err = f.Read(b)
  197. atomic.StoreInt64(&f.at, prev)
  198. return
  199. }
  200. func (f *File) Truncate(size int64) error {
  201. if f.closed {
  202. return ErrFileClosed
  203. }
  204. if f.readOnly {
  205. return &os.PathError{Op: "truncate", Path: f.fileData.name, Err: errors.New("file handle is read only")}
  206. }
  207. if size < 0 {
  208. return ErrOutOfRange
  209. }
  210. f.fileData.Lock()
  211. defer f.fileData.Unlock()
  212. if size > int64(len(f.fileData.data)) {
  213. diff := size - int64(len(f.fileData.data))
  214. f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{00}, int(diff))...)
  215. } else {
  216. f.fileData.data = f.fileData.data[0:size]
  217. }
  218. setModTime(f.fileData, time.Now())
  219. return nil
  220. }
  221. func (f *File) Seek(offset int64, whence int) (int64, error) {
  222. if f.closed {
  223. return 0, ErrFileClosed
  224. }
  225. switch whence {
  226. case io.SeekStart:
  227. atomic.StoreInt64(&f.at, offset)
  228. case io.SeekCurrent:
  229. atomic.AddInt64(&f.at, offset)
  230. case io.SeekEnd:
  231. atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
  232. }
  233. return f.at, nil
  234. }
  235. func (f *File) Write(b []byte) (n int, err error) {
  236. if f.closed {
  237. return 0, ErrFileClosed
  238. }
  239. if f.readOnly {
  240. return 0, &os.PathError{Op: "write", Path: f.fileData.name, Err: errors.New("file handle is read only")}
  241. }
  242. n = len(b)
  243. cur := atomic.LoadInt64(&f.at)
  244. f.fileData.Lock()
  245. defer f.fileData.Unlock()
  246. diff := cur - int64(len(f.fileData.data))
  247. var tail []byte
  248. if n+int(cur) < len(f.fileData.data) {
  249. tail = f.fileData.data[n+int(cur):]
  250. }
  251. if diff > 0 {
  252. f.fileData.data = append(f.fileData.data, append(bytes.Repeat([]byte{00}, int(diff)), b...)...)
  253. f.fileData.data = append(f.fileData.data, tail...)
  254. } else {
  255. f.fileData.data = append(f.fileData.data[:cur], b...)
  256. f.fileData.data = append(f.fileData.data, tail...)
  257. }
  258. setModTime(f.fileData, time.Now())
  259. atomic.AddInt64(&f.at, int64(n))
  260. return
  261. }
  262. func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
  263. atomic.StoreInt64(&f.at, off)
  264. return f.Write(b)
  265. }
  266. func (f *File) WriteString(s string) (ret int, err error) {
  267. return f.Write([]byte(s))
  268. }
  269. func (f *File) Info() *FileInfo {
  270. return &FileInfo{f.fileData}
  271. }
  272. type FileInfo struct {
  273. *FileData
  274. }
  275. // Implements os.FileInfo
  276. func (s *FileInfo) Name() string {
  277. s.Lock()
  278. _, name := filepath.Split(s.name)
  279. s.Unlock()
  280. return name
  281. }
  282. func (s *FileInfo) Mode() os.FileMode {
  283. s.Lock()
  284. defer s.Unlock()
  285. return s.mode
  286. }
  287. func (s *FileInfo) ModTime() time.Time {
  288. s.Lock()
  289. defer s.Unlock()
  290. return s.modtime
  291. }
  292. func (s *FileInfo) IsDir() bool {
  293. s.Lock()
  294. defer s.Unlock()
  295. return s.dir
  296. }
  297. func (s *FileInfo) Sys() interface{} { return nil }
  298. func (s *FileInfo) Size() int64 {
  299. if s.IsDir() {
  300. return int64(42)
  301. }
  302. s.Lock()
  303. defer s.Unlock()
  304. return int64(len(s.data))
  305. }
  306. var (
  307. ErrFileClosed = errors.New("File is closed")
  308. ErrOutOfRange = errors.New("out of range")
  309. ErrTooLarge = errors.New("too large")
  310. ErrFileNotFound = os.ErrNotExist
  311. ErrFileExists = os.ErrExist
  312. ErrDestinationExists = os.ErrExist
  313. )