Directory.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. //
  2. // System.IO.Directory.cs
  3. //
  4. // Authors:
  5. // Jim Richardson ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. // Dan Lewis ([email protected])
  8. // Eduardo Garcia ([email protected])
  9. // Ville Palo ([email protected])
  10. //
  11. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  12. // Copyright (C) 2002 Ximian, Inc.
  13. //
  14. // Created: Monday, August 13, 2001
  15. //
  16. //------------------------------------------------------------------------------
  17. //
  18. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  19. //
  20. // Permission is hereby granted, free of charge, to any person obtaining
  21. // a copy of this software and associated documentation files (the
  22. // "Software"), to deal in the Software without restriction, including
  23. // without limitation the rights to use, copy, modify, merge, publish,
  24. // distribute, sublicense, and/or sell copies of the Software, and to
  25. // permit persons to whom the Software is furnished to do so, subject to
  26. // the following conditions:
  27. //
  28. // The above copyright notice and this permission notice shall be
  29. // included in all copies or substantial portions of the Software.
  30. //
  31. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  34. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  35. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  36. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  37. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. //
  39. using System.Collections;
  40. using System.Security;
  41. using System.Security.Permissions;
  42. using System.Text;
  43. using System.Runtime.InteropServices;
  44. #if !NET_2_1 || MONOTOUCH
  45. using System.Security.AccessControl;
  46. #endif
  47. namespace System.IO
  48. {
  49. [ComVisible (true)]
  50. public static class Directory
  51. {
  52. public static DirectoryInfo CreateDirectory (string path)
  53. {
  54. if (path == null)
  55. throw new ArgumentNullException ("path");
  56. if (path.Length == 0)
  57. throw new ArgumentException ("Path is empty");
  58. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  59. throw new ArgumentException ("Path contains invalid chars");
  60. if (path.Trim ().Length == 0)
  61. throw new ArgumentException ("Only blank characters in path");
  62. if (File.Exists(path))
  63. throw new IOException ("Cannot create " + path + " because a file with the same name already exists.");
  64. // LAMESPEC: with .net 1.0 version this throw NotSupportedException and msdn says so too
  65. // but v1.1 throws ArgumentException.
  66. if (path == ":")
  67. throw new ArgumentException ("Only ':' In path");
  68. return CreateDirectoriesInternal (path);
  69. }
  70. #if !NET_2_1 || MONOTOUCH
  71. [MonoLimitation ("DirectorySecurity not implemented")]
  72. public static DirectoryInfo CreateDirectory (string path, DirectorySecurity directorySecurity)
  73. {
  74. return(CreateDirectory (path));
  75. }
  76. #endif
  77. static DirectoryInfo CreateDirectoriesInternal (string path)
  78. {
  79. #if !NET_2_1 || MONOTOUCH
  80. if (SecurityManager.SecurityEnabled) {
  81. new FileIOPermission (FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, path).Demand ();
  82. }
  83. #endif
  84. DirectoryInfo info = new DirectoryInfo (path, true);
  85. if (info.Parent != null && !info.Parent.Exists)
  86. info.Parent.Create ();
  87. MonoIOError error;
  88. if (!MonoIO.CreateDirectory (path, out error)) {
  89. // LAMESPEC: 1.1 and 1.2alpha allow CreateDirectory on a file path.
  90. // So CreateDirectory ("/tmp/somefile") will succeed if 'somefile' is
  91. // not a directory. However, 1.0 will throw an exception.
  92. // We behave like 1.0 here (emulating 1.1-like behavior is just a matter
  93. // of comparing error to ERROR_FILE_EXISTS, but it's lame to do:
  94. // DirectoryInfo di = Directory.CreateDirectory (something);
  95. // and having di.Exists return false afterwards.
  96. // I hope we don't break anyone's code, as they should be catching
  97. // the exception anyway.
  98. if (error != MonoIOError.ERROR_ALREADY_EXISTS &&
  99. error != MonoIOError.ERROR_FILE_EXISTS)
  100. throw MonoIO.GetException (path, error);
  101. }
  102. return info;
  103. }
  104. public static void Delete (string path)
  105. {
  106. if (path == null)
  107. throw new ArgumentNullException ("path");
  108. if (path.Length == 0)
  109. throw new ArgumentException ("Path is empty");
  110. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  111. throw new ArgumentException ("Path contains invalid chars");
  112. if (path.Trim().Length == 0)
  113. throw new ArgumentException ("Only blank characters in path");
  114. if (path == ":")
  115. throw new NotSupportedException ("Only ':' In path");
  116. MonoIOError error;
  117. bool success;
  118. if (MonoIO.ExistsSymlink (path, out error)) {
  119. /* RemoveDirectory maps to rmdir()
  120. * which fails on symlinks (ENOTDIR)
  121. */
  122. success = MonoIO.DeleteFile (path, out error);
  123. } else {
  124. success = MonoIO.RemoveDirectory (path, out error);
  125. }
  126. if (!success) {
  127. /*
  128. * FIXME:
  129. * In io-layer/io.c rmdir returns error_file_not_found if directory does not exists.
  130. * So maybe this could be handled somewhere else?
  131. */
  132. if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
  133. if (File.Exists (path))
  134. throw new IOException ("Directory does not exist, but a file of the same name exist.");
  135. else
  136. throw new DirectoryNotFoundException ("Directory does not exist.");
  137. } else
  138. throw MonoIO.GetException (path, error);
  139. }
  140. }
  141. static void RecursiveDelete (string path)
  142. {
  143. MonoIOError error;
  144. foreach (string dir in GetDirectories (path)) {
  145. if (MonoIO.ExistsSymlink (dir, out error)) {
  146. MonoIO.DeleteFile (dir, out error);
  147. } else {
  148. RecursiveDelete (dir);
  149. }
  150. }
  151. foreach (string file in GetFiles (path))
  152. File.Delete (file);
  153. Directory.Delete (path);
  154. }
  155. public static void Delete (string path, bool recursive)
  156. {
  157. CheckPathExceptions (path);
  158. if (recursive)
  159. RecursiveDelete (path);
  160. else
  161. Delete (path);
  162. }
  163. public static bool Exists (string path)
  164. {
  165. if (path == null)
  166. return false;
  167. MonoIOError error;
  168. bool exists;
  169. exists = MonoIO.ExistsDirectory (path, out error);
  170. if (error != MonoIOError.ERROR_SUCCESS &&
  171. error != MonoIOError.ERROR_PATH_NOT_FOUND &&
  172. error != MonoIOError.ERROR_INVALID_HANDLE &&
  173. error != MonoIOError.ERROR_ACCESS_DENIED) {
  174. // INVALID_HANDLE might happen if the file is moved
  175. // while testing for the existence, a kernel issue
  176. // according to Larry Ewing.
  177. throw MonoIO.GetException (path, error);
  178. }
  179. return(exists);
  180. }
  181. public static DateTime GetLastAccessTime (string path)
  182. {
  183. return File.GetLastAccessTime (path);
  184. }
  185. public static DateTime GetLastAccessTimeUtc (string path)
  186. {
  187. return GetLastAccessTime (path).ToUniversalTime ();
  188. }
  189. public static DateTime GetLastWriteTime (string path)
  190. {
  191. return File.GetLastWriteTime (path);
  192. }
  193. public static DateTime GetLastWriteTimeUtc (string path)
  194. {
  195. return GetLastWriteTime (path).ToUniversalTime ();
  196. }
  197. public static DateTime GetCreationTime (string path)
  198. {
  199. return File.GetCreationTime (path);
  200. }
  201. public static DateTime GetCreationTimeUtc (string path)
  202. {
  203. return GetCreationTime (path).ToUniversalTime ();
  204. }
  205. public static string GetCurrentDirectory ()
  206. {
  207. MonoIOError error;
  208. string result = MonoIO.GetCurrentDirectory (out error);
  209. if (error != MonoIOError.ERROR_SUCCESS)
  210. throw MonoIO.GetException (error);
  211. #if !NET_2_1 || MONOTOUCH
  212. if ((result != null) && (result.Length > 0) && SecurityManager.SecurityEnabled) {
  213. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, result).Demand ();
  214. }
  215. #endif
  216. return result;
  217. }
  218. public static string [] GetDirectories (string path)
  219. {
  220. return GetDirectories (path, "*");
  221. }
  222. public static string [] GetDirectories (string path, string searchPattern)
  223. {
  224. return GetFileSystemEntries (path, searchPattern, FileAttributes.Directory, FileAttributes.Directory);
  225. }
  226. #if !NET_2_1 || MONOTOUCH
  227. public static string [] GetDirectories (string path, string searchPattern, SearchOption searchOption)
  228. {
  229. if (searchOption == SearchOption.TopDirectoryOnly)
  230. return GetDirectories (path, searchPattern);
  231. ArrayList all = new ArrayList ();
  232. GetDirectoriesRecurse (path, searchPattern, all);
  233. return (string []) all.ToArray (typeof (string));
  234. }
  235. static void GetDirectoriesRecurse (string path, string searchPattern, ArrayList all)
  236. {
  237. all.AddRange (GetDirectories (path, searchPattern));
  238. foreach (string dir in GetDirectories (path))
  239. GetDirectoriesRecurse (dir, searchPattern, all);
  240. }
  241. #endif
  242. public static string GetDirectoryRoot (string path)
  243. {
  244. return new String(Path.DirectorySeparatorChar,1);
  245. }
  246. public static string [] GetFiles (string path)
  247. {
  248. return GetFiles (path, "*");
  249. }
  250. public static string [] GetFiles (string path, string searchPattern)
  251. {
  252. return GetFileSystemEntries (path, searchPattern, FileAttributes.Directory, 0);
  253. }
  254. #if !NET_2_1 || MONOTOUCH
  255. public static string[] GetFiles (string path, string searchPattern, SearchOption searchOption)
  256. {
  257. if (searchOption == SearchOption.TopDirectoryOnly)
  258. return GetFiles (path, searchPattern);
  259. ArrayList all = new ArrayList ();
  260. GetFilesRecurse (path, searchPattern, all);
  261. return (string []) all.ToArray (typeof (string));
  262. }
  263. static void GetFilesRecurse (string path, string searchPattern, ArrayList all)
  264. {
  265. all.AddRange (GetFiles (path, searchPattern));
  266. foreach (string dir in GetDirectories (path))
  267. GetFilesRecurse (dir, searchPattern, all);
  268. }
  269. #endif
  270. public static string [] GetFileSystemEntries (string path)
  271. {
  272. return GetFileSystemEntries (path, "*");
  273. }
  274. public static string [] GetFileSystemEntries (string path, string searchPattern)
  275. {
  276. return GetFileSystemEntries (path, searchPattern, 0, 0);
  277. }
  278. public static string[] GetLogicalDrives ()
  279. {
  280. return Environment.GetLogicalDrives ();
  281. }
  282. static bool IsRootDirectory (string path)
  283. {
  284. // Unix
  285. if (Path.DirectorySeparatorChar == '/' && path == "/")
  286. return true;
  287. // Windows
  288. if (Path.DirectorySeparatorChar == '\\')
  289. if (path.Length == 3 && path.EndsWith (":\\"))
  290. return true;
  291. return false;
  292. }
  293. public static DirectoryInfo GetParent (string path)
  294. {
  295. if (path == null)
  296. throw new ArgumentNullException ("path");
  297. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  298. throw new ArgumentException ("Path contains invalid characters");
  299. if (path.Length == 0)
  300. throw new ArgumentException ("The Path do not have a valid format");
  301. // return null if the path is the root directory
  302. if (IsRootDirectory (path))
  303. return null;
  304. string parent_name = Path.GetDirectoryName (path);
  305. if (parent_name.Length == 0)
  306. parent_name = GetCurrentDirectory();
  307. return new DirectoryInfo (parent_name);
  308. }
  309. public static void Move (string sourceDirName, string destDirName)
  310. {
  311. if (sourceDirName == null)
  312. throw new ArgumentNullException ("sourceDirName");
  313. if (destDirName == null)
  314. throw new ArgumentNullException ("destDirName");
  315. if (sourceDirName.Trim ().Length == 0 || sourceDirName.IndexOfAny (Path.InvalidPathChars) != -1)
  316. throw new ArgumentException ("Invalid source directory name: " + sourceDirName, "sourceDirName");
  317. if (destDirName.Trim ().Length == 0 || destDirName.IndexOfAny (Path.InvalidPathChars) != -1)
  318. throw new ArgumentException ("Invalid target directory name: " + destDirName, "destDirName");
  319. if (sourceDirName == destDirName)
  320. throw new IOException ("Source and destination path must be different.");
  321. if (Exists (destDirName))
  322. throw new IOException (destDirName + " already exists.");
  323. if (!Exists (sourceDirName) && !File.Exists (sourceDirName))
  324. throw new DirectoryNotFoundException (sourceDirName + " does not exist");
  325. MonoIOError error;
  326. if (!MonoIO.MoveFile (sourceDirName, destDirName, out error))
  327. throw MonoIO.GetException (error);
  328. }
  329. #if !NET_2_1 || MONOTOUCH
  330. public static void SetAccessControl (string path, DirectorySecurity directorySecurity)
  331. {
  332. throw new NotImplementedException ();
  333. }
  334. #endif
  335. public static void SetCreationTime (string path, DateTime creationTime)
  336. {
  337. File.SetCreationTime (path, creationTime);
  338. }
  339. public static void SetCreationTimeUtc (string path, DateTime creationTimeUtc)
  340. {
  341. SetCreationTime (path, creationTimeUtc.ToLocalTime ());
  342. }
  343. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  344. public static void SetCurrentDirectory (string path)
  345. {
  346. if (path == null)
  347. throw new ArgumentNullException ("path");
  348. if (path.Trim ().Length == 0)
  349. throw new ArgumentException ("path string must not be an empty string or whitespace string");
  350. MonoIOError error;
  351. if (!Exists (path))
  352. throw new DirectoryNotFoundException ("Directory \"" +
  353. path + "\" not found.");
  354. MonoIO.SetCurrentDirectory (path, out error);
  355. if (error != MonoIOError.ERROR_SUCCESS)
  356. throw MonoIO.GetException (path, error);
  357. }
  358. public static void SetLastAccessTime (string path, DateTime lastAccessTime)
  359. {
  360. File.SetLastAccessTime (path, lastAccessTime);
  361. }
  362. public static void SetLastAccessTimeUtc (string path, DateTime lastAccessTimeUtc)
  363. {
  364. SetLastAccessTime (path, lastAccessTimeUtc.ToLocalTime ());
  365. }
  366. public static void SetLastWriteTime (string path, DateTime lastWriteTime)
  367. {
  368. File.SetLastWriteTime (path, lastWriteTime);
  369. }
  370. public static void SetLastWriteTimeUtc (string path, DateTime lastWriteTimeUtc)
  371. {
  372. SetLastWriteTime (path, lastWriteTimeUtc.ToLocalTime ());
  373. }
  374. // private
  375. private static void CheckPathExceptions (string path)
  376. {
  377. if (path == null)
  378. throw new System.ArgumentNullException("path");
  379. if (path.Length == 0)
  380. throw new System.ArgumentException("Path is Empty");
  381. if (path.Trim().Length == 0)
  382. throw new ArgumentException ("Only blank characters in path");
  383. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  384. throw new ArgumentException ("Path contains invalid chars");
  385. }
  386. private static string [] GetFileSystemEntries (string path, string searchPattern, FileAttributes mask, FileAttributes attrs)
  387. {
  388. if (path == null || searchPattern == null)
  389. throw new ArgumentNullException ();
  390. if (searchPattern.Length == 0)
  391. return new string [] {};
  392. if (path.Trim ().Length == 0)
  393. throw new ArgumentException ("The Path does not have a valid format");
  394. string wild = Path.Combine (path, searchPattern);
  395. string wildpath = Path.GetDirectoryName (wild);
  396. if (wildpath.IndexOfAny (Path.InvalidPathChars) != -1)
  397. throw new ArgumentException ("Path contains invalid characters");
  398. if (wildpath.IndexOfAny (Path.InvalidPathChars) != -1) {
  399. if (path.IndexOfAny (SearchPattern.InvalidChars) == -1)
  400. throw new ArgumentException ("Path contains invalid characters", "path");
  401. throw new ArgumentException ("Pattern contains invalid characters", "pattern");
  402. }
  403. MonoIOError error;
  404. if (!MonoIO.ExistsDirectory (wildpath, out error)) {
  405. if (error == MonoIOError.ERROR_SUCCESS) {
  406. MonoIOError file_error;
  407. if (MonoIO.ExistsFile (wildpath, out file_error)) {
  408. return new string [] { wildpath };
  409. }
  410. }
  411. if (error != MonoIOError.ERROR_PATH_NOT_FOUND)
  412. throw MonoIO.GetException (wildpath, error);
  413. if (wildpath.IndexOfAny (SearchPattern.WildcardChars) == -1)
  414. throw new DirectoryNotFoundException ("Directory '" + wildpath + "' not found.");
  415. if (path.IndexOfAny (SearchPattern.WildcardChars) == -1)
  416. throw new ArgumentException ("Pattern is invalid", "searchPattern");
  417. throw new ArgumentException ("Path is invalid", "path");
  418. }
  419. string path_with_pattern = Path.Combine (wildpath, searchPattern);
  420. string [] result = MonoIO.GetFileSystemEntries (path, path_with_pattern, (int) attrs, (int) mask, out error);
  421. if (error != 0)
  422. throw MonoIO.GetException (wildpath, error);
  423. return result;
  424. }
  425. #if !NET_2_1 || MONOTOUCH
  426. [MonoNotSupported ("DirectorySecurity isn't implemented")]
  427. public static DirectorySecurity GetAccessControl (string path, AccessControlSections includeSections)
  428. {
  429. throw new PlatformNotSupportedException ();
  430. }
  431. [MonoNotSupported ("DirectorySecurity isn't implemented")]
  432. public static DirectorySecurity GetAccessControl (string path)
  433. {
  434. throw new PlatformNotSupportedException ();
  435. }
  436. #endif
  437. }
  438. }