Skip to content

Commit

Permalink
Make the history file mutext name unique across sessions (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxian-dbw authored Sep 23, 2019
1 parent 239955c commit 394bacc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
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

0 comments on commit 394bacc

Please sign in to comment.