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 diff --git a/lib/awful/util.lua b/lib/awful/util.lua index 4c9eb44f..f8edf545 100644 --- a/lib/awful/util.lua +++ b/lib/awful/util.lua @@ -92,9 +92,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 9c0e340f..cdc841fc 100644 --- a/lib/gears/filesystem.lua +++ b/lib/gears/filesystem.lua @@ -11,20 +11,35 @@ local gtable = require("gears.table") local filesystem = {} ---- Create a directory +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 + -- Directory already exists, let this count as success + return true + end + return false, err +end + +--- Create a directory, including all missing parent directories. -- @tparam string dir The directory. -- @return (true, nil) on success, (false, err) on failure +function filesystem.make_directories(dir) + return make_directory(Gio.File.new_for_path(dir)) +end + 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 + 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 +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. @@ -93,7 +108,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.make_directories(result) + return result end --- Get the path to the directory where themes are installed. 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)