mtimefs.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. type mtimeFS struct {
  17. Filesystem
  18. chtimes func(string, time.Time, time.Time) error
  19. db database
  20. caseInsensitive bool
  21. }
  22. type MtimeFSOption func(*mtimeFS)
  23. func WithCaseInsensitivity(v bool) MtimeFSOption {
  24. return func(f *mtimeFS) {
  25. f.caseInsensitive = v
  26. }
  27. }
  28. // NewMtimeFS returns a filesystem with nanosecond mtime precision, regardless
  29. // of what shenanigans the underlying filesystem gets up to.
  30. func NewMtimeFS(fs Filesystem, db database, options ...MtimeFSOption) Filesystem {
  31. return wrapFilesystem(fs, func(underlying Filesystem) Filesystem {
  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. }
  43. func (f *mtimeFS) Chtimes(name string, atime, mtime time.Time) error {
  44. // Do a normal Chtimes call, don't care if it succeeds or not.
  45. f.chtimes(name, atime, mtime)
  46. // Stat the file to see what happened. Here we *do* return an error,
  47. // because it might be "does not exist" or similar.
  48. info, err := f.Filesystem.Lstat(name)
  49. if err != nil {
  50. return err
  51. }
  52. f.save(name, info.ModTime(), mtime)
  53. return nil
  54. }
  55. func (f *mtimeFS) Stat(name string) (FileInfo, error) {
  56. info, err := f.Filesystem.Stat(name)
  57. if err != nil {
  58. return nil, err
  59. }
  60. real, virtual, err := f.load(name)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if real == info.ModTime() {
  65. info = mtimeFileInfo{
  66. FileInfo: info,
  67. mtime: virtual,
  68. }
  69. }
  70. return info, nil
  71. }
  72. func (f *mtimeFS) Lstat(name string) (FileInfo, error) {
  73. info, err := f.Filesystem.Lstat(name)
  74. if err != nil {
  75. return nil, err
  76. }
  77. real, virtual, err := f.load(name)
  78. if err != nil {
  79. return nil, err
  80. }
  81. if real == info.ModTime() {
  82. info = mtimeFileInfo{
  83. FileInfo: info,
  84. mtime: virtual,
  85. }
  86. }
  87. return info, nil
  88. }
  89. func (f *mtimeFS) Walk(root string, walkFn WalkFunc) error {
  90. return f.Filesystem.Walk(root, func(path string, info FileInfo, err error) error {
  91. if info != nil {
  92. real, virtual, loadErr := f.load(path)
  93. if loadErr != nil && err == nil {
  94. // The iterator gets to deal with the error
  95. err = loadErr
  96. }
  97. if real == info.ModTime() {
  98. info = mtimeFileInfo{
  99. FileInfo: info,
  100. mtime: virtual,
  101. }
  102. }
  103. }
  104. return walkFn(path, info, err)
  105. })
  106. }
  107. func (f *mtimeFS) Create(name string) (File, error) {
  108. fd, err := f.Filesystem.Create(name)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return mtimeFile{fd, f}, nil
  113. }
  114. func (f *mtimeFS) Open(name string) (File, error) {
  115. fd, err := f.Filesystem.Open(name)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return mtimeFile{fd, f}, nil
  120. }
  121. func (f *mtimeFS) OpenFile(name string, flags int, mode FileMode) (File, error) {
  122. fd, err := f.Filesystem.OpenFile(name, flags, mode)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return mtimeFile{fd, f}, nil
  127. }
  128. // "real" is the on disk timestamp
  129. // "virtual" is what want the timestamp to be
  130. func (f *mtimeFS) save(name string, real, virtual time.Time) {
  131. if f.caseInsensitive {
  132. name = UnicodeLowercase(name)
  133. }
  134. if real.Equal(virtual) {
  135. // If the virtual time and the real on disk time are equal we don't
  136. // need to store anything.
  137. f.db.Delete(name)
  138. return
  139. }
  140. mtime := dbMtime{
  141. real: real,
  142. virtual: virtual,
  143. }
  144. bs, _ := mtime.Marshal() // Can't fail
  145. f.db.PutBytes(name, bs)
  146. }
  147. func (f *mtimeFS) load(name string) (real, virtual time.Time, err error) {
  148. if f.caseInsensitive {
  149. name = UnicodeLowercase(name)
  150. }
  151. data, exists, err := f.db.Bytes(name)
  152. if err != nil {
  153. return time.Time{}, time.Time{}, err
  154. } else if !exists {
  155. return time.Time{}, time.Time{}, nil
  156. }
  157. var mtime dbMtime
  158. if err := mtime.Unmarshal(data); err != nil {
  159. return time.Time{}, time.Time{}, err
  160. }
  161. return mtime.real, mtime.virtual, nil
  162. }
  163. // The mtimeFileInfo is an os.FileInfo that lies about the ModTime().
  164. type mtimeFileInfo struct {
  165. FileInfo
  166. mtime time.Time
  167. }
  168. func (m mtimeFileInfo) ModTime() time.Time {
  169. return m.mtime
  170. }
  171. type mtimeFile struct {
  172. File
  173. fs *mtimeFS
  174. }
  175. func (f mtimeFile) Stat() (FileInfo, error) {
  176. info, err := f.File.Stat()
  177. if err != nil {
  178. return nil, err
  179. }
  180. real, virtual, err := f.fs.load(f.Name())
  181. if err != nil {
  182. return nil, err
  183. }
  184. if real == info.ModTime() {
  185. info = mtimeFileInfo{
  186. FileInfo: info,
  187. mtime: virtual,
  188. }
  189. }
  190. return info, nil
  191. }
  192. func (f mtimeFile) unwrap() File {
  193. return f.File
  194. }
  195. // The dbMtime is our database representation
  196. type dbMtime struct {
  197. real time.Time
  198. virtual time.Time
  199. }
  200. func (t *dbMtime) Marshal() ([]byte, error) {
  201. bs0, _ := t.real.MarshalBinary()
  202. bs1, _ := t.virtual.MarshalBinary()
  203. return append(bs0, bs1...), nil
  204. }
  205. func (t *dbMtime) Unmarshal(bs []byte) error {
  206. if err := t.real.UnmarshalBinary(bs[:len(bs)/2]); err != nil {
  207. return err
  208. }
  209. if err := t.virtual.UnmarshalBinary(bs[len(bs)/2:]); err != nil {
  210. return err
  211. }
  212. return nil
  213. }