-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.sh
More file actions
executable file
·170 lines (151 loc) · 3.58 KB
/
Copy pathscan.sh
File metadata and controls
executable file
·170 lines (151 loc) · 3.58 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/sh
# usage: scan.sh [root [dbpath]] <proto
# see also:
# • apply.sh
# • replica/updatedb in http://9p.io/magic/man2html/8/replica
# • proto(1) in AIX: https://www.ibm.com/docs/en/aix/7.3?topic=p-proto-command
#
# test while hacking:
# Watch ./scan_test.sh
exec awk -v "root=$1" -v "dbpath=$2" -v "time=`date +%s`" '
BEGIN {
loaddb(dbpath)
} /^[\t ]*(#.*)?$/ {
next
} {
walk(stk, lvl($0), $1, $2, $3, $4, $5)
} END {
dels()
}
function walk(stk, n, wname, mode, uid, gid, serverpath) {
if(wname == "+" || wname == "*") {
walkdir(stk[n], (wname == "+"))
} else {
stk[++n] = pathjoin(stk[n], expand(wname))
walk1(stk[n], serverpath, mode, uid, gid)
}
}
function walkdir(path, deep, _, cmd, s, p, isdir) {
cmd = "ls " q(pathjoin(root, path))
while(cmd | getline s > 0) {
if(s ~ /[ \t]/)
continue
p = pathjoin(path, s)
isdir = walk1(p)
if(isdir && deep)
walkdir(p, deep)
}
close(cmd)
}
function walk1(path, serverpath, mode, uid, gid, _, p, d, od) {
p = path
if(serverpath != "" && serverpath != "-") p = serverpath
if(stat(pathjoin(root, p), d) < 0)
return mode ~ /d/
if(mode != "" && mode != "-") d["mode"] = mode
if(uid != "" && uid != "-") d["uid"] = uid
if(gid != "" && gid != "-") d["gid"] = gid
if(!dbpop(path, od))
return update("a", path, serverpath, d)
if(d["mode"] !~ /d/ && (d["mtime"] != od["mtime"] || d["length"] != od["length"]))
return update("c", path, serverpath, d)
if(d["mode"] != od["mode"] || d["gid"] != od["gid"])
return update("m", path, serverpath, d)
return d["mode"] ~ /d/
}
function dels(_, i, path, d) {
for(i = dbn; i > 0; i--)
if(dbpop(path=dbq[i], d))
update("d", path, "-", d)
}
function update(verb, path, serverpath, d) {
if(serverpath == "") serverpath = "-"
print time, gen++, verb, path, serverpath, \
d["mode"], d["uid"], d["gid"], d["mtime"], d["length"]
if(dbpath != "")
print dbstr(path, d, verb) >> dbpath
system("") # fflush
return d["mode"] ~ /d/
}
function dbstr(path, d, verb, _, mode) {
mode = (verb=="d"? "REMOVED" : d["mode"])
return path " " mode " " d["uid"] " " d["gid"] " " d["mtime"] " " d["length"]
}
function dbput(path, mode, str) {
db[path] = str
dbq[++dbn] = path
if(mode == "REMOVED")
delete db[path]
}
function loaddb(dbpath) {
if(dbpath == "")
return
while(getline<dbpath > 0)
dbput($1, $2, $0)
close(dbpath)
}
function dbpop(path, d, _, r){
if(r = dbstat(path, d))
delete db[path]
return r
}
function dbstat(path, d, _, f) {
delete d
if(!(path in db))
return 0
split(db[path], f)
d["mode"] = f[2]
d["uid"] = f[3]
d["gid"] = f[4]
d["mtime"] = int(f[5])
d["length"] = int(f[6])
return 1
}
function stat(path, d) {
return stat_bsd(path, d)
}
function stat_bsd(path, d, _, cmd, s, f, ret, flag) {
delete d
ret = -1
flag = silentstat? "q" : "" # noise-manage the tests
cmd = "stat -" flag "f %.6Op/%Su/%Sg/%Um/%Uz " q(path)
while(cmd | getline s > 0) {
if(split(s, f, "/") != 5)
continue
d["mode"] = modestr_bsd(f[1])
d["uid"] = f[2]
d["gid"] = f[3]
d["mtime"] = int(f[4])
d["length"] = int(f[5])
ret = 0
}
close(cmd)
return ret
}
function modestr_bsd(s, _, r) {
if(s ~ /^.[4]/)
r = "d"
return r substr(s, 4, 3)
}
function q(s, apos, bsl) {
apos = sprintf("%c", 39)
bsl = sprintf("%c", 92)
gsub(apos, apos bsl apos apos, s)
return apos s apos
}
function pathjoin(a, b, _, sep) {
if(a != "" && b != "")
sep = "/"
return a sep b
}
function lvl(s) {
match(s, "^\t*") # count hard tabs only, by design
return RLENGTH
}
function expand(s, _, v) {
if(s !~ /^[$]/)
return s
v = substr(s, 2, length(s)-1)
return ENVIRON[v]
}
' # <proto