From: Michael Orlitzky Date: Fri, 2 Mar 2018 01:57:50 +0000 (-0500) Subject: src/libadacl.c: minor code simplification in safe_open(). X-Git-Tag: v0.2.0~5 X-Git-Url: http://gitweb.michael.orlitzky.com/?p=apply-default-acl.git;a=commitdiff_plain;h=ee4b602d26225bb5a548eaeb7bd570adaa48721a src/libadacl.c: minor code simplification in safe_open(). --- diff --git a/src/libadacl.c b/src/libadacl.c index 1b31938..36812e8 100644 --- a/src/libadacl.c +++ b/src/libadacl.c @@ -170,25 +170,24 @@ int safe_open(const char* pathname, int flags) { return OPEN_ERROR; } - int fd = 0; - if (strcmp(abspath, "/") == 0) { - fd = open("/", flags | O_DIRECTORY); - } - else { + bool abspath_is_root = (strcmp(abspath, "/") == 0); + int rootflags = flags | O_DIRECTORY; + if (!abspath_is_root) { /* Use O_PATH for some added safety if "/" is not our target */ - fd = open("/", flags | O_DIRECTORY | O_PATH); + rootflags |= O_PATH; } - if (fd == OPEN_ERROR) { + int rootfd = open("/", rootflags); + if (rootfd == OPEN_ERROR) { perror("safe_open (open)"); return OPEN_ERROR; } - if (strcmp(abspath, "/") == 0) { - return fd; + if (abspath_is_root) { + return rootfd; } - int result = safe_open_ex(fd, abspath+1, flags); - if (close(fd) == CLOSE_ERROR) { + int result = safe_open_ex(rootfd, abspath+1, flags); + if (close(rootfd) == CLOSE_ERROR) { perror("safe_open (close)"); return OPEN_ERROR; }