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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
//
// System.IO.Directory.cs
//
// Authors:
// Jim Richardson (develop@wtfo-guru.com)
// Miguel de Icaza (miguel@ximian.com)
// Dan Lewis (dihlewis@yahoo.co.uk)
// Eduardo Garcia (kiwnix@yahoo.es)
// Ville Palo (vi64pa@kolumbus.fi)
//
// Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
// Copyright (C) 2002 Ximian, Inc.
//
// Created: Monday, August 13, 2001
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections;
using System.Security;
using System.Security.Permissions;
using System.Text;
#if NET_2_0 && !NET_2_1
using System.Security.AccessControl;
#endif
#if NET_2_0
using System.Runtime.InteropServices;
#endif
namespace System.IO
{
#if NET_2_0
[ComVisible (true)]
#endif
public
#if NET_2_0
static
#else
sealed
#endif
class Directory
{
#if !NET_2_0
private Directory () {}
#endif
public static DirectoryInfo CreateDirectory (string path)
{
if (path == null)
throw new ArgumentNullException ("path");
if (path.Length == 0)
throw new ArgumentException ("Path is empty");
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid chars");
if (path.Trim ().Length == 0)
throw new ArgumentException ("Only blank characters in path");
#if NET_2_0
if (File.Exists(path))
throw new IOException ("Cannot create " + path + " because a file with the same name already exists.");
#endif
// LAMESPEC: with .net 1.0 version this throw NotSupportedException and msdn says so too
// but v1.1 throws ArgumentException.
if (path == ":")
throw new ArgumentException ("Only ':' In path");
return CreateDirectoriesInternal (path);
}
#if NET_2_0 && !NET_2_1
[MonoTODO ("DirectorySecurity not implemented")]
public static DirectoryInfo CreateDirectory (string path, DirectorySecurity directorySecurity)
{
return(CreateDirectory (path));
}
#endif
static DirectoryInfo CreateDirectoriesInternal (string path)
{
#if !NET_2_1
if (SecurityManager.SecurityEnabled) {
new FileIOPermission (FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, path).Demand ();
}
#endif
DirectoryInfo info = new DirectoryInfo (path, true);
if (info.Parent != null && !info.Parent.Exists)
info.Parent.Create ();
MonoIOError error;
if (!MonoIO.CreateDirectory (path, out error)) {
// LAMESPEC: 1.1 and 1.2alpha allow CreateDirectory on a file path.
// So CreateDirectory ("/tmp/somefile") will succeed if 'somefile' is
// not a directory. However, 1.0 will throw an exception.
// We behave like 1.0 here (emulating 1.1-like behavior is just a matter
// of comparing error to ERROR_FILE_EXISTS, but it's lame to do:
// DirectoryInfo di = Directory.CreateDirectory (something);
// and having di.Exists return false afterwards.
// I hope we don't break anyone's code, as they should be catching
// the exception anyway.
if (error != MonoIOError.ERROR_ALREADY_EXISTS &&
error != MonoIOError.ERROR_FILE_EXISTS)
throw MonoIO.GetException (path, error);
}
return info;
}
public static void Delete (string path)
{
if (path == null)
throw new ArgumentNullException ("path");
if (path.Length == 0)
throw new ArgumentException ("Path is empty");
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid chars");
if (path.Trim().Length == 0)
throw new ArgumentException ("Only blank characters in path");
if (path == ":")
throw new NotSupportedException ("Only ':' In path");
MonoIOError error;
bool success;
if (MonoIO.ExistsSymlink (path, out error)) {
/* RemoveDirectory maps to rmdir()
* which fails on symlinks (ENOTDIR)
*/
success = MonoIO.DeleteFile (path, out error);
} else {
success = MonoIO.RemoveDirectory (path, out error);
}
if (!success) {
/*
* FIXME:
* In io-layer/io.c rmdir returns error_file_not_found if directory does not exists.
* So maybe this could be handled somewhere else?
*/
if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
if (File.Exists (path))
throw new IOException ("Directory does not exist, but a file of the same name exist.");
else
throw new DirectoryNotFoundException ("Directory does not exist.");
} else
throw MonoIO.GetException (path, error);
}
}
static void RecursiveDelete (string path)
{
MonoIOError error;
foreach (string dir in GetDirectories (path)) {
if (MonoIO.ExistsSymlink (dir, out error)) {
MonoIO.DeleteFile (dir, out error);
} else {
RecursiveDelete (dir);
}
}
foreach (string file in GetFiles (path))
File.Delete (file);
Directory.Delete (path);
}
public static void Delete (string path, bool recursive)
{
CheckPathExceptions (path);
if (recursive)
RecursiveDelete (path);
else
Delete (path);
}
public static bool Exists (string path)
{
if (path == null)
return false;
MonoIOError error;
bool exists;
exists = MonoIO.ExistsDirectory (path, out error);
/* This should not throw exceptions */
return exists;
}
public static DateTime GetLastAccessTime (string path)
{
return File.GetLastAccessTime (path);
}
public static DateTime GetLastAccessTimeUtc (string path)
{
return GetLastAccessTime (path).ToUniversalTime ();
}
public static DateTime GetLastWriteTime (string path)
{
return File.GetLastWriteTime (path);
}
public static DateTime GetLastWriteTimeUtc (string path)
{
return GetLastWriteTime (path).ToUniversalTime ();
}
public static DateTime GetCreationTime (string path)
{
return File.GetCreationTime (path);
}
public static DateTime GetCreationTimeUtc (string path)
{
return GetCreationTime (path).ToUniversalTime ();
}
public static string GetCurrentDirectory ()
{
MonoIOError error;
string result = MonoIO.GetCurrentDirectory (out error);
if (error != MonoIOError.ERROR_SUCCESS)
throw MonoIO.GetException (error);
#if !NET_2_1
if ((result != null) && (result.Length > 0) && SecurityManager.SecurityEnabled) {
new FileIOPermission (FileIOPermissionAccess.PathDiscovery, result).Demand ();
}
#endif
return result;
}
public static string [] GetDirectories (string path)
{
return GetDirectories (path, "*");
}
public static string [] GetDirectories (string path, string searchPattern)
{
return GetFileSystemEntries (path, searchPattern, FileAttributes.Directory, FileAttributes.Directory);
}
#if NET_2_0 && !NET_2_1
public static string [] GetDirectories (string path, string searchPattern, SearchOption searchOption)
{
if (searchOption == SearchOption.TopDirectoryOnly)
return GetDirectories (path, searchPattern);
ArrayList all = new ArrayList ();
GetDirectoriesRecurse (path, searchPattern, all);
return (string []) all.ToArray (typeof (string));
}
static void GetDirectoriesRecurse (string path, string searchPattern, ArrayList all)
{
all.AddRange (GetDirectories (path, searchPattern));
foreach (string dir in GetDirectories (path))
GetDirectoriesRecurse (dir, searchPattern, all);
}
#endif
public static string GetDirectoryRoot (string path)
{
return new String(Path.DirectorySeparatorChar,1);
}
public static string [] GetFiles (string path)
{
return GetFiles (path, "*");
}
public static string [] GetFiles (string path, string searchPattern)
{
return GetFileSystemEntries (path, searchPattern, FileAttributes.Directory, 0);
}
#if NET_2_0 && !NET_2_1
public static string[] GetFiles (string path, string searchPattern, SearchOption searchOption)
{
if (searchOption == SearchOption.TopDirectoryOnly)
return GetFiles (path, searchPattern);
ArrayList all = new ArrayList ();
GetFilesRecurse (path, searchPattern, all);
return (string []) all.ToArray (typeof (string));
}
static void GetFilesRecurse (string path, string searchPattern, ArrayList all)
{
all.AddRange (GetFiles (path, searchPattern));
foreach (string dir in GetDirectories (path))
GetFilesRecurse (dir, searchPattern, all);
}
#endif
public static string [] GetFileSystemEntries (string path)
{
return GetFileSystemEntries (path, "*");
}
public static string [] GetFileSystemEntries (string path, string searchPattern)
{
return GetFileSystemEntries (path, searchPattern, 0, 0);
}
public static string[] GetLogicalDrives ()
{
return Environment.GetLogicalDrives ();
}
static bool IsRootDirectory (string path)
{
// Unix
if (Path.DirectorySeparatorChar == '/' && path == "/")
return true;
// Windows
if (Path.DirectorySeparatorChar == '\\')
if (path.Length == 3 && path.EndsWith (":\\"))
return true;
return false;
}
public static DirectoryInfo GetParent (string path)
{
if (path == null)
throw new ArgumentNullException ("path");
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid characters");
if (path.Length == 0)
throw new ArgumentException ("The Path do not have a valid format");
// return null if the path is the root directory
if (IsRootDirectory (path))
return null;
string parent_name = Path.GetDirectoryName (path);
if (parent_name.Length == 0)
parent_name = GetCurrentDirectory();
return new DirectoryInfo (parent_name);
}
public static void Move (string sourceDirName, string destDirName)
{
if (sourceDirName == null)
throw new ArgumentNullException ("sourceDirName");
if (destDirName == null)
throw new ArgumentNullException ("destDirName");
if (sourceDirName.Trim ().Length == 0 || sourceDirName.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Invalid source directory name: " + sourceDirName, "sourceDirName");
if (destDirName.Trim ().Length == 0 || destDirName.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Invalid target directory name: " + destDirName, "destDirName");
if (sourceDirName == destDirName)
throw new IOException ("Source and destination path must be different.");
if (Exists (destDirName))
throw new IOException (destDirName + " already exists.");
if (!Exists (sourceDirName) && !File.Exists (sourceDirName))
throw new DirectoryNotFoundException (sourceDirName + " does not exist");
MonoIOError error;
if (!MonoIO.MoveFile (sourceDirName, destDirName, out error))
throw MonoIO.GetException (error);
}
#if NET_2_0 && !NET_2_1
public static void SetAccessControl (string path, DirectorySecurity directorySecurity)
{
throw new NotImplementedException ();
}
#endif
public static void SetCreationTime (string path, DateTime creationTime)
{
File.SetCreationTime (path, creationTime);
}
public static void SetCreationTimeUtc (string path, DateTime creationTimeUtc)
{
SetCreationTime (path, creationTimeUtc.ToLocalTime ());
}
[SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
public static void SetCurrentDirectory (string path)
{
if (path == null)
throw new ArgumentNullException ("path");
if (path.Trim ().Length == 0)
throw new ArgumentException ("path string must not be an empty string or whitespace string");
MonoIOError error;
if (!Exists (path))
throw new DirectoryNotFoundException ("Directory \"" +
path + "\" not found.");
MonoIO.SetCurrentDirectory (path, out error);
if (error != MonoIOError.ERROR_SUCCESS)
throw MonoIO.GetException (path, error);
}
public static void SetLastAccessTime (string path, DateTime lastAccessTime)
{
File.SetLastAccessTime (path, lastAccessTime);
}
public static void SetLastAccessTimeUtc (string path, DateTime lastAccessTimeUtc)
{
SetLastAccessTime (path, lastAccessTimeUtc.ToLocalTime ());
}
public static void SetLastWriteTime (string path, DateTime lastWriteTime)
{
File.SetLastWriteTime (path, lastWriteTime);
}
public static void SetLastWriteTimeUtc (string path, DateTime lastWriteTimeUtc)
{
SetLastWriteTime (path, lastWriteTimeUtc.ToLocalTime ());
}
// private
private static void CheckPathExceptions (string path)
{
if (path == null)
throw new System.ArgumentNullException("path");
if (path.Length == 0)
throw new System.ArgumentException("Path is Empty");
if (path.Trim().Length == 0)
throw new ArgumentException ("Only blank characters in path");
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid chars");
}
private static string [] GetFileSystemEntries (string path, string searchPattern, FileAttributes mask, FileAttributes attrs)
{
if (path == null || searchPattern == null)
throw new ArgumentNullException ();
if (searchPattern.Length == 0)
return new string [] {};
if (path.Trim ().Length == 0)
throw new ArgumentException ("The Path does not have a valid format");
string wild = Path.Combine (path, searchPattern);
string wildpath = Path.GetDirectoryName (wild);
if (wildpath.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid characters");
if (wildpath.IndexOfAny (Path.InvalidPathChars) != -1) {
if (path.IndexOfAny (SearchPattern.InvalidChars) == -1)
throw new ArgumentException ("Path contains invalid characters", "path");
throw new ArgumentException ("Pattern contains invalid characters", "pattern");
}
MonoIOError error;
if (!MonoIO.ExistsDirectory (wildpath, out error)) {
if (error == MonoIOError.ERROR_SUCCESS) {
MonoIOError file_error;
if (MonoIO.ExistsFile (wildpath, out file_error)) {
return new string [] { wildpath };
}
}
if (error != MonoIOError.ERROR_PATH_NOT_FOUND)
throw MonoIO.GetException (wildpath, error);
if (wildpath.IndexOfAny (SearchPattern.WildcardChars) == -1)
throw new DirectoryNotFoundException ("Directory '" + wildpath + "' not found.");
if (path.IndexOfAny (SearchPattern.WildcardChars) == -1)
throw new ArgumentException ("Pattern is invalid", "searchPattern");
throw new ArgumentException ("Path is invalid", "path");
}
string path_with_pattern = Path.Combine (wildpath, searchPattern);
string [] result = MonoIO.GetFileSystemEntries (path, path_with_pattern, (int) attrs, (int) mask, out error);
if (error != 0)
throw MonoIO.GetException (wildpath, error);
return result;
}
#if NET_2_0 && !NET_2_1
[MonoNotSupported ("DirectorySecurity isn't implemented")]
public static DirectorySecurity GetAccessControl (string path, AccessControlSections includeSections)
{
throw new PlatformNotSupportedException ();
}
[MonoNotSupported ("DirectorySecurity isn't implemented")]
public static DirectorySecurity GetAccessControl (string path)
{
throw new PlatformNotSupportedException ();
}
#endif
}
}
|