mtimefs.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 (f *mtimeFS) save(name string, real, virtual time.Time) {
  126. if f.caseInsensitive {
  127. name = UnicodeLowercaseNormalized(name)
  128. }
  129. if real.Equal(virtual) {
  130. // If the virtual time and the real on disk time are equal we don't
  131. // need to store anything.
  132. f.db.Delete(name)
  133. return
  134. }
  135. mtime := MtimeMapping{
  136. Real: real,
  137. Virtual: virtual,
  138. }
  139. bs, _ := mtime.Marshal() // Can't fail
  140. f.db.PutBytes(name, bs)
  141. }
  142. func (f *mtimeFS) load(name string) (MtimeMapping, error) {
  143. if f.caseInsensitive {
  144. name = UnicodeLowercaseNormalized(name)
  145. }
  146. data, exists, err := f.db.Bytes(name)
  147. if err != nil {
  148. return MtimeMapping{}, err
  149. } else if !exists {
  150. return MtimeMapping{}, nil
  151. }
  152. var mtime MtimeMapping
  153. if err := mtime.Unmarshal(data); err != nil {
  154. return MtimeMapping{}, err
  155. }
  156. return mtime, nil
  157. }
  158. // The mtimeFileInfo is an os.FileInfo that lies about the ModTime().
  159. type mtimeFileInfo struct {
  160. FileInfo
  161. mtime time.Time
  162. }
  163. func (m mtimeFileInfo) ModTime() time.Time {
  164. return m.mtime
  165. }
  166. type mtimeFile struct {
  167. File
  168. fs *mtimeFS
  169. }
  170. func (f mtimeFile) Stat() (FileInfo, error) {
  171. info, err := f.File.Stat()
  172. if err != nil {
  173. return nil, err
  174. }
  175. mtimeMapping, err := f.fs.load(f.Name())
  176. if err != nil {
  177. return nil, err
  178. }
  179. if mtimeMapping.Real.Equal(info.ModTime()) {
  180. info = mtimeFileInfo{
  181. FileInfo: info,
  182. mtime: mtimeMapping.Virtual,
  183. }
  184. }
  185. return info, nil
  186. }
  187. // Used by copyRange to unwrap to the real file and access SyscallConn
  188. func (f mtimeFile) unwrap() File {
  189. return f.File
  190. }
  191. // MtimeMapping represents the mapping as stored in the database
  192. type MtimeMapping struct {
  193. // "Real" is the on disk timestamp
  194. Real time.Time `json:"real"`
  195. // "Virtual" is what want the timestamp to be
  196. Virtual time.Time `json:"virtual"`
  197. }
  198. func (t *MtimeMapping) Marshal() ([]byte, error) {
  199. bs0, _ := t.Real.MarshalBinary()
  200. bs1, _ := t.Virtual.MarshalBinary()
  201. return append(bs0, bs1...), nil
  202. }
  203. func (t *MtimeMapping) Unmarshal(bs []byte) error {
  204. if err := t.Real.UnmarshalBinary(bs[:len(bs)/2]); err != nil {
  205. return err
  206. }
  207. if err := t.Virtual.UnmarshalBinary(bs[len(bs)/2:]); err != nil {
  208. return err
  209. }
  210. return nil
  211. }
  212. func GetMtimeMapping(fs Filesystem, file string) (MtimeMapping, error) {
  213. mtimeFs, ok := unwrapFilesystem[*mtimeFS](fs)
  214. if !ok {
  215. return MtimeMapping{}, errors.New("failed to unwrap")
  216. }
  217. return mtimeFs.load(file)
  218. }