From: Michael Orlitzky Date: Mon, 26 Feb 2018 02:35:02 +0000 (-0500) Subject: Allow apply_default_acl() to take a stat pointer for its path argument. X-Git-Tag: v0.1.0~22 X-Git-Url: https://gitweb.michael.orlitzky.com/?p=apply-default-acl.git;a=commitdiff_plain;h=df929bebddb9852c8e50481169e2fd1b5a01faf6 Allow apply_default_acl() to take a stat pointer for its path argument. When operating recursively, nftw has already called stat() on the path that it feeds to apply_default_acl(). Rather than re-stat it, we can now pass a stat struct pointer in as the second argument to apply_default_acl(). When not operating recursively, you just pass it NULL instead, and apply_default_acl() will do the fstat as before. --- diff --git a/src/apply-default-acl.c b/src/apply-default-acl.c index 1d59030..c4d0c3b 100644 --- a/src/apply-default-acl.c +++ b/src/apply-default-acl.c @@ -534,6 +534,10 @@ int wipe_acls(int fd) { * @param path * The path whose ACL we would like to reset to its default. * + * @param sp + * A pointer to a stat structure for @c path, or @c NULL if you don't + * have one handy. + * * @param no_exec_mask * The value (either true or false) of the --no-exec-mask flag. * @@ -543,7 +547,9 @@ int wipe_acls(int fd) { * or the parent of @c path is not a directory. * - @c ACL_ERROR - Unexpected library error. */ -int apply_default_acl(const char* path, bool no_exec_mask) { +int apply_default_acl(const char* path, + const struct stat* sp, + bool no_exec_mask) { if (path == NULL) { errno = EINVAL; @@ -595,16 +601,24 @@ int apply_default_acl(const char* path, bool no_exec_mask) { * race condition here, but the window is as small as possible * between when we open the file descriptor (look above) and when we * fstat it. + * + * Note: we only need to call fstat ourselves if we weren't passed a + * valid pointer to a stat structure (nftw does that). */ - struct stat s; - if (fstat(fd, &s) == -1) { - perror("apply_default_acl (fstat)"); - goto cleanup; + if (sp == NULL) { + struct stat s; + if (fstat(fd, &s) == -1) { + perror("apply_default_acl (fstat)"); + goto cleanup; + } + + sp = &s; } - if (!S_ISDIR(s.st_mode)) { + + if (!S_ISDIR(sp->st_mode)) { /* If it's not a directory, make sure it's a regular, non-hard-linked file. */ - if (!S_ISREG(s.st_mode) || s.st_nlink != 1) { + if (!S_ISREG(sp->st_mode) || sp->st_nlink != 1) { result = ACL_FAILURE; goto cleanup; } @@ -619,7 +633,7 @@ int apply_default_acl(const char* path, bool no_exec_mask) { if (!no_exec_mask) { /* Never mask the execute bit on directories. */ - int ace_result = any_can_execute(fd) || S_ISDIR(s.st_mode); + int ace_result = any_can_execute(fd) || S_ISDIR(sp->st_mode); if (ace_result == ACL_ERROR) { perror("apply_default_acl (any_can_execute)"); @@ -791,11 +805,11 @@ void usage(const char* program_name) { * */ int apply_default_acl_nftw(const char *target, - const struct stat *s, + const struct stat *sp, int info, struct FTW *ftw) { - if (apply_default_acl(target, false)) { + if (apply_default_acl(target, sp, false)) { return FTW_CONTINUE; } else { @@ -813,11 +827,11 @@ int apply_default_acl_nftw(const char *target, * */ int apply_default_acl_nftw_x(const char *target, - const struct stat *s, + const struct stat *sp, int info, struct FTW *ftw) { - if (apply_default_acl(target, true)) { + if (apply_default_acl(target, sp, true)) { return FTW_CONTINUE; } else { @@ -854,7 +868,7 @@ int apply_default_acl_nftw_x(const char *target, bool apply_default_acl_recursive(const char *target, bool no_exec_mask) { if (!is_path_directory(target)) { - return apply_default_acl(target, no_exec_mask); + return apply_default_acl(target, NULL, no_exec_mask); } int max_levels = 256; @@ -956,7 +970,7 @@ int main(int argc, char* argv[]) { } else { /* It's either a normal file, or we're not operating recursively. */ - reapp_result = apply_default_acl(target, no_exec_mask); + reapp_result = apply_default_acl(target, NULL, no_exec_mask); } if (!reapp_result) {