github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/accounts/keystore/file_cache.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package keystore
    13  
    14  import (
    15  	"io/ioutil"
    16  	"os"
    17  	"path/filepath"
    18  	"strings"
    19  	"sync"
    20  	"time"
    21  
    22  	"github.com/Sberex/go-sberex/log"
    23  	set "gopkg.in/fatih/set.v0"
    24  )
    25  
    26  // fileCache is a cache of files seen during scan of keystore.
    27  type fileCache struct {
    28  	all     *set.SetNonTS // Set of all files from the keystore folder
    29  	lastMod time.Time     // Last time instance when a file was modified
    30  	mu      sync.RWMutex
    31  }
    32  
    33  // scan performs a new scan on the given directory, compares against the already
    34  // cached filenames, and returns file sets: creates, deletes, updates.
    35  func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Interface, error) {
    36  	t0 := time.Now()
    37  
    38  	// List all the failes from the keystore folder
    39  	files, err := ioutil.ReadDir(keyDir)
    40  	if err != nil {
    41  		return nil, nil, nil, err
    42  	}
    43  	t1 := time.Now()
    44  
    45  	fc.mu.Lock()
    46  	defer fc.mu.Unlock()
    47  
    48  	// Iterate all the files and gather their metadata
    49  	all := set.NewNonTS()
    50  	mods := set.NewNonTS()
    51  
    52  	var newLastMod time.Time
    53  	for _, fi := range files {
    54  		// Skip any non-key files from the folder
    55  		path := filepath.Join(keyDir, fi.Name())
    56  		if skipKeyFile(fi) {
    57  			log.Trace("Ignoring file on account scan", "path", path)
    58  			continue
    59  		}
    60  		// Gather the set of all and fresly modified files
    61  		all.Add(path)
    62  
    63  		modified := fi.ModTime()
    64  		if modified.After(fc.lastMod) {
    65  			mods.Add(path)
    66  		}
    67  		if modified.After(newLastMod) {
    68  			newLastMod = modified
    69  		}
    70  	}
    71  	t2 := time.Now()
    72  
    73  	// Update the tracked files and return the three sets
    74  	deletes := set.Difference(fc.all, all)   // Deletes = previous - current
    75  	creates := set.Difference(all, fc.all)   // Creates = current - previous
    76  	updates := set.Difference(mods, creates) // Updates = modified - creates
    77  
    78  	fc.all, fc.lastMod = all, newLastMod
    79  	t3 := time.Now()
    80  
    81  	// Report on the scanning stats and return
    82  	log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
    83  	return creates, deletes, updates, nil
    84  }
    85  
    86  // skipKeyFile ignores editor backups, hidden files and folders/symlinks.
    87  func skipKeyFile(fi os.FileInfo) bool {
    88  	// Skip editor backups and UNIX-style hidden files.
    89  	if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
    90  		return true
    91  	}
    92  	// Skip misc special files, directories (yes, symlinks too).
    93  	if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
    94  		return true
    95  	}
    96  	return false
    97  }