mame/plugins/util.lua
Szunti 25137717c9
Create console history file in homepath (#8026)
* Fix console history path, homepath is a core option

* Create missing directories recursively in lua plugins.

* Add lfs to global environment in a less magical way.

require normally doesn't bind the name globally just returns the
module, mame sets a preloader that does bind lfs globally, but
maybe it's less surprising to do it explicitly
2021-05-03 20:40:10 -04:00

56 lines
1.4 KiB
Lua

local lfs = require("lfs")
-- Returns true if dirname is an existing directory, false if not a directory,
-- or nil, an error message and a system dependent error code on error.
local function is_dir(dirname)
local ret, err, code = lfs.attributes(dirname, "mode")
if ret == nil then
return ret, err, code
else
return ret == "directory"
end
end
-- Get the directory name for the file.
local function dirname(filename)
if filename == "/" then
return "/"
end
local parent = filename:match("(.*)/.")
if parent then
if parent == "" then
parent = "/"
end
return parent
end
return "."
end
-- Create dir and parents for dir if needed. Returns true on success,
-- nil, an error message and a system dependent error code on error.
local function mkdir_recursive(dir)
local ret, err, code = is_dir(dir)
if ret == true then
return true
end
local parent = dirname(dir)
local ret, err, code = mkdir_recursive(parent)
if not ret then
return ret, err, code
end
return lfs.mkdir(dir)
end
-- Create the parents of the file recursively if needed, returns true on success,
-- or nil, an error message and a system dependent error code on error.
local function create_parent_dirs(filename)
local parent = dirname(filename)
return mkdir_recursive(parent)
end
return {
dirname = dirname,
mkdir_recursive = mkdir_recursive,
create_parent_dirs = create_parent_dirs
}