From 1d68712ba2e439f36874c4ed1e3d9ffec177a06c Mon Sep 17 00:00:00 2001 From: Herbert Xu Date: Fri, 2 Apr 2010 22:02:22 +0800 Subject: [BUILTIN] Use faccessat if available Eric Blake suggested that we should use faccessat so that ACLs and other corner cases are handled correctly. This patch does exactly that. Note that faccessat doesn't handle ACLs when euid != uid, as this case is currently implemented by glibc instead of the kernel, using code similar to the existing dash test. Signed-off-by: Herbert Xu --- src/bltin/test.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src') diff --git a/src/bltin/test.c b/src/bltin/test.c index 8e7077a..7888f38 100644 --- a/src/bltin/test.c +++ b/src/bltin/test.c @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -147,8 +148,12 @@ static int isoperand(char **); static int newerf(const char *, const char *); static int olderf(const char *, const char *); static int equalf(const char *, const char *); +#ifdef HAVE_FACCESSAT +static int test_file_access(const char *, int); +#else static int test_st_mode(const struct stat64 *, int); static int bash_group_member(gid_t); +#endif static inline intmax_t getn(const char *s) { @@ -295,6 +300,14 @@ primary(enum token n) return strlen(*t_wp) != 0; case FILTT: return isatty(getn(*t_wp)); +#ifdef HAVE_FACCESSAT + case FILRD: + return test_file_access(*t_wp, R_OK); + case FILWR: + return test_file_access(*t_wp, W_OK); + case FILEX: + return test_file_access(*t_wp, X_OK); +#endif default: return filstat(*t_wp, n); } @@ -364,12 +377,14 @@ filstat(char *nm, enum token mode) return 0; switch (mode) { +#ifndef HAVE_FACCESSAT case FILRD: return test_st_mode(&s, R_OK); case FILWR: return test_st_mode(&s, W_OK); case FILEX: return test_st_mode(&s, X_OK); +#endif case FILEXIST: return 1; case FILREG: @@ -469,6 +484,12 @@ equalf (const char *f1, const char *f2) b1.st_ino == b2.st_ino); } +#ifdef HAVE_FACCESSAT +static int test_file_access(const char *path, int mode) +{ + return !faccessat(AT_FDCWD, path, mode, AT_EACCESS); +} +#else /* HAVE_FACCESSAT */ /* * Similar to what access(2) does, but uses the effective uid and gid. * Doesn't make the mistake of telling root that any file is executable. @@ -519,3 +540,4 @@ bash_group_member(gid_t gid) return (0); } +#endif /* HAVE_FACCESSAT */ -- cgit 1.4.1 de10b5e75e001f7a15c2a6178066b&follow=1'>Collapse)Author 2021-01-20Generate tags for sh files in mtagsJune McEnroe Only matches functions declared at the beginnings of lines, but I'm fine with that. 2021-01-20Add messy sh lexerJune McEnroe Surprisingly seems to work for everything I looked at in my repos. 2021-01-20Add all target to git.causal.agency MakefileJune McEnroe 2021-01-20Remove Lua supportJune McEnroe Lua support is unused and the dlsym fwrite/write hacks horrify me. Clean it up. 2021-01-20Fix tests for diff spansJune McEnroe 2021-01-20Avoid matching ':' in make tagsJune McEnroe Otherwise a :: rule includes one of the ':'s in the tag name. 2021-01-19Prefer tag matches not preceded by [[:alnum:]]June McEnroe Otherwise the "id" in "void" matches for "void id". 2021-01-19Escape \ and / in mtags search patternsJune McEnroe 2021-01-20Use mtags in source-filterJune McEnroe 2021-01-19Add mtags to generate tags for make and mdocJune McEnroe 2021-01-19Map tags to IDs using only [[:alnum:]-._]June McEnroe 2021-01-19Don't use a pager if reading standard inputJune McEnroe 2021-01-19Support BSD make syntax and match *.amJune McEnroe These don't really go together, but... 2021-01-19Match tab following escaped newline in make assignmentsJune McEnroe Otherwise it ends up going into Shell state. 2021-01-18Allow matching lexers using first input lineJune McEnroe