| 473 | } |
| 474 | |
| 475 | public static boolean readLink(Path softLink, Path readTo) { |
| 476 | final int len = readTo.size(); |
| 477 | final int bufSize = 1024; |
| 478 | readTo.zeroPad(bufSize); |
| 479 | // readlink copies link target into the give buffer, without null-terminating it |
| 480 | // the buffer therefor is filled with zeroes. It is also possible that buffer is |
| 481 | // not large enough to copy the entire target. We detect this by checking the return |
| 482 | // value. If the value is the same as the buffer size we make an assumption that |
| 483 | // the link target is perhaps longer than the buffer. |
| 484 | |
| 485 | int res = readLink0(softLink.$().ptr(), readTo.$().ptr() + len, bufSize); |
| 486 | if (res > 0 && res < bufSize) { |
| 487 | readTo.trimTo(len + res); |
| 488 | // check if symlink is absolute or relative |
| 489 | if (readTo.byteAt(0) != '/') { |
| 490 | int prefixLen = Utf8s.lastIndexOfAscii(softLink, '/'); |
| 491 | if (prefixLen > 0) { |
| 492 | readTo.prefix(softLink, prefixLen + 1).$(); |
| 493 | } |
| 494 | } |
| 495 | return true; |
| 496 | } |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | public static byte readNonNegativeByte(long fd, long offset) { |
| 501 | return readNonNegativeByte(toOsFd(fd), offset); |