From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id D867D385AFAF; Wed, 26 Jul 2023 13:18:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D867D385AFAF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1690377536; bh=5K+c8YMU/3hS0xuR+UCuXkosZAYOzWTPVN8M+6mRwWE=; h=From:To:Subject:Date:From; b=VZb7sPs1n9ERlAHe95/4o1lSyxNgRj7Yq/jwUMc1IEsIu7VwWPDnjH7BDdo6IFeKo AhobQAcFpAtCwE6zYVRC1RDCWuOuNNydx9JyQrZXQZrT+yEJj8DKS0IfvnQiHk+q/C Qkw9nQ4/C+tgLWaFUDmDCYUnVpNXGFxwE/O9J0K8= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: Fix and streamline AT_EMPTY_PATH handling X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/main X-Git-Oldrev: 00510919993da49cf91421fe13242a7d8a82c1fd X-Git-Newrev: f48ce81122d4be42c3679dade5724d734091129e Message-Id: <20230726131856.D867D385AFAF@sourceware.org> Date: Wed, 26 Jul 2023 13:18:56 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Df48ce81122d= 4be42c3679dade5724d734091129e commit f48ce81122d4be42c3679dade5724d734091129e Author: Corinna Vinschen AuthorDate: Wed Jul 12 13:47:49 2023 +0200 Commit: Corinna Vinschen CommitDate: Wed Jul 26 15:14:33 2023 +0200 Cygwin: Fix and streamline AT_EMPTY_PATH handling =20 The GLIBC extension AT_EMPTY_PATH allows the functions fchownat and fstatat to operate on dirfd alone, if the given pathname is an empty string. This also allows to operate on any file type, not only directories. =20 Commit fa84aa4dd2fb4 broke this. It only allows dirfd to be a directory in calls to these two functions. =20 Fix that by handling AT_EMPTY_PATH right in gen_full_path_at. A valid dirfd and an empty pathname is now a valid combination and, noticably, this returns a valid path in path_ret. That in turn allows to remove the additional path generation code from the callers. =20 Fixes: fa84aa4dd2fb ("Cygwin: fix errno values set by readlinkat") Reported-by: Johannes Schindelin Tested-by: Johannes Schindelin Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/syscalls.cc | 47 +++++++++++--------------------------------= ---- 1 file changed, 11 insertions(+), 36 deletions(-) diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index 9c7ad61febac..04907b991c48 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -4441,7 +4441,7 @@ gen_full_path_at (char *path_ret, int dirfd, const ch= ar *pathname, cygheap_fdget cfd (dirfd); if (cfd < 0) return -1; - if (!cfd->pc.isdir ()) + if (!cfd->pc.isdir () && !(flags & AT_EMPTY_PATH)) { set_errno (ENOTDIR); return -1; @@ -4452,6 +4452,8 @@ gen_full_path_at (char *path_ret, int dirfd, const ch= ar *pathname, { if (!*pathname) { + if (flags & AT_EMPTY_PATH) + return 0; set_errno (ENOENT); return -1; } @@ -4573,29 +4575,14 @@ fchownat (int dirfd, const char *pathname, uid_t ui= d, gid_t gid, int flags) __leave; } char *path =3D tp.c_get (); - int res =3D gen_full_path_at (path, dirfd, pathname); + int res =3D gen_full_path_at (path, dirfd, pathname, flags); if (res) + __leave; + if (!*pathname) /* Implies AT_EMPTY_PATH */ { - if (!(errno =3D=3D ENOENT && (flags & AT_EMPTY_PATH))) - __leave; - /* pathname is an empty string. Operate on dirfd. */ - if (dirfd =3D=3D AT_FDCWD) - { - cwdstuff::acquire_read (); - strcpy (path, cygheap->cwd.get_posix ()); - cwdstuff::release_read (); - } - else - { - cygheap_fdget cfd (dirfd); - if (cfd < 0) - __leave; - strcpy (path, cfd->get_name ()); - /* If dirfd refers to a symlink (which was necessarily - opened with O_PATH | O_NOFOLLOW), we must operate - directly on that symlink.. */ - flags =3D AT_SYMLINK_NOFOLLOW; - } + /* If dirfd refers to a symlink (which was necessarily opened with + O_PATH | O_NOFOLLOW), we must operate directly on that symlink. */ + flags =3D AT_SYMLINK_NOFOLLOW; } return chown_worker (path, (flags & AT_SYMLINK_NOFOLLOW) ? PC_SYM_NOFOLLOW : PC_SYM_FOLLOW, uid, gid); @@ -4618,21 +4605,9 @@ fstatat (int dirfd, const char *__restrict pathname,= struct stat *__restrict st, __leave; } char *path =3D tp.c_get (); - int res =3D gen_full_path_at (path, dirfd, pathname); + int res =3D gen_full_path_at (path, dirfd, pathname, flags); if (res) - { - if (!(errno =3D=3D ENOENT && (flags & AT_EMPTY_PATH))) - __leave; - /* pathname is an empty string. Operate on dirfd. */ - if (dirfd =3D=3D AT_FDCWD) - { - cwdstuff::acquire_read (); - strcpy (path, cygheap->cwd.get_posix ()); - cwdstuff::release_read (); - } - else - return fstat (dirfd, st); - } + __leave; path_conv pc (path, ((flags & AT_SYMLINK_NOFOLLOW) ? PC_SYM_NOFOLLOW : PC_SYM_FOLLOW) | PC_POSIX | PC_KEEP_HANDLE, stat_suffixes);