2 // Mono.Unix/UnixDirectoryInfo.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2004 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, Stat stat)
50 public override string Name {
52 string r = UnixPath.GetFileName (FullPath);
53 if (r == null || r == "")
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 public void Create (FilePermissions mode)
79 int r = Syscall.mkdir (FullPath, mode);
80 UnixMarshal.ThrowExceptionForLastErrorIf (r);
86 FilePermissions mode = FilePermissions.ACCESSPERMS;
90 public override void Delete ()
95 public void Delete (bool recursive)
98 foreach (UnixFileSystemInfo e in GetFileSystemEntries ()) {
99 UnixDirectoryInfo d = e as UnixDirectoryInfo;
106 int r = Syscall.rmdir (FullPath);
107 UnixMarshal.ThrowExceptionForLastErrorIf (r);
111 public Dirent[] GetEntries ()
113 IntPtr dirp = Syscall.opendir (FullPath);
114 if (dirp == IntPtr.Zero)
115 UnixMarshal.ThrowExceptionForLastError ();
117 bool complete = false;
119 Dirent[] entries = GetEntries (dirp);
124 int r = Syscall.closedir (dirp);
125 // don't throw an exception if an exception is in progress
127 UnixMarshal.ThrowExceptionForLastErrorIf (r);
131 private static Dirent[] GetEntries (IntPtr dirp)
133 ArrayList entries = new ArrayList ();
138 Dirent d = new Dirent ();
139 r = Syscall.readdir_r (dirp, d, out result);
140 if (r == 0 && result != IntPtr.Zero)
141 // don't include current & parent dirs
142 if (d.d_name != "." && d.d_name != "..")
144 } while (r == 0 && result != IntPtr.Zero);
146 UnixMarshal.ThrowExceptionForLastErrorIf (r);
148 return (Dirent[]) entries.ToArray (typeof(Dirent));
151 public Dirent[] GetEntries (Regex regex)
153 IntPtr dirp = Syscall.opendir (FullPath);
154 if (dirp == IntPtr.Zero)
155 UnixMarshal.ThrowExceptionForLastError ();
158 return GetEntries (dirp, regex);
161 int r = Syscall.closedir (dirp);
162 UnixMarshal.ThrowExceptionForLastErrorIf (r);
166 private static Dirent[] GetEntries (IntPtr dirp, Regex regex)
168 ArrayList entries = new ArrayList ();
173 Dirent d = new Dirent ();
174 r = Syscall.readdir_r (dirp, d, out result);
175 if (r == 0 && result != IntPtr.Zero && regex.Match (d.d_name).Success) {
176 // don't include current & parent dirs
177 if (d.d_name != "." && d.d_name != "..")
180 } while (r == 0 && result != IntPtr.Zero);
182 UnixMarshal.ThrowExceptionForLastError ();
184 return (Dirent[]) entries.ToArray (typeof(Dirent));
187 public Dirent[] GetEntries (string regex)
189 Regex re = new Regex (regex);
190 return GetEntries (re);
193 public UnixFileSystemInfo[] GetFileSystemEntries ()
195 Dirent[] dentries = GetEntries ();
196 return GetFileSystemEntries (dentries);
199 private UnixFileSystemInfo[] GetFileSystemEntries (Dirent[] dentries)
201 UnixFileSystemInfo[] entries = new UnixFileSystemInfo[dentries.Length];
202 for (int i = 0; i != entries.Length; ++i)
203 entries [i] = UnixFileSystemInfo.Create (dentries[i].d_name);
207 public UnixFileSystemInfo[] GetFileSystemEntries (Regex regex)
209 Dirent[] dentries = GetEntries (regex);
210 return GetFileSystemEntries (dentries);
213 public UnixFileSystemInfo[] GetFileSystemEntries (string regex)
215 Regex re = new Regex (regex);
216 return GetFileSystemEntries (re);