Skip to content

Commit

Permalink
fix: Termux compatible is_readable_directory function (justinhj#42)
Browse files Browse the repository at this point in the history
* feat(util): use test command to check directory

* fix: use single quotes in util/file.lua

* feat(util): add type hints and documentation, remove old function

* feat(parsers): move all parsers to their own submodule (justinhj#43)

BREAKING CHANGE: Parser modules have been moved

Add `check` functions to all parser modules

* feat(util): use old "readable directory" method when not in Termux
  • Loading branch information
Agent-E11 committed Jul 11, 2024
1 parent 8cddead commit 18fa42d
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lua/util/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ local M = {}

local bit = require('bit')

function M.is_readable_directory(file)
local s = vim.loop.fs_stat(file)
return s ~= nil and s.type == 'directory' and bit.band(s.mode, 4) == 4
---Check if there is a readable directory at the given file path.
---Warning: the `path` may be substituted onto the command line,
---so the input _must_ be trusted.
---@param path string
---@return boolean
function M.is_readable_directory(path)
if not os.getenv('TERMUX_VERSION') then
-- When not running in Termux
local s = vim.loop.fs_stat(path)
return (
s ~= nil -- File exists
and s.type == 'directory' -- File is a directory
and bit.band(s.mode, 4) == 4 -- File is readable (Minimum permissions: ------r--)
)
else
-- In Termux, use the `test` command
return os.execute('test -d \''..path..'\' && test -r \''..path..'\'') == 0
end
end

return M

0 comments on commit 18fa42d

Please sign in to comment.