|
8 | 8 | using System.IO;
|
9 | 9 | using System.Linq;
|
10 | 10 | using System.Management.Automation;
|
| 11 | +using System.Runtime.InteropServices; |
11 | 12 | using System.Text;
|
12 | 13 | using System.Text.RegularExpressions;
|
13 | 14 | using System.Threading;
|
14 | 15 | using Microsoft.PowerShell.PSReadLine;
|
15 | 16 |
|
16 | 17 | namespace Microsoft.PowerShell
|
17 | 18 | {
|
| 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 | + |
18 | 47 | public partial class PSConsoleReadLine
|
19 | 48 | {
|
20 | 49 | /// <summary>
|
@@ -182,7 +211,12 @@ private string GetHistorySaveFileMutexName()
|
182 | 211 | {
|
183 | 212 | // Return a reasonably unique name - it's not too important as there will rarely
|
184 | 213 | // 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(); |
186 | 220 | }
|
187 | 221 |
|
188 | 222 | private void IncrementalHistoryWrite()
|
|
0 commit comments