wasSharpNET – Blame information for rev 27
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
21 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
27 | office | 7 | using System; |
8 | using System.Diagnostics; |
||
21 | office | 9 | using System.IO; |
10 | |||
11 | namespace wasSharpNET.IO.Utilities |
||
12 | { |
||
13 | public static class IOExtensions |
||
14 | { |
||
27 | office | 15 | /// <summary> |
16 | /// Attempt to obtain a filestream by polling a file till the handle becomes available. |
||
17 | /// </summary> |
||
18 | /// <param name="path">the path to the file</param> |
||
19 | /// <param name="mode">the file mode to use</param> |
||
20 | /// <param name="access">the level of access to the file</param> |
||
21 | /// <param name="share">the type of file share locking</param> |
||
22 | /// <param name="timeout">the timeout in milliseconds to fail opening the file</param> |
||
23 | /// <returns>a filestream if the file was opened successfully</returns> |
||
24 | public static FileStream GetWriteStream(string path, FileMode mode, FileAccess access, FileShare share, |
||
25 | int timeout) |
||
26 | { |
||
27 | var time = Stopwatch.StartNew(); |
||
28 | while (time.ElapsedMilliseconds < timeout) |
||
29 | try |
||
30 | { |
||
31 | return new FileStream(path, mode, access, share); |
||
32 | } |
||
33 | catch (IOException e) |
||
34 | { |
||
35 | // access error |
||
36 | if (e.HResult != -2147024864) |
||
37 | throw; |
||
38 | } |
||
39 | |||
40 | throw new TimeoutException($"Failed to get a write handle to {path} within {timeout}ms."); |
||
41 | } |
||
42 | |||
21 | office | 43 | public static bool isRootedIn(this string path, string root) |
44 | { |
||
45 | // Path is empty and root is empty. |
||
46 | if (string.IsNullOrEmpty(path) && string.IsNullOrEmpty(root)) |
||
47 | return true; |
||
48 | |||
49 | // Path is empty but the root path is not empty. |
||
50 | if (string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(root)) |
||
51 | return true; |
||
52 | |||
53 | // The supplied root path is empty. |
||
54 | if (string.IsNullOrEmpty(root)) |
||
55 | return false; |
||
56 | |||
57 | // The path is empty and the root is not. |
||
58 | if (string.IsNullOrEmpty(path)) |
||
59 | return true; |
||
60 | |||
27 | office | 61 | var p = new DirectoryInfo(path); |
62 | var r = new DirectoryInfo(root); |
||
21 | office | 63 | |
64 | return string.Equals(p.Parent?.FullName, r.Parent?.FullName) || isRootedIn(p.Parent?.FullName, root); |
||
65 | } |
||
66 | |||
67 | public static void Empty(this string directory) |
||
68 | { |
||
27 | office | 69 | var dir = new DirectoryInfo(directory); |
21 | office | 70 | |
27 | office | 71 | foreach (var fi in dir.GetFiles()) |
21 | office | 72 | fi.Delete(); |
73 | |||
27 | office | 74 | foreach (var di in dir.GetDirectories()) |
21 | office | 75 | { |
76 | Empty(di.FullName); |
||
77 | di.Delete(); |
||
78 | } |
||
79 | } |
||
80 | } |
||
27 | office | 81 | } |