From 394bacc1b5caeb2e27f24eb6a140956fd72343a9 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 23 Sep 2019 16:16:35 -0700 Subject: [PATCH] Make the history file mutext name unique across sessions (#1061) --- PSReadLine/Cmdlets.cs | 5 +---- PSReadLine/History.cs | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/PSReadLine/Cmdlets.cs b/PSReadLine/Cmdlets.cs index ee747b22..b2ff9de6 100644 --- a/PSReadLine/Cmdlets.cs +++ b/PSReadLine/Cmdlets.cs @@ -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; @@ -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; diff --git a/PSReadLine/History.cs b/PSReadLine/History.cs index 405c36af..e53f36c4 100644 --- a/PSReadLine/History.cs +++ b/PSReadLine/History.cs @@ -8,6 +8,7 @@ using System.IO; using System.Linq; using System.Management.Automation; +using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading; @@ -15,6 +16,34 @@ namespace Microsoft.PowerShell { + /// + /// FNV-1a hashing algorithm: http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1a + /// + 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 { /// @@ -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()