github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/accounts/keystore/file_cache.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2017 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package keystore
    26  
    27  import (
    28  	"io/ioutil"
    29  	"os"
    30  	"path/filepath"
    31  	"strings"
    32  	"sync"
    33  	"time"
    34  
    35  	mapset "github.com/deckarep/golang-set"
    36  	"github.com/ethereum/go-ethereum/log"
    37  )
    38  
    39  //filecache是在扫描密钥库期间看到的文件的缓存。
    40  type fileCache struct {
    41  all     mapset.Set //从keystore文件夹中设置所有文件
    42  lastMod time.Time  //上次修改文件时的实例
    43  	mu      sync.RWMutex
    44  }
    45  
    46  //scan对给定的目录执行新扫描,与
    47  //缓存文件名,并返回文件集:创建、删除、更新。
    48  func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, error) {
    49  	t0 := time.Now()
    50  
    51  //列出keystore文件夹中的所有故障
    52  	files, err := ioutil.ReadDir(keyDir)
    53  	if err != nil {
    54  		return nil, nil, nil, err
    55  	}
    56  	t1 := time.Now()
    57  
    58  	fc.mu.Lock()
    59  	defer fc.mu.Unlock()
    60  
    61  //迭代所有文件并收集其元数据
    62  	all := mapset.NewThreadUnsafeSet()
    63  	mods := mapset.NewThreadUnsafeSet()
    64  
    65  	var newLastMod time.Time
    66  	for _, fi := range files {
    67  		path := filepath.Join(keyDir, fi.Name())
    68  //跳过文件夹中的任何非关键文件
    69  		if nonKeyFile(fi) {
    70  			log.Trace("Ignoring file on account scan", "path", path)
    71  			continue
    72  		}
    73  //收集所有修改过的文件集
    74  		all.Add(path)
    75  
    76  		modified := fi.ModTime()
    77  		if modified.After(fc.lastMod) {
    78  			mods.Add(path)
    79  		}
    80  		if modified.After(newLastMod) {
    81  			newLastMod = modified
    82  		}
    83  	}
    84  	t2 := time.Now()
    85  
    86  //更新跟踪文件并返回三组
    87  deletes := fc.all.Difference(all)   //删除=上一个-当前
    88  creates := all.Difference(fc.all)   //创建=当前-上一个
    89  updates := mods.Difference(creates) //更新=修改-创建
    90  
    91  	fc.all, fc.lastMod = all, newLastMod
    92  	t3 := time.Now()
    93  
    94  //报告扫描数据并返回
    95  	log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
    96  	return creates, deletes, updates, nil
    97  }
    98  
    99  //非关键文件忽略编辑器备份、隐藏文件和文件夹/符号链接。
   100  func nonKeyFile(fi os.FileInfo) bool {
   101  //跳过编辑器备份和Unix样式的隐藏文件。
   102  	if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
   103  		return true
   104  	}
   105  //跳过其他特殊文件、目录(是,也可以跳过符号链接)。
   106  	if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
   107  		return true
   108  	}
   109  	return false
   110  }