github.com/amazechain/amc@v0.1.3/accounts/keystore/file_cache.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package keystore
    18  
    19  import (
    20  	"github.com/amazechain/amc/log"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"sync"
    25  	"time"
    26  
    27  	mapset "github.com/deckarep/golang-set/v2"
    28  )
    29  
    30  // fileCache is a cache of files seen during scan of keystore.
    31  type fileCache struct {
    32  	all     mapset.Set[string] // 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[string], mapset.Set[string], mapset.Set[string], 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[string]()
    54  	mods := mapset.NewThreadUnsafeSet[string]()
    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  			log.Debug("Ignoring file on account scan", "path", path)
    63  			continue
    64  		}
    65  		// Gather the set of all and freshly modified files
    66  		all.Add(path)
    67  
    68  		info, err := fi.Info()
    69  		if err != nil {
    70  			return nil, nil, nil, err
    71  		}
    72  		modified := info.ModTime()
    73  		if modified.After(fc.lastMod) {
    74  			mods.Add(path)
    75  		}
    76  		if modified.After(newLastMod) {
    77  			newLastMod = modified
    78  		}
    79  	}
    80  	t2 := time.Now()
    81  
    82  	// Update the tracked files and return the three sets
    83  	deletes := fc.all.Difference(all)   // Deletes = previous - current
    84  	creates := all.Difference(fc.all)   // Creates = current - previous
    85  	updates := mods.Difference(creates) // Updates = modified - creates
    86  
    87  	fc.all, fc.lastMod = all, newLastMod
    88  	t3 := time.Now()
    89  
    90  	// Report on the scanning stats and return
    91  	log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
    92  	return creates, deletes, updates, nil
    93  }
    94  
    95  // nonKeyFile ignores editor backups, hidden files and folders/symlinks.
    96  func nonKeyFile(fi os.DirEntry) bool {
    97  	// Skip editor backups and UNIX-style hidden files.
    98  	if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
    99  		return true
   100  	}
   101  	// Skip misc special files, directories (yes, symlinks too).
   102  	if fi.IsDir() || !fi.Type().IsRegular() {
   103  		return true
   104  	}
   105  	return false
   106  }