macro: introduce new TAKE_FD() macro

This is similar to TAKE_PTR() but operates on file descriptors, and thus
assigns -1 to the fd parameter after returning it.

Removes 60 lines from our codebase. Pretty good too I think.
This commit is contained in:
Lennart Poettering
2018-03-22 20:30:40 +01:00
parent 2f4cefe6ce
commit c10d6bdb89
25 changed files with 64 additions and 130 deletions
+3 -10
View File
@@ -730,8 +730,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
}
safe_close(fd);
fd = fd_parent;
fd_parent = -1;
fd = TAKE_FD(fd_parent);
continue;
}
@@ -849,8 +848,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
/* And iterate again, but go one directory further down. */
safe_close(fd);
fd = child;
child = -1;
fd = TAKE_FD(child);
}
if (!done) {
@@ -864,16 +862,11 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
*ret = TAKE_PTR(done);
if (flags & CHASE_OPEN) {
int q;
/* Return the O_PATH fd we currently are looking to the caller. It can translate it to a proper fd by
* opening /proc/self/fd/xyz. */
assert(fd >= 0);
q = fd;
fd = -1;
return q;
return TAKE_FD(fd);
}
return exists;