wasSharpNET – Diff between revs 22 and 27
?pathlinks?
Rev 22 | Rev 27 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System; |
7 | using System; |
|
8 | using System.IO; |
8 | using System.IO; |
|
9 | using System.Threading; |
9 | using System.Threading; |
|
10 | |
10 | |
|
11 | namespace wasSharpNET.IO |
11 | namespace wasSharpNET.IO |
|
12 | { |
12 | { |
|
13 | /// <summary> |
13 | /// <summary> |
|
14 | /// This is a wrapper aroung a FileStream. While it is not a Stream itself, it can be cast to |
14 | /// This is a wrapper aroung a FileStream. While it is not a Stream itself, it can be cast to |
|
15 | /// one (keep in mind that this might throw an exception). |
15 | /// one (keep in mind that this might throw an exception). |
|
16 | /// </summary> |
16 | /// </summary> |
|
17 | public class SafeFileStream : IDisposable |
17 | public class SafeFileStream : IDisposable |
|
18 | { |
18 | { |
|
19 | #region Constructors |
19 | #region Constructors |
|
20 | |
20 | |
|
21 | public SafeFileStream(string path, FileMode mode, FileAccess access, FileShare share, uint milliscondsTimeout) |
21 | public SafeFileStream(string path, FileMode mode, FileAccess access, FileShare share, uint milliscondsTimeout) |
|
22 | { |
22 | { |
|
23 | m_mutex = new Mutex(false, string.Format("Global\\{0}", path.Replace('\\', '/'))); |
23 | m_mutex = new Mutex(false, $"Global\\{path.Replace('\\', '/')}"); |
|
24 | m_path = path; |
24 | m_path = path; |
|
25 | m_fileMode = mode; |
25 | m_fileMode = mode; |
|
26 | m_fileAccess = access; |
26 | m_fileAccess = access; |
|
27 | m_fileShare = share; |
27 | m_fileShare = share; |
|
28 | m_millisecondsTimeout = milliscondsTimeout; |
28 | m_millisecondsTimeout = milliscondsTimeout; |
|
29 | } |
29 | } |
|
30 | |
30 | |
|
31 | #endregion//Constructors |
31 | #endregion//Constructors |
|
32 | |
32 | |
|
33 | #region Private Members |
33 | #region Private Members |
|
34 | |
34 | |
|
35 | private readonly Mutex m_mutex; |
35 | private readonly Mutex m_mutex; |
|
36 | private Stream m_stream; |
36 | private Stream m_stream; |
|
37 | private readonly string m_path; |
37 | private readonly string m_path; |
|
38 | private readonly FileMode m_fileMode; |
38 | private readonly FileMode m_fileMode; |
|
39 | private readonly FileAccess m_fileAccess; |
39 | private readonly FileAccess m_fileAccess; |
|
40 | private readonly FileShare m_fileShare; |
40 | private readonly FileShare m_fileShare; |
|
41 | private readonly uint m_millisecondsTimeout; |
41 | private readonly uint m_millisecondsTimeout; |
|
42 | |
42 | |
|
43 | #endregion//Private Members |
43 | #endregion//Private Members |
|
44 | |
44 | |
|
45 | #region Properties |
45 | #region Properties |
|
46 | |
46 | |
|
47 | public Stream Stream |
47 | public Stream Stream |
|
48 | { |
48 | { |
|
49 | get |
49 | get |
|
50 | { |
50 | { |
|
51 | if (!IsOpen && !TryOpen(TimeSpan.FromMilliseconds(m_millisecondsTimeout))) |
51 | if (!IsOpen && !TryOpen(TimeSpan.FromMilliseconds(m_millisecondsTimeout))) |
|
52 | throw new InvalidOperationException("Timeout opening stream."); |
52 | throw new InvalidOperationException("Timeout opening stream."); |
|
53 | return m_stream; |
53 | return m_stream; |
|
54 | } |
54 | } |
|
55 | } |
55 | } |
|
56 | |
56 | |
|
57 | private bool IsOpen => m_stream != null; |
57 | private bool IsOpen => m_stream != null; |
|
58 | |
58 | |
|
59 | #endregion//Properties |
59 | #endregion//Properties |
|
60 | |
60 | |
|
61 | #region Functions |
61 | #region Functions |
|
62 | |
62 | |
|
63 | /// <summary> |
63 | /// <summary> |
|
64 | /// Opens the stream when it is not locked. If the file is locked, then |
64 | /// Opens the stream when it is not locked. If the file is locked, then |
|
65 | /// </summary> |
65 | /// </summary> |
|
66 | public void Open() |
66 | public void Open() |
|
67 | { |
67 | { |
|
68 | if (m_stream != null) |
68 | if (m_stream != null) |
|
69 | throw new InvalidOperationException("The stream is already open."); |
69 | throw new InvalidOperationException("The stream is already open."); |
|
70 | m_mutex.WaitOne(); |
70 | m_mutex.WaitOne(); |
|
71 | m_stream = File.Open(m_path, m_fileMode, m_fileAccess, m_fileShare); |
71 | m_stream = File.Open(m_path, m_fileMode, m_fileAccess, m_fileShare); |
|
72 | } |
72 | } |
|
73 | |
73 | |
|
74 | public bool TryOpen(TimeSpan span) |
74 | public bool TryOpen(TimeSpan span) |
|
75 | { |
75 | { |
|
76 | if (m_stream != null) |
76 | if (m_stream != null) |
|
77 | throw new InvalidOperationException("The stream is already open."); |
77 | throw new InvalidOperationException("The stream is already open."); |
|
78 | if (m_mutex.WaitOne(span)) |
78 | if (m_mutex.WaitOne(span)) |
|
79 | { |
79 | { |
|
80 | m_stream = File.Open(m_path, m_fileMode, m_fileAccess, m_fileShare); |
80 | m_stream = File.Open(m_path, m_fileMode, m_fileAccess, m_fileShare); |
|
81 | return true; |
81 | return true; |
|
82 | } |
82 | } |
|
83 | return false; |
83 | return false; |
|
84 | } |
84 | } |
|
85 | |
85 | |
|
86 | public void Close() |
86 | public void Close() |
|
87 | { |
87 | { |
|
88 | if (m_stream == null) |
88 | if (m_stream == null) |
|
89 | return; |
89 | return; |
|
90 | m_stream.Close(); |
90 | m_stream.Close(); |
|
91 | m_stream = null; |
91 | m_stream = null; |
|
92 | m_mutex.ReleaseMutex(); |
92 | m_mutex.ReleaseMutex(); |
|
93 | } |
93 | } |
|
94 | |
94 | |
|
95 | public void Dispose() |
95 | public void Dispose() |
|
96 | { |
96 | { |
|
97 | Close(); |
97 | Close(); |
|
98 | GC.SuppressFinalize(this); |
98 | GC.SuppressFinalize(this); |
|
99 | } |
99 | } |
|
100 | |
100 | |
|
101 | public static implicit operator Stream(SafeFileStream sfs) |
101 | public static implicit operator Stream(SafeFileStream sfs) |
|
102 | { |
102 | { |
|
103 | return sfs.Stream; |
103 | return sfs.Stream; |
|
104 | } |
104 | } |
|
105 | |
105 | |
|
106 | #endregion//Functions |
106 | #endregion//Functions |
|
107 | } |
107 | } |
|
108 | } |
108 | } |
|
109 | |
109 | |
|
110 |
|
110 |
|
|
111 | |
111 | |
|
112 | |
112 | |
|
113 | |
113 | |