+
+
+/**************************************************************************
+ * *at semnatics support functions (like openat, renameat standard funcs)
+ **************************************************************************/
+
+/*
+ * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
+ */
+int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
+{
+ int ret = 0;
+ int sfd = -1;
+ int dfd = -1;
+ ssize_t cc;
+ size_t buflen;
+ char filebuf[8192];
+
+#ifdef HAVE_ATFUNCS
+ if (dirfd == -1)
+ dirfd = AT_FDCWD;
+ sfd = openat(dirfd, src, O_RDONLY);
+#else
+ sfd = open(src, O_RDONLY);
+#endif
+ if (sfd < 0) {
+ LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
+ src, dst, src, strerror(errno));
+ return -1;
+ }
+
+ if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
+ LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
+ src, dst, dst, strerror(errno));
+ ret = -1;
+ goto exit;
+ }
+
+ while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
+ if (cc < 0) {
+ if (errno == EINTR)
+ continue;
+ LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
+ src, dst, src, strerror(errno));
+ ret = -1;
+ goto exit;
+ }
+
+ buflen = cc;
+ while (buflen > 0) {
+ if ((cc = write(dfd, filebuf, buflen)) < 0) {
+ if (errno == EINTR)
+ continue;
+ LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
+ src, dst, dst, strerror(errno));
+ ret = -1;
+ goto exit;
+ }
+ buflen -= cc;
+ }
+ }
+
+exit:
+ if (sfd != -1)
+ close(sfd);
+
+ if (dfd != -1) {
+ int err;
+
+ err = close(dfd);
+ if (!ret && err) {
+ /* don't bother to report an error if there's already one */
+ LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
+ src, dst, dst, strerror(errno));
+ ret = -1;
+ }
+ }
+
+ return ret;
+}
+
+/*
+ * at wrapper for netatalk_unlink
+ */
+int netatalk_unlinkat(int dirfd, const char *name)
+{
+#ifdef HAVE_ATFUNCS
+ if (dirfd == -1)
+ dirfd = AT_FDCWD;
+
+ if (unlinkat(dirfd, name, 0) < 0) {
+ switch (errno) {
+ case ENOENT :
+ break;
+ case EROFS:
+ return AFPERR_VLOCK;
+ case EPERM:
+ case EACCES :
+ return AFPERR_ACCESS;
+ default :
+ return AFPERR_PARAM;
+ }
+ }
+ return AFP_OK;
+#else
+ return netatalk_unlink(name);
+#endif
+
+ /* DEADC0DE */
+ return 0;
+}
+
+/*
+ * @brief This is equivalent of unix rename()
+ *
+ * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
+ * are ignored.
+ *
+ * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
+ * @param oldpath (r) guess what
+ * @param dfd (r) same as sfd
+ * @param newpath (r) guess what
+ */
+int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
+{
+#ifdef HAVE_ATFUNCS
+ if (sfd == -1)
+ sfd = AT_FDCWD;
+ if (dfd == -1)
+ dfd = AT_FDCWD;
+
+ if (renameat(sfd, oldpath, dfd, newpath) < 0)
+ return -1;
+#else
+ if (rename(oldpath, newpath) < 0)
+ return -1;
+#endif /* HAVE_ATFUNCS */
+
+ return 0;
+}
+
+/*
+ * @brief stat/fsstatat multiplexer
+ *
+ * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
+ *
+ * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
+ * @param path (r) pathname
+ * @param st (rw) pointer to struct stat
+ */
+int statat(int dirfd, const char *path, struct stat *st)
+{
+#ifdef HAVE_ATFUNCS
+ if (dirfd == -1)
+ dirfd = AT_FDCWD;
+ return (fstatat(dirfd, path, st, 0));
+#else
+ return (stat(path, st));
+#endif
+
+ /* DEADC0DE */
+ return -1;
+}
+
+/*
+ * @brief lstat/fsstatat multiplexer
+ *
+ * lstatat mulitplexes lstat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
+ *
+ * @param dirfd (r) Only used if HAVE_ATFUNCS, 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_ATFUNCS
+ 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;
+ }
+ }
+
+ ret = opendir(path);
+
+ if (dirfd != -1 && fchdir(cwd) != 0) {
+ LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");
+ exit(EXITERR_SYS);
+ }
+
+exit:
+ if (cwd != -1)
+ close(cwd);
+
+ return ret;
+}