fakefs.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. // Copyright (C) 2018 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. "context"
  9. "errors"
  10. "fmt"
  11. "hash/fnv"
  12. "io"
  13. "io/ioutil"
  14. "math/rand"
  15. "net/url"
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "testing"
  22. "time"
  23. )
  24. // see readShortAt()
  25. const randomBlockShift = 14 // 128k
  26. // fakeFS is a fake filesystem for testing and benchmarking. It has the
  27. // following properties:
  28. //
  29. // - File metadata is kept in RAM. Specifically, we remember which files and
  30. // directories exist, their dates, permissions and sizes. Symlinks are
  31. // not supported.
  32. //
  33. // - File contents are generated pseudorandomly with just the file name as
  34. // seed. Writes are discarded, other than having the effect of increasing
  35. // the file size. If you only write data that you've read from a file with
  36. // the same name on a different fakeFS, you'll never know the difference...
  37. //
  38. // - We totally ignore permissions - pretend you are root.
  39. //
  40. // - The root path can contain URL query-style parameters that pre populate
  41. // the filesystem at creation with a certain amount of random data:
  42. //
  43. // files=n to generate n random files (default 0)
  44. // maxsize=n to generate files up to a total of n MiB (default 0)
  45. // sizeavg=n to set the average size of random files, in bytes (default 1<<20)
  46. // seed=n to set the initial random seed (default 0)
  47. // insens=b "true" makes filesystem case-insensitive Windows- or OSX-style (default false)
  48. // latency=d to set the amount of time each "disk" operation takes, where d is time.ParseDuration format
  49. //
  50. // - Two fakeFS:s pointing at the same root path see the same files.
  51. //
  52. type fakeFS struct {
  53. counters fakeFSCounters
  54. uri string
  55. mut sync.Mutex
  56. root *fakeEntry
  57. insens bool
  58. withContent bool
  59. latency time.Duration
  60. }
  61. type fakeFSCounters struct {
  62. Chmod int64
  63. Lchown int64
  64. Chtimes int64
  65. Create int64
  66. DirNames int64
  67. Lstat int64
  68. Mkdir int64
  69. MkdirAll int64
  70. Open int64
  71. OpenFile int64
  72. ReadSymlink int64
  73. Remove int64
  74. RemoveAll int64
  75. Rename int64
  76. }
  77. var (
  78. fakeFSMut sync.Mutex
  79. fakeFSCache = make(map[string]*fakeFS)
  80. )
  81. func newFakeFilesystem(rootURI string, _ ...Option) *fakeFS {
  82. fakeFSMut.Lock()
  83. defer fakeFSMut.Unlock()
  84. root := rootURI
  85. var params url.Values
  86. uri, err := url.Parse(rootURI)
  87. if err == nil {
  88. root = uri.Path
  89. params = uri.Query()
  90. }
  91. if fs, ok := fakeFSCache[rootURI]; ok {
  92. // Already have an fs at this path
  93. return fs
  94. }
  95. fs := &fakeFS{
  96. uri: "fake://" + rootURI,
  97. root: &fakeEntry{
  98. name: "/",
  99. entryType: fakeEntryTypeDir,
  100. mode: 0700,
  101. mtime: time.Now(),
  102. children: make(map[string]*fakeEntry),
  103. },
  104. }
  105. files, _ := strconv.Atoi(params.Get("files"))
  106. maxsize, _ := strconv.Atoi(params.Get("maxsize"))
  107. sizeavg, _ := strconv.Atoi(params.Get("sizeavg"))
  108. seed, _ := strconv.Atoi(params.Get("seed"))
  109. fs.insens = params.Get("insens") == "true"
  110. fs.withContent = params.Get("content") == "true"
  111. if sizeavg == 0 {
  112. sizeavg = 1 << 20
  113. }
  114. if files > 0 || maxsize > 0 {
  115. // Generate initial data according to specs. Operations in here
  116. // *look* like file I/O, but they are not. Do not worry that they
  117. // might fail.
  118. rng := rand.New(rand.NewSource(int64(seed)))
  119. var createdFiles int
  120. var writtenData int64
  121. for (files == 0 || createdFiles < files) && (maxsize == 0 || writtenData>>20 < int64(maxsize)) {
  122. dir := filepath.Join(fmt.Sprintf("%02x", rng.Intn(255)), fmt.Sprintf("%02x", rng.Intn(255)))
  123. file := fmt.Sprintf("%016x", rng.Int63())
  124. fs.MkdirAll(dir, 0755)
  125. fd, _ := fs.Create(filepath.Join(dir, file))
  126. createdFiles++
  127. fsize := int64(sizeavg/2 + rng.Intn(sizeavg))
  128. fd.Truncate(fsize)
  129. writtenData += fsize
  130. ftime := time.Unix(1000000000+rng.Int63n(10*365*86400), 0)
  131. fs.Chtimes(filepath.Join(dir, file), ftime, ftime)
  132. }
  133. }
  134. // Also create a default folder marker for good measure
  135. fs.Mkdir(".stfolder", 0700)
  136. // We only set the latency after doing the operations required to create
  137. // the filesystem initially.
  138. fs.latency, _ = time.ParseDuration(params.Get("latency"))
  139. fakeFSCache[root] = fs
  140. return fs
  141. }
  142. type fakeEntryType int
  143. const (
  144. fakeEntryTypeFile fakeEntryType = iota
  145. fakeEntryTypeDir
  146. fakeEntryTypeSymlink
  147. )
  148. // fakeEntry is an entry (file or directory) in the fake filesystem
  149. type fakeEntry struct {
  150. name string
  151. entryType fakeEntryType
  152. dest string // for symlinks
  153. size int64
  154. mode FileMode
  155. uid int
  156. gid int
  157. mtime time.Time
  158. children map[string]*fakeEntry
  159. content []byte
  160. }
  161. func (fs *fakeFS) entryForName(name string) *fakeEntry {
  162. // bug: lookup doesn't work through symlinks.
  163. if fs.insens {
  164. name = UnicodeLowercaseNormalized(name)
  165. }
  166. name = filepath.ToSlash(name)
  167. if name == "." || name == "/" {
  168. return fs.root
  169. }
  170. name = strings.Trim(name, "/")
  171. comps := strings.Split(name, "/")
  172. entry := fs.root
  173. for _, comp := range comps {
  174. if entry.entryType != fakeEntryTypeDir {
  175. return nil
  176. }
  177. var ok bool
  178. entry, ok = entry.children[comp]
  179. if !ok {
  180. return nil
  181. }
  182. }
  183. return entry
  184. }
  185. func (fs *fakeFS) Chmod(name string, mode FileMode) error {
  186. fs.mut.Lock()
  187. defer fs.mut.Unlock()
  188. fs.counters.Chmod++
  189. time.Sleep(fs.latency)
  190. entry := fs.entryForName(name)
  191. if entry == nil {
  192. return os.ErrNotExist
  193. }
  194. entry.mode = mode
  195. return nil
  196. }
  197. func (fs *fakeFS) Lchown(name string, uid, gid int) error {
  198. fs.mut.Lock()
  199. defer fs.mut.Unlock()
  200. fs.counters.Lchown++
  201. time.Sleep(fs.latency)
  202. entry := fs.entryForName(name)
  203. if entry == nil {
  204. return os.ErrNotExist
  205. }
  206. entry.uid = uid
  207. entry.gid = gid
  208. return nil
  209. }
  210. func (fs *fakeFS) Chtimes(name string, atime time.Time, mtime time.Time) error {
  211. fs.mut.Lock()
  212. defer fs.mut.Unlock()
  213. fs.counters.Chtimes++
  214. time.Sleep(fs.latency)
  215. entry := fs.entryForName(name)
  216. if entry == nil {
  217. return os.ErrNotExist
  218. }
  219. entry.mtime = mtime
  220. return nil
  221. }
  222. func (fs *fakeFS) create(name string) (*fakeEntry, error) {
  223. fs.mut.Lock()
  224. defer fs.mut.Unlock()
  225. fs.counters.Create++
  226. time.Sleep(fs.latency)
  227. if entry := fs.entryForName(name); entry != nil {
  228. if entry.entryType == fakeEntryTypeDir {
  229. return nil, os.ErrExist
  230. } else if entry.entryType == fakeEntryTypeSymlink {
  231. return nil, errors.New("following symlink not supported")
  232. }
  233. entry.size = 0
  234. entry.mtime = time.Now()
  235. entry.mode = 0666
  236. entry.content = nil
  237. if fs.withContent {
  238. entry.content = make([]byte, 0)
  239. }
  240. return entry, nil
  241. }
  242. dir := filepath.Dir(name)
  243. base := filepath.Base(name)
  244. entry := fs.entryForName(dir)
  245. if entry == nil {
  246. return nil, os.ErrNotExist
  247. }
  248. new := &fakeEntry{
  249. name: base,
  250. mode: 0666,
  251. mtime: time.Now(),
  252. }
  253. if fs.insens {
  254. base = UnicodeLowercaseNormalized(base)
  255. }
  256. if fs.withContent {
  257. new.content = make([]byte, 0)
  258. }
  259. entry.children[base] = new
  260. return new, nil
  261. }
  262. func (fs *fakeFS) Create(name string) (File, error) {
  263. entry, err := fs.create(name)
  264. if err != nil {
  265. return nil, err
  266. }
  267. if fs.insens {
  268. return &fakeFile{fakeEntry: entry, presentedName: filepath.Base(name)}, nil
  269. }
  270. return &fakeFile{fakeEntry: entry}, nil
  271. }
  272. func (fs *fakeFS) CreateSymlink(target, name string) error {
  273. entry, err := fs.create(name)
  274. if err != nil {
  275. return err
  276. }
  277. entry.entryType = fakeEntryTypeSymlink
  278. entry.dest = target
  279. return nil
  280. }
  281. func (fs *fakeFS) DirNames(name string) ([]string, error) {
  282. fs.mut.Lock()
  283. defer fs.mut.Unlock()
  284. fs.counters.DirNames++
  285. time.Sleep(fs.latency)
  286. entry := fs.entryForName(name)
  287. if entry == nil {
  288. return nil, os.ErrNotExist
  289. }
  290. names := make([]string, 0, len(entry.children))
  291. for _, child := range entry.children {
  292. names = append(names, child.name)
  293. }
  294. return names, nil
  295. }
  296. func (fs *fakeFS) Lstat(name string) (FileInfo, error) {
  297. fs.mut.Lock()
  298. defer fs.mut.Unlock()
  299. fs.counters.Lstat++
  300. time.Sleep(fs.latency)
  301. entry := fs.entryForName(name)
  302. if entry == nil {
  303. return nil, os.ErrNotExist
  304. }
  305. info := &fakeFileInfo{*entry}
  306. if fs.insens {
  307. info.name = filepath.Base(name)
  308. }
  309. return info, nil
  310. }
  311. func (fs *fakeFS) Mkdir(name string, perm FileMode) error {
  312. fs.mut.Lock()
  313. defer fs.mut.Unlock()
  314. fs.counters.Mkdir++
  315. time.Sleep(fs.latency)
  316. dir := filepath.Dir(name)
  317. base := filepath.Base(name)
  318. entry := fs.entryForName(dir)
  319. key := base
  320. if entry == nil {
  321. return os.ErrNotExist
  322. }
  323. if entry.entryType != fakeEntryTypeDir {
  324. return os.ErrExist
  325. }
  326. if fs.insens {
  327. key = UnicodeLowercaseNormalized(key)
  328. }
  329. if _, ok := entry.children[key]; ok {
  330. return os.ErrExist
  331. }
  332. entry.children[key] = &fakeEntry{
  333. name: base,
  334. entryType: fakeEntryTypeDir,
  335. mode: perm,
  336. mtime: time.Now(),
  337. children: make(map[string]*fakeEntry),
  338. }
  339. return nil
  340. }
  341. func (fs *fakeFS) MkdirAll(name string, perm FileMode) error {
  342. fs.mut.Lock()
  343. defer fs.mut.Unlock()
  344. fs.counters.MkdirAll++
  345. time.Sleep(fs.latency)
  346. name = filepath.ToSlash(name)
  347. name = strings.Trim(name, "/")
  348. comps := strings.Split(name, "/")
  349. entry := fs.root
  350. for _, comp := range comps {
  351. key := comp
  352. if fs.insens {
  353. key = UnicodeLowercaseNormalized(key)
  354. }
  355. next, ok := entry.children[key]
  356. if !ok {
  357. new := &fakeEntry{
  358. name: comp,
  359. entryType: fakeEntryTypeDir,
  360. mode: perm,
  361. mtime: time.Now(),
  362. children: make(map[string]*fakeEntry),
  363. }
  364. entry.children[key] = new
  365. next = new
  366. } else if next.entryType != fakeEntryTypeDir {
  367. return errors.New("not a directory")
  368. }
  369. entry = next
  370. }
  371. return nil
  372. }
  373. func (fs *fakeFS) Open(name string) (File, error) {
  374. fs.mut.Lock()
  375. defer fs.mut.Unlock()
  376. fs.counters.Open++
  377. time.Sleep(fs.latency)
  378. entry := fs.entryForName(name)
  379. if entry == nil || entry.entryType != fakeEntryTypeFile {
  380. return nil, os.ErrNotExist
  381. }
  382. if fs.insens {
  383. return &fakeFile{fakeEntry: entry, presentedName: filepath.Base(name)}, nil
  384. }
  385. return &fakeFile{fakeEntry: entry}, nil
  386. }
  387. func (fs *fakeFS) OpenFile(name string, flags int, mode FileMode) (File, error) {
  388. if flags&os.O_CREATE == 0 {
  389. return fs.Open(name)
  390. }
  391. fs.mut.Lock()
  392. defer fs.mut.Unlock()
  393. fs.counters.OpenFile++
  394. time.Sleep(fs.latency)
  395. dir := filepath.Dir(name)
  396. base := filepath.Base(name)
  397. entry := fs.entryForName(dir)
  398. key := base
  399. if entry == nil {
  400. return nil, os.ErrNotExist
  401. } else if entry.entryType != fakeEntryTypeDir {
  402. return nil, errors.New("not a directory")
  403. }
  404. if fs.insens {
  405. key = UnicodeLowercaseNormalized(key)
  406. }
  407. if flags&os.O_EXCL != 0 {
  408. if _, ok := entry.children[key]; ok {
  409. return nil, os.ErrExist
  410. }
  411. }
  412. newEntry := &fakeEntry{
  413. name: base,
  414. mode: mode,
  415. mtime: time.Now(),
  416. }
  417. if fs.withContent {
  418. newEntry.content = make([]byte, 0)
  419. }
  420. entry.children[key] = newEntry
  421. return &fakeFile{fakeEntry: newEntry}, nil
  422. }
  423. func (fs *fakeFS) ReadSymlink(name string) (string, error) {
  424. fs.mut.Lock()
  425. defer fs.mut.Unlock()
  426. fs.counters.ReadSymlink++
  427. time.Sleep(fs.latency)
  428. entry := fs.entryForName(name)
  429. if entry == nil {
  430. return "", os.ErrNotExist
  431. } else if entry.entryType != fakeEntryTypeSymlink {
  432. return "", errors.New("not a symlink")
  433. }
  434. return entry.dest, nil
  435. }
  436. func (fs *fakeFS) Remove(name string) error {
  437. fs.mut.Lock()
  438. defer fs.mut.Unlock()
  439. fs.counters.Remove++
  440. time.Sleep(fs.latency)
  441. if fs.insens {
  442. name = UnicodeLowercaseNormalized(name)
  443. }
  444. entry := fs.entryForName(name)
  445. if entry == nil {
  446. return os.ErrNotExist
  447. }
  448. if len(entry.children) != 0 {
  449. return errors.New("not empty")
  450. }
  451. entry = fs.entryForName(filepath.Dir(name))
  452. delete(entry.children, filepath.Base(name))
  453. return nil
  454. }
  455. func (fs *fakeFS) RemoveAll(name string) error {
  456. fs.mut.Lock()
  457. defer fs.mut.Unlock()
  458. fs.counters.RemoveAll++
  459. time.Sleep(fs.latency)
  460. if fs.insens {
  461. name = UnicodeLowercaseNormalized(name)
  462. }
  463. entry := fs.entryForName(filepath.Dir(name))
  464. if entry == nil {
  465. return nil // all tested real systems exhibit this behaviour
  466. }
  467. // RemoveAll is easy when the file system uses garbage collection under
  468. // the hood... We even get the correct semantics for open fd:s for free.
  469. delete(entry.children, filepath.Base(name))
  470. return nil
  471. }
  472. func (fs *fakeFS) Rename(oldname, newname string) error {
  473. fs.mut.Lock()
  474. defer fs.mut.Unlock()
  475. fs.counters.Rename++
  476. time.Sleep(fs.latency)
  477. oldKey := filepath.Base(oldname)
  478. newKey := filepath.Base(newname)
  479. if fs.insens {
  480. oldKey = UnicodeLowercaseNormalized(oldKey)
  481. newKey = UnicodeLowercaseNormalized(newKey)
  482. }
  483. p0 := fs.entryForName(filepath.Dir(oldname))
  484. if p0 == nil {
  485. return os.ErrNotExist
  486. }
  487. entry := p0.children[oldKey]
  488. if entry == nil {
  489. return os.ErrNotExist
  490. }
  491. p1 := fs.entryForName(filepath.Dir(newname))
  492. if p1 == nil {
  493. return os.ErrNotExist
  494. }
  495. dst, ok := p1.children[newKey]
  496. if ok {
  497. if fs.insens && newKey == oldKey {
  498. // case-only in-place rename
  499. entry.name = filepath.Base(newname)
  500. return nil
  501. }
  502. if dst.entryType == fakeEntryTypeDir {
  503. return errors.New("is a directory")
  504. }
  505. }
  506. p1.children[newKey] = entry
  507. entry.name = filepath.Base(newname)
  508. delete(p0.children, oldKey)
  509. return nil
  510. }
  511. func (fs *fakeFS) Stat(name string) (FileInfo, error) {
  512. return fs.Lstat(name)
  513. }
  514. func (fs *fakeFS) SymlinksSupported() bool {
  515. return false
  516. }
  517. func (fs *fakeFS) Walk(name string, walkFn WalkFunc) error {
  518. return errors.New("not implemented")
  519. }
  520. func (fs *fakeFS) Watch(path string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, <-chan error, error) {
  521. return nil, nil, ErrWatchNotSupported
  522. }
  523. func (fs *fakeFS) Hide(name string) error {
  524. return nil
  525. }
  526. func (fs *fakeFS) Unhide(name string) error {
  527. return nil
  528. }
  529. func (fs *fakeFS) Glob(pattern string) ([]string, error) {
  530. // gnnh we don't seem to actually require this in practice
  531. return nil, errors.New("not implemented")
  532. }
  533. func (fs *fakeFS) Roots() ([]string, error) {
  534. return []string{"/"}, nil
  535. }
  536. func (fs *fakeFS) Usage(name string) (Usage, error) {
  537. return Usage{}, errors.New("not implemented")
  538. }
  539. func (fs *fakeFS) Type() FilesystemType {
  540. return FilesystemTypeFake
  541. }
  542. func (fs *fakeFS) URI() string {
  543. return fs.uri
  544. }
  545. func (fs *fakeFS) Options() []Option {
  546. return nil
  547. }
  548. func (fs *fakeFS) SameFile(fi1, fi2 FileInfo) bool {
  549. // BUG: real systems base file sameness on path, inodes, etc
  550. // we try our best, but FileInfo just doesn't have enough data
  551. // so there be false positives, especially on Windows
  552. // where ModTime is not that precise
  553. var ok bool
  554. if fs.insens {
  555. ok = UnicodeLowercaseNormalized(fi1.Name()) == UnicodeLowercaseNormalized(fi2.Name())
  556. } else {
  557. ok = fi1.Name() == fi2.Name()
  558. }
  559. return ok && fi1.ModTime().Equal(fi2.ModTime()) && fi1.Mode() == fi2.Mode() && fi1.IsDir() == fi2.IsDir() && fi1.IsRegular() == fi2.IsRegular() && fi1.IsSymlink() == fi2.IsSymlink() && fi1.Owner() == fi2.Owner() && fi1.Group() == fi2.Group()
  560. }
  561. func (fs *fakeFS) underlying() (Filesystem, bool) {
  562. return nil, false
  563. }
  564. func (fs *fakeFS) wrapperType() filesystemWrapperType {
  565. return filesystemWrapperTypeNone
  566. }
  567. func (fs *fakeFS) resetCounters() {
  568. fs.mut.Lock()
  569. fs.counters = fakeFSCounters{}
  570. fs.mut.Unlock()
  571. }
  572. func (fs *fakeFS) reportMetricsPerOp(b *testing.B) {
  573. fs.reportMetricsPer(b, 1, "op")
  574. }
  575. func (fs *fakeFS) reportMetricsPer(b *testing.B, divisor float64, unit string) {
  576. fs.mut.Lock()
  577. defer fs.mut.Unlock()
  578. b.ReportMetric(float64(fs.counters.Lstat)/divisor/float64(b.N), "Lstat/"+unit)
  579. b.ReportMetric(float64(fs.counters.DirNames)/divisor/float64(b.N), "DirNames/"+unit)
  580. }
  581. // fakeFile is the representation of an open file. We don't care if it's
  582. // opened for reading or writing, it's all good.
  583. type fakeFile struct {
  584. *fakeEntry
  585. mut sync.Mutex
  586. rng io.Reader
  587. seed int64
  588. offset int64
  589. seedOffs int64
  590. presentedName string // present (i.e. != "") on insensitive fs only
  591. }
  592. func (f *fakeFile) Close() error {
  593. return nil
  594. }
  595. func (f *fakeFile) Read(p []byte) (int, error) {
  596. f.mut.Lock()
  597. defer f.mut.Unlock()
  598. return f.readShortAt(p, f.offset)
  599. }
  600. func (f *fakeFile) ReadAt(p []byte, offs int64) (int, error) {
  601. f.mut.Lock()
  602. defer f.mut.Unlock()
  603. // ReadAt is spec:ed to always read a full block unless EOF or failure,
  604. // so we must loop. It's also not supposed to affect the seek position,
  605. // but that would make things annoying or inefficient in terms of
  606. // generating the appropriate RNG etc so I ignore that. In practice we
  607. // currently don't depend on that aspect of it...
  608. var read int
  609. for {
  610. n, err := f.readShortAt(p[read:], offs+int64(read))
  611. read += n
  612. if err != nil {
  613. return read, err
  614. }
  615. if read == len(p) {
  616. return read, nil
  617. }
  618. }
  619. }
  620. func (f *fakeFile) readShortAt(p []byte, offs int64) (int, error) {
  621. // Here be a certain amount of magic... We want to return pseudorandom,
  622. // predictable data so that a read from the same offset in the same file
  623. // always returns the same data. But the RNG is a stream, and reads can
  624. // be random.
  625. //
  626. // We split the file into "blocks" numbered by "seedNo", where each
  627. // block becomes an instantiation of the RNG, seeded with the hash of
  628. // the file number plus the seedNo (block number). We keep the RNG
  629. // around in the hope that the next read will be sequential to this one
  630. // and we can continue reading from the same RNG.
  631. //
  632. // When that's not the case we create a new RNG for the block we are in,
  633. // read as many bytes from it as necessary to get to the right offset,
  634. // and then serve the read from there. We limit the length of the read
  635. // to the end of the block, as another RNG needs to be created to serve
  636. // the next block.
  637. //
  638. // The size of the blocks are a matter of taste... Larger blocks give
  639. // better performance for sequential reads, but worse for random reads
  640. // as we often need to generate and throw away a lot of data at the
  641. // start of the block to serve a given read. 128 KiB blocks fit
  642. // reasonably well with the type of IO Syncthing tends to do.
  643. if f.entryType == fakeEntryTypeDir {
  644. return 0, errors.New("is a directory")
  645. }
  646. if offs >= f.size {
  647. return 0, io.EOF
  648. }
  649. if f.content != nil {
  650. n := copy(p, f.content[int(offs):])
  651. f.offset = offs + int64(n)
  652. return n, nil
  653. }
  654. // Lazily calculate our main seed, a simple 64 bit FNV hash our file
  655. // name.
  656. if f.seed == 0 {
  657. hf := fnv.New64()
  658. hf.Write([]byte(f.name))
  659. f.seed = int64(hf.Sum64())
  660. }
  661. // Check whether the read is a continuation of an RNG we already have or
  662. // we need to set up a new one.
  663. seedNo := offs >> randomBlockShift
  664. minOffs := seedNo << randomBlockShift
  665. nextBlockOffs := (seedNo + 1) << randomBlockShift
  666. if f.rng == nil || f.offset != offs || seedNo != f.seedOffs {
  667. // This is not a straight read continuing from a previous one
  668. f.rng = rand.New(rand.NewSource(f.seed + seedNo))
  669. // If the read is not at the start of the block, discard data
  670. // accordingly.
  671. diff := offs - minOffs
  672. if diff > 0 {
  673. lr := io.LimitReader(f.rng, diff)
  674. io.Copy(ioutil.Discard, lr)
  675. }
  676. f.offset = offs
  677. f.seedOffs = seedNo
  678. }
  679. size := len(p)
  680. // Don't read past the end of the file
  681. if offs+int64(size) > f.size {
  682. size = int(f.size - offs)
  683. }
  684. // Don't read across the block boundary
  685. if offs+int64(size) > nextBlockOffs {
  686. size = int(nextBlockOffs - offs)
  687. }
  688. f.offset += int64(size)
  689. return f.rng.Read(p[:size])
  690. }
  691. func (f *fakeFile) Seek(offset int64, whence int) (int64, error) {
  692. f.mut.Lock()
  693. defer f.mut.Unlock()
  694. if f.entryType == fakeEntryTypeDir {
  695. return 0, errors.New("is a directory")
  696. }
  697. f.rng = nil
  698. switch whence {
  699. case io.SeekCurrent:
  700. f.offset += offset
  701. case io.SeekEnd:
  702. f.offset = f.size - offset
  703. case io.SeekStart:
  704. f.offset = offset
  705. }
  706. if f.offset < 0 {
  707. f.offset = 0
  708. return f.offset, errors.New("seek before start")
  709. }
  710. if f.offset > f.size {
  711. f.offset = f.size
  712. return f.offset, io.EOF
  713. }
  714. return f.offset, nil
  715. }
  716. func (f *fakeFile) Write(p []byte) (int, error) {
  717. f.mut.Lock()
  718. offs := f.offset
  719. f.mut.Unlock()
  720. return f.WriteAt(p, offs)
  721. }
  722. func (f *fakeFile) WriteAt(p []byte, off int64) (int, error) {
  723. f.mut.Lock()
  724. defer f.mut.Unlock()
  725. if f.entryType == fakeEntryTypeDir {
  726. return 0, errors.New("is a directory")
  727. }
  728. if f.content != nil {
  729. if len(f.content) < int(off)+len(p) {
  730. newc := make([]byte, int(off)+len(p))
  731. copy(newc, f.content)
  732. f.content = newc
  733. }
  734. copy(f.content[int(off):], p)
  735. }
  736. f.rng = nil
  737. f.offset = off + int64(len(p))
  738. if f.offset > f.size {
  739. f.size = f.offset
  740. }
  741. return len(p), nil
  742. }
  743. func (f *fakeFile) Name() string {
  744. if f.presentedName != "" {
  745. return f.presentedName
  746. }
  747. return f.name
  748. }
  749. func (f *fakeFile) Truncate(size int64) error {
  750. f.mut.Lock()
  751. defer f.mut.Unlock()
  752. if f.content != nil {
  753. if int64(cap(f.content)) < size {
  754. c := make([]byte, size)
  755. copy(c[:len(f.content)], f.content)
  756. f.content = c
  757. } else {
  758. f.content = f.content[:int(size)]
  759. }
  760. }
  761. f.rng = nil
  762. f.size = size
  763. if f.offset > size {
  764. f.offset = size
  765. }
  766. return nil
  767. }
  768. func (f *fakeFile) Stat() (FileInfo, error) {
  769. info := &fakeFileInfo{*f.fakeEntry}
  770. if f.presentedName != "" {
  771. info.name = f.presentedName
  772. }
  773. return info, nil
  774. }
  775. func (f *fakeFile) Sync() error {
  776. return nil
  777. }
  778. // fakeFileInfo is the stat result.
  779. type fakeFileInfo struct {
  780. fakeEntry // intentionally a copy of the struct
  781. }
  782. func (f *fakeFileInfo) Name() string {
  783. return f.name
  784. }
  785. func (f *fakeFileInfo) Mode() FileMode {
  786. return f.mode
  787. }
  788. func (f *fakeFileInfo) Size() int64 {
  789. return f.size
  790. }
  791. func (f *fakeFileInfo) ModTime() time.Time {
  792. return f.mtime
  793. }
  794. func (f *fakeFileInfo) IsDir() bool {
  795. return f.entryType == fakeEntryTypeDir
  796. }
  797. func (f *fakeFileInfo) IsRegular() bool {
  798. return f.entryType == fakeEntryTypeFile
  799. }
  800. func (f *fakeFileInfo) IsSymlink() bool {
  801. return f.entryType == fakeEntryTypeSymlink
  802. }
  803. func (f *fakeFileInfo) Owner() int {
  804. return f.uid
  805. }
  806. func (f *fakeFileInfo) Group() int {
  807. return f.gid
  808. }