2017-11-20 13:21:52 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package keystore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-11-14 17:16:52 +03:00
|
|
|
mapset "github.com/deckarep/golang-set/v2"
|
2017-11-20 13:21:52 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// fileCache is a cache of files seen during scan of keystore.
|
|
|
|
type fileCache struct {
|
2022-11-14 17:16:52 +03:00
|
|
|
all mapset.Set[string] // Set of all files from the keystore folder
|
|
|
|
lastMod time.Time // Last time instance when a file was modified
|
2020-10-13 11:58:41 +03:00
|
|
|
mu sync.Mutex
|
2017-11-20 13:21:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// scan performs a new scan on the given directory, compares against the already
|
|
|
|
// cached filenames, and returns file sets: creates, deletes, updates.
|
2022-11-14 17:16:52 +03:00
|
|
|
func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string], mapset.Set[string], error) {
|
2017-11-20 13:21:52 +03:00
|
|
|
t0 := time.Now()
|
|
|
|
|
2022-08-19 09:00:21 +03:00
|
|
|
// List all the files from the keystore folder
|
2022-05-17 15:27:04 +03:00
|
|
|
files, err := os.ReadDir(keyDir)
|
2017-11-20 13:21:52 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
t1 := time.Now()
|
|
|
|
|
|
|
|
fc.mu.Lock()
|
|
|
|
defer fc.mu.Unlock()
|
|
|
|
|
|
|
|
// Iterate all the files and gather their metadata
|
2022-11-14 17:16:52 +03:00
|
|
|
all := mapset.NewThreadUnsafeSet[string]()
|
|
|
|
mods := mapset.NewThreadUnsafeSet[string]()
|
2017-11-20 13:21:52 +03:00
|
|
|
|
|
|
|
var newLastMod time.Time
|
|
|
|
for _, fi := range files {
|
|
|
|
path := filepath.Join(keyDir, fi.Name())
|
2018-08-03 10:25:17 +03:00
|
|
|
// Skip any non-key files from the folder
|
|
|
|
if nonKeyFile(fi) {
|
2017-11-20 13:21:52 +03:00
|
|
|
log.Trace("Ignoring file on account scan", "path", path)
|
|
|
|
continue
|
|
|
|
}
|
2022-08-19 09:00:21 +03:00
|
|
|
// Gather the set of all and freshly modified files
|
2017-11-20 13:21:52 +03:00
|
|
|
all.Add(path)
|
|
|
|
|
2022-05-17 15:27:04 +03:00
|
|
|
info, err := fi.Info()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
modified := info.ModTime()
|
2017-11-20 13:21:52 +03:00
|
|
|
if modified.After(fc.lastMod) {
|
|
|
|
mods.Add(path)
|
|
|
|
}
|
|
|
|
if modified.After(newLastMod) {
|
|
|
|
newLastMod = modified
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t2 := time.Now()
|
|
|
|
|
|
|
|
// Update the tracked files and return the three sets
|
2018-07-16 10:54:19 +03:00
|
|
|
deletes := fc.all.Difference(all) // Deletes = previous - current
|
|
|
|
creates := all.Difference(fc.all) // Creates = current - previous
|
|
|
|
updates := mods.Difference(creates) // Updates = modified - creates
|
2017-11-20 13:21:52 +03:00
|
|
|
|
|
|
|
fc.all, fc.lastMod = all, newLastMod
|
|
|
|
t3 := time.Now()
|
|
|
|
|
|
|
|
// Report on the scanning stats and return
|
|
|
|
log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
|
|
|
|
return creates, deletes, updates, nil
|
|
|
|
}
|
|
|
|
|
2018-08-03 10:25:17 +03:00
|
|
|
// nonKeyFile ignores editor backups, hidden files and folders/symlinks.
|
2022-05-17 15:27:04 +03:00
|
|
|
func nonKeyFile(fi os.DirEntry) bool {
|
2017-11-20 13:21:52 +03:00
|
|
|
// Skip editor backups and UNIX-style hidden files.
|
|
|
|
if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Skip misc special files, directories (yes, symlinks too).
|
2022-05-17 15:27:04 +03:00
|
|
|
if fi.IsDir() || !fi.Type().IsRegular() {
|
2017-11-20 13:21:52 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|