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;
}