Merge pull request #1699 from psychon/cache-dirs

Some fixes and changes to cache dirs
This commit is contained in:
Daniel Hahler 2017-04-18 14:40:22 +02:00 committed by GitHub
commit e0eb0f14b3
4 changed files with 34 additions and 24 deletions

View File

@ -210,15 +210,8 @@ end
-- @param id The data.history identifier -- @param id The data.history identifier
local function history_save(id) local function history_save(id)
if data.history[id] then if data.history[id] then
local f = io.open(id, "w") assert(gfs.make_parent_directories(id))
if not f then local f = assert(io.open(id, "w"))
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
for i = 1, math.min(#data.history[id].table, data.history[id].max) do for i = 1, math.min(#data.history[id].table, data.history[id].max) do
f:write(data.history[id].table[i] .. "\n") f:write(data.history[id].table[i] .. "\n")
end end

View File

@ -92,9 +92,9 @@ end
-- @return mkdir return code -- @return mkdir return code
-- @see gears.filesystem -- @see gears.filesystem
function util.mkdir(dir) 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 end
--- Eval Lua code. --- Eval Lua code.

View File

@ -11,22 +11,37 @@ 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, 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)
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. --- 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.
@ -93,7 +108,9 @@ end
--- Get the path to a directory that should be used for caching data. --- 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. -- @return A string with the requested path with a slash at the end.
function filesystem.get_cache_dir() 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 end
--- Get the path to the directory where themes are installed. --- Get the path to the directory where themes are installed.

View File

@ -146,7 +146,7 @@ end
local function write_count_table(count_table) local function write_count_table(count_table)
count_table = count_table or instance.count_table count_table = count_table or instance.count_table
local count_file_name = gfs.get_dir("cache") .. "/menu_count_file" 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 for name, count in pairs(count_table) do
local str = string.format("%s;%d\n", name, count) local str = string.format("%s;%d\n", name, count)
count_file:write(str) count_file:write(str)