github.com/jonkofee/go-ethereum@v1.11.1/accounts/keystore/file_cache.go (about)

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