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