- if (stat(pd_name, &pd_stat) < 0) {
- LOG(log_error, logtype_afpd, "stat() of parent dir failed: pd_name = %s, uid = %d: %s",
- pd_name, geteuid(), strerror(errno));
- return 0;
+/*
+ * @brief lstat/fsstatat multiplexer
+ *
+ * lstatat mulitplexes lstat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
+ *
+ * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
+ * @param path (r) pathname
+ * @param st (rw) pointer to struct stat
+ */
+int lstatat(int dirfd, const char *path, struct stat *st)
+{
+#ifdef HAVE_RENAMEAT
+ if (dirfd == -1)
+ dirfd = AT_FDCWD;
+ return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
+#else
+ return (lstat(path, st));
+#endif
+
+ /* DEADC0DE */
+ return -1;
+}
+
+/*
+ * @brief opendir wrapper for *at semantics support
+ *
+ * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
+ *
+ * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
+ * @param path (r) pathname
+ */
+DIR *opendirat(int dirfd, const char *path)
+{
+ DIR *ret;
+ int cwd = -1;
+
+ if (dirfd != -1) {
+ if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
+ ret = NULL;
+ goto exit;
+ }