cacheOnReadFs.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package afero
  2. import (
  3. "os"
  4. "syscall"
  5. "time"
  6. )
  7. // If the cache duration is 0, cache time will be unlimited, i.e. once
  8. // a file is in the layer, the base will never be read again for this file.
  9. //
  10. // For cache times greater than 0, the modification time of a file is
  11. // checked. Note that a lot of file system implementations only allow a
  12. // resolution of a second for timestamps... or as the godoc for os.Chtimes()
  13. // states: "The underlying filesystem may truncate or round the values to a
  14. // less precise time unit."
  15. //
  16. // This caching union will forward all write calls also to the base file
  17. // system first. To prevent writing to the base Fs, wrap it in a read-only
  18. // filter - Note: this will also make the overlay read-only, for writing files
  19. // in the overlay, use the overlay Fs directly, not via the union Fs.
  20. type CacheOnReadFs struct {
  21. base Fs
  22. layer Fs
  23. cacheTime time.Duration
  24. }
  25. func NewCacheOnReadFs(base Fs, layer Fs, cacheTime time.Duration) Fs {
  26. return &CacheOnReadFs{base: base, layer: layer, cacheTime: cacheTime}
  27. }
  28. type cacheState int
  29. const (
  30. // not present in the overlay, unknown if it exists in the base:
  31. cacheMiss cacheState = iota
  32. // present in the overlay and in base, base file is newer:
  33. cacheStale
  34. // present in the overlay - with cache time == 0 it may exist in the base,
  35. // with cacheTime > 0 it exists in the base and is same age or newer in the
  36. // overlay
  37. cacheHit
  38. // happens if someone writes directly to the overlay without
  39. // going through this union
  40. cacheLocal
  41. )
  42. func (u *CacheOnReadFs) cacheStatus(name string) (state cacheState, fi os.FileInfo, err error) {
  43. var lfi, bfi os.FileInfo
  44. lfi, err = u.layer.Stat(name)
  45. if err == nil {
  46. if u.cacheTime == 0 {
  47. return cacheHit, lfi, nil
  48. }
  49. if lfi.ModTime().Add(u.cacheTime).Before(time.Now()) {
  50. bfi, err = u.base.Stat(name)
  51. if err != nil {
  52. return cacheLocal, lfi, nil
  53. }
  54. if bfi.ModTime().After(lfi.ModTime()) {
  55. return cacheStale, bfi, nil
  56. }
  57. }
  58. return cacheHit, lfi, nil
  59. }
  60. if err == syscall.ENOENT || os.IsNotExist(err) {
  61. return cacheMiss, nil, nil
  62. }
  63. return cacheMiss, nil, err
  64. }
  65. func (u *CacheOnReadFs) copyToLayer(name string) error {
  66. return copyToLayer(u.base, u.layer, name)
  67. }
  68. func (u *CacheOnReadFs) copyFileToLayer(name string, flag int, perm os.FileMode) error {
  69. return copyFileToLayer(u.base, u.layer, name, flag, perm)
  70. }
  71. func (u *CacheOnReadFs) Chtimes(name string, atime, mtime time.Time) error {
  72. st, _, err := u.cacheStatus(name)
  73. if err != nil {
  74. return err
  75. }
  76. switch st {
  77. case cacheLocal:
  78. case cacheHit:
  79. err = u.base.Chtimes(name, atime, mtime)
  80. case cacheStale, cacheMiss:
  81. if err := u.copyToLayer(name); err != nil {
  82. return err
  83. }
  84. err = u.base.Chtimes(name, atime, mtime)
  85. }
  86. if err != nil {
  87. return err
  88. }
  89. return u.layer.Chtimes(name, atime, mtime)
  90. }
  91. func (u *CacheOnReadFs) Chmod(name string, mode os.FileMode) error {
  92. st, _, err := u.cacheStatus(name)
  93. if err != nil {
  94. return err
  95. }
  96. switch st {
  97. case cacheLocal:
  98. case cacheHit:
  99. err = u.base.Chmod(name, mode)
  100. case cacheStale, cacheMiss:
  101. if err := u.copyToLayer(name); err != nil {
  102. return err
  103. }
  104. err = u.base.Chmod(name, mode)
  105. }
  106. if err != nil {
  107. return err
  108. }
  109. return u.layer.Chmod(name, mode)
  110. }
  111. func (u *CacheOnReadFs) Chown(name string, uid, gid int) error {
  112. st, _, err := u.cacheStatus(name)
  113. if err != nil {
  114. return err
  115. }
  116. switch st {
  117. case cacheLocal:
  118. case cacheHit:
  119. err = u.base.Chown(name, uid, gid)
  120. case cacheStale, cacheMiss:
  121. if err := u.copyToLayer(name); err != nil {
  122. return err
  123. }
  124. err = u.base.Chown(name, uid, gid)
  125. }
  126. if err != nil {
  127. return err
  128. }
  129. return u.layer.Chown(name, uid, gid)
  130. }
  131. func (u *CacheOnReadFs) Stat(name string) (os.FileInfo, error) {
  132. st, fi, err := u.cacheStatus(name)
  133. if err != nil {
  134. return nil, err
  135. }
  136. switch st {
  137. case cacheMiss:
  138. return u.base.Stat(name)
  139. default: // cacheStale has base, cacheHit and cacheLocal the layer os.FileInfo
  140. return fi, nil
  141. }
  142. }
  143. func (u *CacheOnReadFs) Rename(oldname, newname string) error {
  144. st, _, err := u.cacheStatus(oldname)
  145. if err != nil {
  146. return err
  147. }
  148. switch st {
  149. case cacheLocal:
  150. case cacheHit:
  151. err = u.base.Rename(oldname, newname)
  152. case cacheStale, cacheMiss:
  153. if err := u.copyToLayer(oldname); err != nil {
  154. return err
  155. }
  156. err = u.base.Rename(oldname, newname)
  157. }
  158. if err != nil {
  159. return err
  160. }
  161. return u.layer.Rename(oldname, newname)
  162. }
  163. func (u *CacheOnReadFs) Remove(name string) error {
  164. st, _, err := u.cacheStatus(name)
  165. if err != nil {
  166. return err
  167. }
  168. switch st {
  169. case cacheLocal:
  170. case cacheHit, cacheStale, cacheMiss:
  171. err = u.base.Remove(name)
  172. }
  173. if err != nil {
  174. return err
  175. }
  176. return u.layer.Remove(name)
  177. }
  178. func (u *CacheOnReadFs) RemoveAll(name string) error {
  179. st, _, err := u.cacheStatus(name)
  180. if err != nil {
  181. return err
  182. }
  183. switch st {
  184. case cacheLocal:
  185. case cacheHit, cacheStale, cacheMiss:
  186. err = u.base.RemoveAll(name)
  187. }
  188. if err != nil {
  189. return err
  190. }
  191. return u.layer.RemoveAll(name)
  192. }
  193. func (u *CacheOnReadFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
  194. st, _, err := u.cacheStatus(name)
  195. if err != nil {
  196. return nil, err
  197. }
  198. switch st {
  199. case cacheLocal, cacheHit:
  200. default:
  201. if err := u.copyFileToLayer(name, flag, perm); err != nil {
  202. return nil, err
  203. }
  204. }
  205. if flag&(os.O_WRONLY|syscall.O_RDWR|os.O_APPEND|os.O_CREATE|os.O_TRUNC) != 0 {
  206. bfi, err := u.base.OpenFile(name, flag, perm)
  207. if err != nil {
  208. return nil, err
  209. }
  210. lfi, err := u.layer.OpenFile(name, flag, perm)
  211. if err != nil {
  212. bfi.Close() // oops, what if O_TRUNC was set and file opening in the layer failed...?
  213. return nil, err
  214. }
  215. return &UnionFile{Base: bfi, Layer: lfi}, nil
  216. }
  217. return u.layer.OpenFile(name, flag, perm)
  218. }
  219. func (u *CacheOnReadFs) Open(name string) (File, error) {
  220. st, fi, err := u.cacheStatus(name)
  221. if err != nil {
  222. return nil, err
  223. }
  224. switch st {
  225. case cacheLocal:
  226. return u.layer.Open(name)
  227. case cacheMiss:
  228. bfi, err := u.base.Stat(name)
  229. if err != nil {
  230. return nil, err
  231. }
  232. if bfi.IsDir() {
  233. return u.base.Open(name)
  234. }
  235. if err := u.copyToLayer(name); err != nil {
  236. return nil, err
  237. }
  238. return u.layer.Open(name)
  239. case cacheStale:
  240. if !fi.IsDir() {
  241. if err := u.copyToLayer(name); err != nil {
  242. return nil, err
  243. }
  244. return u.layer.Open(name)
  245. }
  246. case cacheHit:
  247. if !fi.IsDir() {
  248. return u.layer.Open(name)
  249. }
  250. }
  251. // the dirs from cacheHit, cacheStale fall down here:
  252. bfile, _ := u.base.Open(name)
  253. lfile, err := u.layer.Open(name)
  254. if err != nil && bfile == nil {
  255. return nil, err
  256. }
  257. return &UnionFile{Base: bfile, Layer: lfile}, nil
  258. }
  259. func (u *CacheOnReadFs) Mkdir(name string, perm os.FileMode) error {
  260. err := u.base.Mkdir(name, perm)
  261. if err != nil {
  262. return err
  263. }
  264. return u.layer.MkdirAll(name, perm) // yes, MkdirAll... we cannot assume it exists in the cache
  265. }
  266. func (u *CacheOnReadFs) Name() string {
  267. return "CacheOnReadFs"
  268. }
  269. func (u *CacheOnReadFs) MkdirAll(name string, perm os.FileMode) error {
  270. err := u.base.MkdirAll(name, perm)
  271. if err != nil {
  272. return err
  273. }
  274. return u.layer.MkdirAll(name, perm)
  275. }
  276. func (u *CacheOnReadFs) Create(name string) (File, error) {
  277. bfh, err := u.base.Create(name)
  278. if err != nil {
  279. return nil, err
  280. }
  281. lfh, err := u.layer.Create(name)
  282. if err != nil {
  283. // oops, see comment about OS_TRUNC above, should we remove? then we have to
  284. // remember if the file did not exist before
  285. bfh.Close()
  286. return nil, err
  287. }
  288. return &UnionFile{Base: bfh, Layer: lfh}, nil
  289. }