[tests] Add support for creating temporary directories on Windows.

This commit is contained in:
Rolf Bjarne Kvinge 2023-02-03 11:46:38 +01:00
Родитель 736662d8ee
Коммит 5511849baa
1 изменённых файлов: 18 добавлений и 6 удалений

Просмотреть файл

@ -44,6 +44,23 @@ namespace Xamarin {
[DllImport ("libc", SetLastError = true)]
static extern int mkdir (string path, ushort mode);
[DllImport ("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool CreateDirectory (string path, IntPtr lpSecurityAttributes);
static bool TryCreateDirectory (string directory)
{
// There's no way to know if Directory.CreateDirectory
// created the directory or not (which would happen if the directory
// already existed). Checking if the directory exists before
// creating it would result in a race condition if multiple
// threads create temporary directories at the same time.
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
return CreateDirectory (directory, IntPtr.Zero);
} else {
return mkdir (directory, Convert.ToUInt16 ("777", 8)) == 0;
}
}
public static string CreateTemporaryDirectory (string? name = null)
{
if (string.IsNullOrEmpty (name)) {
@ -57,12 +74,7 @@ namespace Xamarin {
var rv = Path.Combine (root, name);
for (int i = last_number; i < 10000 + last_number; i++) {
// There's no way to know if Directory.CreateDirectory
// created the directory or not (which would happen if the directory
// already existed). Checking if the directory exists before
// creating it would result in a race condition if multiple
// threads create temporary directories at the same time.
if (mkdir (rv, Convert.ToUInt16 ("777", 8)) == 0) {
if (TryCreateDirectory (rv)) {
last_number = i;
return rv;
}