mtimefs.go 5.6 KB

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