Drop more obsolete defines
[mono.git] / mcs / class / corlib / System.IO / DirectoryInfo.cs
1 // 
2 // System.IO.DirectoryInfo.cs 
3 //
4 // Authors:
5 //   Miguel de Icaza, miguel@ximian.com
6 //   Jim Richardson, develop@wtfo-guru.com
7 //   Dan Lewis, dihlewis@yahoo.co.uk
8 //   Sebastien Pouliot  <sebastien@ximian.com>
9 //
10 // Copyright (C) 2002 Ximian, Inc.
11 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
12 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Collections;
35 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
37 using System.Text;
38 #if !NET_2_1
39 using System.Security.AccessControl;
40 #endif
41
42 namespace System.IO {
43         
44         [Serializable]
45         [ComVisible (true)]
46         public sealed class DirectoryInfo : FileSystemInfo {
47
48                 private string current;
49                 private string parent;
50         
51                 public DirectoryInfo (string path) : this (path, false)
52                 {
53                 }
54
55                 internal DirectoryInfo (string path, bool simpleOriginalPath)
56                 {
57                         CheckPath (path);
58
59                         FullPath = Path.GetFullPath (path);
60                         if (simpleOriginalPath)
61                                 OriginalPath = Path.GetFileName (path);
62                         else
63                                 OriginalPath = path;
64
65                         Initialize ();
66                 }
67
68                 private DirectoryInfo (SerializationInfo info, StreamingContext context)
69                         : base (info, context)
70                 {
71                         Initialize ();
72                 }
73
74                 void Initialize ()
75                 {
76                         int len = FullPath.Length - 1;
77                         if ((len > 1) && (FullPath [len] == Path.DirectorySeparatorChar))
78                                 len--;
79                         int last = FullPath.LastIndexOf (Path.DirectorySeparatorChar, len);
80                         if ((last == -1) || ((last == 0) && (len == 0))) {
81                                 current = FullPath;
82                                 parent = null;
83                         } else {
84                                 current = FullPath.Substring (last + 1, len - last);
85                                 if (last == 0 && !Environment.IsRunningOnWindows)
86                                         parent = Path.DirectorySeparatorStr;
87                                 else
88                                         parent = FullPath.Substring (0, last);
89                                 // adjust for drives, i.e. a special case for windows
90                                 if (Environment.IsRunningOnWindows) {
91                                         if ((parent.Length == 2) && (parent [1] == ':') && Char.IsLetter (parent [0]))
92                                                 parent += Path.DirectorySeparatorChar;
93                                 }
94                         }
95                 }
96
97                 // properties
98
99                 public override bool Exists {
100                         get {
101                                 Refresh (false);
102
103                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)
104                                         return false;
105
106                                 if ((stat.Attributes & FileAttributes.Directory) == 0)
107                                         return false;
108
109                                 return true;
110                         }
111                 }
112
113                 public override string Name {
114                         get { return current; }
115                 }
116
117                 public DirectoryInfo Parent {
118                         get {
119                                 if ((parent == null) || (parent.Length == 0))
120                                         return null;
121                                 return new DirectoryInfo (parent);
122                         }
123                 }
124
125                 public DirectoryInfo Root {
126                         get {
127                                 string root = Path.GetPathRoot (FullPath);
128                                 if (root == null)
129                                         return null;
130
131                                 return new DirectoryInfo (root);
132                         }
133                 }
134
135                 // creational methods
136
137                 public void Create ()
138                 {
139                         Directory.CreateDirectory (FullPath);
140                 }
141
142                 public DirectoryInfo CreateSubdirectory (string path)
143                 {
144                         CheckPath (path);
145
146                         path = Path.Combine (FullPath, path);
147                         Directory.CreateDirectory (path);
148                         return new DirectoryInfo (path);
149                 }
150
151                 // directory listing methods
152
153                 public FileInfo [] GetFiles ()
154                 {
155                         return GetFiles ("*");
156                 }
157
158                 public FileInfo [] GetFiles (string searchPattern)
159                 {
160                         if (searchPattern == null)
161                                 throw new ArgumentNullException ("searchPattern");
162
163                         string [] names = Directory.GetFiles (FullPath, searchPattern);
164
165                         FileInfo[] infos = new FileInfo [names.Length];
166                         int i = 0;
167                         foreach (string name in names)
168                                 infos [i++] = new FileInfo (name);
169
170                         return infos;
171                 }
172
173                 public DirectoryInfo [] GetDirectories ()
174                 {
175                         return GetDirectories ("*");
176                 }
177
178                 public DirectoryInfo [] GetDirectories (string searchPattern)
179                 {
180                         if (searchPattern == null)
181                                 throw new ArgumentNullException ("searchPattern");
182
183                         string [] names = Directory.GetDirectories (FullPath, searchPattern);
184
185                         DirectoryInfo[] infos = new DirectoryInfo [names.Length];
186                         int i = 0;
187                         foreach (string name in names)
188                                 infos [i++] = new DirectoryInfo (name);
189
190                         return infos;
191                 }
192
193                 public FileSystemInfo [] GetFileSystemInfos ()
194                 {
195                         return GetFileSystemInfos ("*");
196                 }
197
198                 public FileSystemInfo [] GetFileSystemInfos (string searchPattern)
199                 {
200                         if (searchPattern == null)
201                                 throw new ArgumentNullException ("searchPattern");
202
203                         if (!Directory.Exists (FullPath))
204                                 throw new IOException ("Invalid directory");
205                         string [] dirs = Directory.GetDirectories (FullPath, searchPattern);
206                         string [] files = Directory.GetFiles (FullPath, searchPattern);
207
208                         FileSystemInfo[] infos = new FileSystemInfo [dirs.Length + files.Length];
209                         int i = 0;
210                         foreach (string dir in dirs)
211                                 infos [i++] = new DirectoryInfo (dir);
212                         foreach (string file in files)
213                                 infos [i++] = new FileInfo (file);
214
215                         return infos;
216                 }
217
218                 // directory management methods
219
220                 public override void Delete ()
221                 {
222                         Delete (false);
223                 }
224
225                 public void Delete (bool recursive)
226                 {
227                         Directory.Delete (FullPath, recursive);
228                 }
229
230                 public void MoveTo (string destDirName)
231                 {
232                         if (destDirName == null)
233                                 throw new ArgumentNullException ("destDirName");
234                         if (destDirName.Length == 0)
235                                 throw new ArgumentException ("An empty file name is not valid.", "destDirName");
236
237                         Directory.Move (FullPath, Path.GetFullPath (destDirName));
238                 }
239
240                 public override string ToString ()
241                 {
242                         return OriginalPath;
243                 }
244
245 #if !NET_2_1
246                 public DirectoryInfo[] GetDirectories (string searchPattern, SearchOption searchOption)
247                 {
248                         switch (searchOption) {
249                         case SearchOption.TopDirectoryOnly:
250                                 return GetDirectories (searchPattern);
251                         case SearchOption.AllDirectories:
252                                 Queue workq = new Queue(GetDirectories(searchPattern));
253                                 Queue doneq = new Queue();
254                                 while (workq.Count > 0)
255                                         {
256                                                 DirectoryInfo cinfo = (DirectoryInfo) workq.Dequeue();
257                                                 DirectoryInfo[] cinfoDirs = cinfo.GetDirectories(searchPattern);
258                                                 foreach (DirectoryInfo i in cinfoDirs) workq.Enqueue(i);
259                                                 doneq.Enqueue(cinfo);
260                                         }
261
262                                 DirectoryInfo[] infos = new DirectoryInfo[doneq.Count];
263                                 doneq.CopyTo(infos, 0);
264                                 return infos;
265                         default:
266                                 string msg = Locale.GetText ("Invalid enum value '{0}' for '{1}'.", searchOption, "SearchOption");
267                                 throw new ArgumentOutOfRangeException ("searchOption", msg);
268                         }
269                 }       
270
271                 internal int GetFilesSubdirs (ArrayList l, string pattern)
272                 {
273                         int count;
274                         FileInfo [] thisdir = null;
275
276                         try {
277                                 thisdir = GetFiles (pattern);
278                         } catch (System.UnauthorizedAccessException){
279                                 return 0;
280                         }
281                         
282                         count = thisdir.Length;
283                         l.Add (thisdir);
284
285                         foreach (DirectoryInfo subdir in GetDirectories ()){
286                                 count += subdir.GetFilesSubdirs (l, pattern);
287                         }
288                         return count;
289                 }
290                 
291                 public FileInfo[] GetFiles (string searchPattern, SearchOption searchOption)
292                 {
293                         switch (searchOption) {
294                         case SearchOption.TopDirectoryOnly:
295                                 return GetFiles (searchPattern);
296                         case SearchOption.AllDirectories: {
297                                 ArrayList groups = new ArrayList ();
298                                 int count = GetFilesSubdirs (groups, searchPattern);
299                                 int current = 0;
300                                 
301                                 FileInfo [] all = new FileInfo [count];
302                                 foreach (FileInfo [] p in groups){
303                                         p.CopyTo (all, current);
304                                         current += p.Length;
305                                 }
306                                 return all;
307                         }
308                         default:
309                                 string msg = Locale.GetText ("Invalid enum value '{0}' for '{1}'.", searchOption, "SearchOption");
310                                 throw new ArgumentOutOfRangeException ("searchOption", msg);
311                         }
312                 }
313
314                 // access control methods
315
316                 [MonoLimitation ("DirectorySecurity isn't implemented")]
317                 public void Create (DirectorySecurity directorySecurity)
318                 {
319                         if (directorySecurity != null)
320                                 throw new UnauthorizedAccessException ();
321                         Create ();
322                 }
323
324                 [MonoLimitation ("DirectorySecurity isn't implemented")]
325                 public DirectoryInfo CreateSubdirectory (string path, DirectorySecurity directorySecurity)
326                 {
327                         if (directorySecurity != null)
328                                 throw new UnauthorizedAccessException ();
329                         return CreateSubdirectory (path);
330                 }
331
332                 [MonoNotSupported ("DirectorySecurity isn't implemented")]
333                 public DirectorySecurity GetAccessControl ()
334                 {
335                         throw new UnauthorizedAccessException ();
336                 }
337
338                 [MonoNotSupported ("DirectorySecurity isn't implemented")]
339                 public DirectorySecurity GetAccessControl (AccessControlSections includeSections)
340                 {
341                         throw new UnauthorizedAccessException ();
342                 }
343
344                 [MonoLimitation ("DirectorySecurity isn't implemented")]
345                 public void SetAccessControl (DirectorySecurity directorySecurity)
346                 {
347                         if (directorySecurity != null)
348                                 throw new ArgumentNullException ("directorySecurity");
349                         throw new UnauthorizedAccessException ();
350                 }
351 #endif
352         }
353 }