hurd: Factorize at/non-at functions

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.
This commit is contained in:
Samuel Thibault 2022-09-17 19:47:57 +00:00
parent 6841aed6c4
commit 7ae60af75b
29 changed files with 123 additions and 226 deletions

View File

@ -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);

View File

@ -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

View File

@ -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,

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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 */

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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 */

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

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

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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