diff mbox series

[hurd,commited] hurd: Factorize at/non-at functions

Message ID 20220917201337.1985767-1-samuel.thibault@ens-lyon.org
State New
Headers show
Series [hurd,commited] hurd: Factorize at/non-at functions | expand

Commit Message

Samuel Thibault Sept. 17, 2022, 8:13 p.m. UTC
Non-at functions can be implemented by just calling the corresponding at
function with AT_FDCWD and zero at_flags.

In the linkat case, the at behavior is different (O_NOLINK), so this introduces
__linkat_common to pass O_NOLINK as appropriate.

lstat functions can also be implemented with fstatat by adding
__fstatat64_common which takes a flags parameter in addition to the at_flags
parameter,

In the end this factorizes chmod, chown, link, lstat64, mkdir, readlink,
rename, stat64, symlink, unlink, utimes.

This also makes __lstat, __lxstat64, __stat and __xstat64 directly use
__fstatat64_common instead of __lstat64 or __stat64.
---
 include/sys/stat.h                 |  2 ++
 include/time.h                     |  1 +
 include/unistd.h                   |  4 +++
 sysdeps/mach/hurd/chmod.c          | 10 +------
 sysdeps/mach/hurd/chown.c          | 10 +------
 sysdeps/mach/hurd/fchmodat.c       |  4 ++-
 sysdeps/mach/hurd/fchownat.c       |  3 ++-
 sysdeps/mach/hurd/fstatat64.c      | 12 +++++++--
 sysdeps/mach/hurd/fstatat_common.h | 23 ++++++++++++++++
 sysdeps/mach/hurd/futimesat.c      |  3 ++-
 sysdeps/mach/hurd/link.c           | 32 +++--------------------
 sysdeps/mach/hurd/linkat.c         | 16 +++++++++---
 sysdeps/mach/hurd/linkat_common.h  | 23 ++++++++++++++++
 sysdeps/mach/hurd/lstat.c          |  6 ++++-
 sysdeps/mach/hurd/lstat64.c        | 14 +++-------
 sysdeps/mach/hurd/lxstat64.c       |  4 ++-
 sysdeps/mach/hurd/mkdir.c          | 14 +---------
 sysdeps/mach/hurd/mkdirat.c        |  4 ++-
 sysdeps/mach/hurd/readlink.c       | 42 +-----------------------------
 sysdeps/mach/hurd/readlinkat.c     |  3 ++-
 sysdeps/mach/hurd/rename.c         | 21 +--------------
 sysdeps/mach/hurd/stat.c           |  5 +++-
 sysdeps/mach/hurd/stat64.c         | 12 +--------
 sysdeps/mach/hurd/symlink.c        | 40 +---------------------------
 sysdeps/mach/hurd/symlinkat.c      |  4 ++-
 sysdeps/mach/hurd/unlink.c         | 15 +----------
 sysdeps/mach/hurd/unlinkat.c       |  4 ++-
 sysdeps/mach/hurd/utimes.c         | 14 +---------
 sysdeps/mach/hurd/xstat64.c        |  4 ++-
 29 files changed, 123 insertions(+), 226 deletions(-)
 create mode 100644 sysdeps/mach/hurd/fstatat_common.h
 create mode 100644 sysdeps/mach/hurd/linkat_common.h
diff mbox series

Patch

diff --git a/include/sys/stat.h b/include/sys/stat.h
index 13e777b3c7..4a29e842de 100644
--- a/include/sys/stat.h
+++ b/include/sys/stat.h
@@ -66,10 +66,12 @@  hidden_proto (__fstatat64_time64);
 extern int __chmod (const char *__file, __mode_t __mode);
 libc_hidden_proto (__chmod)
 extern int __fchmod (int __fd, __mode_t __mode);
+extern int __fchmodat (int __fd, const char *__file, mode_t __mode, int __flag);
 libc_hidden_proto (fchmodat)
 extern __mode_t __umask (__mode_t __mask);
 extern int __mkdir (const char *__path, __mode_t __mode);
 libc_hidden_proto (__mkdir)
