public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Ken Brown <kbrown@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: rmdir: fail if last component is a symlink, as on Linux
Date: Wed, 25 Sep 2019 00:05:00 -0000	[thread overview]
Message-ID: <20190925000540.71446.qmail@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=d1b5feef8232e82a0f31e5ed421abda84c3f4705

commit d1b5feef8232e82a0f31e5ed421abda84c3f4705
Author: Ken Brown <kbrown@cornell.edu>
Date:   Sun Sep 22 11:33:34 2019 -0400

    Cygwin: rmdir: fail if last component is a symlink, as on Linux
    
    If the last component of the directory name is a symlink followed by a
    slash, rmdir now fails, following Linux but not POSIX, even if the
    symlink resolves to an existing empty directory.
    
    mkdir was similarly changed in 2009 in commit
    52dba6a5c45e8d8ba1e237a15213311dc11d91fb.  Modify a comment to clarify
    the purpose of that commit.
    
    Addresses https://cygwin.com/ml/cygwin/2019-09/msg00221.html.

Diff:
---
 winsup/cygwin/dir.cc | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/dir.cc b/winsup/cygwin/dir.cc
index b757851..29a9dfa 100644
--- a/winsup/cygwin/dir.cc
+++ b/winsup/cygwin/dir.cc
@@ -305,15 +305,15 @@ mkdir (const char *dir, mode_t mode)
 
   __try
     {
-      /* POSIX says mkdir("symlink-to-missing/") should create the
-	 directory "missing", but Linux rejects it with EEXIST.  Copy
-	 Linux behavior for now.  */
-
       if (!*dir)
 	{
 	  set_errno (ENOENT);
 	  __leave;
 	}
+      /* Following Linux, and intentionally ignoring POSIX, do not
+	 resolve the last component of DIR if it is a symlink, even if
+	 DIR has a trailing slash.  Achieve this by stripping trailing
+	 slashes or backslashes.  */
       if (isdirsep (dir[strlen (dir) - 1]))
 	{
 	  /* This converts // to /, but since both give EEXIST, we're okay.  */
@@ -351,9 +351,30 @@ rmdir (const char *dir)
 {
   int res = -1;
   fhandler_base *fh = NULL;
+  tmp_pathbuf tp;
 
   __try
     {
+      if (!*dir)
+	{
+	  set_errno (ENOENT);
+	  __leave;
+	}
+
+      /* Following Linux, and intentionally ignoring POSIX, do not
+	 resolve the last component of DIR if it is a symlink, even if
+	 DIR has a trailing slash.  Achieve this by stripping trailing
+	 slashes or backslashes.  */
+      if (isdirsep (dir[strlen (dir) - 1]))
+	{
+	  /* This converts // to /, but since both give ENOTEMPTY,
+	     we're okay.  */
+	  char *buf;
+	  char *p = stpcpy (buf = tp.c_get (), dir) - 1;
+	  dir = buf;
+	  while (p > dir && isdirsep (*p))
+	    *p-- = '\0';
+	}
       if (!(fh = build_fh_name (dir, PC_SYM_NOFOLLOW)))
 	__leave;   /* errno already set */;


                 reply	other threads:[~2019-09-25  0:05 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190925000540.71446.qmail@sourceware.org \
    --to=kbrown@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).