2 // Mono.Unix/UnixDirectoryInfo.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2004-2006 Jonathan Pryor
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
33 using System.Text.RegularExpressions;
38 public sealed class UnixDirectoryInfo : UnixFileSystemInfo
40 public UnixDirectoryInfo (string path)
45 internal UnixDirectoryInfo (string path, Native.Stat stat)
50 public override string Name {
52 string r = UnixPath.GetFileName (FullPath);
53 if (r == null || r.Length == 0)
59 public UnixDirectoryInfo Parent {
61 string dirname = UnixPath.GetDirectoryName (FullPath);
64 return new UnixDirectoryInfo (dirname);
68 public UnixDirectoryInfo Root {
70 string root = UnixPath.GetPathRoot (FullPath);
73 return new UnixDirectoryInfo (root);
77 [CLSCompliant (false)]
78 [Obsolete ("Use Create(Mono.Unix.Native.FilePermissions)")]
79 public void Create (FilePermissions mode)
81 Mono.Unix.Native.FilePermissions _mode =
82 (Mono.Unix.Native.FilePermissions) mode;
86 [CLSCompliant (false)]
87 public void Create (Mono.Unix.Native.FilePermissions mode)
89 int r = Mono.Unix.Native.Syscall.mkdir (FullPath, mode);
90 UnixMarshal.ThrowExceptionForLastErrorIf (r);
94 public void Create (FileAccessPermissions mode)
96 Create ((Native.FilePermissions) mode);
101 Mono.Unix.Native.FilePermissions mode =
102 Mono.Unix.Native.FilePermissions.ACCESSPERMS;
106 public override void Delete ()
111 public void Delete (bool recursive)
114 foreach (UnixFileSystemInfo e in GetFileSystemEntries ()) {
115 UnixDirectoryInfo d = e as UnixDirectoryInfo;
122 int r = Native.Syscall.rmdir (FullPath);
123 UnixMarshal.ThrowExceptionForLastErrorIf (r);
127 public Native.Dirent[] GetEntries ()
129 IntPtr dirp = Native.Syscall.opendir (FullPath);
130 if (dirp == IntPtr.Zero)
131 UnixMarshal.ThrowExceptionForLastError ();
133 bool complete = false;
135 Native.Dirent[] entries = GetEntries (dirp);
140 int r = Native.Syscall.closedir (dirp);
141 // don't throw an exception if an exception is in progress
143 UnixMarshal.ThrowExceptionForLastErrorIf (r);
147 private static Native.Dirent[] GetEntries (IntPtr dirp)
149 ArrayList entries = new ArrayList ();
154 Native.Dirent d = new Native.Dirent ();
155 r = Native.Syscall.readdir_r (dirp, d, out result);
156 if (r == 0 && result != IntPtr.Zero)
157 // don't include current & parent dirs
158 if (d.d_name != "." && d.d_name != "..")
160 } while (r == 0 && result != IntPtr.Zero);
162 UnixMarshal.ThrowExceptionForLastErrorIf (r);
164 return (Native.Dirent[]) entries.ToArray (typeof(Native.Dirent));
167 public Native.Dirent[] GetEntries (Regex regex)
169 IntPtr dirp = Native.Syscall.opendir (FullPath);
170 if (dirp == IntPtr.Zero)
171 UnixMarshal.ThrowExceptionForLastError ();
174 return GetEntries (dirp, regex);
177 int r = Native.Syscall.closedir (dirp);
178 UnixMarshal.ThrowExceptionForLastErrorIf (r);
182 private static Native.Dirent[] GetEntries (IntPtr dirp, Regex regex)
184 ArrayList entries = new ArrayList ();
189 Native.Dirent d = new Native.Dirent ();
190 r = Native.Syscall.readdir_r (dirp, d, out result);
191 if (r == 0 && result != IntPtr.Zero && regex.Match (d.d_name).Success) {
192 // don't include current & parent dirs
193 if (d.d_name != "." && d.d_name != "..")
196 } while (r == 0 && result != IntPtr.Zero);
198 UnixMarshal.ThrowExceptionForLastError ();
200 return (Native.Dirent[]) entries.ToArray (typeof(Native.Dirent));
203 public Native.Dirent[] GetEntries (string regex)
205 Regex re = new Regex (regex);
206 return GetEntries (re);
209 public UnixFileSystemInfo[] GetFileSystemEntries ()
211 Native.Dirent[] dentries = GetEntries ();
212 return GetFileSystemEntries (dentries);
215 private UnixFileSystemInfo[] GetFileSystemEntries (Native.Dirent[] dentries)
217 UnixFileSystemInfo[] entries = new UnixFileSystemInfo[dentries.Length];
218 for (int i = 0; i != entries.Length; ++i)
219 entries [i] = UnixFileSystemInfo.Create (
220 UnixPath.Combine (FullPath, dentries[i].d_name));
224 public UnixFileSystemInfo[] GetFileSystemEntries (Regex regex)
226 Native.Dirent[] dentries = GetEntries (regex);
227 return GetFileSystemEntries (dentries);
230 public UnixFileSystemInfo[] GetFileSystemEntries (string regex)
232 Regex re = new Regex (regex);
233 return GetFileSystemEntries (re);
236 public static string GetCurrentDirectory ()
238 StringBuilder buf = new StringBuilder (16);
239 IntPtr r = IntPtr.Zero;
242 r = Native.Syscall.getcwd (buf, (ulong) buf.Capacity);
243 } while (r == IntPtr.Zero && Native.Syscall.GetLastError() == Native.Errno.ERANGE);
244 if (r == IntPtr.Zero)
245 UnixMarshal.ThrowExceptionForLastError ();
246 return buf.ToString ();
249 public static void SetCurrentDirectory (string path)
251 int r = Native.Syscall.chdir (path);
252 UnixMarshal.ThrowExceptionForLastErrorIf (r);