+extern int __mkdirat (int __fd, const char *__path, mode_t __mode);
 
 extern int __mknodat (int fd, const char *path, mode_t mode, dev_t dev);
 libc_hidden_proto (__mknodat);
diff --git a/include/time.h b/include/time.h
index a64eff54f5..20abea69d4 100644
--- a/include/time.h
+++ b/include/time.h
@@ -196,6 +196,7 @@  extern int __utimensat64_helper (int fd, const char *file,
                                  const struct __timespec64 tsp[2], int flags);
 libc_hidden_proto (__utimensat64_helper);
 
+extern int __futimesat (int __fd, const char *__file, const struct timeval __tvp[2]);
 #if __TIMESIZE == 64
 # define __futimes64 __futimes
 # define __futimesat64 __futimesat
diff --git a/include/unistd.h b/include/unistd.h
index af795a37c8..4345d08d60 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -78,6 +78,7 @@  extern int __chown (const char *__file,
 libc_hidden_proto (__chown)
 extern int __fchown (int __fd,
 		     __uid_t __owner, __gid_t __group);
+extern int __fchownat (int __fd, const char *__file, uid_t __owner, gid_t __group, int __flag);
 extern int __lchown (const char *__file, __uid_t __owner,
 		     __gid_t __group);
 extern int __chdir (const char *__path) attribute_hidden;
@@ -148,9 +149,12 @@  libc_hidden_proto (_Fork);
 extern int __isatty (int __fd) attribute_hidden;
 extern int __link (const char *__from, const char *__to);
 extern int __symlink (const char *__from, const char *__to);
+extern int __symlinkat (const char *__from, int __fd, const char *__to);
 extern ssize_t __readlink (const char *__path, char *__buf, size_t __len)
      attribute_hidden;
+extern ssize_t __readlinkat (int __fd, const char *__file_name, char *__buf, size_t __len);
 extern int __unlink (const char *__name) attribute_hidden;
+extern int __unlinkat (int __fd, const char *__name, int __flag);
 extern int __gethostname (char *__name, size_t __len) attribute_hidden;
 extern int __revoke (const char *__file);
 extern int __profil (unsigned short int *__sample_buffer, size_t __size,
diff --git a/sysdeps/mach/hurd/chmod.c b/sysdeps/mach/hurd/chmod.c
index 7b79d24990..313483193d 100644
--- a/sysdeps/mach/hurd/chmod.c
+++ b/sysdeps/mach/hurd/chmod.c
@@ -24,15 +24,7 @@ 
 int
 __chmod (const char *file, mode_t mode)
 {
-  error_t err;
-  file_t port = __file_name_lookup (file, 0, 0);
-  if (port == MACH_PORT_NULL)
-    return -1;
-  err = __file_chmod (port, mode);
-  __mach_port_deallocate (__mach_task_self (), port);
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __fchmodat (AT_FDCWD, file, mode, 0);
 }
 
 libc_hidden_def (__chmod)
diff --git a/sysdeps/mach/hurd/chown.c b/sysdeps/mach/hurd/chown.c
index 0c5565bea3..f2915211c5 100644
--- a/sysdeps/mach/hurd/chown.c
+++ b/sysdeps/mach/hurd/chown.c
@@ -24,15 +24,7 @@ 
 int
 __chown (const char *file, uid_t owner, gid_t group)
 {
-  error_t err;
-  file_t port = __file_name_lookup (file, 0, 0);
-  if (port == MACH_PORT_NULL)
-    return -1;
-  err = __file_chown (port, owner, group);
-  __mach_port_deallocate (__mach_task_self (), port);
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __fchownat (AT_FDCWD, file, owner, group, 0);
 }
 libc_hidden_def (__chown)
 weak_alias (__chown, chown)
diff --git a/sysdeps/mach/hurd/fchmodat.c b/sysdeps/mach/hurd/fchmodat.c
index 5bdd5c4f25..44385cc07e 100644
--- a/sysdeps/mach/hurd/fchmodat.c
+++ b/sysdeps/mach/hurd/fchmodat.c
@@ -25,7 +25,7 @@ 
 #include <hurd/fd.h>
 
 int
-fchmodat (int fd, const char *file, mode_t mode, int flag)
+__fchmodat (int fd, const char *file, mode_t mode, int flag)
 {
   error_t err;
   file_t port = __file_name_lookup_at (fd, flag, file, 0, 0);
@@ -37,4 +37,6 @@  fchmodat (int fd, const char *file, mode_t mode, int flag)
     return __hurd_fail (err);
   return 0;
 }
+
+weak_alias (__fchmodat, fchmodat)
 libc_hidden_def (fchmodat)
diff --git a/sysdeps/mach/hurd/fchownat.c b/sysdeps/mach/hurd/fchownat.c
index a34632c63e..580f38f057 100644
--- a/sysdeps/mach/hurd/fchownat.c
+++ b/sysdeps/mach/hurd/fchownat.c
@@ -26,7 +26,7 @@ 
 
 /* Change the owner and group of FILE.  */
 int
-fchownat (int fd, const char *file, uid_t owner, gid_t group, int flag)
+__fchownat (int fd, const char *file, uid_t owner, gid_t group, int flag)
 {
   error_t err;
   file_t port = __file_name_lookup_at (fd, flag, file, 0, 0);
@@ -38,3 +38,4 @@  fchownat (int fd, const char *file, uid_t owner, gid_t group, int flag)
     return __hurd_fail (err);
   return 0;
 }
+weak_alias (__fchownat, fchownat)
diff --git a/sysdeps/mach/hurd/fstatat64.c b/sysdeps/mach/hurd/fstatat64.c
index 56c1291b77..ca60ba43c4 100644
--- a/sysdeps/mach/hurd/fstatat64.c
+++ b/sysdeps/mach/hurd/fstatat64.c
@@ -23,14 +23,16 @@ 
 #include <hurd.h>
 #include <hurd/fd.h>
 
+#include <fstatat_common.h>
+
 /* Get information about the file descriptor FD in BUF.  */
 int
-__fstatat64 (int fd, const char *filename, struct stat64 *buf, int flag)
+__fstatat64_common (int fd, const char *filename, struct stat64 *buf, int at_flags, int flags)
 {
   error_t err;
   io_t port;
 
-  port = __file_name_lookup_at (fd, flag, filename, 0, 0);
+  port = __file_name_lookup_at (fd, at_flags, filename, flags, 0);
   if (port == MACH_PORT_NULL)
     return -1;
 
@@ -39,5 +41,11 @@  __fstatat64 (int fd, const char *filename, struct stat64 *buf, int flag)
 
   return __hurd_fail (err);
 }
+
+int
+__fstatat64 (int fd, const char *filename, struct stat64 *buf, int at_flags)
+{
+  return __fstatat64_common (fd, filename, buf, at_flags, 0);
+}
 libc_hidden_def (__fstatat64)
 weak_alias (__fstatat64, fstatat64)
diff --git a/sysdeps/mach/hurd/fstatat_common.h b/sysdeps/mach/hurd/fstatat_common.h
new file mode 100644
index 0000000000..dd95a70a25
--- /dev/null
+++ b/sysdeps/mach/hurd/fstatat_common.h
@@ -0,0 +1,23 @@ 
+/* Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef	_FSTATAT_COMMON_H
+#define	_FSTATAT_COMMON_H	1
+
+extern int __fstatat64_common (int fd, const char *filename, struct stat64 *buf, int at_flags, int flags);
+
+#endif /* link_common.h */
diff --git a/sysdeps/mach/hurd/futimesat.c b/sysdeps/mach/hurd/futimesat.c
index 9745a3936b..0976f2dfa9 100644
--- a/sysdeps/mach/hurd/futimesat.c
+++ b/sysdeps/mach/hurd/futimesat.c
@@ -27,7 +27,7 @@ 
 /* Change the access time of FILE relative to FD to TVP[0] and
    the modification time of FILE to TVP[1].  */
 int
-futimesat (int fd, const char *file, const struct timeval tvp[2])
+__futimesat (int fd, const char *file, const struct timeval tvp[2])
 {
   error_t err;
   file_t port;
@@ -43,3 +43,4 @@  futimesat (int fd, const char *file, const struct timeval tvp[2])
     return __hurd_fail (err);
   return 0;
 }
+weak_alias (__futimesat, futimesat)
diff --git a/sysdeps/mach/hurd/link.c b/sysdeps/mach/hurd/link.c
index 48856bdce9..2267ae0732 100644
--- a/sysdeps/mach/hurd/link.c
+++ b/sysdeps/mach/hurd/link.c
@@ -20,39 +20,13 @@ 
 #include <unistd.h>
 #include <hurd.h>
 
+#include <linkat_common.h>
+
 /* Make a link to FROM called TO.  */
 int
 __link (const char *from, const char *to)
 {
-  error_t err;
-  file_t oldfile, linknode, todir;
-  char *toname;
-
-  oldfile = __file_name_lookup (from, 0, 0);
-  if (oldfile == MACH_PORT_NULL)
-    return -1;
-
-  /* The file_getlinknode RPC returns the port that should be passed to
-     the receiving filesystem (the one containing TODIR) in dir_link.  */
-
-  err = __file_getlinknode (oldfile, &linknode);
-  __mach_port_deallocate (__mach_task_self (), oldfile);
-  if (err)
-    return __hurd_fail (err);
-
-  todir = __file_name_split (to, &toname);
-  if (todir != MACH_PORT_NULL)
-    {
-      err = __dir_link (todir, linknode, toname, 1);
-      __mach_port_deallocate (__mach_task_self (), todir);
-    }
-  __mach_port_deallocate (__mach_task_self (), linknode);
-  if (todir == MACH_PORT_NULL)
-    return -1;
-
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __linkat_common (AT_FDCWD, from, AT_FDCWD, to, 0, 0);
 }
 
 weak_alias (__link, link)
diff --git a/sysdeps/mach/hurd/linkat.c b/sysdeps/mach/hurd/linkat.c
index 2433614cfb..9ccef6f334 100644
--- a/sysdeps/mach/hurd/linkat.c
+++ b/sysdeps/mach/hurd/linkat.c
@@ -23,18 +23,17 @@ 
 #include <hurd.h>
 #include <hurd/fd.h>
 
+#include <linkat_common.h>
 
 /* Make a link to FROM relative to FROMFD called TO relative to TOFD.  */
 int
-linkat (int fromfd, const char *from, int tofd, const char *to, int flags)
+__linkat_common (int fromfd, const char *from, int tofd, const char *to, int at_flags, int flags)
 {
   error_t err;
   file_t oldfile, linknode, todir;
   char *toname;
 
-  /* POSIX says linkat doesn't follow symlinks by default, so pass
-     O_NOLINK.  That can be overridden by AT_SYMLINK_FOLLOW in FLAGS.  */
-  oldfile = __file_name_lookup_at (fromfd, flags, from, O_NOLINK, 0);
+  oldfile = __file_name_lookup_at (fromfd, at_flags, from, flags, 0);
   if (oldfile == MACH_PORT_NULL)
     return -1;
 
@@ -60,3 +59,12 @@  linkat (int fromfd, const char *from, int tofd, const char *to, int flags)
     return __hurd_fail (err);
   return 0;
 }
+
+int
+__linkat (int fromfd, const char *from, int tofd, const char *to, int at_flags)
+{
+  /* POSIX says linkat doesn't follow symlinks by default, so pass
+     O_NOLINK.  That can be overridden by AT_SYMLINK_FOLLOW in FLAGS.  */
+  return __linkat_common (fromfd, from, tofd, to, at_flags, O_NOLINK);
+}
+weak_alias (__linkat, linkat)
diff --git a/sysdeps/mach/hurd/linkat_common.h b/sysdeps/mach/hurd/linkat_common.h
new file mode 100644
index 0000000000..6c98c57cbe
--- /dev/null
+++ b/sysdeps/mach/hurd/linkat_common.h
@@ -0,0 +1,23 @@ 
+/* Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef	_LINKAT_COMMON_H
+#define	_LINKAT_COMMON_H	1
+
+extern int __linkat_common (int fromfd, const char *from, int tofd, const char *to, int at_flags, int flags);
+
+#endif /* link_common.h */
diff --git a/sysdeps/mach/hurd/lstat.c b/sysdeps/mach/hurd/lstat.c
index 2acfda1934..4fdc312186 100644
--- a/sysdeps/mach/hurd/lstat.c
+++ b/sysdeps/mach/hurd/lstat.c
@@ -18,6 +18,9 @@ 
 #include <errno.h>
 #include <sys/stat.h>
 #include <stddef.h>
+#include <fcntl.h>
+
+#include <fstatat_common.h>
 
 #include "statconv.c"
 
@@ -25,6 +28,7 @@  int
 __lstat (const char *file, struct stat *buf)
 {
   struct stat64 buf64;
-  return __lstat64 (file, &buf64) ?: stat64_conv (buf, &buf64);
+  return __fstatat64_common (AT_FDCWD, file, &buf64, 0, O_NOLINK) ?:
+                             stat64_conv (buf, &buf64);
 }
 weak_alias (__lstat, lstat)
diff --git a/sysdeps/mach/hurd/lstat64.c b/sysdeps/mach/hurd/lstat64.c
index 55ce1552b3..d67daf9f61 100644
--- a/sysdeps/mach/hurd/lstat64.c
+++ b/sysdeps/mach/hurd/lstat64.c
@@ -21,21 +21,13 @@ 
 #include <fcntl.h>
 #include <hurd.h>
 
+#include <fstatat_common.h>
+
 /* Get information about the file descriptor FD in BUF.  */
 int
 __lstat64 (const char *file, struct stat64 *buf)
 {
-  error_t err;
-  file_t port;
-
-  port = __file_name_lookup (file, O_NOLINK, 0);
-  if (port == MACH_PORT_NULL)
-    return -1;
-  err = __io_stat (port, buf);
-  __mach_port_deallocate (__mach_task_self (), port);
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __fstatat64_common (AT_FDCWD, file, buf, 0, O_NOLINK);
 }
 hidden_def (__lstat64)
 weak_alias (__lstat64, lstat64)
diff --git a/sysdeps/mach/hurd/lxstat64.c b/sysdeps/mach/hurd/lxstat64.c
index 450fed0cad..7cd86ab4d7 100644
--- a/sysdeps/mach/hurd/lxstat64.c
+++ b/sysdeps/mach/hurd/lxstat64.c
@@ -22,6 +22,8 @@ 
 #include <hurd.h>
 #include <shlib-compat.h>
 
+#include <fstatat_common.h>
+
 #if LIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_33)
 
 /* Get information about the file descriptor FD in BUF.  */
@@ -31,6 +33,6 @@  __lxstat64 (int vers, const char *file, struct stat64 *buf)
   if (vers != _STAT_VER)
     return __hurd_fail (EINVAL);
 
-  return __lstat64 (file, buf);
+  return __fstatat64_common (AT_FDCWD, file, buf, 0, O_NOLINK);
 }
 #endif
diff --git a/sysdeps/mach/hurd/mkdir.c b/sysdeps/mach/hurd/mkdir.c
index 06a7c16945..9dbb47b77b 100644
--- a/sysdeps/mach/hurd/mkdir.c
+++ b/sysdeps/mach/hurd/mkdir.c
@@ -25,19 +25,7 @@ 
 int
 __mkdir (const char *file_name, mode_t mode)
 {
-  error_t err;
-  const char *name;
-  file_t parent;
-  if (!strcmp (file_name, "/"))
-    return __hurd_fail (EEXIST);
-  parent = __directory_name_split (file_name, (char **) &name);
-  if (parent == MACH_PORT_NULL)
-    return -1;
-  err = __dir_mkdir (parent, name, mode & ~_hurd_umask);
-  __mach_port_deallocate (__mach_task_self (), parent);
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __mkdirat (AT_FDCWD, file_name, mode);
 }
 
 libc_hidden_def (__mkdir)
diff --git a/sysdeps/mach/hurd/mkdirat.c b/sysdeps/mach/hurd/mkdirat.c
index 367b2d0052..b27da52faf 100644
--- a/sysdeps/mach/hurd/mkdirat.c
+++ b/sysdeps/mach/hurd/mkdirat.c
@@ -24,7 +24,7 @@ 
 #include <string.h>
 
 int
-mkdirat (int fd, const char *path, mode_t mode)
+__mkdirat (int fd, const char *path, mode_t mode)
 {
   error_t err;
   const char *name;
@@ -40,3 +40,5 @@  mkdirat (int fd, const char *path, mode_t mode)
     return __hurd_fail (err);
   return 0;
 }
+
+weak_alias (__mkdirat, mkdirat)
diff --git a/sysdeps/mach/hurd/readlink.c b/sysdeps/mach/hurd/readlink.c
index 34f132cb92..ddad3d2919 100644
--- a/sysdeps/mach/hurd/readlink.c
+++ b/sysdeps/mach/hurd/readlink.c
@@ -27,46 +27,6 @@ 
 ssize_t
 __readlink (const char *file_name, char *buf, size_t len)
 {
-  error_t err;
-  file_t file_stat;
-  struct stat64 st;
-
-  file_stat = __file_name_lookup (file_name, O_NOLINK, 0);
-  if (file_stat == MACH_PORT_NULL)
-    return -1;
-
-  err = __io_stat (file_stat, &st);
-  if (! err)
-    if (S_ISLNK (st.st_mode))
-      {
-	enum retry_type doretry;
-	char retryname[1024];
-	file_t file;
-	char *rbuf = buf;
-
-	err = __dir_lookup (file_stat, "", O_READ | O_NOLINK, 0, &doretry, retryname, &file);
-	if (! err && (doretry != FS_RETRY_NORMAL || retryname[0] != '\0'))
-	  err = EGRATUITOUS;
-	if (! err)
-	  {
-	    err = __io_read (file, &rbuf, &len, 0, len);
-	    if (!err && rbuf != buf)
-	      {
-		memcpy (buf, rbuf, len);
-		__vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
-	      }
-
-	    __mach_port_deallocate (__mach_task_self (), file);
-	  }
-      }
-    else
-      err = EINVAL;
-
-  __mach_port_deallocate (__mach_task_self (), file_stat);
-
-  if (err)
-    return __hurd_fail (err);
-  else
-    return len;
+  return __readlinkat (AT_FDCWD, file_name, buf, len);
 }
 weak_alias (__readlink, readlink)
diff --git a/sysdeps/mach/hurd/readlinkat.c b/sysdeps/mach/hurd/readlinkat.c
index 278a0b5b00..b5e7781bd5 100644
--- a/sysdeps/mach/hurd/readlinkat.c
+++ b/sysdeps/mach/hurd/readlinkat.c
@@ -26,7 +26,7 @@ 
    than LEN bytes of BUF.  The contents are not null-terminated.
    Returns the number of characters read, or -1 for errors.  */
 ssize_t
-readlinkat (int fd, const char *file_name, char *buf, size_t len)
+__readlinkat (int fd, const char *file_name, char *buf, size_t len)
 {
   error_t err;
   file_t file_stat;
@@ -67,4 +67,5 @@  readlinkat (int fd, const char *file_name, char *buf, size_t len)
 
   return err ? __hurd_fail (err) : len;
 }
+weak_alias (__readlinkat, readlinkat)
 libc_hidden_def (readlinkat)
diff --git a/sysdeps/mach/hurd/rename.c b/sysdeps/mach/hurd/rename.c
index 75b4e75e93..cf2fa19913 100644
--- a/sysdeps/mach/hurd/rename.c
+++ b/sysdeps/mach/hurd/rename.c
@@ -22,24 +22,5 @@ 
 int
 rename (const char *old, const char *new)
 {
-  error_t err;
-  file_t olddir, newdir;
-  const char *oldname, *newname;
-
-  olddir = __directory_name_split (old, (char **) &oldname);
-  if (olddir == MACH_PORT_NULL)
-    return -1;
-  newdir = __directory_name_split (new, (char **) &newname);
-  if (newdir == MACH_PORT_NULL)
-    {
-       __mach_port_deallocate (__mach_task_self (), olddir);
-      return -1;
-    }
-
-  err = __dir_rename (olddir, oldname, newdir, newname, 0);
-  __mach_port_deallocate (__mach_task_self (), olddir);
-  __mach_port_deallocate (__mach_task_self (), newdir);
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __renameat2 (AT_FDCWD, old, AT_FDCWD, new, 0);
 }
diff --git a/sysdeps/mach/hurd/stat.c b/sysdeps/mach/hurd/stat.c
index 224b11a79c..ffcdf226f0 100644
--- a/sysdeps/mach/hurd/stat.c
+++ b/sysdeps/mach/hurd/stat.c
@@ -16,8 +16,11 @@ 
    <https://www.gnu.org/licenses/>.  */
 
 #include <errno.h>
+#include <fcntl.h>
 #include <sys/stat.h>
 
+#include <fstatat_common.h>
+
 #include "statconv.c"
 
 /* Get file information about FILE in BUF.  */
@@ -25,6 +28,6 @@  int
 __stat (const char *file, struct stat *buf)
 {
   struct stat64 buf64;
-  return __stat64 (file, &buf64) ?: stat64_conv (buf, &buf64);
+  return __fstatat64_common (AT_FDCWD, file, &buf64, 0, 0) ?: stat64_conv (buf, &buf64);
 }
 weak_alias (__stat, stat)
diff --git a/sysdeps/mach/hurd/stat64.c b/sysdeps/mach/hurd/stat64.c
index bb73d5f018..6796783fa5 100644
--- a/sysdeps/mach/hurd/stat64.c
+++ b/sysdeps/mach/hurd/stat64.c
@@ -24,17 +24,7 @@ 
 int
 __stat64 (const char *file, struct stat64 *buf)
 {
-  error_t err;
-  file_t port;
-
-  port = __file_name_lookup (file, 0, 0);
-  if (port == MACH_PORT_NULL)
-    return -1;
-  err = __io_stat (port, buf);
-  __mach_port_deallocate (__mach_task_self (), port);
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __fstatat64 (AT_FDCWD, file, buf, 0);
 }
 hidden_def (__stat64)
 weak_alias (__stat64, stat64)
diff --git a/sysdeps/mach/hurd/symlink.c b/sysdeps/mach/hurd/symlink.c
index bb73ea3529..a987e41048 100644
--- a/sysdeps/mach/hurd/symlink.c
+++ b/sysdeps/mach/hurd/symlink.c
@@ -25,45 +25,7 @@ 
 int
 __symlink (const char *from, const char *to)
 {
-  error_t err;
-  file_t dir, node;
-  char *name;
-  const size_t len = strlen (from) + 1;
-  char buf[sizeof (_HURD_SYMLINK) + len];
-
-  /* A symlink is a file whose translator is "/hurd/symlink\0target\0".  */
-
-  memcpy (buf, _HURD_SYMLINK, sizeof (_HURD_SYMLINK));
-  memcpy (&buf[sizeof (_HURD_SYMLINK)], from, len);
-
-  dir = __file_name_split (to, &name);
-  if (dir == MACH_PORT_NULL)
-    return -1;
-
-  /* Create a new, unlinked node in the target directory.  */
-  err = __dir_mkfile (dir, O_WRITE, 0777 & ~_hurd_umask, &node);
-
-  if (! err)
-    {
-      /* Set the node's translator to make it a symlink.  */
-      err = __file_set_translator (node,
-                                   FS_TRANS_EXCL|FS_TRANS_SET,
-                                   FS_TRANS_EXCL|FS_TRANS_SET, 0,
-                                   buf, sizeof (_HURD_SYMLINK) + len,
-                                   MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
-
-      if (! err)
-        /* Link the node, now a valid symlink, into the target directory.  */
-        err = __dir_link (dir, node, name, 1);
-
-      __mach_port_deallocate (__mach_task_self (), node);
-    }
-
-  __mach_port_deallocate (__mach_task_self (), dir);
-
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __symlinkat (from, AT_FDCWD, to);
 }
 
 weak_alias (__symlink, symlink)
diff --git a/sysdeps/mach/hurd/symlinkat.c b/sysdeps/mach/hurd/symlinkat.c
index c70650948e..91ea7520c2 100644
--- a/sysdeps/mach/hurd/symlinkat.c
+++ b/sysdeps/mach/hurd/symlinkat.c
@@ -28,7 +28,7 @@ 
 
 /* Make a link to FROM called TO relative to FD.  */
 int
-symlinkat (const char *from, int fd, const char *to)
+__symlinkat (const char *from, int fd, const char *to)
 {
   error_t err;
   file_t dir, node;
@@ -70,3 +70,5 @@  symlinkat (const char *from, int fd, const char *to)
     return __hurd_fail (err);
   return 0;
 }
+
+weak_alias (__symlinkat, symlinkat)
diff --git a/sysdeps/mach/hurd/unlink.c b/sysdeps/mach/hurd/unlink.c
index fa6763fca2..fe4761006c 100644
--- a/sysdeps/mach/hurd/unlink.c
+++ b/sysdeps/mach/hurd/unlink.c
@@ -25,20 +25,7 @@ 
 int
 __unlink (const char *name)
 {
-  error_t err;
-  file_t dir;
-  const char *file;
-
-  dir = __directory_name_split (name, (char **) &file);
-  if (dir == MACH_PORT_NULL)
-    return -1;
-
-  err = __dir_unlink (dir, file);
-  __mach_port_deallocate (__mach_task_self (), dir);
-
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __unlinkat (AT_FDCWD, name, 0);
 }
 
 weak_alias (__unlink, unlink)
diff --git a/sysdeps/mach/hurd/unlinkat.c b/sysdeps/mach/hurd/unlinkat.c
index f402e6d5d5..4f3492ea8e 100644
--- a/sysdeps/mach/hurd/unlinkat.c
+++ b/sysdeps/mach/hurd/unlinkat.c
@@ -26,7 +26,7 @@ 
 
 /* Remove the link named NAME.  */
 int
-unlinkat (int fd, const char *name, int flag)
+__unlinkat (int fd, const char *name, int flag)
 {
   error_t err;
   file_t dir;
@@ -49,3 +49,5 @@  unlinkat (int fd, const char *name, int flag)
     return __hurd_fail (err);
   return 0;
 }
+
+weak_alias (__unlinkat, unlinkat)
diff --git a/sysdeps/mach/hurd/utimes.c b/sysdeps/mach/hurd/utimes.c
index c599878887..b0a71bbefc 100644
--- a/sysdeps/mach/hurd/utimes.c
+++ b/sysdeps/mach/hurd/utimes.c
@@ -28,18 +28,6 @@ 
 int
 __utimes (const char *file, const struct timeval tvp[2])
 {
-  error_t err;
-  file_t port;
-
-  port = __file_name_lookup (file, 0, 0);
-  if (port == MACH_PORT_NULL)
-    return -1;
-
-  err = hurd_futimes (port, tvp);
-
-  __mach_port_deallocate (__mach_task_self (), port);
-  if (err)
-    return __hurd_fail (err);
-  return 0;
+  return __futimesat (AT_FDCWD, file, tvp);
 }
 weak_alias (__utimes, utimes)
diff --git a/sysdeps/mach/hurd/xstat64.c b/sysdeps/mach/hurd/xstat64.c
index c30d457f16..5c674d31ff 100644
--- a/sysdeps/mach/hurd/xstat64.c
+++ b/sysdeps/mach/hurd/xstat64.c
@@ -21,6 +21,8 @@ 
 #include <hurd.h>
 #include <shlib-compat.h>
 
+#include <fstatat_common.h>
+
 #if LIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_33)
 
 /* Get information about the file descriptor FD in BUF.  */
@@ -30,7 +32,7 @@  __xstat64 (int vers, const char *file, struct stat64 *buf)
   if (vers != _STAT_VER)
     return __hurd_fail (EINVAL);
 
-  return __stat64 (file, buf);
+  return __fstatat64_common (AT_FDCWD, file, buf, 0, 0);
 }
 
 #endif