X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Fapply-default-acl.c;h=c4d0c3b9b4871094edf9f08a5053c0bf083cf653;hb=df929bebddb9852c8e50481169e2fd1b5a01faf6;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..c4d0c3b 100644 --- a/src/apply-default-acl.c +++ b/src/apply-default-acl.c @@ -464,7 +464,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 +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. * @@ -542,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; @@ -594,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; } @@ -618,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)"); @@ -790,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 { @@ -812,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 { @@ -853,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; @@ -955,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) {