github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/accounts/keystore/file_cache.go (about) 1 package keystore 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 "sync" 9 "time" 10 11 "github.com/neatio-net/neatio/chain/log" 12 set "github.com/neatio-net/set-go" 13 ) 14 15 type fileCache struct { 16 all *set.SetNonTS 17 lastMod time.Time 18 mu sync.RWMutex 19 } 20 21 func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Interface, error) { 22 t0 := time.Now() 23 24 files, err := ioutil.ReadDir(keyDir) 25 if err != nil { 26 return nil, nil, nil, err 27 } 28 t1 := time.Now() 29 30 fc.mu.Lock() 31 defer fc.mu.Unlock() 32 33 all := set.NewNonTS() 34 mods := set.NewNonTS() 35 36 var newLastMod time.Time 37 for _, fi := range files { 38 39 path := filepath.Join(keyDir, fi.Name()) 40 if skipKeyFile(fi) { 41 log.Trace("Ignoring file on account scan", "path", path) 42 continue 43 } 44 45 all.Add(path) 46 47 modified := fi.ModTime() 48 if modified.After(fc.lastMod) { 49 mods.Add(path) 50 } 51 if modified.After(newLastMod) { 52 newLastMod = modified 53 } 54 } 55 t2 := time.Now() 56 57 deletes := set.Difference(fc.all, all) 58 creates := set.Difference(all, fc.all) 59 updates := set.Difference(mods, creates) 60 61 fc.all, fc.lastMod = all, newLastMod 62 t3 := time.Now() 63 64 log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2)) 65 return creates, deletes, updates, nil 66 } 67 68 func skipKeyFile(fi os.FileInfo) bool { 69 70 if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") { 71 return true 72 } 73 74 if fi.IsDir() || fi.Mode()&os.ModeType != 0 { 75 return true 76 } 77 return false 78 }