Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,14 @@ public static string SelectProductNameForDirectory(Platform.XDG_Type dirpath)
// create the xdg folder if needed
if (!Directory.Exists(xdgDataHomeDefault))
{
Directory.CreateDirectory(xdgDataHomeDefault);
try
{
Directory.CreateDirectory(xdgDataHomeDefault);
}
catch (UnauthorizedAccessException)
{
//service accounts won't have permission to create user folder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What sort of contract should this api have? If the directory can't be used, should this api return null instead?

Copy link
Member Author

@SteveL-MSFT SteveL-MSFT Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path.Combine() is used often with this api and would have a problem with null. My thinking was that this api returns the expected path which may be used in error message, so even returning an empty string may produce undesired results.

}
}
return xdgDataHomeDefault;
}
Expand All @@ -277,7 +284,14 @@ public static string SelectProductNameForDirectory(Platform.XDG_Type dirpath)
//xdg values have not been set
if (!Directory.Exists(xdgModuleDefault)) //module folder not always guaranteed to exist
{
Directory.CreateDirectory(xdgModuleDefault);
try
{
Directory.CreateDirectory(xdgModuleDefault);
}
catch (UnauthorizedAccessException)
{
//service accounts won't have permission to create user folder
}
}
return xdgModuleDefault;
}
Expand All @@ -296,7 +310,14 @@ public static string SelectProductNameForDirectory(Platform.XDG_Type dirpath)
//xdg values have not been set
if (!Directory.Exists(xdgCacheDefault)) //module folder not always guaranteed to exist
{
Directory.CreateDirectory(xdgCacheDefault);
try
{
Directory.CreateDirectory(xdgCacheDefault);
}
catch (UnauthorizedAccessException)
{
//service accounts won't have permission to create user folder
}
}

return xdgCacheDefault;
Expand All @@ -306,7 +327,14 @@ public static string SelectProductNameForDirectory(Platform.XDG_Type dirpath)
{
if (!Directory.Exists(Path.Combine(xdgcachehome, "powershell")))
{
Directory.CreateDirectory(Path.Combine(xdgcachehome, "powershell"));
try
{
Directory.CreateDirectory(Path.Combine(xdgcachehome, "powershell"));
}
catch (UnauthorizedAccessException)
{
//service accounts won't have permission to create user folder
}
}

return Path.Combine(xdgcachehome, "powershell");
Expand Down
17 changes: 15 additions & 2 deletions src/System.Management.Automation/engine/Modules/AnalysisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,16 @@ private static byte[] GetHeader()

private int _saveCacheToDiskQueued;

private bool _saveCacheToDisk = true;

public void QueueSerialization()
{
// We expect many modules to rapidly call for serialization.
// Instead of doing it right away, we'll queue a task that starts writing
// after it seems like we've stopped adding stuff to write out. This is
// avoids blocking the pipeline thread waiting for the write to finish.
// We want to make sure we only queue one task.
if (Interlocked.Increment(ref _saveCacheToDiskQueued) == 1)
if (_saveCacheToDisk && Interlocked.Increment(ref _saveCacheToDiskQueued) == 1)
{
Task.Run(async delegate
{
Expand Down Expand Up @@ -694,6 +696,8 @@ private static void Write(string val, byte[] bytes, FileStream stream)
private void Serialize(string filename)
{
AnalysisCacheData fromOtherProcess = null;
Diagnostics.Assert(_saveCacheToDisk != false, "Serialize should never be called without going through QueueSerialization which has a check");

try
{
if (Utils.NativeFileExists(filename))
Expand All @@ -710,7 +714,16 @@ private void Serialize(string filename)
var folder = Path.GetDirectoryName(filename);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
try
{
Directory.CreateDirectory(folder);
}
catch (UnauthorizedAccessException)
{
// service accounts won't be able to create directory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't thought through the implications - but should we have a fallback? e.g. $env:TEMP?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This usage is an edge case, I'm not sure how much we want to optimize particularly as one can use the XDG_*_HOME env vars. Also, I don't think it's guaranteed the user account has write access to $env:TEMP so we'd have to check that anyways.

_saveCacheToDisk = false;
return;
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,28 @@ foo
}
}
}

Context "Data, Config, and Cache locations" {
BeforeEach {
$XDG_CACHE_HOME = $env:XDG_CACHE_HOME
$XDG_DATA_HOME = $env:XDG_DATA_HOME
$XDG_CONFIG_HOME = $env:XDG_CONFIG_HOME
}

AfterEach {
$env:XDG_CACHE_HOME = $XDG_CACHE_HOME
$env:XDG_DATA_HOME = $XDG_DATA_HOME
$env:XDG_CONFIG_HOME = $XDG_CONFIG_HOME
}

It "Should start if Data, Config, and Cache location is not accessible" -skip:($IsWindows) {
$env:XDG_CACHE_HOME = "/dev/cpu"
$env:XDG_DATA_HOME = "/dev/cpu"
$env:XDG_CONFIG_HOME = "/dev/cpu"
$output = & powershell -noprofile -Command { (get-command).count }
[int]$output | Should BeGreaterThan 0
}
}
}

Describe "Console host api tests" -Tag CI {
Expand Down