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 class UnixDirectoryInfo : UnixFileSystemInfo
40 public UnixDirectoryInfo (string path)
45 internal UnixDirectoryInfo (string path, Stat stat)
50 public void Create (FilePermissions mode)
52 int r = Syscall.mkdir (Path, mode);
53 UnixMarshal.ThrowExceptionForLastErrorIf (r);
59 FilePermissions mode = FilePermissions.ACCESSPERMS;
63 public override void Delete ()
68 public void Delete (bool recursive)
71 foreach (UnixFileSystemInfo e in GetFileSystemEntries ()) {
72 UnixDirectoryInfo d = e as UnixDirectoryInfo;
79 int r = Syscall.rmdir (Path);
80 UnixMarshal.ThrowExceptionForLastErrorIf (r);
84 public Dirent[] GetEntries ()
86 IntPtr dirp = Syscall.opendir (Path);
87 if (dirp == IntPtr.Zero)
88 UnixMarshal.ThrowExceptionForLastError ();
90 bool complete = false;
92 Dirent[] entries = GetEntries (dirp);
97 int r = Syscall.closedir (dirp);
98 // don't throw an exception if an exception is in progress
100 UnixMarshal.ThrowExceptionForLastErrorIf (r);
104 private static Dirent[] GetEntries (IntPtr dirp)
106 ArrayList entries = new ArrayList ();
111 Dirent d = new Dirent ();
112 r = Syscall.readdir_r (dirp, d, out result);
113 if (r == 0 && result != IntPtr.Zero)
114 // don't include current & parent dirs
115 if (d.d_name != "." && d.d_name != "..")
117 } while (r == 0 && result != IntPtr.Zero);
119 UnixMarshal.ThrowExceptionForLastErrorIf (r);
121 return (Dirent[]) entries.ToArray (typeof(Dirent));
124 public Dirent[] GetEntries (Regex regex)
126 IntPtr dirp = Syscall.opendir (Path);
127 if (dirp == IntPtr.Zero)
128 UnixMarshal.ThrowExceptionForLastError ();
131 return GetEntries (dirp, regex);
134 int r = Syscall.closedir (dirp);
135 UnixMarshal.ThrowExceptionForLastErrorIf (r);
139 private static Dirent[] GetEntries (IntPtr dirp, Regex regex)
141 ArrayList entries = new ArrayList ();
146 Dirent d = new Dirent ();
147 r = Syscall.readdir_r (dirp, d, out result);
148 if (r == 0 && result != IntPtr.Zero && regex.Match (d.d_name).Success) {
149 // don't include current & parent dirs
150 if (d.d_name != "." && d.d_name != "..")
153 } while (r == 0 && result != IntPtr.Zero);
155 UnixMarshal.ThrowExceptionForLastError ();
157 return (Dirent[]) entries.ToArray (typeof(Dirent));
160 public Dirent[] GetEntries (string regex)
162 Regex re = new Regex (regex);
163 return GetEntries (re);
166 public UnixFileSystemInfo[] GetFileSystemEntries ()
168 Dirent[] dentries = GetEntries ();
169 return GetFileSystemEntries (dentries);
172 private UnixFileSystemInfo[] GetFileSystemEntries (Dirent[] dentries)
174 UnixFileSystemInfo[] entries = new UnixFileSystemInfo[dentries.Length];
175 for (int i = 0; i != entries.Length; ++i)
176 entries [i] = UnixFileSystemInfo.Create (dentries[i].d_name);
180 public UnixFileSystemInfo[] GetFileSystemEntries (Regex regex)
182 Dirent[] dentries = GetEntries (regex);
183 return GetFileSystemEntries (dentries);
186 public UnixFileSystemInfo[] GetFileSystemEntries (string regex)
188 Regex re = new Regex (regex);
189 return GetFileSystemEntries (re);