3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System ;
6
+ using System . Collections . Concurrent ;
6
7
using System . Collections . Generic ;
7
8
using System . Diagnostics ;
8
9
using System . IO ;
@@ -93,7 +94,7 @@ public void Dispose()
93
94
// more than once.
94
95
// REVIEW: Should we garbage collect to some degree? Currently we don't delete any
95
96
// of these temp files until the repository is disposed.
96
- protected readonly Dictionary < string , string > PathMap ;
97
+ protected readonly ConcurrentDictionary < string , string > PathMap ;
97
98
98
99
/// <summary>
99
100
/// Exception context.
@@ -107,7 +108,7 @@ internal Repository(bool needDir, IExceptionContext ectx)
107
108
Contracts . AssertValueOrNull ( ectx ) ;
108
109
_ectx = ectx ;
109
110
110
- PathMap = new Dictionary < string , string > ( ) ;
111
+ PathMap = new ConcurrentDictionary < string , string > ( ) ;
111
112
_open = new List < Entry > ( ) ;
112
113
if ( needDir )
113
114
DirTemp = GetShortTempDir ( ) ;
@@ -256,7 +257,7 @@ protected void GetPath(out string pathEnt, out string pathTemp, string dir, stri
256
257
string root = Path . GetFullPath ( DirTemp ?? @"x:\dummy" ) ;
257
258
string entityPath = Path . Combine ( root , dir ?? "" , name ) ;
258
259
entityPath = Path . GetFullPath ( entityPath ) ;
259
- string tempPath = Path . Combine ( root , PathMap . Count . ToString ( ) ) ;
260
+ string tempPath = Path . Combine ( root , Path . GetRandomFileName ( ) ) ;
260
261
tempPath = Path . GetFullPath ( tempPath ) ;
261
262
262
263
string parent = Path . GetDirectoryName ( entityPath ) ;
@@ -333,10 +334,8 @@ public Entry CreateEntry(string dir, string name)
333
334
string pathEnt ;
334
335
string pathTemp ;
335
336
GetPath ( out pathEnt , out pathTemp , dir , name , true ) ;
336
- if ( PathMap . ContainsKey ( pathEnt ) )
337
+ if ( ! PathMap . TryAdd ( pathEnt , pathTemp ) )
337
338
throw ExceptionContext . ExceptParam ( nameof ( name ) , "Duplicate entry: '{0}'" , pathEnt ) ;
338
- else
339
- PathMap . Add ( pathEnt , pathTemp ) ;
340
339
341
340
Stream stream ;
342
341
if ( pathTemp != null )
@@ -525,7 +524,9 @@ public Entry OpenEntryOrNull(string dir, string name)
525
524
// Extract to a temporary file.
526
525
Directory . CreateDirectory ( Path . GetDirectoryName ( pathTemp ) ) ;
527
526
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
+
529
530
stream = new FileStream ( pathTemp , FileMode . Open , FileAccess . Read ) ;
530
531
}
531
532
else
0 commit comments