Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the history file mutext name unique across sessions #1061

Merged
merged 4 commits into from
Sep 23, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions PSReadLine/Cmdlets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Reflection;
Expand Down Expand Up @@ -648,9 +647,7 @@ public string HistorySavePath
get => _historySavePath;
set
{
// Normalize the path
var altPathChar = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? '/' : '\\';
_historySavePath = value?.Replace(altPathChar, Path.DirectorySeparatorChar);
_historySavePath = GetUnresolvedProviderPathFromPSPath(value);
}
}
private string _historySavePath;
Expand Down
36 changes: 35 additions & 1 deletion PSReadLine/History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,42 @@
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.PowerShell.PSReadLine;

namespace Microsoft.PowerShell
{
/// <summary>
/// FNV-1a hashing algorithm: http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1a
/// </summary>
internal class FNV1a32Hash
{
// FNV-1a algorithm parameters: http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param
private const uint FNV32_PRIME = 16777619;
private const uint FNV32_OFFSETBASIS = 2166136261;

internal static uint ComputeHash(string input)
{
char ch;
uint hash = FNV32_OFFSETBASIS, lowByte, highByte;

for (int i = 0; i < input.Length; i++)
{
ch = input[i];
lowByte = (uint)(ch & 0x00FF);
hash = (hash ^ lowByte) * FNV32_PRIME;

highByte = (uint)(ch >> 8);
hash = (hash ^ highByte) * FNV32_PRIME;
}

return hash;
}
}

public partial class PSConsoleReadLine
{
/// <summary>
Expand Down Expand Up @@ -182,7 +211,12 @@ private string GetHistorySaveFileMutexName()
{
// Return a reasonably unique name - it's not too important as there will rarely
// be any contention.
return "PSReadLineHistoryFile_" + _options.HistorySavePath.GetHashCode();
uint hashFromPath = FNV1a32Hash.ComputeHash(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? _options.HistorySavePath.ToLower()
: _options.HistorySavePath);

return "PSReadLineHistoryFile_" + hashFromPath.ToString();
}

private void IncrementalHistoryWrite()
Expand Down