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