1ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes/* 2ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Copyright (C) 2011 The Android Open Source Project 3ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * 4ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * you may not use this file except in compliance with the License. 6ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * You may obtain a copy of the License at 7ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * 8ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * 10ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * See the License for the specific language governing permissions and 14ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes * limitations under the License. 15ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes */ 16ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 17ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughespackage libcore.io; 18ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 195d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.ErrnoException; 205d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.GaiException; 215d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructAddrinfo; 225744e4a762ca12243b47dd8aed24d87d963b4d3bAndreas Gampeimport android.system.StructCapUserData; 235744e4a762ca12243b47dd8aed24d87d963b4d3bAndreas Gampeimport android.system.StructCapUserHeader; 245d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructFlock; 255d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructGroupReq; 265d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructGroupSourceReq; 276d1649c5b37b21ef2b690bc0f86306e19a32db02Yi Kongimport android.system.StructIfaddrs; 285d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructLinger; 295d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructPasswd; 305d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructPollfd; 315d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructStat; 325d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructStatVfs; 335d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructTimeval; 345d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructUcred; 355d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructUtsname; 365d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.util.MutableInt; 375d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.util.MutableLong; 3852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesimport java.io.FileDescriptor; 39f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fullerimport java.io.InterruptedIOException; 400a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesimport java.net.InetAddress; 4123ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughesimport java.net.InetSocketAddress; 420a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesimport java.net.SocketAddress; 4326010ab930a2cee3bf10b9612cf070183c21228bElliott Hughesimport java.net.SocketException; 4426c7025a7a919044771fb89031161bd26fe03032Elliott Hughesimport java.nio.ByteBuffer; 454af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughesimport java.nio.NioUtils; 4652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 470965ac3ceff49f3c090c417c0e30ea51c6f294a7Andreas Gampepublic final class Linux implements Os { 480965ac3ceff49f3c090c417c0e30ea51c6f294a7Andreas Gampe Linux() { } 49ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 500ab1a26ca767ae36fbbe27b62893670b208fa494Neil Fuller public native FileDescriptor accept(FileDescriptor fd, SocketAddress peerAddress) throws ErrnoException, SocketException; 51ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes public native boolean access(String path, int mode) throws ErrnoException; 523e58734d651080009c9190c7062837fca5c7cf4ePaul Jensen public native InetAddress[] android_getaddrinfo(String node, StructAddrinfo hints, int netId) throws GaiException; 5326010ab930a2cee3bf10b9612cf070183c21228bElliott Hughes public native void bind(FileDescriptor fd, InetAddress address, int port) throws ErrnoException, SocketException; 548f5b46d72e5c1b1b1dd4357580c4fb5a60e3f4deErik Kline public native void bind(FileDescriptor fd, SocketAddress address) throws ErrnoException, SocketException; 555744e4a762ca12243b47dd8aed24d87d963b4d3bAndreas Gampe @Override 565744e4a762ca12243b47dd8aed24d87d963b4d3bAndreas Gampe public native StructCapUserData[] capget(StructCapUserHeader hdr) throws ErrnoException; 575744e4a762ca12243b47dd8aed24d87d963b4d3bAndreas Gampe @Override 585744e4a762ca12243b47dd8aed24d87d963b4d3bAndreas Gampe public native void capset(StructCapUserHeader hdr, StructCapUserData[] data) 595744e4a762ca12243b47dd8aed24d87d963b4d3bAndreas Gampe throws ErrnoException; 60b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes public native void chmod(String path, int mode) throws ErrnoException; 6144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes public native void chown(String path, int uid, int gid) throws ErrnoException; 62462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes public native void close(FileDescriptor fd) throws ErrnoException; 6326010ab930a2cee3bf10b9612cf070183c21228bElliott Hughes public native void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException, SocketException; 648f5b46d72e5c1b1b1dd4357580c4fb5a60e3f4deErik Kline public native void connect(FileDescriptor fd, SocketAddress address) throws ErrnoException, SocketException; 65396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native FileDescriptor dup(FileDescriptor oldFd) throws ErrnoException; 66396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native FileDescriptor dup2(FileDescriptor oldFd, int newFd) throws ErrnoException; 67ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes public native String[] environ(); 68798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich public native void execv(String filename, String[] argv) throws ErrnoException; 69798f855f67b74d481346d9dc2293ba0eedd8daf9Nick Kralevich public native void execve(String filename, String[] argv, String[] envp) throws ErrnoException; 7044f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes public native void fchmod(FileDescriptor fd, int mode) throws ErrnoException; 7144f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes public native void fchown(FileDescriptor fd, int uid, int gid) throws ErrnoException; 7299a0c82619a88c6aea038757cf14090f5d33afebNeil Fuller public native int fcntlFlock(FileDescriptor fd, int cmd, StructFlock arg) throws ErrnoException, InterruptedIOException; 73c8d9ea662de6f4856b28907b4119087cfc5a44d2Narayan Kamath public native int fcntlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException; 74c8d9ea662de6f4856b28907b4119087cfc5a44d2Narayan Kamath public native int fcntlVoid(FileDescriptor fd, int cmd) throws ErrnoException; 7552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes public native void fdatasync(FileDescriptor fd) throws ErrnoException; 7647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes public native StructStat fstat(FileDescriptor fd) throws ErrnoException; 77721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes public native StructStatVfs fstatvfs(FileDescriptor fd) throws ErrnoException; 7852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes public native void fsync(FileDescriptor fd) throws ErrnoException; 79f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes public native void ftruncate(FileDescriptor fd, long length) throws ErrnoException; 804f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes public native String gai_strerror(int error); 81396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native int getegid(); 82396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native int geteuid(); 83396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native int getgid(); 84ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes public native String getenv(String name); 854f11ebea266eada830d507b8f011e811a8e5d7bcElliott Hughes public native String getnameinfo(InetAddress address, int flags) throws GaiException; 86482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes public native SocketAddress getpeername(FileDescriptor fd) throws ErrnoException; 878f0f2ac7fcd8f366a78cc51181d065ab93385e46Elliott Hughes public native int getpgid(int pid); 88396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native int getpid(); 89396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native int getppid(); 90d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes public native StructPasswd getpwnam(String name) throws ErrnoException; 91d4419fce71d11ec8494525eca65e54d1aab51de6Elliott Hughes public native StructPasswd getpwuid(int uid) throws ErrnoException; 920a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native SocketAddress getsockname(FileDescriptor fd) throws ErrnoException; 930a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native int getsockoptByte(FileDescriptor fd, int level, int option) throws ErrnoException; 940a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native InetAddress getsockoptInAddr(FileDescriptor fd, int level, int option) throws ErrnoException; 950a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native int getsockoptInt(FileDescriptor fd, int level, int option) throws ErrnoException; 960a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native StructLinger getsockoptLinger(FileDescriptor fd, int level, int option) throws ErrnoException; 970a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native StructTimeval getsockoptTimeval(FileDescriptor fd, int level, int option) throws ErrnoException; 98482a3fc5635ac431b8a7476d7fe3397af4c2e8ecElliott Hughes public native StructUcred getsockoptUcred(FileDescriptor fd, int level, int option) throws ErrnoException; 99069525a39125c203b658c805685b6045a7d4dfebElliott Hughes public native int gettid(); 100396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native int getuid(); 10140afa6bcd4652a16fe461a59f205dd0d65652118Yi Kong public native byte[] getxattr(String path, String name) throws ErrnoException; 1026d1649c5b37b21ef2b690bc0f86306e19a32db02Yi Kong public native StructIfaddrs[] getifaddrs() throws ErrnoException; 103a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes public native String if_indextoname(int index); 1046d1649c5b37b21ef2b690bc0f86306e19a32db02Yi Kong public native int if_nametoindex(String name); 105fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public native InetAddress inet_pton(int family, String address); 10645348e170536651c0e5f8d5cb0319ba232a1b833Yi Kong public native int ioctlFlags(FileDescriptor fd, String interfaceName) throws ErrnoException; 107a37e971343883bb582a93ffbd9f0ba84f10e55baElliott Hughes public native InetAddress ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName) throws ErrnoException; 1088b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes public native int ioctlInt(FileDescriptor fd, int cmd, MutableInt arg) throws ErrnoException; 10945348e170536651c0e5f8d5cb0319ba232a1b833Yi Kong public native int ioctlMTU(FileDescriptor fd, String interfaceName) throws ErrnoException; 1109a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes public native boolean isatty(FileDescriptor fd); 111a5fb706fe4a6dbeaaf4cb1f8bbc2c68b0a2a3f3cElliott Hughes public native void kill(int pid, int signal) throws ErrnoException; 11244f7875f2985d7944c5afecc8394ad9f2219c806Elliott Hughes public native void lchown(String path, int uid, int gid) throws ErrnoException; 11304428d61d7000e17ab21d08a1d672c34eb68f6e2Elliott Hughes public native void link(String oldPath, String newPath) throws ErrnoException; 114e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes public native void listen(FileDescriptor fd, int backlog) throws ErrnoException; 11555dd377d62cf780df94d8e0198b66e99456b6f50Yi Kong public native String[] listxattr(String path) throws ErrnoException; 116dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes public native long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException; 117e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes public native StructStat lstat(String path) throws ErrnoException; 1180f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes public native void mincore(long address, long byteCount, byte[] vector) throws ErrnoException; 119c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes public native void mkdir(String path, int mode) throws ErrnoException; 120f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public native void mkfifo(String path, int mode) throws ErrnoException; 1217e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes public native void mlock(long address, long byteCount) throws ErrnoException; 1227e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes public native long mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset) throws ErrnoException; 1237e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes public native void msync(long address, long byteCount, int flags) throws ErrnoException; 1247e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes public native void munlock(long address, long byteCount) throws ErrnoException; 1257e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes public native void munmap(long address, long byteCount) throws ErrnoException; 1260ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes public native FileDescriptor open(String path, int flags, int mode) throws ErrnoException; 1270d8b5c3692c36837d22c4e7d9c4d7d95f6a10235Elliott Hughes public native FileDescriptor[] pipe2(int flags) throws ErrnoException; 12870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes public native int poll(StructPollfd[] fds, int timeoutMs) throws ErrnoException; 12911f07d30d2e344b48f132ec6ed105d85423052c2Elliott Hughes public native void posix_fallocate(FileDescriptor fd, long offset, long length) throws ErrnoException; 1305215e4c0db7530519981f1e505e6db82401802f2Nick Kralevich public native int prctl(int option, long arg2, long arg3, long arg4, long arg5) throws ErrnoException; 131f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public int pread(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException { 132d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int bytesRead; 133d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int position = buffer.position(); 134d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 135e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes if (buffer.isDirect()) { 136d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesRead = preadBytes(fd, buffer, position, buffer.remaining(), offset); 137e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } else { 138d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesRead = preadBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset); 139e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 140d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 141d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath maybeUpdateBufferPosition(buffer, position, bytesRead); 142d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath return bytesRead; 143e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 144f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public int pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException { 145e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 146e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes return preadBytes(fd, bytes, byteOffset, byteCount, offset); 147e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 148f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller private native int preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException; 149f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public int pwrite(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException { 150d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int bytesWritten; 151d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int position = buffer.position(); 152d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 153e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes if (buffer.isDirect()) { 154d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesWritten = pwriteBytes(fd, buffer, position, buffer.remaining(), offset); 155e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } else { 156d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesWritten = pwriteBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset); 157e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 158d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 159d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath maybeUpdateBufferPosition(buffer, position, bytesWritten); 160d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath return bytesWritten; 161e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 162f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public int pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException { 163e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 164e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes return pwriteBytes(fd, bytes, byteOffset, byteCount, offset); 165e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes } 166f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller private native int pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException; 167f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException { 168d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int bytesRead; 169d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int position = buffer.position(); 170d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 17126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes if (buffer.isDirect()) { 172d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesRead = readBytes(fd, buffer, position, buffer.remaining()); 1730568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes } else { 174d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesRead = readBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining()); 17526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes } 176d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 177d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath maybeUpdateBufferPosition(buffer, position, bytesRead); 178d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath return bytesRead; 17926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes } 180f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException { 1810568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 1820568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return readBytes(fd, bytes, byteOffset, byteCount); 1830568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes } 184f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller private native int readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException; 1850b6b3e10459fc3a3f4dd280dab8d4a145f7bf2f0Elliott Hughes public native String readlink(String path) throws ErrnoException; 1865d45c289afe04d12f41612e33e8df83e5db650d8Narayan Kamath public native String realpath(String path) throws ErrnoException; 187f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public native int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException; 18826010ab930a2cee3bf10b9612cf070183c21228bElliott Hughes public int recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { 189d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int bytesReceived; 190d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int position = buffer.position(); 191d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 19223ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes if (buffer.isDirect()) { 193d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesReceived = recvfromBytes(fd, buffer, position, buffer.remaining(), flags, srcAddress); 19423ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes } else { 195d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesReceived = recvfromBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, srcAddress); 19623ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes } 197d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 198d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath maybeUpdateBufferPosition(buffer, position, bytesReceived); 199d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath return bytesReceived; 20023ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes } 20126010ab930a2cee3bf10b9612cf070183c21228bElliott Hughes public int recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { 20223ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 203553d98af897f7202de5e5a776287de0b5ca8fe39Elliott Hughes return recvfromBytes(fd, bytes, byteOffset, byteCount, flags, srcAddress); 20423ec09188303a874b3b391f96ae0a29af002bff9Elliott Hughes } 20526010ab930a2cee3bf10b9612cf070183c21228bElliott Hughes private native int recvfromBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException; 206c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes public native void remove(String path) throws ErrnoException; 20790246a0ae7117d780e077c9e84cdb73ff5b44af7Jeff Sharkey public native void removexattr(String path, String name) throws ErrnoException; 208a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes public native void rename(String oldPath, String newPath) throws ErrnoException; 2098b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException; 21026010ab930a2cee3bf10b9612cf070183c21228bElliott Hughes public int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 211d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int bytesSent; 212d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int position = buffer.position(); 213d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 21490d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes if (buffer.isDirect()) { 215d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesSent = sendtoBytes(fd, buffer, position, buffer.remaining(), flags, inetAddress, port); 21690d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes } else { 217d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesSent = sendtoBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, inetAddress, port); 21890d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes } 219d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 220d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath maybeUpdateBufferPosition(buffer, position, bytesSent); 221d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath return bytesSent; 22290d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes } 22326010ab930a2cee3bf10b9612cf070183c21228bElliott Hughes public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 22490d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 22590d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); 22690d96a4f168b7e56cff54dc94dca2f3cde60ebcdElliott Hughes } 227a8b7587c5001db3489c64ac1d16c254a683f76bdLorenzo Colitti public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException { 228a8b7587c5001db3489c64ac1d16c254a683f76bdLorenzo Colitti return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, address); 229a8b7587c5001db3489c64ac1d16c254a683f76bdLorenzo Colitti } 23026010ab930a2cee3bf10b9612cf070183c21228bElliott Hughes private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException; 231a8b7587c5001db3489c64ac1d16c254a683f76bdLorenzo Colitti private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException; 232396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native void setegid(int egid) throws ErrnoException; 233895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes public native void setenv(String name, String value, boolean overwrite) throws ErrnoException; 234396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native void seteuid(int euid) throws ErrnoException; 235396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native void setgid(int gid) throws ErrnoException; 2368f0f2ac7fcd8f366a78cc51181d065ab93385e46Elliott Hughes public native void setpgid(int pid, int pgid) throws ErrnoException; 2378f0f2ac7fcd8f366a78cc51181d065ab93385e46Elliott Hughes public native void setregid(int rgid, int egid) throws ErrnoException; 2388f0f2ac7fcd8f366a78cc51181d065ab93385e46Elliott Hughes public native void setreuid(int ruid, int euid) throws ErrnoException; 2396c9b5377550a9649ed1532d1fcdfeba116c74eadElliott Hughes public native int setsid() throws ErrnoException; 240c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes public native void setsockoptByte(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 241b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes public native void setsockoptIfreq(FileDescriptor fd, int level, int option, String value) throws ErrnoException; 242454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes public native void setsockoptInt(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 243b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes public native void setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 244438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes public native void setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value) throws ErrnoException; 245df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller public native void setsockoptGroupSourceReq(FileDescriptor fd, int level, int option, StructGroupSourceReq value) throws ErrnoException; 246c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes public native void setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value) throws ErrnoException; 247c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes public native void setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value) throws ErrnoException; 248396a9c666da353b910b515d12eb1c43adfddb0c8Elliott Hughes public native void setuid(int uid) throws ErrnoException; 24990246a0ae7117d780e077c9e84cdb73ff5b44af7Jeff Sharkey public native void setxattr(String path, String name, byte[] value, int flags) throws ErrnoException; 25059e4744d27231f260271dbbca406e0cc39768116Elliott Hughes public native void shutdown(FileDescriptor fd, int how) throws ErrnoException; 251454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes public native FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException; 2523218082325b6b8713a8ac15731482e3da86a7df9Elliott Hughes public native void socketpair(int domain, int type, int protocol, FileDescriptor fd1, FileDescriptor fd2) throws ErrnoException; 25347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes public native StructStat stat(String path) throws ErrnoException; 254721ceca2a52a3c27aa751476c8562e1e68088e15Elliott Hughes public native StructStatVfs statvfs(String path) throws ErrnoException; 255ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes public native String strerror(int errno); 256763f8ed6195707d0c30bfae1ca8a3bb886b746ccElliott Hughes public native String strsignal(int signal); 257a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes public native void symlink(String oldPath, String newPath) throws ErrnoException; 2586fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes public native long sysconf(int name); 259bb5816aa1626eb2f6263bd21479600b114c8a1bdJohan Redestig public native void tcdrain(FileDescriptor fd) throws ErrnoException; 260039f7599559fd7e48c354b99dcb94ff391f53349Elliott Hughes public native void tcsendbreak(FileDescriptor fd, int duration) throws ErrnoException; 261c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevich public int umask(int mask) { 262c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevich if ((mask & 0777) != mask) { 263c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevich throw new IllegalArgumentException("Invalid umask: " + mask); 264c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevich } 265c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevich return umaskImpl(mask); 266c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevich } 267c79d3ef4dcd25f0e264bf957861e2632da93e62dNick Kralevich private native int umaskImpl(int mask); 2689b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes public native StructUtsname uname(); 2695e8f82f2592498e75ac4685ccd0d31d601d37bdcNeil Fuller public native void unlink(String pathname) throws ErrnoException; 270895a613aa2aec6aa6d03b29755cf2fea584909adElliott Hughes public native void unsetenv(String name) throws ErrnoException; 2719e67ca71d3d4a489a157abadc5ba519ced7a0b50Elliott Hughes public native int waitpid(int pid, MutableInt status, int options) throws ErrnoException; 272f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException { 273d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int bytesWritten; 274d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath final int position = buffer.position(); 27578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes if (buffer.isDirect()) { 276d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesWritten = writeBytes(fd, buffer, position, buffer.remaining()); 2770568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes } else { 278d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath bytesWritten = writeBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining()); 27978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes } 280d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 281d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath maybeUpdateBufferPosition(buffer, position, bytesWritten); 282d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath return bytesWritten; 28378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes } 284f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException { 2850568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 2860568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes return writeBytes(fd, bytes, byteOffset, byteCount); 2870568a63ba1086a78ffb4cff68dd2eac4f9908e13Elliott Hughes } 288f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller private native int writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException; 289f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public native int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException; 290d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath 291d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath private static void maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten) { 292d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath if (bytesReadOrWritten > 0) { 293d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath buffer.position(bytesReadOrWritten + originalPosition); 294d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath } 295d9f7e57f5d09b587d8c8d1bd42b895f7de8fbf54Narayan Kamath } 296ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 297