Skip to content

Commit 394bacc

Browse files
authored
Make the history file mutext name unique across sessions (#1061)
1 parent 239955c commit 394bacc

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Collections.ObjectModel;
99
using System.Diagnostics.CodeAnalysis;
1010
using System.Globalization;
11-
using System.IO;
1211
using System.Management.Automation;
1312
using System.Management.Automation.Language;
1413
using System.Reflection;
@@ -648,9 +647,7 @@ public string HistorySavePath
648647
get => _historySavePath;
649648
set
650649
{
651-
// Normalize the path
652-
var altPathChar = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? '/' : '\\';
653-
_historySavePath = value?.Replace(altPathChar, Path.DirectorySeparatorChar);
650+
_historySavePath = GetUnresolvedProviderPathFromPSPath(value);
654651
}
655652
}
656653
private string _historySavePath;

PSReadLine/History.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,42 @@
88
using System.IO;
99
using System.Linq;
1010
using System.Management.Automation;
11+
using System.Runtime.InteropServices;
1112
using System.Text;
1213
using System.Text.RegularExpressions;
1314
using System.Threading;
1415
using Microsoft.PowerShell.PSReadLine;
1516

1617
namespace Microsoft.PowerShell
1718
{
19+
/// <summary>
20+
/// FNV-1a hashing algorithm: http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1a
21+
/// </summary>
22+
internal class FNV1a32Hash
23+
{
24+
// FNV-1a algorithm parameters: http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param
25+
private const uint FNV32_PRIME = 16777619;
26+
private const uint FNV32_OFFSETBASIS = 2166136261;
27+
28+
internal static uint ComputeHash(string input)
29+
{
30+
char ch;
31+
uint hash = FNV32_OFFSETBASIS, lowByte, highByte;
32+
33+
for (int i = 0; i < input.Length; i++)
34+
{
35+
ch = input[i];
36+
lowByte = (uint)(ch & 0x00FF);
37+
hash = (hash ^ lowByte) * FNV32_PRIME;
38+
39+
highByte = (uint)(ch >> 8);
40+
hash = (hash ^ highByte) * FNV32_PRIME;
41+
}
42+
43+
return hash;
44+
}
45+
}
46+
1847
public partial class PSConsoleReadLine
1948
{
2049
/// <summary>
@@ -182,7 +211,12 @@ private string GetHistorySaveFileMutexName()
182211
{
183212
// Return a reasonably unique name - it's not too important as there will rarely
184213
// be any contention.
185-
return "PSReadLineHistoryFile_" + _options.HistorySavePath.GetHashCode();
214+
uint hashFromPath = FNV1a32Hash.ComputeHash(
215+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
216+
? _options.HistorySavePath.ToLower()
217+
: _options.HistorySavePath);
218+
219+
return "PSReadLineHistoryFile_" + hashFromPath.ToString();
186220
}
187221

188222
private void IncrementalHistoryWrite()

0 commit comments

Comments
 (0)