Skip to content

Commit

Permalink
(NuGet#9) Switch to Chocolatey specific paths for environment folders
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCakeIsNaOH committed Jan 9, 2023
1 parent 6e0c01c commit 5769cd9
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/NuGet.Core/NuGet.Common/PathUtil/NuGetEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,79 @@ public static class NuGetEnvironment

public static string GetFolderPath(NuGetFolderPath folder)
{
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CHOCOLATEY_VERSION")))
{
#pragma warning disable RS0030 // Do not used banned APIs
// This is the only place in the product code we can use GetTempPath().
// Because $env:temp is set to the config cacheLocation by Chocolatey, this is inside the Chocolatey cacheLocation when run by Chocolatey
var tempPath = Path.GetTempPath();
#pragma warning restore RS0030 // Do not used banned APIs

var invalidFolder = Path.Combine(tempPath, "chocolatey-invalid");
switch (folder)
{
case NuGetFolderPath.MachineWideSettingsBaseDirectory:
return invalidFolder;

case NuGetFolderPath.MachineWideConfigDirectory:
return Path.Combine(invalidFolder, "config");

case NuGetFolderPath.UserSettingsDirectory:
return Path.Combine(invalidFolder, "user-settings");

case NuGetFolderPath.HttpCacheDirectory:
return Path.Combine(invalidFolder, "http-cache");

case NuGetFolderPath.NuGetHome:
return Path.Combine(invalidFolder, "home");

case NuGetFolderPath.DefaultMsBuildPath:
return Path.Combine(invalidFolder, "msbuild");

case NuGetFolderPath.Temp:
{
var nuGetScratch = Path.Combine(tempPath, "ChocolateyScratch");

// On Windows and Mac the temp directories are per-user, but on Linux it's /tmp for everyone
if (RuntimeEnvironmentHelper.IsLinux)
{
// ConcurrencyUtility uses the lock subdirectory, so make sure it exists, and create with world write
string lockPath = Path.Combine(nuGetScratch, "lock");
if (!Directory.Exists(lockPath))
{
void CreateSharedDirectory(string path)
{
Directory.CreateDirectory(path);
if (chmod(path, 0x1ff) == -1) // 0x1ff == 777 permissions
{
// it's very unlikely we can't set the permissions of a directory we just created
var errno = Marshal.GetLastWin32Error(); // fetch the errno before running any other operation
throw new InvalidOperationException($"Unable to set permission while creating {path}, errno={errno}.");
}
}

CreateSharedDirectory(nuGetScratch);
CreateSharedDirectory(lockPath);
}
}

return nuGetScratch;
}

case NuGetFolderPath.NuGetPluginsCacheDirectory:
return Path.Combine(invalidFolder, "plugins-cache");

default:
return null;
}
}
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

switch (folder)
{
case NuGetFolderPath.MachineWideSettingsBaseDirectory:
Expand Down

0 comments on commit 5769cd9

Please sign in to comment.