unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Kiên Nguyễn Quang" <kien.n.quang@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 57386@debbugs.gnu.org
Subject: bug#57386: 29.0.50; support make-symbolic-link without admin priviledge on Windows
Date: Thu, 25 Aug 2022 03:05:37 +0900	[thread overview]
Message-ID: <CAL7sU5jA0ssv8PjsBXnGVm7Gd8n6S-DeqBGYfWcbL8TOQmxi8Q@mail.gmail.com> (raw)
In-Reply-To: <838rnd638m.fsf@gnu.org>


[-- Attachment #1.1: Type: text/plain, Size: 1003 bytes --]

You're right, there's a stupid check against parameters on the version that
doesn't support the new flag.
Since from Windows 10, the Windows version checking is not recommended (and
not accurate) anymore, I think we can just retry if the function is called
with invalid parameters.
That will probably slow down the down-level Windows versions, but the
difference should not be too much.

On Thu, Aug 25, 2022 at 1:00 AM Eli Zaretskii <eliz@gnu.org> wrote:

> > From: Kiên Nguyễn Quang
> >  <kien.n.quang@gmail.com>
> > Date: Thu, 25 Aug 2022 00:33:58 +0900
> >
> > The CreateSymbolicLink Win32API has a flag that allows creating
> sym-links without admin privileges
> > SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE.
> > This patch added that support for w32 Emacs.
>
> Thanks, but we cannot use that flag unconditionally, because it is not
> supported on all Windows versions.  The addition of the flags should
> be conditioned on the version of Windows on which Emacs runs.
>

[-- Attachment #1.2: Type: text/html, Size: 1460 bytes --]

[-- Attachment #2: 0001-symlink-allow-w32-user-to-create-symlink-without-adm.patch --]
[-- Type: application/octet-stream, Size: 2088 bytes --]

From 3ade8436b9c4a0c8d71a252068abf370ebe848ba Mon Sep 17 00:00:00 2001
From: Kien Nguyen <kien.n.quang@gmail.com>
Date: Wed, 24 Aug 2022 23:19:35 +0900
Subject: [PATCH] symlink: allow w32 non-admin user to create symlink

* src/w32.c (symlink): allow w32 user to create symlink without admin privileged

---
 src/w32.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/w32.c b/src/w32.c
index cbcfcdd4f..407befbda 100644
--- a/src/w32.c
+++ b/src/w32.c
@@ -5992,12 +5992,15 @@ sys_umask (int mode)
 #ifndef SYMBOLIC_LINK_FLAG_DIRECTORY
 #define SYMBOLIC_LINK_FLAG_DIRECTORY 0x1
 #endif
+#ifndef SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
+#define SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE 0x2
+#endif
 
 int
 symlink (char const *filename, char const *linkname)
 {
   char linkfn[MAX_UTF8_PATH], *tgtfn;
-  DWORD flags = 0;
+  DWORD flags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
   int dir_access, filename_ends_in_slash;
 
   /* Diagnostics follows Posix as much as possible.  */
@@ -6055,7 +6058,7 @@ symlink (char const *filename, char const *linkname)
      directory.  */
   filename_ends_in_slash = IS_DIRECTORY_SEP (filename[strlen (filename) - 1]);
   if (dir_access == 0 || filename_ends_in_slash)
-    flags = SYMBOLIC_LINK_FLAG_DIRECTORY;
+    flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
 
   tgtfn = (char *)map_w32_filename (filename, NULL);
   if (filename_ends_in_slash)
@@ -6069,6 +6072,16 @@ symlink (char const *filename, char const *linkname)
       if (errno != ENOSYS)
 	{
 	  DWORD w32err = GetLastError ();
+	  /* Remove the flag and try again if the API is not supported */
+	  if (w32err == ERROR_INVALID_PARAMETER)
+	    {
+	      errno = 0;
+	      flags &= ~SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
+	      if (create_symbolic_link (linkfn, tgtfn, flags))
+		goto success;
+
+	      w32err = GetLastError ();
+	    }
 
 	  switch (w32err)
 	    {
@@ -6104,6 +6117,8 @@ symlink (char const *filename, char const *linkname)
 	}
       return -1;
     }
+
+ success:
   return 0;
 }
 
-- 
2.37.0.windows.1


  reply	other threads:[~2022-08-24 18:05 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-24 15:33 bug#57386: 29.0.50; support make-symbolic-link without admin priviledge on Windows Kiên Nguyễn Quang
2022-08-24 16:00 ` Eli Zaretskii
2022-08-24 18:05   ` Kiên Nguyễn Quang [this message]
2022-08-24 18:26     ` Eli Zaretskii
2022-08-25  1:30       ` Kiên Nguyễn Quang
2022-08-25  5:50         ` Eli Zaretskii
2022-08-25  8:52           ` Kiên Nguyễn Quang
2022-08-25  9:09             ` Eli Zaretskii
2022-08-25  9:51               ` Kiên Nguyễn Quang
2022-08-25  9:57                 ` Eli Zaretskii
2022-08-25 10:32                   ` Kiên Nguyễn Quang
2022-08-25 13:22                     ` Eli Zaretskii
2022-08-25 11:35                 ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-08-25 12:59                   ` Eli Zaretskii

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=CAL7sU5jA0ssv8PjsBXnGVm7Gd8n6S-DeqBGYfWcbL8TOQmxi8Q@mail.gmail.com \
    --to=kien.n.quang@gmail.com \
    --cc=57386@debbugs.gnu.org \
    --cc=eliz@gnu.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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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