github.com/aquanetwork/aquachain@v1.7.8/aqua/accounts/keystore/file_cache.go (about)

     1  // Copyright 2017 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain 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 aquachain 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 aquachain 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  	"gitlab.com/aquachain/aquachain/common/log"
    28  	set "gopkg.in/fatih/set.v0"
    29  )
    30  
    31  // fileCache is a cache of files seen during scan of keystore.
    32  type fileCache struct {
    33  	all     *set.SetNonTS // Set of all files from the keystore folder
    34  	lastMod time.Time     // Last time instance when a file was modified
    35  	mu      sync.RWMutex
    36  }
    37  
    38  // scan performs a new scan on the given directory, compares against the already
    39  // cached filenames, and returns file sets: creates, deletes, updates.
    40  func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Interface, error) {
    41  	t0 := time.Now()
    42  
    43  	// List all the failes from the keystore folder
    44  	files, err := ioutil.ReadDir(keyDir)
    45  	if err != nil {
    46  		return nil, nil, nil, err
    47  	}
    48  	t1 := time.Now()
    49  
    50  	fc.mu.Lock()
    51  	defer fc.mu.Unlock()
    52  
    53  	// Iterate all the files and gather their metadata
    54  	all := set.NewNonTS()
    55  	mods := set.NewNonTS()
    56  
    57  	var newLastMod time.Time
    58  	for _, fi := range files {
    59  		// Skip any non-key files from the folder
    60  		path := filepath.Join(keyDir, fi.Name())
    61  		if skipKeyFile(fi) {
    62  			log.Trace("Ignoring file on account scan", "path", path)
    63  			continue
    64  		}
    65  		// Gather the set of all and fresly modified files
    66  		all.Add(path)
    67  
    68  		modified := fi.ModTime()
    69  		if modified.After(fc.lastMod) {
    70  			mods.Add(path)
    71  		}
    72  		if modified.After(newLastMod) {
    73  			newLastMod = modified
    74  		}
    75  	}
    76  	t2 := time.Now()
    77  
    78  	// Update the tracked files and return the three sets
    79  	deletes := set.Difference(fc.all, all)   // Deletes = previous - current
    80  	creates := set.Difference(all, fc.all)   // Creates = current - previous
    81  	updates := set.Difference(mods, creates) // Updates = modified - creates
    82  
    83  	fc.all, fc.lastMod = all, newLastMod
    84  	t3 := time.Now()
    85  
    86  	// Report on the scanning stats and return
    87  	log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
    88  	return creates, deletes, updates, nil
    89  }
    90  
    91  // skipKeyFile ignores editor backups, hidden files and folders/symlinks.
    92  func skipKeyFile(fi os.FileInfo) bool {
    93  	// Skip editor backups and UNIX-style hidden files.
    94  	if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
    95  		return true
    96  	}
    97  	// Skip misc special files, directories (yes, symlinks too).
    98  	if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
    99  		return true
   100  	}
   101  	return false
   102  }