file_cache.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. "sync"
  23. "time"
  24. "github.com/ethereum/go-ethereum/log"
  25. set "gopkg.in/fatih/set.v0"
  26. )
  27. // fileCache is a cache of files seen during scan of keystore.
  28. type fileCache struct {
  29. all *set.SetNonTS // Set of all files from the keystore folder
  30. lastMod time.Time // Last time instance when a file was modified
  31. mu sync.RWMutex
  32. }
  33. // scan performs a new scan on the given directory, compares against the already
  34. // cached filenames, and returns file sets: creates, deletes, updates.
  35. func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Interface, error) {
  36. t0 := time.Now()
  37. // List all the failes from the keystore folder
  38. files, err := ioutil.ReadDir(keyDir)
  39. if err != nil {
  40. return nil, nil, nil, err
  41. }
  42. t1 := time.Now()
  43. fc.mu.Lock()
  44. defer fc.mu.Unlock()
  45. // Iterate all the files and gather their metadata
  46. all := set.NewNonTS()
  47. mods := set.NewNonTS()
  48. var newLastMod time.Time
  49. for _, fi := range files {
  50. // Skip any non-key files from the folder
  51. path := filepath.Join(keyDir, fi.Name())
  52. if skipKeyFile(fi) {
  53. log.Trace("Ignoring file on account scan", "path", path)
  54. continue
  55. }
  56. // Gather the set of all and fresly modified files
  57. all.Add(path)
  58. modified := fi.ModTime()
  59. if modified.After(fc.lastMod) {
  60. mods.Add(path)
  61. }
  62. if modified.After(newLastMod) {
  63. newLastMod = modified
  64. }
  65. }
  66. t2 := time.Now()
  67. // Update the tracked files and return the three sets
  68. deletes := set.Difference(fc.all, all) // Deletes = previous - current
  69. creates := set.Difference(all, fc.all) // Creates = current - previous
  70. updates := set.Difference(mods, creates) // Updates = modified - creates
  71. fc.all, fc.lastMod = all, newLastMod
  72. t3 := time.Now()
  73. // Report on the scanning stats and return
  74. log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
  75. return creates, deletes, updates, nil
  76. }
  77. // skipKeyFile ignores editor backups, hidden files and folders/symlinks.
  78. func skipKeyFile(fi os.FileInfo) bool {
  79. // Skip editor backups and UNIX-style hidden files.
  80. if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
  81. return true
  82. }
  83. // Skip misc special files, directories (yes, symlinks too).
  84. if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
  85. return true
  86. }
  87. return false
  88. }