Permalink
Browse files

Added apr.filepath_executable() function, improved apr.filepath_which()

  • Loading branch information...
xolox committed Nov 6, 2011
1 parent fa3d6bf commit 5d07b22bc2e1d52790bdd75454f58c6fe22c6898
Showing with 30 additions and 4 deletions.
  1. +24 −3 src/apr.lua
  2. +6 −1 test/filepath.lua
View
@@ -14,7 +14,17 @@
--]]
local apr = require 'apr.core'
-apr._VERSION = '0.21.5'
+apr._VERSION = '0.21.6'
+
+local function executable(type, user, group, protection)
+ if type == 'file' and user and group and protection then
+ local current_user, current_group = assert(apr.user_get())
+ return (protection:find '^(r.x)(...)(...)$' and user == current_user)
+ or (protection:find '^(...)(r.x)(...)$' and group == current_group)
+ or (protection:find '^(...)(...)(r.[xt])$' and user ~= current_user and group ~= current_group)
+ end
+ return false
+end
-- apr.md5(input [, binary]) -> digest {{{1
--
@@ -84,8 +94,8 @@ function apr.filepath_which(program, find_all)
local results = find_all and {}
for _, directory in ipairs(split(apr.env_get 'PATH')) do
local candidate = apr.filepath_merge(directory, program)
- if apr.stat(candidate, 'type') == 'file' then
- -- TODO if not is_windows check executable bits
+ local t, u, g, p = apr.stat(candidate, 'type', 'user', 'group', 'protection')
+ if t == 'file' and is_windows or executable(t, u, g, p) then
if not find_all then return candidate end
results[#results + 1] = candidate
end
@@ -102,6 +112,17 @@ function apr.filepath_which(program, find_all)
return results
end
+-- apr.filepath_executable(path) -> is_executable {{{1
+--
+-- Check whether the file pointed to by @path is executable.
+-- Returns true when the file is executable, false otherwise.
+--
+-- Part of the "File path manipulation" module.
+
+function apr.filepath_executable(path)
+ return executable(apr.stat(path, 'type', 'user', 'group', 'protection'))
+end
+
-- apr.glob(pattern [, ignorecase]) -> iterator {{{1
--
-- Split @pattern into a directory path and a filename pattern and return an
View
@@ -3,7 +3,7 @@
Unit tests for the file path manipulation module of the Lua/APR binding.
Author: Peter Odding <peter@peterodding.com>
- Last Change: March 27, 2011
+ Last Change: November 6, 2011
Homepage: http://peterodding.com/code/lua/apr/
License: MIT
@@ -112,3 +112,8 @@ for i = 1, #parts_in do
assert(liststr == parts_in[i])
end
end
+
+-- Test apr.filepath_which() and apr.filepath_executable(). {{{1
+
+local lua_program = assert(apr.filepath_which 'lua')
+assert(apr.filepath_executable(lua_program))

0 comments on commit 5d07b22

Please sign in to comment.