2 // Mono.Unix/UnixFile.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.
36 public struct UnixPipes
38 public UnixPipes (UnixStream reading, UnixStream writing)
44 public UnixStream Reading;
45 public UnixStream Writing;
48 public sealed /* static */ class UnixFile
50 private UnixFile () {}
52 public static bool CanAccess (string path, AccessMode mode)
54 int r = Syscall.access (path, mode);
58 public static void Delete (string path)
60 int r = Syscall.unlink (path);
61 UnixMarshal.ThrowExceptionForLastErrorIf (r);
64 public static bool Exists (string path)
66 int r = Syscall.access (path, AccessMode.F_OK);
72 public static long GetConfigurationValue (string path, PathConf name)
74 Syscall.SetLastError ((Error) 0);
75 long r = Syscall.pathconf (path, name);
76 if (r == -1 && Syscall.GetLastError() != (Error) 0)
77 UnixMarshal.ThrowExceptionForLastError ();
81 public static DateTime GetLastAccessTime (string path)
83 return new UnixFileInfo (path).LastAccessTime;
86 public static Stat GetFileStatus (string path)
89 int r = Syscall.stat (path, out stat);
90 UnixMarshal.ThrowExceptionForLastErrorIf (r);
94 public static DateTime GetLastWriteTime (string path)
96 return new UnixFileInfo(path).LastWriteTime;
99 public static DateTime GetLastStatusChangeTime (string path)
101 return new UnixFileInfo (path).LastStatusChangeTime;
104 public static FilePermissions GetPermissions (string path)
106 return new UnixFileInfo (path).Permissions;
109 public static string ReadLink (string path)
111 string r = TryReadLink (path);
113 UnixMarshal.ThrowExceptionForLastError ();
117 public static string TryReadLink (string path)
119 // Who came up with readlink(2)? There doesn't seem to be a way to
120 // properly handle it.
121 StringBuilder sb = new StringBuilder (512);
122 int r = Syscall.readlink (path, sb);
125 return sb.ToString (0, r);
128 public static void SetPermissions (string path, FilePermissions perms)
130 int r = Syscall.chmod (path, perms);
131 UnixMarshal.ThrowExceptionForLastErrorIf (r);
134 public static UnixStream Create (string path)
136 FilePermissions mode = // 0644
137 FilePermissions.S_IRUSR | FilePermissions.S_IWUSR |
138 FilePermissions.S_IRGRP | FilePermissions.S_IROTH;
139 return Create (path, mode);
142 public static UnixStream Create (string path, FilePermissions mode)
144 int fd = Syscall.creat (path, mode);
146 UnixMarshal.ThrowExceptionForLastError ();
147 return new UnixStream (fd);
150 public static UnixPipes CreatePipes ()
152 int reading, writing;
153 int r = Syscall.pipe (out reading, out writing);
154 UnixMarshal.ThrowExceptionForLastErrorIf (r);
155 return new UnixPipes (new UnixStream (reading), new UnixStream (writing));
158 public static UnixStream Open (string path, OpenFlags flags)
160 int fd = Syscall.open (path, flags);
162 UnixMarshal.ThrowExceptionForLastError ();
163 return new UnixStream (fd);
166 public static UnixStream Open (string path, OpenFlags flags, FilePermissions mode)
168 int fd = Syscall.open (path, flags, mode);
170 UnixMarshal.ThrowExceptionForLastError ();
171 return new UnixStream (fd);
174 public static UnixStream Open (string path, FileMode mode)
176 OpenFlags flags = UnixConvert.ToOpenFlags (mode, FileAccess.ReadWrite);
177 int fd = Syscall.open (path, flags);
179 UnixMarshal.ThrowExceptionForLastError ();
180 return new UnixStream (fd);
183 public static UnixStream Open (string path, FileMode mode, FileAccess access)
185 OpenFlags flags = UnixConvert.ToOpenFlags (mode, access);
186 int fd = Syscall.open (path, flags);
188 UnixMarshal.ThrowExceptionForLastError ();
189 return new UnixStream (fd);
192 public static UnixStream Open (string path, FileMode mode, FileAccess access, FilePermissions perms)
194 OpenFlags flags = UnixConvert.ToOpenFlags (mode, access);
195 int fd = Syscall.open (path, flags, perms);
197 UnixMarshal.ThrowExceptionForLastError ();
198 return new UnixStream (fd);
201 public static UnixStream OpenRead (string path)
203 return Open (path, FileMode.Open, FileAccess.Read);
206 public static UnixStream OpenWrite (string path)
208 return Open (path, FileMode.OpenOrCreate, FileAccess.Write);
211 public static void SetOwner (string path, uint owner, uint group)
213 int r = Syscall.chown (path, owner, group);
214 UnixMarshal.ThrowExceptionForLastErrorIf (r);
217 public static void SetOwner (string path, string owner)
219 Passwd pw = Syscall.getpwnam (owner);
221 throw new ArgumentException (Locale.GetText ("invalid username"), "owner");
222 uint uid = pw.pw_uid;
223 uint gid = pw.pw_gid;
224 SetOwner (path, uid, gid);
227 public static void SetOwner (string path, string owner, string group)
229 uint uid = UnixUser.GetUserId (owner);
230 uint gid = UnixGroup.GetGroupId (group);
232 SetOwner (path, uid, gid);
235 public static void SetLinkOwner (string path, uint owner, uint group)
237 int r = Syscall.lchown (path, owner, group);
238 UnixMarshal.ThrowExceptionForLastErrorIf (r);
241 public static void SetLinkOwner (string path, string owner)
243 Passwd pw = Syscall.getpwnam (owner);
245 throw new ArgumentException (Locale.GetText ("invalid username"), "owner");
246 uint uid = pw.pw_uid;
247 uint gid = pw.pw_gid;
248 SetLinkOwner (path, uid, gid);
251 public static void SetLinkOwner (string path, string owner, string group)
253 uint uid = UnixUser.GetUserId (owner);
254 uint gid = UnixGroup.GetGroupId (group);
256 SetLinkOwner (path, uid, gid);
259 public static void AdviseNormalAccess (int fd, long offset, long len)
261 int r = Syscall.posix_fadvise (fd, offset, len,
262 PosixFadviseAdvice.POSIX_FADV_NORMAL);
263 UnixMarshal.ThrowExceptionForLastErrorIf (r);
266 public static void AdviseNormalAccess (int fd)
268 AdviseNormalAccess (fd, 0, 0);
271 public static void AdviseNormalAccess (FileStream file, long offset, long len)
273 AdviseNormalAccess (file.Handle.ToInt32(), offset, len);
276 public static void AdviseNormalAccess (FileStream file)
278 AdviseNormalAccess (file.Handle.ToInt32());
281 public static void AdviseNormalAccess (UnixStream stream, long offset, long len)
283 AdviseNormalAccess (stream.Handle, offset, len);
286 public static void AdviseNormalAccess (UnixStream stream)
288 AdviseNormalAccess (stream.Handle);
291 public static void AdviseSequentialAccess (int fd, long offset, long len)
293 int r = Syscall.posix_fadvise (fd, offset, len,
294 PosixFadviseAdvice.POSIX_FADV_SEQUENTIAL);
295 UnixMarshal.ThrowExceptionForLastErrorIf (r);
298 public static void AdviseSequentialAccess (int fd)
300 AdviseSequentialAccess (fd, 0, 0);
303 public static void AdviseSequentialAccess (FileStream file, long offset, long len)
305 AdviseSequentialAccess (file.Handle.ToInt32(), offset, len);
308 public static void AdviseSequentialAccess (FileStream file)
310 AdviseSequentialAccess (file.Handle.ToInt32());
313 public static void AdviseSequentialAccess (UnixStream stream, long offset, long len)
315 AdviseSequentialAccess (stream.Handle, offset, len);
318 public static void AdviseSequentialAccess (UnixStream stream)
320 AdviseSequentialAccess (stream.Handle);
323 public static void AdviseRandomAccess (int fd, long offset, long len)
325 int r = Syscall.posix_fadvise (fd, offset, len,
326 PosixFadviseAdvice.POSIX_FADV_RANDOM);
327 UnixMarshal.ThrowExceptionForLastErrorIf (r);
330 public static void AdviseRandomAccess (int fd)
332 AdviseRandomAccess (fd, 0, 0);
335 public static void AdviseRandomAccess (FileStream file, long offset, long len)
337 AdviseRandomAccess (file.Handle.ToInt32(), offset, len);
340 public static void AdviseRandomAccess (FileStream file)
342 AdviseRandomAccess (file.Handle.ToInt32());
345 public static void AdviseRandomAccess (UnixStream stream, long offset, long len)
347 AdviseRandomAccess (stream.Handle, offset, len);
350 public static void AdviseRandomAccess (UnixStream stream)
352 AdviseRandomAccess (stream.Handle);
355 public static void AdviseNeedAccess (int fd, long offset, long len)
357 int r = Syscall.posix_fadvise (fd, offset, len,
358 PosixFadviseAdvice.POSIX_FADV_WILLNEED);
359 UnixMarshal.ThrowExceptionForLastErrorIf (r);
362 public static void AdviseNeedAccess (int fd)
364 AdviseNeedAccess (fd, 0, 0);
367 public static void AdviseNeedAccess (FileStream file, long offset, long len)
369 AdviseNeedAccess (file.Handle.ToInt32(), offset, len);
372 public static void AdviseNeedAccess (FileStream file)
374 AdviseNeedAccess (file.Handle.ToInt32());
377 public static void AdviseNeedAccess (UnixStream stream, long offset, long len)
379 AdviseNeedAccess (stream.Handle, offset, len);
382 public static void AdviseNeedAccess (UnixStream stream)
384 AdviseNeedAccess (stream.Handle);
387 public static void AdviseNoAccess (int fd, long offset, long len)
389 int r = Syscall.posix_fadvise (fd, offset, len,
390 PosixFadviseAdvice.POSIX_FADV_DONTNEED);
391 UnixMarshal.ThrowExceptionForLastErrorIf (r);
394 public static void AdviseNoAccess (int fd)
396 AdviseNoAccess (fd, 0, 0);
399 public static void AdviseNoAccess (FileStream file, long offset, long len)
401 AdviseNoAccess (file.Handle.ToInt32(), offset, len);
404 public static void AdviseNoAccess (FileStream file)
406 AdviseNoAccess (file.Handle.ToInt32());
409 public static void AdviseNoAccess (UnixStream stream, long offset, long len)
411 AdviseNoAccess (stream.Handle, offset, len);
414 public static void AdviseNoAccess (UnixStream stream)
416 AdviseNoAccess (stream.Handle);
419 public static void AdviseOnceAccess (int fd, long offset, long len)
421 int r = Syscall.posix_fadvise (fd, offset, len,
422 PosixFadviseAdvice.POSIX_FADV_NOREUSE);
423 UnixMarshal.ThrowExceptionForLastErrorIf (r);
426 public static void AdviseOnceAccess (int fd)
428 AdviseOnceAccess (fd, 0, 0);
431 public static void AdviseOnceAccess (FileStream file, long offset, long len)
433 AdviseOnceAccess (file.Handle.ToInt32(), offset, len);
436 public static void AdviseOnceAccess (FileStream file)
438 AdviseOnceAccess (file.Handle.ToInt32());
441 public static void AdviseOnceAccess (UnixStream stream, long offset, long len)
443 AdviseOnceAccess (stream.Handle, offset, len);
446 public static void AdviseOnceAccess (UnixStream stream)
448 AdviseOnceAccess (stream.Handle);