mtimefs.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "time"
  9. )
  10. // The database is where we store the virtual mtimes
  11. type database interface {
  12. Bytes(key string) (data []byte, ok bool, err error)
  13. PutBytes(key string, data []byte) error
  14. Delete(key string) error
  15. }
  16. // The MtimeFS is a filesystem with nanosecond mtime precision, regardless
  17. // of what shenanigans the underlying filesystem gets up to. A nil MtimeFS
  18. // just does the underlying operations with no additions.
  19. type MtimeFS struct {
  20. Filesystem
  21. chtimes func(string, time.Time, time.Time) error
  22. db database
  23. caseInsensitive bool
  24. }
  25. type MtimeFSOption func(*MtimeFS)
  26. func WithCaseInsensitivity(v bool) MtimeFSOption {
  27. return func(f *MtimeFS) {
  28. f.caseInsensitive = v
  29. }
  30. }
  31. func NewMtimeFS(underlying Filesystem, db database, options ...MtimeFSOption) *MtimeFS {
  32. f := &MtimeFS{
  33. Filesystem: underlying,
  34. chtimes: underlying.Chtimes, // for mocking it out in the tests
  35. db: db,
  36. }
  37. for _, opt := range options {
  38. opt(f)
  39. }
  40. return f
  41. }
  42. func (f *MtimeFS) Chtimes(name string, atime, mtime time.Time) error {
  43. if f == nil {
  44. return f.chtimes(name, atime, mtime)
  45. }
  46. // Do a normal Chtimes call, don't care if it succeeds or not.
  47. f.chtimes(name, atime, mtime)
  48. // Stat the file to see what happened. Here we *do* return an error,
  49. // because it might be "does not exist" or similar.
  50. info, err := f.Filesystem.Lstat(name)
  51. if err != nil {
  52. return err
  53. }
  54. f.save(name, info.ModTime(), mtime)
  55. return nil
  56. }
  57. func (f *MtimeFS) Stat(name string) (FileInfo, error) {
  58. info, err := f.Filesystem.Stat(name)
  59. if err != nil {
  60. return nil, err
  61. }
  62. real, virtual, err := f.load(name)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if real == info.ModTime() {
  67. info = mtimeFileInfo{
  68. FileInfo: info,
  69. mtime: virtual,
  70. }
  71. }
  72. return info, nil
  73. }
  74. func (f *MtimeFS) Lstat(name string) (FileInfo, error) {
  75. info, err := f.Filesystem.Lstat(name)
  76. if err != nil {
  77. return nil, err
  78. }
  79. real, virtual, err := f.load(name)
  80. if err != nil {
  81. return nil, err
  82. }
  83. if real == info.ModTime() {
  84. info = mtimeFileInfo{
  85. FileInfo: info,
  86. mtime: virtual,
  87. }
  88. }
  89. return info, nil
  90. }
  91. func (f *MtimeFS) Walk(root string, walkFn WalkFunc) error {
  92. return f.Filesystem.Walk(root, func(path string, info FileInfo, err error) error {
  93. if info != nil {
  94. real, virtual, loadErr := f.load(path)
  95. if loadErr != nil && err == nil {
  96. // The iterator gets to deal with the error
  97. err = loadErr
  98. }
  99. if real == info.ModTime() {
  100. info = mtimeFileInfo{
  101. FileInfo: info,
  102. mtime: virtual,
  103. }
  104. }
  105. }
  106. return walkFn(path, info, err)
  107. })
  108. }
  109. func (f *MtimeFS) Create(name string) (File, error) {
  110. fd, err := f.Filesystem.Create(name)
  111. if err != nil {
  112. return nil, err
  113. }
  114. return mtimeFile{fd, f}, nil
  115. }
  116. func (f *MtimeFS) Open(name string) (File, error) {
  117. fd, err := f.Filesystem.Open(name)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return mtimeFile{fd, f}, nil
  122. }
  123. func (f *MtimeFS) OpenFile(name string, flags int, mode FileMode) (File, error) {
  124. fd, err := f.Filesystem.OpenFile(name, flags, mode)
  125. if err != nil {
  126. return nil, err
  127. }
  128. return mtimeFile{fd, f}, nil
  129. }
  130. // "real" is the on disk timestamp
  131. // "virtual" is what want the timestamp to be
  132. func (f *MtimeFS) save(name string, real, virtual time.Time) {
  133. if f.caseInsensitive {
  134. name = UnicodeLowercase(name)
  135. }
  136. if real.Equal(virtual) {
  137. // If the virtual time and the real on disk time are equal we don't
  138. // need to store anything.
  139. f.db.Delete(name)
  140. return
  141. }
  142. mtime := dbMtime{
  143. real: real,
  144. virtual: virtual,
  145. }
  146. bs, _ := mtime.Marshal() // Can't fail
  147. f.db.PutBytes(name, bs)
  148. }
  149. func (f *MtimeFS) load(name string) (real, virtual time.Time, err error) {
  150. if f.caseInsensitive {
  151. name = UnicodeLowercase(name)
  152. }
  153. data, exists, err := f.db.Bytes(name)
  154. if err != nil {
  155. return time.Time{}, time.Time{}, err
  156. } else if !exists {
  157. return time.Time{}, time.Time{}, nil
  158. }
  159. var mtime dbMtime
  160. if err := mtime.Unmarshal(data); err != nil {
  161. return time.Time{}, time.Time{}, err
  162. }
  163. return mtime.real, mtime.virtual, nil
  164. }
  165. // The mtimeFileInfo is an os.FileInfo that lies about the ModTime().
  166. type mtimeFileInfo struct {
  167. FileInfo
  168. mtime time.Time
  169. }
  170. func (m mtimeFileInfo) ModTime() time.Time {
  171. return m.mtime
  172. }
  173. type mtimeFile struct {
  174. File
  175. fs *MtimeFS
  176. }
  177. func (f mtimeFile) Stat() (FileInfo, error) {
  178. info, err := f.File.Stat()
  179. if err != nil {
  180. return nil, err
  181. }
  182. real, virtual, err := f.fs.load(f.Name())
  183. if err != nil {
  184. return nil, err
  185. }
  186. if real == info.ModTime() {
  187. info = mtimeFileInfo{
  188. FileInfo: info,
  189. mtime: virtual,
  190. }
  191. }
  192. return info, nil
  193. }
  194. func (f mtimeFile) unwrap() File {
  195. return f.File
  196. }
  197. // The dbMtime is our database representation
  198. type dbMtime struct {
  199. real time.Time
  200. virtual time.Time
  201. }
  202. func (t *dbMtime) Marshal() ([]byte, error) {
  203. bs0, _ := t.real.MarshalBinary()
  204. bs1, _ := t.virtual.MarshalBinary()
  205. return append(bs0, bs1...), nil
  206. }
  207. func (t *dbMtime) Unmarshal(bs []byte) error {
  208. if err := t.real.UnmarshalBinary(bs[:len(bs)/2]); err != nil {
  209. return err
  210. }
  211. if err := t.virtual.UnmarshalBinary(bs[len(bs)/2:]); err != nil {
  212. return err
  213. }
  214. return nil
  215. }