|
6 | 6 | using System.Collections.Generic;
|
7 | 7 | using System.IO;
|
8 | 8 | using System.IO.Compression;
|
9 |
| -using Microsoft.ML.Runtime.Data; |
10 | 9 | using Microsoft.ML.Runtime.Internal.Utilities;
|
11 | 10 |
|
12 | 11 | namespace Microsoft.ML.Runtime.Model
|
@@ -73,7 +72,7 @@ public void Dispose()
|
73 | 72 | }
|
74 | 73 | }
|
75 | 74 |
|
76 |
| - // These are the open entries that may contain streams into our _dirTemp. |
| 75 | + // These are the open entries that may contain streams into our DirTemp. |
77 | 76 | private List<Entry> _open;
|
78 | 77 |
|
79 | 78 | private bool _disposed;
|
@@ -108,19 +107,37 @@ internal Repository(bool needDir, IExceptionContext ectx)
|
108 | 107 | PathMap = new Dictionary<string, string>();
|
109 | 108 | _open = new List<Entry>();
|
110 | 109 | if (needDir)
|
111 |
| - { |
112 |
| - DirTemp = GetTempPath(); |
113 |
| - Directory.CreateDirectory(DirTemp); |
114 |
| - } |
| 110 | + DirTemp = GetShortTempDir(); |
115 | 111 | else
|
116 | 112 | GC.SuppressFinalize(this);
|
117 | 113 | }
|
118 | 114 |
|
119 |
| - // REVIEW: This should use host environment functionality. |
120 |
| - private static string GetTempPath() |
| 115 | + private static string GetShortTempDir() |
| 116 | + { |
| 117 | + var rnd = RandomUtils.Create(); |
| 118 | + string path; |
| 119 | + do |
| 120 | + { |
| 121 | + path = Path.Combine(Path.GetTempPath(), "TLC_" + rnd.Next().ToString("X")); |
| 122 | + path = Path.GetFullPath(path); |
| 123 | + Directory.CreateDirectory(path); |
| 124 | + } |
| 125 | + while (!EnsureDirectory(path)); |
| 126 | + return path; |
| 127 | + } |
| 128 | + |
| 129 | + private static bool EnsureDirectory(string path) |
121 | 130 | {
|
122 |
| - Guid guid = Guid.NewGuid(); |
123 |
| - return Path.GetFullPath(Path.Combine(Path.GetTempPath(), "TLC_" + guid.ToString())); |
| 131 | + path = Path.GetFullPath(Path.Combine(path, ".lock")); |
| 132 | + try |
| 133 | + { |
| 134 | + using (var stream = new FileStream(path, FileMode.CreateNew)) |
| 135 | + return true; |
| 136 | + } |
| 137 | + catch |
| 138 | + { |
| 139 | + return false; |
| 140 | + } |
124 | 141 | }
|
125 | 142 |
|
126 | 143 | ~Repository()
|
@@ -232,7 +249,7 @@ protected void GetPath(out string pathEnt, out string pathTemp, string dir, stri
|
232 | 249 | _ectx.CheckParam(!name.Contains(".."), nameof(name));
|
233 | 250 |
|
234 | 251 | // The gymnastics below are meant to deal with bad invocations including absolute paths, etc.
|
235 |
| - // That's why we go through it even if _dirTemp is null. |
| 252 | + // That's why we go through it even if DirTemp is null. |
236 | 253 | string root = Path.GetFullPath(DirTemp ?? @"x:\dummy");
|
237 | 254 | string entityPath = Path.Combine(root, dir ?? "", name);
|
238 | 255 | entityPath = Path.GetFullPath(entityPath);
|
|
0 commit comments