# HG changeset patch # User Markus Mützel # Date 1582388794 -3600 # Node ID baf16e6f498b866d1d62feeb1dd838cf5191bd45 # Parent 4cfe24f563368e7ed9a2f3ba92f2c66aab3de14f pkg.m: Install packages globally if process has elevated rights (bug #44548). * pkg.m: On Windows, default to install packages globally if process is running with elevated rights. Otherwise, install packages locally. * sysdep.cc (F__is_elevated_process__): New function that checks if Octave is running as a Windows process with elevated rights. diff -r 4cfe24f56336 -r baf16e6f498b libinterp/corefcn/sysdep.cc --- a/libinterp/corefcn/sysdep.cc Sat Feb 22 17:03:01 2020 +0100 +++ b/libinterp/corefcn/sysdep.cc Sat Feb 22 17:26:34 2020 +0100 @@ -245,6 +245,44 @@ #endif } +DEFUN (__is_elevated_process__, args, , + doc: /* -*- texinfo -*- +@deftypefn {} {@var{retval} =} __is_elevated_process__ () +Check if current process has elevated rights. + +On Windows, return true if the current process has elevated right. Otherwise, +return false. +On non-Windows platforms, this function fails with an error. +@end deftypefn */) +{ +#if defined (OCTAVE_USE_WINDOWS_API) + if (args.length () != 0) + print_usage (); + + bool retval = false; + HANDLE h_token = nullptr; + + if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h_token)) + { + TOKEN_ELEVATION elevation; + DWORD return_length = sizeof (TOKEN_ELEVATION); + if (GetTokenInformation (h_token, TokenElevation, &elevation, + sizeof (elevation), &return_length)) + retval = elevation.TokenIsElevated; + } + + if (h_token) + CloseHandle (h_token); + + return ovl (retval); + +#else + octave_unused_parameter (args); + error ("__is_elevated_process__: " + "Function is only supported on Windows platforms."); +#endif +} + namespace octave { #if defined (__MINGW32__) diff -r 4cfe24f56336 -r baf16e6f498b scripts/pkg/pkg.m --- a/scripts/pkg/pkg.m Sat Feb 22 17:03:01 2020 +0100 +++ b/scripts/pkg/pkg.m Sat Feb 22 17:26:34 2020 +0100 @@ -329,7 +329,7 @@ function [local_packages, global_packages] = pkg (varargin) - ## Installation prefix (FIXME: what should these be on windows?) + ## Installation prefix persistent user_prefix = false; persistent prefix = false; persistent archprefix = -1; @@ -337,9 +337,13 @@ persistent global_list = fullfile (OCTAVE_HOME (), "share", "octave", "octave_packages"); - ## If user is superuser set global_istall to true - ## FIXME: is it OK to set this always true on windows? - global_install = ((ispc () && ! isunix ()) || (geteuid () == 0)); + ## If user is superuser (posix) or the process has elevated rights (Windows), + ## set global_install to true. + if (ispc () && ! isunix ()) + global_install = __is_elevated_process__ (); + else + global_install = (geteuid () == 0); + endif if (isbool (prefix)) [prefix, archprefix] = default_prefix (global_install);