aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/IO/SharpCifs/Util/Sharpen/FilePath.cs
blob: 1b2f5eddcde11ed0a8fd3241b23f66b5de7d4787 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace SharpCifs.Util.Sharpen
{
    public class FilePath
	{
		private string _path;
		private static long _tempCounter;

		public FilePath ()
		{
		}

		public FilePath (string path)
			: this ((string) null, path)
		{

		}

		public FilePath (FilePath other, string child)
			: this ((string) other, child)
		{

		}

		public FilePath (string other, string child)
		{
			if (other == null) {
				_path = child;
			} else {
				while (!string.IsNullOrEmpty(child) && (child[0] == Path.DirectorySeparatorChar || child[0] == Path.AltDirectorySeparatorChar))
					child = child.Substring (1);

				if (!string.IsNullOrEmpty(other) && other[other.Length - 1] == Path.VolumeSeparatorChar)
					other += Path.DirectorySeparatorChar;

				_path = Path.Combine (other, child);
			}
		}
		
		public static implicit operator FilePath (string name)
		{
			return new FilePath (name);
		}

		public static implicit operator string (FilePath filePath)
		{
			return filePath == null ? null : filePath._path;
		}
		
		public override bool Equals (object obj)
		{
			FilePath other = obj as FilePath;
			if (other == null)
				return false;
			return GetCanonicalPath () == other.GetCanonicalPath ();
		}
		
		public override int GetHashCode ()
		{
			return _path.GetHashCode ();
		}

		public bool CreateNewFile ()
		{
			try {
                //Stream.`Close` method deleted
                //File.Open (_path, FileMode.CreateNew).Close ();
                File.Open(_path, FileMode.CreateNew).Dispose();
                return true;
			} catch {
				return false;
			}
		}

		public static FilePath CreateTempFile ()
		{
			return new FilePath (Path.GetTempFileName ());
		}

		public static FilePath CreateTempFile (string prefix, string suffix)
		{
			return CreateTempFile (prefix, suffix, null);
		}

		public static FilePath CreateTempFile (string prefix, string suffix, FilePath directory)
		{
			string file;
			if (prefix == null) {
				throw new ArgumentNullException ("prefix");
			}
			if (prefix.Length < 3) {
				throw new ArgumentException ("prefix must have at least 3 characters");
			}
			string str = (directory == null) ? Path.GetTempPath () : directory.GetPath ();
			do {
				file = Path.Combine (str, prefix + Interlocked.Increment (ref _tempCounter) + suffix);
			} while (File.Exists (file));
			
			new FileOutputStream (file).Close ();
			return new FilePath (file);
		}


		public void DeleteOnExit ()
		{
		}


		public FilePath GetAbsoluteFile ()
		{
			return new FilePath (Path.GetFullPath (_path));
		}

		public string GetAbsolutePath ()
		{
			return Path.GetFullPath (_path);
		}

		public FilePath GetCanonicalFile ()
		{
			return new FilePath (GetCanonicalPath ());
		}

		public string GetCanonicalPath ()
		{
			string p = Path.GetFullPath (_path);
			p.TrimEnd (Path.DirectorySeparatorChar);
			return p;
		}

		public string GetName ()
		{
			return Path.GetFileName (_path);
		}

		public FilePath GetParentFile ()
		{
			return new FilePath (Path.GetDirectoryName (_path));
		}

		public string GetPath ()
		{
			return _path;
		}

		public bool IsAbsolute ()
		{
			return Path.IsPathRooted (_path);
		}

		public bool IsDirectory ()
		{
            return false; // FileHelper.Instance.IsDirectory(this);
		}

		public bool IsFile ()
		{
			return false; //FileHelper.Instance.IsFile (this);
		}

		public long LastModified ()
		{
            return 0; // FileHelper.Instance.LastModified(this);
		}

		public long Length ()
		{
            return 0; // FileHelper.Instance.Length(this);
		}

		public string[] List ()
		{
			return List (null);
		}

		public string[] List (IFilenameFilter filter)
		{
			try {
				if (IsFile ())
					return null;
				List<string> list = new List<string> ();
				foreach (string filePth in Directory.GetFileSystemEntries (_path)) {
					string fileName = Path.GetFileName (filePth);
					if ((filter == null) || filter.Accept (this, fileName)) {
						list.Add (fileName);
					}
				}
				return list.ToArray ();
			} catch {
				return null;
			}
		}

		public FilePath[] ListFiles ()
		{
			try {
				if (IsFile ())
					return null;
				List<FilePath> list = new List<FilePath> ();
				foreach (string filePath in Directory.GetFileSystemEntries (_path)) {
					list.Add (new FilePath (filePath));
				}
				return list.ToArray ();
			} catch {
				return null;
			}
		}

		static void MakeDirWritable (string dir)
		{
			//FileHelper.Instance.MakeDirWritable (dir);
		}

		static void MakeFileWritable (string file)
		{
			//FileHelper.Instance.MakeFileWritable (file);
		}

		public bool Mkdir ()
		{
			try {
				if (Directory.Exists (_path))
					return false;
				Directory.CreateDirectory (_path);
				return true;
			} catch (Exception) {
				return false;
			}
		}

		public bool Mkdirs ()
		{
			try {
				if (Directory.Exists (_path))
					return false;
				Directory.CreateDirectory (_path);
				return true;
			} catch {
				return false;
			}
		}

		public bool RenameTo (FilePath file)
		{
			return RenameTo (file._path);
		}

		public bool RenameTo (string name)
		{
            return false; // FileHelper.Instance.RenameTo(this, name);
		}

		public bool SetLastModified (long milis)
		{
            return false; // FileHelper.Instance.SetLastModified(this, milis);
		}

		public bool SetReadOnly ()
		{
            return false; // FileHelper.Instance.SetReadOnly(this);
		}
		
		public Uri ToUri ()
		{
			return new Uri (_path);
		}
		
		// Don't change the case of this method, since ngit does reflection on it
		public bool CanExecute ()
		{
            return false; // FileHelper.Instance.CanExecute(this);
		}
		
		// Don't change the case of this method, since ngit does reflection on it
		public bool SetExecutable (bool exec)
		{
            return false; // FileHelper.Instance.SetExecutable(this, exec);
		}
		
		public string GetParent ()
		{
			string p = Path.GetDirectoryName (_path);
			if (string.IsNullOrEmpty(p) || p == _path)
				return null;
		    return p;
		}

		public override string ToString ()
		{
			return _path;
		}
		
		static internal string PathSeparator {
			get { return Path.PathSeparator.ToString (); }
		}

		static internal char PathSeparatorChar {
			get { return Path.PathSeparator; }
		}

		static internal char SeparatorChar {
			get { return Path.DirectorySeparatorChar; }
		}

		static internal string Separator {
			get { return Path.DirectorySeparatorChar.ToString (); }
		}
	}
}