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:
Uli Schlachter 2017-04-02 18:52:23 +02:00
parent dc717af3ad
commit b4b070785f
1 changed files with 16 additions and 6 deletions

View File

@ -11,22 +11,32 @@ local gtable = require("gears.table")
local filesystem = {} local filesystem = {}
--- Create a directory local function make_directory(gfile)
-- @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 success, err = gfile:make_directory_with_parents() local success, err = gfile:make_directory_with_parents()
if success then if success then
return true return true
end end
if err.domain == Gio.IOErrorEnum and err.code == "EXISTS" then 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 return true
end end
return false, err return false, err
end 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. --- Check if a file exists, is readable and not a directory.
-- @tparam string filename The file path. -- @tparam string filename The file path.
-- @treturn boolean True if file exists and is readable. -- @treturn boolean True if file exists and is readable.