github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/accounts/caching.go (about) 1 // Copyright 2016 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 17 // Contains cache(ing) interface and associated helper structs and functions. 18 19 package accounts 20 21 import ( 22 "fmt" 23 "os" 24 "strings" 25 "time" 26 27 "github.com/ethereumproject/go-ethereum/common" 28 ) 29 30 // Minimum amount of time between cache reloads. This limit applies if the platform does 31 // not support change notifications. It also applies if the keystore directory does not 32 // exist yet, the code will attempt to create a watcher at most this often. 33 const minReloadInterval = 2 * time.Second 34 35 type accountsByFile []Account 36 37 func (s accountsByFile) Len() int { return len(s) } 38 func (s accountsByFile) Less(i, j int) bool { return s[i].File < s[j].File } 39 func (s accountsByFile) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 40 41 // AmbiguousAddrError is returned when attempting to unlock 42 // an address for which more than one file exists. 43 type AmbiguousAddrError struct { 44 Addr common.Address 45 Matches []Account 46 } 47 48 func (err *AmbiguousAddrError) Error() string { 49 files := "" 50 for i, a := range err.Matches { 51 files += a.File 52 if i < len(err.Matches)-1 { 53 files += ", " 54 } 55 } 56 return fmt.Sprintf("multiple keys match address (%s)", files) 57 } 58 59 type caching interface { 60 muLock() 61 muUnlock() 62 getKeydir() string 63 getWatcher() *watcher 64 getThrottle() *time.Timer 65 66 maybeReload() 67 reload() 68 Syncfs2db(time.Time) []error 69 70 hasAddress(address common.Address) bool 71 accounts() []Account 72 add(Account) 73 delete(Account) 74 find(Account) (Account, error) 75 76 close() 77 } 78 79 func skipKeyFile(fi os.FileInfo) bool { 80 // Skip editor backups and UNIX-style hidden files. 81 if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") { 82 return true 83 } 84 if strings.HasSuffix(fi.Name(), "accounts.db") { 85 return true 86 } 87 // Skip misc special files, directories (yes, symlinks too). 88 if fi.IsDir() || fi.Mode()&os.ModeType != 0 { 89 return true 90 } 91 return false 92 }