Add gears.filesystem.make_parent_dir(path)
Given a path, this function tries to recursively create parent directories. Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
dc717af3ad
commit
b4b070785f
|
@ -11,22 +11,32 @@ local gtable = require("gears.table")
|
|||
|
||||
local filesystem = {}
|
||||
|
||||
--- Create a directory
|
||||
-- @tparam string dir The directory.
|
||||
-- @return (true, nil) on success, (false, err) on failure
|
||||
function filesystem.mkdir(dir)
|
||||
local gfile = Gio.File.new_for_path(dir)
|
||||
local function make_directory(gfile)
|
||||
local success, err = gfile:make_directory_with_parents()
|
||||
if success then
|
||||
return true
|
||||
end
|
||||
if err.domain == Gio.IOErrorEnum and err.code == "EXISTS" then
|
||||
-- Direcotry already exists, let this count as success
|
||||
-- Directory already exists, let this count as success
|
||||
return true
|
||||
end
|
||||
return false, err
|
||||
end
|
||||
|
||||
--- Create a directory
|
||||
-- @tparam string dir The directory.
|
||||
-- @return (true, nil) on success, (false, err) on failure
|
||||
function filesystem.mkdir(dir)
|
||||
return make_directory(Gio.File.new_for_path(dir))
|
||||
end
|
||||
|
||||
--- Create all parent directories for a given file.
|
||||
-- @tparam string path The path whose parents should be created.
|
||||
-- @return (true, nil) on success, (false, err) on failure
|
||||
function filesystem.make_parent_directories(file)
|
||||
return make_directory(Gio.File.new_for_path(file):get_parent())
|
||||
end
|
||||
|
||||
--- Check if a file exists, is readable and not a directory.
|
||||
-- @tparam string filename The file path.
|
||||
-- @treturn boolean True if file exists and is readable.
|
||||
|
|
Loading…
Reference in New Issue