github.com/bcskill/bcschain/v3@v3.4.9-beta2/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  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"sync"
    25  	"time"
    26  
    27  	"github.com/bcskill/bcschain/v3/log"
    28  )
    29  
    30  // fileCache is a cache of files seen during scan of keystore.
    31  type fileCache struct {
    32  	all     map[string]struct{} // Set of all files from the keystore folder
    33  	lastMod time.Time           // Last time instance when a file was modified
    34  	mu      sync.RWMutex
    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) (map[string]struct{}, map[string]struct{}, map[string]struct{}, error) {
    40  	t0 := time.Now()
    41  
    42  	// List all the failes from the keystore folder
    43  	files, err := ioutil.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 := make(map[string]struct{})
    54  	mods := make(map[string]struct{})
    55  
    56  	var newLastMod time.Time
    57  	for _, fi := range files {
    58  		// Skip any non-key files from the folder
    59  		path := filepath.Join(keyDir, fi.Name())
    60  		if skipKeyFile(fi) {
    61  			log.Trace("Ignoring file on account scan", "path", path)
    62  			continue
    63  		}
    64  		// Gather the set of all and fresly modified files
    65  		all[path] = struct{}{}
    66  
    67  		modified := fi.ModTime()
    68  		if modified.After(fc.lastMod) {
    69  			mods[path] = struct{}{}
    70  		}
    71  		if modified.After(newLastMod) {
    72  			newLastMod = modified
    73  		}
    74  	}
    75  	t2 := time.Now()
    76  
    77  	// Update the tracked files and return the three sets.
    78  
    79  	// Deletes = previous - current
    80  	deletes := make(map[string]struct{})
    81  	for p := range fc.all {
    82  		if _, ok := all[p]; !ok {
    83  			deletes[p] = struct{}{}
    84  		}
    85  	}
    86  
    87  	// Creates = current - previous
    88  	creates := make(map[string]struct{})
    89  	for p := range all {
    90  		if _, ok := fc.all[p]; !ok {
    91  			creates[p] = struct{}{}
    92  		}
    93  	}
    94  
    95  	// Updates = modified - creates
    96  	updates := make(map[string]struct{})
    97  	for p := range mods {
    98  		if _, ok := creates[p]; !ok {
    99  			updates[p] = struct{}{}
   100  		}
   101  	}
   102  
   103  	fc.all, fc.lastMod = all, newLastMod
   104  	t3 := time.Now()
   105  
   106  	// Report on the scanning stats and return
   107  	log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
   108  	return creates, deletes, updates, nil
   109  }
   110  
   111  // skipKeyFile ignores editor backups, hidden files and folders/symlinks.
   112  func skipKeyFile(fi os.FileInfo) bool {
   113  	// Skip editor backups and UNIX-style hidden files.
   114  	if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
   115  		return true
   116  	}
   117  	// Skip misc special files, directories (yes, symlinks too).
   118  	if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
   119  		return true
   120  	}
   121  	return false
   122  }