mtimefs.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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)
  13. PutBytes(key string, data []byte)
  14. Delete(key string)
  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 := f.load(name)
  63. if real == info.ModTime() {
  64. info = mtimeFileInfo{
  65. FileInfo: info,
  66. mtime: virtual,
  67. }
  68. }
  69. return info, nil
  70. }
  71. func (f *MtimeFS) Lstat(name string) (FileInfo, error) {
  72. info, err := f.Filesystem.Lstat(name)
  73. if err != nil {
  74. return nil, err
  75. }
  76. real, virtual := f.load(name)
  77. if real == info.ModTime() {
  78. info = mtimeFileInfo{
  79. FileInfo: info,
  80. mtime: virtual,
  81. }
  82. }
  83. return info, nil
  84. }
  85. func (f *MtimeFS) Walk(root string, walkFn WalkFunc) error {
  86. return f.Filesystem.Walk(root, func(path string, info FileInfo, err error) error {
  87. if info != nil {
  88. real, virtual := f.load(path)
  89. if real == info.ModTime() {
  90. info = mtimeFileInfo{
  91. FileInfo: info,
  92. mtime: virtual,
  93. }
  94. }
  95. }
  96. return walkFn(path, info, err)
  97. })
  98. }
  99. func (f *MtimeFS) Create(name string) (File, error) {
  100. fd, err := f.Filesystem.Create(name)
  101. if err != nil {
  102. return nil, err
  103. }
  104. return &mtimeFile{fd, f}, nil
  105. }
  106. func (f *MtimeFS) Open(name string) (File, error) {
  107. fd, err := f.Filesystem.Open(name)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return &mtimeFile{fd, f}, nil
  112. }
  113. func (f *MtimeFS) OpenFile(name string, flags int, mode FileMode) (File, error) {
  114. fd, err := f.Filesystem.OpenFile(name, flags, mode)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return &mtimeFile{fd, f}, nil
  119. }
  120. // "real" is the on disk timestamp
  121. // "virtual" is what want the timestamp to be
  122. func (f *MtimeFS) save(name string, real, virtual time.Time) {
  123. if f.caseInsensitive {
  124. name = UnicodeLowercase(name)
  125. }
  126. if real.Equal(virtual) {
  127. // If the virtual time and the real on disk time are equal we don't
  128. // need to store anything.
  129. f.db.Delete(name)
  130. return
  131. }
  132. mtime := dbMtime{
  133. real: real,
  134. virtual: virtual,
  135. }
  136. bs, _ := mtime.Marshal() // Can't fail
  137. f.db.PutBytes(name, bs)
  138. }
  139. func (f *MtimeFS) load(name string) (real, virtual time.Time) {
  140. if f.caseInsensitive {
  141. name = UnicodeLowercase(name)
  142. }
  143. data, exists := f.db.Bytes(name)
  144. if !exists {
  145. return
  146. }
  147. var mtime dbMtime
  148. if err := mtime.Unmarshal(data); err != nil {
  149. return
  150. }
  151. return mtime.real, mtime.virtual
  152. }
  153. // The mtimeFileInfo is an os.FileInfo that lies about the ModTime().
  154. type mtimeFileInfo struct {
  155. FileInfo
  156. mtime time.Time
  157. }
  158. func (m mtimeFileInfo) ModTime() time.Time {
  159. return m.mtime
  160. }
  161. type mtimeFile struct {
  162. File
  163. fs *MtimeFS
  164. }
  165. func (f *mtimeFile) Stat() (FileInfo, error) {
  166. info, err := f.File.Stat()
  167. if err != nil {
  168. return nil, err
  169. }
  170. real, virtual := f.fs.load(f.Name())
  171. if real == info.ModTime() {
  172. info = mtimeFileInfo{
  173. FileInfo: info,
  174. mtime: virtual,
  175. }
  176. }
  177. return info, nil
  178. }
  179. // The dbMtime is our database representation
  180. type dbMtime struct {
  181. real time.Time
  182. virtual time.Time
  183. }
  184. func (t *dbMtime) Marshal() ([]byte, error) {
  185. bs0, _ := t.real.MarshalBinary()
  186. bs1, _ := t.virtual.MarshalBinary()
  187. return append(bs0, bs1...), nil
  188. }
  189. func (t *dbMtime) Unmarshal(bs []byte) error {
  190. if err := t.real.UnmarshalBinary(bs[:len(bs)/2]); err != nil {
  191. return err
  192. }
  193. if err := t.virtual.UnmarshalBinary(bs[len(bs)/2:]); err != nil {
  194. return err
  195. }
  196. return nil
  197. }