Skip to content

Commit

Permalink
Cygwin: Fix and streamline AT_EMPTY_PATH handling
Browse files Browse the repository at this point in the history
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.

Commit fa84aa4 broke this.  It only allows dirfd to be a
directory in calls to these two functions.

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.

Fixes: fa84aa4 ("Cygwin: fix errno values set by readlinkat")
Reported-by: Johannes Schindelin <[email protected]>
Signed-off-by: Corinna Vinschen <[email protected]>
Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
github-cygwin authored and dscho committed Aug 9, 2023
1 parent e928d06 commit 763848a
Showing 1 changed file with 11 additions and 36 deletions.
47 changes: 11 additions & 36 deletions winsup/cygwin/syscalls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4440,7 +4440,7 @@ gen_full_path_at (char *path_ret, int dirfd, const char *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;
Expand All @@ -4456,6 +4456,8 @@ gen_full_path_at (char *path_ret, int dirfd, const char *pathname,
{
if (!*pathname)
{
if (flags & AT_EMPTY_PATH)
return 0;
set_errno (ENOENT);
return -1;
}
Expand Down Expand Up @@ -4577,29 +4579,14 @@ fchownat (int dirfd, const char *pathname, uid_t uid, gid_t gid, int flags)
__leave;
}
char *path = tp.c_get ();
int res = gen_full_path_at (path, dirfd, pathname);
int res = gen_full_path_at (path, dirfd, pathname, flags);
if (res)
__leave;
if (!*pathname) /* Implies AT_EMPTY_PATH */
{
if (!(errno == ENOENT && (flags & AT_EMPTY_PATH)))
__leave;
/* pathname is an empty string. Operate on dirfd. */
if (dirfd == 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 = 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 = AT_SYMLINK_NOFOLLOW;
}
return chown_worker (path, (flags & AT_SYMLINK_NOFOLLOW)
? PC_SYM_NOFOLLOW : PC_SYM_FOLLOW, uid, gid);
Expand All @@ -4622,21 +4609,9 @@ fstatat (int dirfd, const char *__restrict pathname, struct stat *__restrict st,
__leave;
}
char *path = tp.c_get ();
int res = gen_full_path_at (path, dirfd, pathname);
int res = gen_full_path_at (path, dirfd, pathname, flags);
if (res)
{
if (!(errno == ENOENT && (flags & AT_EMPTY_PATH)))
__leave;
/* pathname is an empty string. Operate on dirfd. */
if (dirfd == 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);
Expand Down

0 comments on commit 763848a

Please sign in to comment.