diff options
author | 2021-05-04 02:40:10 +0200 | |
---|---|---|
committer | 2021-05-03 20:40:10 -0400 | |
commit | 25137717c9392d142650fcd679b09c400a2f5c4a (patch) | |
tree | 3d0f917bd4ef51a36cb45f78e3b8d33dcf375ffe /plugins/util.lua | |
parent | a90f1c885d8cbb7105cf6af2e1e2661799b5c529 (diff) |
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
Diffstat (limited to 'plugins/util.lua')
-rw-r--r-- | plugins/util.lua | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/plugins/util.lua b/plugins/util.lua new file mode 100644 index 00000000000..6b2d7f438bf --- /dev/null +++ b/plugins/util.lua @@ -0,0 +1,55 @@ +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 +} |