-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdu.lua
More file actions
46 lines (42 loc) · 1.62 KB
/
du.lua
File metadata and controls
46 lines (42 loc) · 1.62 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
local filesystem = require "system.filesystem"
local util = require "system.util"
local args = assert(util.argparse({a = false, s = false, k = false, h = false, x = false, H = false, L = false}, ...))
if #args == 0 then args[1] = "." end
local retval = 0
local function sz(n)
if args.h then
if n >= 1000000000000 then return ("%.3gT"):format(n / 1000000000000)
elseif n >= 1000000000 then return ("%.3gG"):format(n / 1000000000)
elseif n >= 1000000 then return ("%.3gM"):format(n / 1000000)
elseif n >= 1000 then return ("%.3gK"):format(n / 1000)
else return ("%.3g "):format(n) end
elseif args.k then return math.ceil(n / 1024)
else return math.ceil(n / 512) end
end
local function search(path, mp, first)
local stat, err = filesystem.stat(path, not ((first and args.H) or args.L))
if stat then
if mp == true then mp = stat.mountpoint end
if mp and mp ~= stat.mountpoint then return 0 end
if stat.type == "directory" then
local size = 0
for _, v in ipairs(filesystem.list(path)) do
size = size + search(filesystem.combine(path, v), mp)
end
if not args.s then print(sz(size), path) end
return size
else
if args.a and not args.s then print(sz(stat.size), path) end
return stat.size
end
else
io.stderr:write("du: could not stat " .. path .. ": " .. (err or "") .. "\n")
retval = 1
return 0
end
end
for _, v in ipairs(args) do
local size = search(v, args.x, true)
if args.s then print(sz(size), v) end
end
return retval