Skip to content

Commit b4ec406

Browse files
alericksonanamnavi
authored andcommitted
Update unix local user paths (#1464)
1 parent 4a52b31 commit b4ec406

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/code/Utils.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.PowerShell.Commands;
1717
using Microsoft.PowerShell.PSResourceGet.Cmdlets;
1818
using System.Net.Http;
19+
using System.Globalization;
1920

2021
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
2122
{
@@ -83,6 +84,12 @@ function ConvertToHash
8384

8485
#endregion
8586

87+
#region Path fields
88+
89+
private static string s_tempHome = null;
90+
91+
#endregion
92+
8693
#region String methods
8794

8895
public static string TrimQuotes(string name)
@@ -976,23 +983,53 @@ public static List<string> GetAllInstallationPaths(
976983
return installationPaths;
977984
}
978985

986+
private static string GetHomeOrCreateTempHome()
987+
{
988+
const string tempHomeFolderName = "psresourceget-{0}-98288ff9-5712-4a14-9a11-23693b9cd91a";
989+
990+
string envHome = Environment.GetEnvironmentVariable("HOME") ?? s_tempHome;
991+
if (envHome is not null)
992+
{
993+
return envHome;
994+
}
995+
996+
try
997+
{
998+
var s_tempHome = Path.Combine(Path.GetTempPath(), string.Format(CultureInfo.CurrentCulture, tempHomeFolderName, Environment.UserName));
999+
Directory.CreateDirectory(s_tempHome);
1000+
}
1001+
catch (UnauthorizedAccessException)
1002+
{
1003+
// Directory creation may fail if the account doesn't have filesystem permission such as some service accounts.
1004+
// Return an empty string in this case so the process working directory will be used.
1005+
s_tempHome = string.Empty;
1006+
}
1007+
1008+
return s_tempHome;
1009+
}
1010+
9791011
private readonly static Version PSVersion6 = new Version(6, 0);
9801012
private static void GetStandardPlatformPaths(
9811013
PSCmdlet psCmdlet,
982-
out string myDocumentsPath,
983-
out string programFilesPath)
1014+
out string localUserDir,
1015+
out string allUsersDir)
9841016
{
9851017
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
9861018
{
9871019
string powerShellType = (psCmdlet.Host.Version >= PSVersion6) ? "PowerShell" : "WindowsPowerShell";
988-
myDocumentsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), powerShellType);
989-
programFilesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), powerShellType);
1020+
localUserDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), powerShellType);
1021+
allUsersDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), powerShellType);
9901022
}
9911023
else
9921024
{
9931025
// paths are the same for both Linux and macOS
994-
myDocumentsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "powershell");
995-
programFilesPath = System.IO.Path.Combine("/usr", "local", "share", "powershell");
1026+
localUserDir = Path.Combine(GetHomeOrCreateTempHome(), ".local", "share", "powershell");
1027+
// Create the default data directory if it doesn't exist.
1028+
if (!Directory.Exists(localUserDir)) {
1029+
Directory.CreateDirectory(localUserDir);
1030+
}
1031+
1032+
allUsersDir = System.IO.Path.Combine("/usr", "local", "share", "powershell");
9961033
}
9971034
}
9981035

0 commit comments

Comments
 (0)