mtimefs.go 5.9 KB

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