fort/bin/lua/wipf/util/fs.lua

83 lines
1.8 KiB
Lua
Raw Normal View History

2015-01-19 06:40:38 +00:00
-- WIPF File-System Utilities
local sys = require"sys"
local win32 = sys.win32
2015-01-26 04:55:17 +00:00
local util_fs = {}
2015-01-19 06:40:38 +00:00
-- Get Process's Native path by identifier
2015-01-26 04:55:17 +00:00
function util_fs.pid_dospath(id)
2015-01-19 06:40:38 +00:00
local pid = sys.pid(id, true)
local path = (pid and pid:path()) or ""
pid:close()
return path
end
2015-01-26 04:55:17 +00:00
-- DOS device name <-> drive letter (A: .. Z:)
2015-01-19 06:40:38 +00:00
do
local drives
local function fill_drives()
drives = {}
for drive in sys.dir("/") do
local dos_name = win32.drive_dosname(drive):lower()
drives[dos_name] = drive
drives[drive] = dos_name
end
end
local function get_value(key)
if not drives or not drives[key] then
fill_drives()
end
return drives[key] or ""
end
2015-01-26 04:55:17 +00:00
-- Convert DOS device name to drive letter (A: .. Z:)
function util_fs.dosname_to_drive(dos_name)
2015-01-19 06:40:38 +00:00
return get_value(dos_name:lower())
end
2015-01-26 04:55:17 +00:00
-- Convert drive letter (A: .. Z:) to DOS device name
function util_fs.drive_to_dosname(drive)
2015-01-19 06:40:38 +00:00
return get_value(drive:upper())
end
end
-- Convert Native path to Win32 path
2015-01-26 04:55:17 +00:00
function util_fs.dospath_to_path(dos_path)
2015-01-19 06:40:38 +00:00
local dos_name, sub_path = string.match(dos_path, [[(\[^\]+\[^\]+)(\.+)]])
if not dos_name then
return dos_path
end
2015-01-26 04:55:17 +00:00
return util_fs.dosname_to_drive(dos_name) .. sub_path
2015-01-19 06:40:38 +00:00
end
-- Convert Win32 path to Native path
2015-01-26 04:55:17 +00:00
function util_fs.path_to_dospath(path)
2015-01-19 12:24:15 +00:00
local drive, sub_path = string.match(path, [[(%a:)(\.+)]])
2015-01-19 06:40:38 +00:00
if not drive then
return path
end
2015-01-26 04:55:17 +00:00
return util_fs.drive_to_dosname(drive) .. sub_path
2015-01-19 06:40:38 +00:00
end
2015-01-21 09:59:17 +00:00
-- Load file, run it in sandbox and return it's globals in a table
2015-01-26 04:55:17 +00:00
function util_fs.sandbox(path)
2015-01-21 09:59:17 +00:00
local chunk, err_msg = loadfile(path)
if not chunk then
return nil, err_msg
end
local env = setmetatable({}, nil)
setfenv(chunk, env)
chunk()
return env
end
2015-01-19 06:40:38 +00:00
2015-01-26 04:55:17 +00:00
return util_fs