Skip to content

Commit 1ef5fa9

Browse files
authored
Use a GUID when creating the temp path (#4645)
* Use a GUID when creating the temp path * Update where the GUID is in the temp path * Remove braces * Use random file name instead of GUID * Switch to concurrent dictionary * TryAdd updates Note: method implemented is `Path.GetRandomFileName()` instead of a GUID as implied in the PR title.
1 parent 24c8274 commit 1ef5fa9

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/Microsoft.ML.Core/Data/Repository.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Concurrent;
67
using System.Collections.Generic;
78
using System.Diagnostics;
89
using System.IO;
@@ -93,7 +94,7 @@ public void Dispose()
9394
// more than once.
9495
// REVIEW: Should we garbage collect to some degree? Currently we don't delete any
9596
// of these temp files until the repository is disposed.
96-
protected readonly Dictionary<string, string> PathMap;
97+
protected readonly ConcurrentDictionary<string, string> PathMap;
9798

9899
/// <summary>
99100
/// Exception context.
@@ -107,7 +108,7 @@ internal Repository(bool needDir, IExceptionContext ectx)
107108
Contracts.AssertValueOrNull(ectx);
108109
_ectx = ectx;
109110

110-
PathMap = new Dictionary<string, string>();
111+
PathMap = new ConcurrentDictionary<string, string>();
111112
_open = new List<Entry>();
112113
if (needDir)
113114
DirTemp = GetShortTempDir();
@@ -256,7 +257,7 @@ protected void GetPath(out string pathEnt, out string pathTemp, string dir, stri
256257
string root = Path.GetFullPath(DirTemp ?? @"x:\dummy");
257258
string entityPath = Path.Combine(root, dir ?? "", name);
258259
entityPath = Path.GetFullPath(entityPath);
259-
string tempPath = Path.Combine(root, PathMap.Count.ToString());
260+
string tempPath = Path.Combine(root, Path.GetRandomFileName());
260261
tempPath = Path.GetFullPath(tempPath);
261262

262263
string parent = Path.GetDirectoryName(entityPath);
@@ -333,10 +334,8 @@ public Entry CreateEntry(string dir, string name)
333334
string pathEnt;
334335
string pathTemp;
335336
GetPath(out pathEnt, out pathTemp, dir, name, true);
336-
if (PathMap.ContainsKey(pathEnt))
337+
if (!PathMap.TryAdd(pathEnt, pathTemp))
337338
throw ExceptionContext.ExceptParam(nameof(name), "Duplicate entry: '{0}'", pathEnt);
338-
else
339-
PathMap.Add(pathEnt, pathTemp);
340339

341340
Stream stream;
342341
if (pathTemp != null)
@@ -525,7 +524,9 @@ public Entry OpenEntryOrNull(string dir, string name)
525524
// Extract to a temporary file.
526525
Directory.CreateDirectory(Path.GetDirectoryName(pathTemp));
527526
entry.ExtractToFile(pathTemp);
528-
PathMap.Add(pathLower, pathTemp);
527+
if (!PathMap.TryAdd(pathLower, pathTemp))
528+
throw ExceptionContext.ExceptParam(nameof(name), "Duplicate entry: '{0}'", pathLower);
529+
529530
stream = new FileStream(pathTemp, FileMode.Open, FileAccess.Read);
530531
}
531532
else

0 commit comments

Comments
 (0)