- int r = Syscall.readlink (path, buf);
- if (r < 0) {
- Error e;
- switch (e = Syscall.GetLastError()) {
- case Error.EINVAL:
- // path isn't a symbolic link
- return null;
- default:
- UnixMarshal.ThrowExceptionForError (e);
- break;
- }
- }
- else if (r == buf.Capacity) {
- buf.Capacity *= 2;
- }
+ long r = Native.Syscall.readlink (path, buf);
+ if (r < 0)
+ return null;
+ else if (r == buf.Length)
+ buf = new byte[checked (buf.LongLength * 2)];
+ else
+ return UnixEncoding.Instance.GetString (buf, 0, checked ((int) r));
+ } while (true);
+ }
+
+ public static string TryReadLinkAt (int dirfd, string path)
+ {
+ byte[] buf = new byte[256];
+ do {
+ long r = Native.Syscall.readlinkat (dirfd, path, buf);
+ if (r < 0)
+ return null;
+ else if (r == buf.Length)
+ buf = new byte[checked (buf.LongLength * 2)];