file_cache.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package keystore
  17. import (
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. "sync"
  22. "time"
  23. mapset "github.com/deckarep/golang-set"
  24. "github.com/ethereum/go-ethereum/log"
  25. )
  26. // fileCache is a cache of files seen during scan of keystore.
  27. type fileCache struct {
  28. all mapset.Set // Set of all files from the keystore folder
  29. lastMod time.Time // Last time instance when a file was modified
  30. mu sync.Mutex
  31. }
  32. // scan performs a new scan on the given directory, compares against the already
  33. // cached filenames, and returns file sets: creates, deletes, updates.
  34. func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, error) {
  35. t0 := time.Now()
  36. // List all the files from the keystore folder
  37. files, err := os.ReadDir(keyDir)
  38. if err != nil {
  39. return nil, nil, nil, err
  40. }
  41. t1 := time.Now()
  42. fc.mu.Lock()
  43. defer fc.mu.Unlock()
  44. // Iterate all the files and gather their metadata
  45. all := mapset.NewThreadUnsafeSet()
  46. mods := mapset.NewThreadUnsafeSet()
  47. var newLastMod time.Time
  48. for _, fi := range files {
  49. path := filepath.Join(keyDir, fi.Name())
  50. // Skip any non-key files from the folder
  51. if nonKeyFile(fi) {
  52. log.Trace("Ignoring file on account scan", "path", path)
  53. continue
  54. }
  55. // Gather the set of all and freshly modified files
  56. all.Add(path)
  57. info, err := fi.Info()
  58. if err != nil {
  59. return nil, nil, nil, err
  60. }
  61. modified := info.ModTime()
  62. if modified.After(fc.lastMod) {
  63. mods.Add(path)
  64. }
  65. if modified.After(newLastMod) {
  66. newLastMod = modified
  67. }
  68. }
  69. t2 := time.Now()
  70. // Update the tracked files and return the three sets
  71. deletes := fc.all.Difference(all) // Deletes = previous - current
  72. creates := all.Difference(fc.all) // Creates = current - previous
  73. updates := mods.Difference(creates) // Updates = modified - creates
  74. fc.all, fc.lastMod = all, newLastMod
  75. t3 := time.Now()
  76. // Report on the scanning stats and return
  77. log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
  78. return creates, deletes, updates, nil
  79. }
  80. // nonKeyFile ignores editor backups, hidden files and folders/symlinks.
  81. func nonKeyFile(fi os.DirEntry) bool {
  82. // Skip editor backups and UNIX-style hidden files.
  83. if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
  84. return true
  85. }
  86. // Skip misc special files, directories (yes, symlinks too).
  87. if fi.IsDir() || !fi.Type().IsRegular() {
  88. return true
  89. }
  90. return false
  91. }