FilePath.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. namespace SharpCifs.Util.Sharpen
  6. {
  7. public class FilePath
  8. {
  9. private string _path;
  10. private static long _tempCounter;
  11. public FilePath ()
  12. {
  13. }
  14. public FilePath (string path)
  15. : this ((string) null, path)
  16. {
  17. }
  18. public FilePath (FilePath other, string child)
  19. : this ((string) other, child)
  20. {
  21. }
  22. public FilePath (string other, string child)
  23. {
  24. if (other == null) {
  25. _path = child;
  26. } else {
  27. while (!string.IsNullOrEmpty(child) && (child[0] == Path.DirectorySeparatorChar || child[0] == Path.AltDirectorySeparatorChar))
  28. child = child.Substring (1);
  29. if (!string.IsNullOrEmpty(other) && other[other.Length - 1] == Path.VolumeSeparatorChar)
  30. other += Path.DirectorySeparatorChar;
  31. _path = Path.Combine (other, child);
  32. }
  33. }
  34. public static implicit operator FilePath (string name)
  35. {
  36. return new FilePath (name);
  37. }
  38. public static implicit operator string (FilePath filePath)
  39. {
  40. return filePath == null ? null : filePath._path;
  41. }
  42. public override bool Equals (object obj)
  43. {
  44. FilePath other = obj as FilePath;
  45. if (other == null)
  46. return false;
  47. return GetCanonicalPath () == other.GetCanonicalPath ();
  48. }
  49. public override int GetHashCode ()
  50. {
  51. return _path.GetHashCode ();
  52. }
  53. public bool CreateNewFile ()
  54. {
  55. try {
  56. //Stream.`Close` method deleted
  57. //File.Open (_path, FileMode.CreateNew).Close ();
  58. File.Open(_path, FileMode.CreateNew).Dispose();
  59. return true;
  60. } catch {
  61. return false;
  62. }
  63. }
  64. public static FilePath CreateTempFile ()
  65. {
  66. return new FilePath (Path.GetTempFileName ());
  67. }
  68. public static FilePath CreateTempFile (string prefix, string suffix)
  69. {
  70. return CreateTempFile (prefix, suffix, null);
  71. }
  72. public static FilePath CreateTempFile (string prefix, string suffix, FilePath directory)
  73. {
  74. string file;
  75. if (prefix == null) {
  76. throw new ArgumentNullException ("prefix");
  77. }
  78. if (prefix.Length < 3) {
  79. throw new ArgumentException ("prefix must have at least 3 characters");
  80. }
  81. string str = (directory == null) ? Path.GetTempPath () : directory.GetPath ();
  82. do {
  83. file = Path.Combine (str, prefix + Interlocked.Increment (ref _tempCounter) + suffix);
  84. } while (File.Exists (file));
  85. new FileOutputStream (file).Close ();
  86. return new FilePath (file);
  87. }
  88. public void DeleteOnExit ()
  89. {
  90. }
  91. public FilePath GetAbsoluteFile ()
  92. {
  93. return new FilePath (Path.GetFullPath (_path));
  94. }
  95. public string GetAbsolutePath ()
  96. {
  97. return Path.GetFullPath (_path);
  98. }
  99. public FilePath GetCanonicalFile ()
  100. {
  101. return new FilePath (GetCanonicalPath ());
  102. }
  103. public string GetCanonicalPath ()
  104. {
  105. string p = Path.GetFullPath (_path);
  106. p.TrimEnd (Path.DirectorySeparatorChar);
  107. return p;
  108. }
  109. public string GetName ()
  110. {
  111. return Path.GetFileName (_path);
  112. }
  113. public FilePath GetParentFile ()
  114. {
  115. return new FilePath (Path.GetDirectoryName (_path));
  116. }
  117. public string GetPath ()
  118. {
  119. return _path;
  120. }
  121. public bool IsAbsolute ()
  122. {
  123. return Path.IsPathRooted (_path);
  124. }
  125. public bool IsDirectory ()
  126. {
  127. return false; // FileHelper.Instance.IsDirectory(this);
  128. }
  129. public bool IsFile ()
  130. {
  131. return false; //FileHelper.Instance.IsFile (this);
  132. }
  133. public long LastModified ()
  134. {
  135. return 0; // FileHelper.Instance.LastModified(this);
  136. }
  137. public long Length ()
  138. {
  139. return 0; // FileHelper.Instance.Length(this);
  140. }
  141. public string[] List ()
  142. {
  143. return List (null);
  144. }
  145. public string[] List (IFilenameFilter filter)
  146. {
  147. try {
  148. if (IsFile ())
  149. return null;
  150. List<string> list = new List<string> ();
  151. foreach (string filePth in Directory.GetFileSystemEntries (_path)) {
  152. string fileName = Path.GetFileName (filePth);
  153. if ((filter == null) || filter.Accept (this, fileName)) {
  154. list.Add (fileName);
  155. }
  156. }
  157. return list.ToArray ();
  158. } catch {
  159. return null;
  160. }
  161. }
  162. public FilePath[] ListFiles ()
  163. {
  164. try {
  165. if (IsFile ())
  166. return null;
  167. List<FilePath> list = new List<FilePath> ();
  168. foreach (string filePath in Directory.GetFileSystemEntries (_path)) {
  169. list.Add (new FilePath (filePath));
  170. }
  171. return list.ToArray ();
  172. } catch {
  173. return null;
  174. }
  175. }
  176. static void MakeDirWritable (string dir)
  177. {
  178. //FileHelper.Instance.MakeDirWritable (dir);
  179. }
  180. static void MakeFileWritable (string file)
  181. {
  182. //FileHelper.Instance.MakeFileWritable (file);
  183. }
  184. public bool Mkdir ()
  185. {
  186. try {
  187. if (Directory.Exists (_path))
  188. return false;
  189. Directory.CreateDirectory (_path);
  190. return true;
  191. } catch (Exception) {
  192. return false;
  193. }
  194. }
  195. public bool Mkdirs ()
  196. {
  197. try {
  198. if (Directory.Exists (_path))
  199. return false;
  200. Directory.CreateDirectory (_path);
  201. return true;
  202. } catch {
  203. return false;
  204. }
  205. }
  206. public bool RenameTo (FilePath file)
  207. {
  208. return RenameTo (file._path);
  209. }
  210. public bool RenameTo (string name)
  211. {
  212. return false; // FileHelper.Instance.RenameTo(this, name);
  213. }
  214. public bool SetLastModified (long milis)
  215. {
  216. return false; // FileHelper.Instance.SetLastModified(this, milis);
  217. }
  218. public bool SetReadOnly ()
  219. {
  220. return false; // FileHelper.Instance.SetReadOnly(this);
  221. }
  222. public Uri ToUri ()
  223. {
  224. return new Uri (_path);
  225. }
  226. // Don't change the case of this method, since ngit does reflection on it
  227. public bool CanExecute ()
  228. {
  229. return false; // FileHelper.Instance.CanExecute(this);
  230. }
  231. // Don't change the case of this method, since ngit does reflection on it
  232. public bool SetExecutable (bool exec)
  233. {
  234. return false; // FileHelper.Instance.SetExecutable(this, exec);
  235. }
  236. public string GetParent ()
  237. {
  238. string p = Path.GetDirectoryName (_path);
  239. if (string.IsNullOrEmpty(p) || p == _path)
  240. return null;
  241. return p;
  242. }
  243. public override string ToString ()
  244. {
  245. return _path;
  246. }
  247. static internal string PathSeparator {
  248. get { return Path.PathSeparator.ToString (); }
  249. }
  250. static internal char PathSeparatorChar {
  251. get { return Path.PathSeparator; }
  252. }
  253. static internal char SeparatorChar {
  254. get { return Path.DirectorySeparatorChar; }
  255. }
  256. static internal string Separator {
  257. get { return Path.DirectorySeparatorChar.ToString (); }
  258. }
  259. }
  260. }