github.com/core-coin/go-core/v2@v2.1.9/accounts/keystore/file_cache.go (about) 1 // Copyright 2017 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package keystore 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "strings" 24 "sync" 25 "time" 26 27 mapset "github.com/deckarep/golang-set" 28 29 "github.com/core-coin/go-core/v2/log" 30 ) 31 32 // fileCache is a cache of files seen during scan of keystore. 33 type fileCache struct { 34 all mapset.Set // Set of all files from the keystore folder 35 lastMod time.Time // Last time instance when a file was modified 36 mu sync.Mutex 37 } 38 39 // scan performs a new scan on the given directory, compares against the already 40 // cached filenames, and returns file sets: creates, deletes, updates. 41 func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, error) { 42 t0 := time.Now() 43 44 // List all the failes from the keystore folder 45 files, err := ioutil.ReadDir(keyDir) 46 if err != nil { 47 return nil, nil, nil, err 48 } 49 t1 := time.Now() 50 51 fc.mu.Lock() 52 defer fc.mu.Unlock() 53 54 // Iterate all the files and gather their metadata 55 all := mapset.NewThreadUnsafeSet() 56 mods := mapset.NewThreadUnsafeSet() 57 58 var newLastMod time.Time 59 for _, fi := range files { 60 path := filepath.Join(keyDir, fi.Name()) 61 // Skip any non-key files from the folder 62 if nonKeyFile(fi) { 63 log.Trace("Ignoring file on account scan", "path", path) 64 continue 65 } 66 // Gather the set of all and fresly modified files 67 all.Add(path) 68 69 modified := fi.ModTime() 70 if modified.After(fc.lastMod) { 71 mods.Add(path) 72 } 73 if modified.After(newLastMod) { 74 newLastMod = modified 75 } 76 } 77 t2 := time.Now() 78 79 // Update the tracked files and return the three sets 80 deletes := fc.all.Difference(all) // Deletes = previous - current 81 creates := all.Difference(fc.all) // Creates = current - previous 82 updates := mods.Difference(creates) // Updates = modified - creates 83 84 fc.all, fc.lastMod = all, newLastMod 85 t3 := time.Now() 86 87 // Report on the scanning stats and return 88 log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2)) 89 return creates, deletes, updates, nil 90 } 91 92 // nonKeyFile ignores editor backups, hidden files and folders/symlinks. 93 func nonKeyFile(fi os.FileInfo) bool { 94 // Skip editor backups and UNIX-style hidden files. 95 if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") { 96 return true 97 } 98 // Skip misc special files, directories (yes, symlinks too). 99 if fi.IsDir() || fi.Mode()&os.ModeType != 0 { 100 return true 101 } 102 return false 103 }