本站源代码
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

321 lines
6.6KB

  1. package afero
  2. import (
  3. "io"
  4. "os"
  5. "path/filepath"
  6. "syscall"
  7. )
  8. // The UnionFile implements the afero.File interface and will be returned
  9. // when reading a directory present at least in the overlay or opening a file
  10. // for writing.
  11. //
  12. // The calls to
  13. // Readdir() and Readdirnames() merge the file os.FileInfo / names from the
  14. // base and the overlay - for files present in both layers, only those
  15. // from the overlay will be used.
  16. //
  17. // When opening files for writing (Create() / OpenFile() with the right flags)
  18. // the operations will be done in both layers, starting with the overlay. A
  19. // successful read in the overlay will move the cursor position in the base layer
  20. // by the number of bytes read.
  21. type UnionFile struct {
  22. Base File
  23. Layer File
  24. Merger DirsMerger
  25. off int
  26. files []os.FileInfo
  27. }
  28. func (f *UnionFile) Close() error {
  29. // first close base, so we have a newer timestamp in the overlay. If we'd close
  30. // the overlay first, we'd get a cacheStale the next time we access this file
  31. // -> cache would be useless ;-)
  32. if f.Base != nil {
  33. f.Base.Close()
  34. }
  35. if f.Layer != nil {
  36. return f.Layer.Close()
  37. }
  38. return BADFD
  39. }
  40. func (f *UnionFile) Read(s []byte) (int, error) {
  41. if f.Layer != nil {
  42. n, err := f.Layer.Read(s)
  43. if (err == nil || err == io.EOF) && f.Base != nil {
  44. // advance the file position also in the base file, the next
  45. // call may be a write at this position (or a seek with SEEK_CUR)
  46. if _, seekErr := f.Base.Seek(int64(n), os.SEEK_CUR); seekErr != nil {
  47. // only overwrite err in case the seek fails: we need to
  48. // report an eventual io.EOF to the caller
  49. err = seekErr
  50. }
  51. }
  52. return n, err
  53. }
  54. if f.Base != nil {
  55. return f.Base.Read(s)
  56. }
  57. return 0, BADFD
  58. }
  59. func (f *UnionFile) ReadAt(s []byte, o int64) (int, error) {
  60. if f.Layer != nil {
  61. n, err := f.Layer.ReadAt(s, o)
  62. if (err == nil || err == io.EOF) && f.Base != nil {
  63. _, err = f.Base.Seek(o+int64(n), os.SEEK_SET)
  64. }
  65. return n, err
  66. }
  67. if f.Base != nil {
  68. return f.Base.ReadAt(s, o)
  69. }
  70. return 0, BADFD
  71. }
  72. func (f *UnionFile) Seek(o int64, w int) (pos int64, err error) {
  73. if f.Layer != nil {
  74. pos, err = f.Layer.Seek(o, w)
  75. if (err == nil || err == io.EOF) && f.Base != nil {
  76. _, err = f.Base.Seek(o, w)
  77. }
  78. return pos, err
  79. }
  80. if f.Base != nil {
  81. return f.Base.Seek(o, w)
  82. }
  83. return 0, BADFD
  84. }
  85. func (f *UnionFile) Write(s []byte) (n int, err error) {
  86. if f.Layer != nil {
  87. n, err = f.Layer.Write(s)
  88. if err == nil && f.Base != nil { // hmm, do we have fixed size files where a write may hit the EOF mark?
  89. _, err = f.Base.Write(s)
  90. }
  91. return n, err
  92. }
  93. if f.Base != nil {
  94. return f.Base.Write(s)
  95. }
  96. return 0, BADFD
  97. }
  98. func (f *UnionFile) WriteAt(s []byte, o int64) (n int, err error) {
  99. if f.Layer != nil {
  100. n, err = f.Layer.WriteAt(s, o)
  101. if err == nil && f.Base != nil {
  102. _, err = f.Base.WriteAt(s, o)
  103. }
  104. return n, err
  105. }
  106. if f.Base != nil {
  107. return f.Base.WriteAt(s, o)
  108. }
  109. return 0, BADFD
  110. }
  111. func (f *UnionFile) Name() string {
  112. if f.Layer != nil {
  113. return f.Layer.Name()
  114. }
  115. return f.Base.Name()
  116. }
  117. // DirsMerger is how UnionFile weaves two directories together.
  118. // It takes the FileInfo slices from the layer and the base and returns a
  119. // single view.
  120. type DirsMerger func(lofi, bofi []os.FileInfo) ([]os.FileInfo, error)
  121. var defaultUnionMergeDirsFn = func(lofi, bofi []os.FileInfo) ([]os.FileInfo, error) {
  122. var files = make(map[string]os.FileInfo)
  123. for _, fi := range lofi {
  124. files[fi.Name()] = fi
  125. }
  126. for _, fi := range bofi {
  127. if _, exists := files[fi.Name()]; !exists {
  128. files[fi.Name()] = fi
  129. }
  130. }
  131. rfi := make([]os.FileInfo, len(files))
  132. i := 0
  133. for _, fi := range files {
  134. rfi[i] = fi
  135. i++
  136. }
  137. return rfi, nil
  138. }
  139. // Readdir will weave the two directories together and
  140. // return a single view of the overlayed directories.
  141. // At the end of the directory view, the error is io.EOF if c > 0.
  142. func (f *UnionFile) Readdir(c int) (ofi []os.FileInfo, err error) {
  143. var merge DirsMerger = f.Merger
  144. if merge == nil {
  145. merge = defaultUnionMergeDirsFn
  146. }
  147. if f.off == 0 {
  148. var lfi []os.FileInfo
  149. if f.Layer != nil {
  150. lfi, err = f.Layer.Readdir(-1)
  151. if err != nil {
  152. return nil, err
  153. }
  154. }
  155. var bfi []os.FileInfo
  156. if f.Base != nil {
  157. bfi, err = f.Base.Readdir(-1)
  158. if err != nil {
  159. return nil, err
  160. }
  161. }
  162. merged, err := merge(lfi, bfi)
  163. if err != nil {
  164. return nil, err
  165. }
  166. f.files = append(f.files, merged...)
  167. }
  168. if c <= 0 && len(f.files) == 0 {
  169. return f.files, nil
  170. }
  171. if f.off >= len(f.files) {
  172. return nil, io.EOF
  173. }
  174. if c <= 0 {
  175. return f.files[f.off:], nil
  176. }
  177. if c > len(f.files) {
  178. c = len(f.files)
  179. }
  180. defer func() { f.off += c }()
  181. return f.files[f.off:c], nil
  182. }
  183. func (f *UnionFile) Readdirnames(c int) ([]string, error) {
  184. rfi, err := f.Readdir(c)
  185. if err != nil {
  186. return nil, err
  187. }
  188. var names []string
  189. for _, fi := range rfi {
  190. names = append(names, fi.Name())
  191. }
  192. return names, nil
  193. }
  194. func (f *UnionFile) Stat() (os.FileInfo, error) {
  195. if f.Layer != nil {
  196. return f.Layer.Stat()
  197. }
  198. if f.Base != nil {
  199. return f.Base.Stat()
  200. }
  201. return nil, BADFD
  202. }
  203. func (f *UnionFile) Sync() (err error) {
  204. if f.Layer != nil {
  205. err = f.Layer.Sync()
  206. if err == nil && f.Base != nil {
  207. err = f.Base.Sync()
  208. }
  209. return err
  210. }
  211. if f.Base != nil {
  212. return f.Base.Sync()
  213. }
  214. return BADFD
  215. }
  216. func (f *UnionFile) Truncate(s int64) (err error) {
  217. if f.Layer != nil {
  218. err = f.Layer.Truncate(s)
  219. if err == nil && f.Base != nil {
  220. err = f.Base.Truncate(s)
  221. }
  222. return err
  223. }
  224. if f.Base != nil {
  225. return f.Base.Truncate(s)
  226. }
  227. return BADFD
  228. }
  229. func (f *UnionFile) WriteString(s string) (n int, err error) {
  230. if f.Layer != nil {
  231. n, err = f.Layer.WriteString(s)
  232. if err == nil && f.Base != nil {
  233. _, err = f.Base.WriteString(s)
  234. }
  235. return n, err
  236. }
  237. if f.Base != nil {
  238. return f.Base.WriteString(s)
  239. }
  240. return 0, BADFD
  241. }
  242. func copyToLayer(base Fs, layer Fs, name string) error {
  243. bfh, err := base.Open(name)
  244. if err != nil {
  245. return err
  246. }
  247. defer bfh.Close()
  248. // First make sure the directory exists
  249. exists, err := Exists(layer, filepath.Dir(name))
  250. if err != nil {
  251. return err
  252. }
  253. if !exists {
  254. err = layer.MkdirAll(filepath.Dir(name), 0777) // FIXME?
  255. if err != nil {
  256. return err
  257. }
  258. }
  259. // Create the file on the overlay
  260. lfh, err := layer.Create(name)
  261. if err != nil {
  262. return err
  263. }
  264. n, err := io.Copy(lfh, bfh)
  265. if err != nil {
  266. // If anything fails, clean up the file
  267. layer.Remove(name)
  268. lfh.Close()
  269. return err
  270. }
  271. bfi, err := bfh.Stat()
  272. if err != nil || bfi.Size() != n {
  273. layer.Remove(name)
  274. lfh.Close()
  275. return syscall.EIO
  276. }
  277. err = lfh.Close()
  278. if err != nil {
  279. layer.Remove(name)
  280. lfh.Close()
  281. return err
  282. }
  283. return layer.Chtimes(name, bfi.ModTime(), bfi.ModTime())
  284. }
上海开阖软件有限公司 沪ICP备12045867号-1