github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/accounts/keystore/file_cache.go (about)

     1  // Copyright 2017 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package keystore
    18  
    19  import (
    20  	"github.com/SmartMeshFoundation/Spectrum/log"
    21  	mapset "github.com/deckarep/golang-set"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  	"sync"
    27  	"time"
    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.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) (mapset.Set, mapset.Set, mapset.Set, 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 := mapset.NewThreadUnsafeSet()
    54  	mods := mapset.NewThreadUnsafeSet()
    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.Add(path)
    66  
    67  		modified := fi.ModTime()
    68  		if modified.After(fc.lastMod) {
    69  			mods.Add(path)
    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  	deletes := fc.all.Difference(all)   // Deletes = previous - current
    79  	creates := all.Difference(fc.all)   // Creates = current - previous
    80  	updates := mods.Difference(creates) // Updates = modified - creates
    81  
    82  	fc.all, fc.lastMod = all, newLastMod
    83  	t3 := time.Now()
    84  
    85  	// Report on the scanning stats and return
    86  	log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
    87  	return creates, deletes, updates, nil
    88  }
    89  
    90  // skipKeyFile ignores editor backups, hidden files and folders/symlinks.
    91  func skipKeyFile(fi os.FileInfo) bool {
    92  	// Skip editor backups and UNIX-style hidden files.
    93  	if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
    94  		return true
    95  	}
    96  	// Skip misc special files, directories (yes, symlinks too).
    97  	if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
    98  		return true
    99  	}
   100  	return false
   101  }