本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
6.7KB

  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. "os"
  20. "path/filepath"
  21. "sync"
  22. "sync/atomic"
  23. )
  24. import "time"
  25. const FilePathSeparator = string(filepath.Separator)
  26. type File struct {
  27. // atomic requires 64-bit alignment for struct field access
  28. at int64
  29. readDirCount int64
  30. closed bool
  31. readOnly bool
  32. fileData *FileData
  33. }
  34. func NewFileHandle(data *FileData) *File {
  35. return &File{fileData: data}
  36. }
  37. func NewReadOnlyFileHandle(data *FileData) *File {
  38. return &File{fileData: data, readOnly: true}
  39. }
  40. func (f File) Data() *FileData {
  41. return f.fileData
  42. }
  43. type FileData struct {
  44. sync.Mutex
  45. name string
  46. data []byte
  47. memDir Dir
  48. dir bool
  49. mode os.FileMode
  50. modtime time.Time
  51. }
  52. func (d *FileData) Name() string {
  53. d.Lock()
  54. defer d.Unlock()
  55. return d.name
  56. }
  57. func CreateFile(name string) *FileData {
  58. return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}
  59. }
  60. func CreateDir(name string) *FileData {
  61. return &FileData{name: name, memDir: &DirMap{}, dir: true}
  62. }
  63. func ChangeFileName(f *FileData, newname string) {
  64. f.Lock()
  65. f.name = newname
  66. f.Unlock()
  67. }
  68. func SetMode(f *FileData, mode os.FileMode) {
  69. f.Lock()
  70. f.mode = mode
  71. f.Unlock()
  72. }
  73. func SetModTime(f *FileData, mtime time.Time) {
  74. f.Lock()
  75. setModTime(f, mtime)
  76. f.Unlock()
  77. }
  78. func setModTime(f *FileData, mtime time.Time) {
  79. f.modtime = mtime
  80. }
  81. func GetFileInfo(f *FileData) *FileInfo {
  82. return &FileInfo{f}
  83. }
  84. func (f *File) Open() error {
  85. atomic.StoreInt64(&f.at, 0)
  86. atomic.StoreInt64(&f.readDirCount, 0)
  87. f.fileData.Lock()
  88. f.closed = false
  89. f.fileData.Unlock()
  90. return nil
  91. }
  92. func (f *File) Close() error {
  93. f.fileData.Lock()
  94. f.closed = true
  95. if !f.readOnly {
  96. setModTime(f.fileData, time.Now())
  97. }
  98. f.fileData.Unlock()
  99. return nil
  100. }
  101. func (f *File) Name() string {
  102. return f.fileData.Name()
  103. }
  104. func (f *File) Stat() (os.FileInfo, error) {
  105. return &FileInfo{f.fileData}, nil
  106. }
  107. func (f *File) Sync() error {
  108. return nil
  109. }
  110. func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
  111. if !f.fileData.dir {
  112. return nil, &os.PathError{Op: "readdir", Path: f.fileData.name, Err: errors.New("not a dir")}
  113. }
  114. var outLength int64
  115. f.fileData.Lock()
  116. files := f.fileData.memDir.Files()[f.readDirCount:]
  117. if count > 0 {
  118. if len(files) < count {
  119. outLength = int64(len(files))
  120. } else {
  121. outLength = int64(count)
  122. }
  123. if len(files) == 0 {
  124. err = io.EOF
  125. }
  126. } else {
  127. outLength = int64(len(files))
  128. }
  129. f.readDirCount += outLength
  130. f.fileData.Unlock()
  131. res = make([]os.FileInfo, outLength)
  132. for i := range res {
  133. res[i] = &FileInfo{files[i]}
  134. }
  135. return res, err
  136. }
  137. func (f *File) Readdirnames(n int) (names []string, err error) {
  138. fi, err := f.Readdir(n)
  139. names = make([]string, len(fi))
  140. for i, f := range fi {
  141. _, names[i] = filepath.Split(f.Name())
  142. }
  143. return names, err
  144. }
  145. func (f *File) Read(b []byte) (n int, err error) {
  146. f.fileData.Lock()
  147. defer f.fileData.Unlock()
  148. if f.closed == true {
  149. return 0, ErrFileClosed
  150. }
  151. if len(b) > 0 && int(f.at) == len(f.fileData.data) {
  152. return 0, io.EOF
  153. }
  154. if int(f.at) > len(f.fileData.data) {
  155. return 0, io.ErrUnexpectedEOF
  156. }
  157. if len(f.fileData.data)-int(f.at) >= len(b) {
  158. n = len(b)
  159. } else {
  160. n = len(f.fileData.data) - int(f.at)
  161. }
  162. copy(b, f.fileData.data[f.at:f.at+int64(n)])
  163. atomic.AddInt64(&f.at, int64(n))
  164. return
  165. }
  166. func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
  167. atomic.StoreInt64(&f.at, off)
  168. return f.Read(b)
  169. }
  170. func (f *File) Truncate(size int64) error {
  171. if f.closed == true {
  172. return ErrFileClosed
  173. }
  174. if f.readOnly {
  175. return &os.PathError{Op: "truncate", Path: f.fileData.name, Err: errors.New("file handle is read only")}
  176. }
  177. if size < 0 {
  178. return ErrOutOfRange
  179. }
  180. if size > int64(len(f.fileData.data)) {
  181. diff := size - int64(len(f.fileData.data))
  182. f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{00}, int(diff))...)
  183. } else {
  184. f.fileData.data = f.fileData.data[0:size]
  185. }
  186. setModTime(f.fileData, time.Now())
  187. return nil
  188. }
  189. func (f *File) Seek(offset int64, whence int) (int64, error) {
  190. if f.closed == true {
  191. return 0, ErrFileClosed
  192. }
  193. switch whence {
  194. case 0:
  195. atomic.StoreInt64(&f.at, offset)
  196. case 1:
  197. atomic.AddInt64(&f.at, int64(offset))
  198. case 2:
  199. atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
  200. }
  201. return f.at, nil
  202. }
  203. func (f *File) Write(b []byte) (n int, err error) {
  204. if f.readOnly {
  205. return 0, &os.PathError{Op: "write", Path: f.fileData.name, Err: errors.New("file handle is read only")}
  206. }
  207. n = len(b)
  208. cur := atomic.LoadInt64(&f.at)
  209. f.fileData.Lock()
  210. defer f.fileData.Unlock()
  211. diff := cur - int64(len(f.fileData.data))
  212. var tail []byte
  213. if n+int(cur) < len(f.fileData.data) {
  214. tail = f.fileData.data[n+int(cur):]
  215. }
  216. if diff > 0 {
  217. f.fileData.data = append(bytes.Repeat([]byte{00}, int(diff)), b...)
  218. f.fileData.data = append(f.fileData.data, tail...)
  219. } else {
  220. f.fileData.data = append(f.fileData.data[:cur], b...)
  221. f.fileData.data = append(f.fileData.data, tail...)
  222. }
  223. setModTime(f.fileData, time.Now())
  224. atomic.StoreInt64(&f.at, int64(len(f.fileData.data)))
  225. return
  226. }
  227. func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
  228. atomic.StoreInt64(&f.at, off)
  229. return f.Write(b)
  230. }
  231. func (f *File) WriteString(s string) (ret int, err error) {
  232. return f.Write([]byte(s))
  233. }
  234. func (f *File) Info() *FileInfo {
  235. return &FileInfo{f.fileData}
  236. }
  237. type FileInfo struct {
  238. *FileData
  239. }
  240. // Implements os.FileInfo
  241. func (s *FileInfo) Name() string {
  242. s.Lock()
  243. _, name := filepath.Split(s.name)
  244. s.Unlock()
  245. return name
  246. }
  247. func (s *FileInfo) Mode() os.FileMode {
  248. s.Lock()
  249. defer s.Unlock()
  250. return s.mode
  251. }
  252. func (s *FileInfo) ModTime() time.Time {
  253. s.Lock()
  254. defer s.Unlock()
  255. return s.modtime
  256. }
  257. func (s *FileInfo) IsDir() bool {
  258. s.Lock()
  259. defer s.Unlock()
  260. return s.dir
  261. }
  262. func (s *FileInfo) Sys() interface{} { return nil }
  263. func (s *FileInfo) Size() int64 {
  264. if s.IsDir() {
  265. return int64(42)
  266. }
  267. s.Lock()
  268. defer s.Unlock()
  269. return int64(len(s.data))
  270. }
  271. var (
  272. ErrFileClosed = errors.New("File is closed")
  273. ErrOutOfRange = errors.New("Out of range")
  274. ErrTooLarge = errors.New("Too large")
  275. ErrFileNotFound = os.ErrNotExist
  276. ErrFileExists = os.ErrExist
  277. ErrDestinationExists = os.ErrExist
  278. )
上海开阖软件有限公司 沪ICP备12045867号-1