summaryrefslogtreecommitdiff
path: root/server/src/async/scanfiles.lua
blob: f54b0866edc3f8441ec68d3d93d47fd018c79810 (plain)
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
local args = ...

require 'utility'
local fs = require 'bee.filesystem'
local path_filter = require 'path_filter'

local function scan(root, filter)
    local result = {}
    local len = #root:string()
    local name = root:string():sub(len+2):gsub('/', '\\')
    if filter(name) then
        OUT:push('log', '过滤文件:', root:string())
    else
        result[#result+1] = root
    end
    local i = 0
    return function ()
        i = i + 1
        local current = result[i]
        if not current then
            return nil
        end
        if fs.is_directory(current) then
            for path in current:list_directory() do
                local name = path:string():sub(len+2):gsub('/', '\\')
                if filter(name) then
                    OUT:push('log', '过滤文件:', path:string())
                else
                    result[#result+1] = path
                end
            end
        end
        return current
    end
end

local ignore = {}
for _, name in ipairs(args.ignored) do
    if name:sub(1, 1) ~= '!' then
        ignore[#ignore+1] = name
    end
end
local filter = path_filter(ignore)
for path in scan(fs.path(args.root), filter) do
    local ok, msg = IN:pop()
    if ok and msg == 'stop' then
        OUT:push 'stop'
        return
    end
    OUT:push('path', fs.absolute(path):string())
end

OUT:push 'ok'