From b4b070785ff6a196298bec4067fcb3ca87a3b451 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 2 Apr 2017 18:52:23 +0200 Subject: [PATCH] Add gears.filesystem.make_parent_dir(path) Given a path, this function tries to recursively create parent directories. Signed-off-by: Uli Schlachter --- lib/gears/filesystem.lua | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/gears/filesystem.lua b/lib/gears/filesystem.lua index c382bf212..3bbbfc1a9 100644 --- a/lib/gears/filesystem.lua +++ b/lib/gears/filesystem.lua @@ -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.