Skip to content

Commit

Permalink
(chocolatey#2383) Use "/" instead of SystemDrive on non-Windows platf…
Browse files Browse the repository at this point in the history
…orms

Linux and MacOS do not normally have a "SystemDrive" environment
variable since they have everything under a single root of "/".
This switches to use "/" instead of "SystemDrive" on non-Windows
platforms. These two instances are the only ones I see in the C#
codebase. There are a couple of uses of "SystemDrive" in the PowerShell
helpers, but those are not run on non-Windows platforms.
  • Loading branch information
TheCakeIsNaOH authored and gep13 committed Oct 2, 2021
1 parent c78a0f7 commit 0faa1a5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,10 @@ public void create_directory(string directoryPath)
public void move_directory(string directoryPath, string newDirectoryPath)
{
if (string.IsNullOrWhiteSpace(directoryPath) || string.IsNullOrWhiteSpace(newDirectoryPath)) throw new ApplicationException("You must provide a directory to move from or to.");
if (combine_paths(directoryPath, "").is_equal_to(combine_paths(Environment.GetEnvironmentVariable("SystemDrive"), ""))) throw new ApplicationException("Cannot move or delete the root of the system drive");

//Linux/MacOS do not have a SystemDrive environment variable, instead, everything is under "/"
var systemDrive = Platform.get_platform() == PlatformType.Windows ? Environment.GetEnvironmentVariable("SystemDrive") : "/";
if (combine_paths(directoryPath, "").is_equal_to(combine_paths(systemDrive, ""))) throw new ApplicationException("Cannot move or delete the root of the system drive");

try
{
Expand Down Expand Up @@ -741,7 +744,10 @@ public void delete_directory(string directoryPath, bool recursive, bool override
public void delete_directory(string directoryPath, bool recursive, bool overrideAttributes, bool isSilent)
{
if (string.IsNullOrWhiteSpace(directoryPath)) throw new ApplicationException("You must provide a directory to delete.");
if (combine_paths(directoryPath, "").is_equal_to(combine_paths(Environment.GetEnvironmentVariable("SystemDrive"), ""))) throw new ApplicationException("Cannot move or delete the root of the system drive");

//Linux / MacOS do not have a SystemDrive environment variable, instead, everything is under "/"
var systemDrive = Platform.get_platform() == PlatformType.Windows ? Environment.GetEnvironmentVariable("SystemDrive") : "/";
if (combine_paths(directoryPath, "").is_equal_to(combine_paths(systemDrive, ""))) throw new ApplicationException("Cannot move or delete the root of the system drive");

if (overrideAttributes)
{
Expand Down

0 comments on commit 0faa1a5

Please sign in to comment.