From c1b6b204d68b6e84ca7040b014cc537522bb73bb Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Tue, 25 Oct 2016 00:23:18 +0200 Subject: [PATCH] awful.util.file_readable: Use Gio (#1187) Instead of doing Linux-specific magic with error codes and trying to read the first byte of a file, just use Gio to check if a file exists and is readable. Signed-off-by: Uli Schlachter --- lib/awful/util.lua | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/awful/util.lua b/lib/awful/util.lua index dd114fb7c..899d62946 100644 --- a/lib/awful/util.lua +++ b/lib/awful/util.lua @@ -8,7 +8,6 @@ -- Grab environment we need local os = os -local io = io local assert = assert local load = loadstring or load -- luacheck: globals loadstring (compatibility with Lua 5.1) local loadfile = loadfile @@ -255,17 +254,11 @@ end -- @param filename The file path. -- @return True if file exists and is readable. function util.file_readable(filename) - local file = io.open(filename) - if file then - local _, _, code = file:read(1) - io.close(file) - if code == 21 then - -- "Is a directory". - return false - end - return true - end - return false + local gfile = Gio.File.new_for_path(filename) + 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 --- Check if a path exists, is readable and is a directory.