forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cs
More file actions
115 lines (96 loc) · 3.17 KB
/
FileSystem.cs
File metadata and controls
115 lines (96 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Diagnostics;
namespace ScriptCs.Tests.Acceptance.Support
{
using System;
using System.Globalization;
using System.IO;
using Should;
// NOTE (adamralph): difficult to believe the retry stuff is required, but it is. System.IO and the filesystem race.
public static class FileSystem
{
public static void EnsureDirectoryCreated(string path)
{
Debug.WriteLine($"Current Directory [{Environment.CurrentDirectory.Length}]: {Environment.CurrentDirectory}");
if (Directory.Exists(path))
{
return;
}
var timeout = 0.05d;
var createTimeout = DateTime.Now.AddSeconds(timeout);
while (true)
{
try
{
Directory.CreateDirectory(path);
var existsTimeout = DateTime.Now.AddSeconds(timeout);
while (true)
{
if (Directory.Exists(path))
{
break;
}
if (DateTime.Now < existsTimeout)
{
continue;
}
throw new IOException(
string.Format(CultureInfo.InvariantCulture, "Failed to create directory '{0}'", path));
}
}
catch (Exception)
{
if (DateTime.Now < createTimeout)
{
continue;
}
throw;
}
break;
}
}
public static void EnsureDirectoryDeleted(string path)
{
if (!Directory.Exists(path))
{
return;
}
var timeout = 0.05d;
var deleteTimeout = DateTime.Now.AddSeconds(timeout);
while (true)
{
try
{
Directory.Delete(path, true);
var goneTimeout = DateTime.Now.AddSeconds(timeout);
while (true)
{
if (!Directory.Exists(path))
{
break;
}
if (DateTime.Now < goneTimeout)
{
continue;
}
throw new IOException(
string.Format(CultureInfo.InvariantCulture, "Failed to delete directory '{0}'", path));
}
}
catch (Exception)
{
if (DateTime.Now < deleteTimeout)
{
continue;
}
throw;
}
break;
}
}
public static void EnsureFileDeleted(string fileName)
{
File.Delete(fileName);
File.Exists(fileName).ShouldBeFalse(fileName + " should be deleted");
}
}
}