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