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 <psychon@znc.in>
This commit is contained in:
parent
70d4961a3e
commit
c1b6b204d6
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
-- Grab environment we need
|
-- Grab environment we need
|
||||||
local os = os
|
local os = os
|
||||||
local io = io
|
|
||||||
local assert = assert
|
local assert = assert
|
||||||
local load = loadstring or load -- luacheck: globals loadstring (compatibility with Lua 5.1)
|
local load = loadstring or load -- luacheck: globals loadstring (compatibility with Lua 5.1)
|
||||||
local loadfile = loadfile
|
local loadfile = loadfile
|
||||||
|
@ -255,17 +254,11 @@ end
|
||||||
-- @param filename The file path.
|
-- @param filename The file path.
|
||||||
-- @return True if file exists and is readable.
|
-- @return True if file exists and is readable.
|
||||||
function util.file_readable(filename)
|
function util.file_readable(filename)
|
||||||
local file = io.open(filename)
|
local gfile = Gio.File.new_for_path(filename)
|
||||||
if file then
|
local gfileinfo = gfile:query_info("standard::type,access::can-read",
|
||||||
local _, _, code = file:read(1)
|
Gio.FileQueryInfoFlags.NONE)
|
||||||
io.close(file)
|
return gfileinfo and gfileinfo:get_file_type() ~= "DIRECTORY" and
|
||||||
if code == 21 then
|
gfileinfo:get_attribute_boolean("access::can-read")
|
||||||
-- "Is a directory".
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Check if a path exists, is readable and is a directory.
|
--- Check if a path exists, is readable and is a directory.
|
||||||
|
|
Loading…
Reference in New Issue