Merge pull request #517 from blueyed/move-is_readable_directory

Move is_readable_directory to awful.util.dir_readable
This commit is contained in:
Daniel Hahler 2015-10-11 18:21:10 +02:00
commit 71ae967a84
2 changed files with 17 additions and 16 deletions

View File

@ -21,6 +21,7 @@ local rtable = table
local pairs = pairs local pairs = pairs
local string = string local string = string
local lgi = require("lgi") local lgi = require("lgi")
local Gio = require("lgi").Gio
local Pango = lgi.Pango local Pango = lgi.Pango
local capi = local capi =
{ {
@ -202,6 +203,17 @@ function util.file_readable(filename)
return false return false
end end
--- Check if a path exists, is readable and is a directory.
-- @tparam string path The directory path.
-- @treturn boolean True if dir exists and is readable.
function util.dir_readable(path)
local gfile = Gio.File.new_for_path(path)
local gfileinfo = gfile:query_info("standard::type,access::can-read",
Gio.FileQueryInfoFlags.NONE)
return gfileinfo and gfileinfo:get_file_type() == "DIRECTORY" and
gfileinfo:get_attribute_boolean("access::can-read")
end
local function subset_mask_apply(mask, set) local function subset_mask_apply(mask, set)
local ret = {} local ret = {}
for i = 1, #set do for i = 1, #set do

View File

@ -14,7 +14,6 @@
local beautiful = require("beautiful") local beautiful = require("beautiful")
local awful = require("awful") local awful = require("awful")
local GLib = require("lgi").GLib local GLib = require("lgi").GLib
local Gio = require("lgi").Gio
local index_theme = require("menubar.index_theme") local index_theme = require("menubar.index_theme")
local ipairs = ipairs local ipairs = ipairs
@ -38,32 +37,22 @@ local get_default_base_directories = function()
return dirs return dirs
end end
local is_readable_directory = function(path)
local gfile = Gio.File.new_for_path(path)
local gfileinfo = gfile:query_info("standard::type,access::can-read",
Gio.FileQueryInfoFlags.NONE)
if not gfileinfo then return false end -- practically ENOENT
local is_dir = (gfileinfo:get_file_type() == "DIRECTORY")
local is_readable = gfileinfo:get_attribute_boolean("access::can-read")
return is_dir and is_readable
end
local get_pragmatic_base_directories = function() local get_pragmatic_base_directories = function()
local dirs = {} local dirs = {}
local dir = GLib.build_filenamev({GLib.get_home_dir(), ".icons"}) local dir = GLib.build_filenamev({GLib.get_home_dir(), ".icons"})
if is_readable_directory(dir) then if awful.util.dir_readable(dir) then
table.insert(dirs, dir) table.insert(dirs, dir)
end end
dir = GLib.build_filenamev({GLib.get_user_data_dir(), "icons"}) dir = GLib.build_filenamev({GLib.get_user_data_dir(), "icons"})
if is_readable_directory(dir) then if awful.util.dir_readable(dir) then
table.insert(dirs, dir) table.insert(dirs, dir)
end end
for _, v in ipairs(GLib.get_system_data_dirs()) do for _, v in ipairs(GLib.get_system_data_dirs()) do
dir = GLib.build_filenamev({v, "icons"}) dir = GLib.build_filenamev({v, "icons"})
if is_readable_directory(dir) then if awful.util.dir_readable(dir) then
table.insert(dirs, dir) table.insert(dirs, dir)
end end
end end
@ -71,7 +60,7 @@ local get_pragmatic_base_directories = function()
local need_usr_share_pixmaps = true local need_usr_share_pixmaps = true
for _, v in ipairs(GLib.get_system_data_dirs()) do for _, v in ipairs(GLib.get_system_data_dirs()) do
dir = GLib.build_filenamev({v, "pixmaps"}) dir = GLib.build_filenamev({v, "pixmaps"})
if is_readable_directory(dir) then if awful.util.dir_readable(dir) then
table.insert(dirs, dir) table.insert(dirs, dir)
end end
if dir == "/usr/share/pixmaps" then if dir == "/usr/share/pixmaps" then
@ -80,7 +69,7 @@ local get_pragmatic_base_directories = function()
end end
dir = "/usr/share/pixmaps" dir = "/usr/share/pixmaps"
if need_usr_share_pixmaps and is_readable_directory(dir) then if need_usr_share_pixmaps and awful.util.dir_readable(dir) then
table.insert(dirs, dir) table.insert(dirs, dir)
end end