-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdf.lua
More file actions
63 lines (62 loc) · 2.69 KB
/
df.lua
File metadata and controls
63 lines (62 loc) · 2.69 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
local filesystem = require "system.filesystem"
local util = require "system.util"
local args = assert(util.argparse({k = false, P = false, t = false, h = false}, ...))
local mounts = filesystem.mountlist()
local info = {}
if #args == 0 then for i, v in ipairs(mounts) do info[i], v.stat = v, filesystem.stat(v.path) end
else
for i, v in ipairs(args) do
local stat, err = filesystem.stat(v)
if stat then for _, m in ipairs(mounts) do if m.path == stat.mountpoint then info[i], m.stat = m, stat break end end
else io.stderr:write("df: could not stat " .. v .. ": " .. (err or "") .. "\n") end
end
end
local function inf(n) if n == math.huge then return "inf" elseif n ~= n then return "nan" else return tostring(n) end end
if args.P then
local bs = args.k and 1024 or 512
print("Filesystem " .. bs .. "-blocks Used Available Capacity Mounted on")
for _, v in ipairs(info) do
print(("%s %s %s %s %s%% %s"):format(
v.source,
inf(math.ceil(v.stat.capacity / bs)),
inf(math.ceil((v.stat.capacity - v.stat.freeSpace) / bs)),
inf(math.ceil(v.stat.freeSpace / bs)),
inf(math.floor((v.stat.capacity - v.stat.freeSpace) / v.stat.capacity * 100)),
v.path
))
end
elseif args.h then
local function sz(n)
if n == math.huge then return "inf"
elseif n ~= n then return "nan"
elseif 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
end
print("Filesystem\tSize\tUsed\tAvail\tUse%\tMounted on")
for _, v in ipairs(info) do
print(("%s\t%s\t%s\t%s\t%s%%\t%s"):format(
v.source .. (#v.source < 8 and "\t" or ""),
sz(v.stat.capacity),
sz(v.stat.capacity - v.stat.freeSpace),
sz(v.stat.freeSpace),
inf(math.floor((v.stat.capacity - v.stat.freeSpace) / v.stat.capacity * 100)),
v.path
))
end
else
local bs = args.k and 1024 or 512
print("Filesystem\tSize\tUsed\tAvail\tUse%\tMounted on")
for _, v in ipairs(info) do
print(("%s\t%s\t%s\t%s\t%s%%\t%s"):format(
v.source .. (#v.source < 8 and "\t" or ""),
inf(math.ceil(v.stat.capacity / bs)),
inf(math.ceil((v.stat.capacity - v.stat.freeSpace) / bs)),
inf(math.ceil(v.stat.freeSpace / bs)),
inf(math.floor((v.stat.capacity - v.stat.freeSpace) / v.stat.capacity * 100)),
v.path
))
end
end