Skip to content

Commit

Permalink
Merge pull request NuGet#17 from TheCakeIsNaOH/switch-paths
Browse files Browse the repository at this point in the history
(NuGet#9) Don't step on NuGet paths
  • Loading branch information
gep13 authored Jan 11, 2023
2 parents a0210f0 + 32b1713 commit f18e0ab
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/NuGet.Core/NuGet.Common/Migrations/MigrationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ public static class MigrationRunner

public static void Run()
{
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
// We don't want any chance of these migrations happening, even though they don't currently get called via code that Chocolatey CLI uses.
return;
#pragma warning disable CS0162 // Unreachable code detected
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////


string migrationsDirectory = GetMigrationsDirectory();
var expectedMigrationFilename = Path.Combine(migrationsDirectory, MaxMigrationFilename);

Expand Down Expand Up @@ -43,6 +54,14 @@ public static void Run()
}
}

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
#pragma warning disable CS0162 // Unreachable code detected
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

static bool WaitForMutex(Mutex mutex)
{
bool captured;
Expand Down
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
16 changes: 14 additions & 2 deletions test/NuGet.Core.Tests/NuGet.Common.Test/MigrationRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ namespace NuGet.Common.Test
[CollectionDefinition("MigrationRunner", DisableParallelization = true)]
public class MigrationRunnerTests
{
[Fact]
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
[Fact(Skip = "Intentionally broken by Chocolatey changes")]
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
public void Run_WhenExecutedOnSingleThreadThenOneMigrationFileIsCreated_Success()
{
// Arrange
Expand All @@ -31,7 +37,13 @@ public void Run_WhenExecutedOnSingleThreadThenOneMigrationFileIsCreated_Success(
Assert.Equal(Path.Combine(directory, "1"), files[0]);
}

[Fact]
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
[Fact(Skip = "Intentionally broken by Chocolatey changes")]
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
public void Run_WhenExecutedInParallelThenOnlyOneMigrationFileIsCreated_Success()
{
var threads = new List<Thread>();
Expand Down

0 comments on commit f18e0ab

Please sign in to comment.