From dc717af3add3439e045d634626f979e65ffc47e1 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 2 Apr 2017 18:46:54 +0200 Subject: [PATCH 1/6] gears.filesystem: Convert tabs to spaces Signed-off-by: Uli Schlachter --- lib/gears/filesystem.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/gears/filesystem.lua b/lib/gears/filesystem.lua index 9c0e340f..c382bf21 100644 --- a/lib/gears/filesystem.lua +++ b/lib/gears/filesystem.lua @@ -15,16 +15,16 @@ local filesystem = {} -- @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() - if success then - return true - end - if err.domain == Gio.IOErrorEnum and err.code == "EXISTS" then - -- Direcotry already exists, let this count as success - return true - end - return false, err + local gfile = Gio.File.new_for_path(dir) + 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 + return true + end + return false, err end --- Check if a file exists, is readable and not a directory. From b4b070785ff6a196298bec4067fcb3ca87a3b451 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 2 Apr 2017 18:52:23 +0200 Subject: [PATCH 2/6] 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 c382bf21..3bbbfc1a 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. From 4bbedebea49180bdca2920cd4a206fb820efc6f1 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 2 Apr 2017 18:54:08 +0200 Subject: [PATCH 3/6] awful.prompt: Use g.filesystem.make_parent_dir() The changes should not actually make a difference. If creating the directory fails, the error will now be different, but that should be about it. Signed-off-by: Uli Schlachter --- lib/awful/prompt.lua | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/awful/prompt.lua b/lib/awful/prompt.lua index 0755f03c..a61016e7 100644 --- a/lib/awful/prompt.lua +++ b/lib/awful/prompt.lua @@ -210,15 +210,8 @@ end -- @param id The data.history identifier local function history_save(id) if data.history[id] then - local f = io.open(id, "w") - if not f then - local i = 0 - for d in id:gmatch(".-/") do - i = i + #d - end - gfs.mkdir(id:sub(1, i - 1)) - f = assert(io.open(id, "w")) - end + assert(gfs.make_parent_directories(id)) + local f = assert(io.open(id, "w")) for i = 1, math.min(#data.history[id].table, data.history[id].max) do f:write(data.history[id].table[i] .. "\n") end From 48c15e4dfb5c695666e85ef26cdbf052b3f38a89 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 2 Apr 2017 18:56:24 +0200 Subject: [PATCH 4/6] g.fs.get_cache_dir: Ensure the dir exists This makes get_cache_dir() try to ensure the cache directory that it returns exists. Should-fix: https://github.com/awesomeWM/awesome/issues/1663 Signed-off-by: Uli Schlachter --- lib/gears/filesystem.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/gears/filesystem.lua b/lib/gears/filesystem.lua index 3bbbfc1a..ffc83fee 100644 --- a/lib/gears/filesystem.lua +++ b/lib/gears/filesystem.lua @@ -103,7 +103,9 @@ end --- Get the path to a directory that should be used for caching data. -- @return A string with the requested path with a slash at the end. function filesystem.get_cache_dir() - return filesystem.get_xdg_cache_home() .. "awesome/" + local result = filesystem.get_xdg_cache_home() .. "awesome/" + filesystem.mkdir(result) + return result end --- Get the path to the directory where themes are installed. From f1b78a6ff22d05398ea5a4c4fd759bd88ad641ff Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 2 Apr 2017 18:58:03 +0200 Subject: [PATCH 5/6] menubar: assert() that io.open() succeeds Improves-the-error-for: https://github.com/awesomeWM/awesome/issues/1663 Signed-off-by: Uli Schlachter --- lib/menubar/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/menubar/init.lua b/lib/menubar/init.lua index 87acfa88..0a1ca496 100644 --- a/lib/menubar/init.lua +++ b/lib/menubar/init.lua @@ -146,7 +146,7 @@ end local function write_count_table(count_table) count_table = count_table or instance.count_table local count_file_name = gfs.get_dir("cache") .. "/menu_count_file" - local count_file = io.open (count_file_name, "w") + local count_file = assert(io.open(count_file_name, "w")) for name, count in pairs(count_table) do local str = string.format("%s;%d\n", name, count) count_file:write(str) From 4d0c9eb86e747c2749a86f68b6bda52a32e9658d Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 8 Apr 2017 11:02:53 +0200 Subject: [PATCH 6/6] Rename filesystem.mkdir to make_directories The longer name is a bit more self-explanatory. The plural is meant to indicate that this recursively creates missing parent directories and does not just try to create the single given target directory. Since filesystem.mkdir() is part of the v4.1 release, a deprecation stub is needed. Signed-off-by: Uli Schlachter --- lib/awful/util.lua | 4 ++-- lib/gears/filesystem.lua | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/awful/util.lua b/lib/awful/util.lua index 11323f7c..b32694f0 100644 --- a/lib/awful/util.lua +++ b/lib/awful/util.lua @@ -91,9 +91,9 @@ end -- @return mkdir return code -- @see gears.filesystem function util.mkdir(dir) - util.deprecate("gears.filesystem.mkdir", {deprecated_in=5}) + util.deprecate("gears.filesystem.make_directories", {deprecated_in=5}) - return gfs.mkdir(dir) + return gfs.make_directories(dir) end --- Eval Lua code. diff --git a/lib/gears/filesystem.lua b/lib/gears/filesystem.lua index ffc83fee..cdc841fc 100644 --- a/lib/gears/filesystem.lua +++ b/lib/gears/filesystem.lua @@ -23,13 +23,18 @@ local function make_directory(gfile) return false, err end ---- Create a directory +--- Create a directory, including all missing parent directories. -- @tparam string dir The directory. -- @return (true, nil) on success, (false, err) on failure -function filesystem.mkdir(dir) +function filesystem.make_directories(dir) return make_directory(Gio.File.new_for_path(dir)) end +function filesystem.mkdir(dir) + require("gears.debug").deprecate("gears.filesystem.make_directories", {deprecated_in=5}) + return filesystem.make_directories(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 @@ -104,7 +109,7 @@ end -- @return A string with the requested path with a slash at the end. function filesystem.get_cache_dir() local result = filesystem.get_xdg_cache_home() .. "awesome/" - filesystem.mkdir(result) + filesystem.make_directories(result) return result end