X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Fapply-default-acl.c;h=0030431f481f5ec6acf8790ac923de4178a064eb;hb=354d04860687e5d3dd42550ce3ef634b067bc2f0;hp=d2f8f536b530fa5b63d8fe605c75ab1ac1f737af;hpb=1be99786d05f3a7a75a17badae7b42491442a17a;p=apply-default-acl.git diff --git a/src/apply-default-acl.c b/src/apply-default-acl.c index d2f8f53..0030431 100644 --- a/src/apply-default-acl.c +++ b/src/apply-default-acl.c @@ -351,12 +351,15 @@ int acl_execute_masked(acl_t acl) { * @param fd * The file descriptor to check. * + * @param sp + * A pointer to a stat structure for @c fd. + * * @return * - @c ACL_SUCCESS - Someone has effective execute permissions on @c fd. * - @c ACL_FAILURE - Nobody can execute @c fd. * - @c ACL_ERROR - Unexpected library error. */ -int any_can_execute(int fd) { +int any_can_execute(int fd, const struct stat* sp) { acl_t acl = acl_get_fd(fd); if (acl == (acl_t)NULL) { @@ -368,13 +371,7 @@ int any_can_execute(int fd) { int result = ACL_FAILURE; if (acl_is_minimal(acl)) { - struct stat s; - if (fstat(fd, &s) == -1) { - perror("any_can_execute (fstat)"); - result = ACL_ERROR; - goto cleanup; - } - if (s.st_mode & (S_IXUSR | S_IXOTH | S_IXGRP)) { + if (sp->st_mode & (S_IXUSR | S_IXOTH | S_IXGRP)) { result = ACL_SUCCESS; goto cleanup; } @@ -464,7 +461,8 @@ int any_can_execute(int fd) { int assign_default_acl(const char* path, acl_t acl) { if (path == NULL) { - errno = ENOENT; + errno = EINVAL; + perror("assign_default_acl (args)"); return ACL_ERROR; } @@ -533,6 +531,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. * @@ -542,7 +544,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; @@ -594,16 +598,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; } @@ -618,7 +630,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,sp) || S_ISDIR(sp->st_mode); if (ace_result == ACL_ERROR) { perror("apply_default_acl (any_can_execute)"); @@ -790,11 +802,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 { @@ -812,11 +824,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 { @@ -853,7 +865,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; @@ -955,7 +967,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) {