Integrate mkdirp function in common module (#265)
Move the function mkdirp into common to be generally available. Use the new common.mkdirp from create_user_directory() from core/init.lua replacing previous parent directory creation code within the function. The previous mkdirp function did not work on Windows where absolute paths starts with a drive letter. The code from create_user_directory() did not have this problem but was wrong in the way it was creating the nested directories. The new implementation in common.mkdirp fix both problems.
This commit is contained in:
@@ -273,4 +273,26 @@ function common.relative_path(ref_dir, dir)
|
||||
end
|
||||
|
||||
|
||||
function common.mkdirp(path)
|
||||
local stat = system.get_file_info(path)
|
||||
if stat and stat.type then
|
||||
return false, "path exists", path
|
||||
end
|
||||
local subdirs = {}
|
||||
while path and path ~= "" do
|
||||
local success_mkdir = system.mkdir(path)
|
||||
if success_mkdir then break end
|
||||
local updir, basedir = path:match("(.*)[/\\](.+)$")
|
||||
table.insert(subdirs, 1, basedir or path)
|
||||
path = updir
|
||||
end
|
||||
for _, dirname in ipairs(subdirs) do
|
||||
path = path and path .. PATHSEP .. dirname or dirname
|
||||
if not system.mkdir(path) then
|
||||
return false, "cannot create directory", path
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
return common
|
||||
|
||||
Reference in New Issue
Block a user