scan.lua

Sun, 06 Sep 2020 22:12:52 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 06 Sep 2020 22:12:52 +0300
changeset 35
2f927eae429b
parent 26
77cd7b8fb6a6
permissions
-rw-r--r--

Don't auto-create directories that will be empty

2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
1
7
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
2 --@module scan
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
3 local scan={}
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
4
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
5 local lfs=require('lfs')
26
77cd7b8fb6a6 Rename path.lua mypath.lua not conflict with other lua path modules.
Tuomo Valkonen <tuomov@iki.fi>
parents: 7
diff changeset
6 local path=require('mypath')
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 local function filtered_dir(d)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 local f, s_, v_ = lfs.dir(d)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 local function g(s, v)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 while true do
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 local vn=f(s, v)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 if vn~='.' and vn~='..' then
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 return vn
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 return g, s_, v_
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20
7
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
21 function scan.scan(d)
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 local h={}
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 for f in filtered_dir(d) do
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 local n=d..'/'..f
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 local a=lfs.attributes(n)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 if a.mode=='directory' then
7
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
27 local nh=scan.scan(n)
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
28 h[f]=nh
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 elseif a.mode=='file' then
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
30 h[f]=true
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 return h
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35
7
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
36 function scan.hfold(h, a, f, g)
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 for n, c in pairs(h) do
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 if type(c)=='table' then
7
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
39 scan.hfold(c, f(a, n), f, g)
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 else
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 g(a, n)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 end
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
45
7
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
46 function scan.map(h, g, d)
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
47 local function dir(prefix, name)
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
48 local p=path.join(prefix, name)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
49 if d then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
50 d(p)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
51 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
52 return p
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
53 end
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
54
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
55 local function file(prefix, name)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
56 local f=path.join(prefix, name)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
57 g(f)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
58 end
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
59
7
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
60 scan.hfold(h, "", dir, file)
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
61 end
7
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
62
038275cd92ed Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
63 return scan

mercurial