Posix.java revision b974666d79ebc392b37ec1ae83aae57ae6331c08
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 1952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesimport java.io.FileDescriptor; 200a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesimport java.net.InetAddress; 210a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughesimport java.net.SocketAddress; 2226c7025a7a919044771fb89031161bd26fe03032Elliott Hughesimport java.nio.ByteBuffer; 234af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughesimport java.nio.NioUtils; 248b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesimport libcore.util.MutableInt; 258b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesimport libcore.util.MutableLong; 2652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes 27ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughespublic final class Posix implements Os { 28ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes Posix() { } 29ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes 30ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes public native boolean access(String path, int mode) throws ErrnoException; 31b7190190e0ef8de883c952efb319ce7748831faaElliott Hughes public native void chmod(String path, int mode) throws ErrnoException; 32462bdac45c10f43d88d8f07f6994e272a27c14a2Elliott Hughes public native void close(FileDescriptor fd) throws ErrnoException; 33ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes public native String[] environ(); 34fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes public native int fcntlVoid(FileDescriptor fd, int cmd) throws ErrnoException; 35fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes public native int fcntlLong(FileDescriptor fd, int cmd, long arg) throws ErrnoException; 36fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes public native int fcntlFlock(FileDescriptor fd, int cmd, StructFlock arg) throws ErrnoException; 3752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes public native void fdatasync(FileDescriptor fd) throws ErrnoException; 3847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes public native StructStat fstat(FileDescriptor fd) throws ErrnoException; 3959fa7163774d6930a174bc038414a4b780581957Elliott Hughes public native StructStatFs fstatfs(FileDescriptor fd) throws ErrnoException; 4052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes public native void fsync(FileDescriptor fd) throws ErrnoException; 41f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes public native void ftruncate(FileDescriptor fd, long length) throws ErrnoException; 42ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes public native String getenv(String name); 430a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native SocketAddress getsockname(FileDescriptor fd) throws ErrnoException; 440a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native int getsockoptByte(FileDescriptor fd, int level, int option) throws ErrnoException; 450a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native InetAddress getsockoptInAddr(FileDescriptor fd, int level, int option) throws ErrnoException; 460a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native int getsockoptInt(FileDescriptor fd, int level, int option) throws ErrnoException; 470a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native StructLinger getsockoptLinger(FileDescriptor fd, int level, int option) throws ErrnoException; 480a9d1ee45a9884a9616624d747172e18734e8fe0Elliott Hughes public native StructTimeval getsockoptTimeval(FileDescriptor fd, int level, int option) throws ErrnoException; 498b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes public native int ioctlInt(FileDescriptor fd, int cmd, MutableInt arg) throws ErrnoException; 509a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes public native boolean isatty(FileDescriptor fd); 51e1502d64e937001636fca3d62b2552ef2a34d05fElliott Hughes public native void listen(FileDescriptor fd, int backlog) throws ErrnoException; 52dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes public native long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException; 530f746ff511162add42eeabaf14ba70ace874c6f4Elliott Hughes public native void mincore(long address, long byteCount, byte[] vector) throws ErrnoException; 54c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes public native void mkdir(String path, int mode) throws ErrnoException; 557e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes public native void mlock(long address, long byteCount) throws ErrnoException; 567e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes public native long mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset) throws ErrnoException; 577e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes public native void msync(long address, long byteCount, int flags) throws ErrnoException; 587e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes public native void munlock(long address, long byteCount) throws ErrnoException; 597e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes public native void munmap(long address, long byteCount) throws ErrnoException; 600ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes public native FileDescriptor open(String path, int flags, int mode) throws ErrnoException; 6141f0605d2c809bd9bc1c0fb68d86b49a0f59b6c5Elliott Hughes public native FileDescriptor[] pipe() throws ErrnoException; 6247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes public native StructStat lstat(String path) throws ErrnoException; 6326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 6426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes if (buffer.isDirect()) { 6526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes return readDirectBuffer(fd, buffer, buffer.position(), buffer.remaining()); 6626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes } 674af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes return read(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining()); 6826c7025a7a919044771fb89031161bd26fe03032Elliott Hughes } 6926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes private native int readDirectBuffer(FileDescriptor fd, ByteBuffer buffer, int position, int remaining) throws ErrnoException; 7026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes public native int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException; 71bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes public native int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException; 72c7fa20701d5e9398c38f4615ed293acfce1c0cf6Elliott Hughes public native void remove(String path) throws ErrnoException; 73a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes public native void rename(String oldPath, String newPath) throws ErrnoException; 748b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException; 75c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes public native void setsockoptByte(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 76b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes public native void setsockoptIfreq(FileDescriptor fd, int level, int option, String value) throws ErrnoException; 77454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes public native void setsockoptInt(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 78b974666d79ebc392b37ec1ae83aae57ae6331c08Elliott Hughes public native void setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 79438cb9e440d250c8aa5daf4fae0c400dce8b1499Elliott Hughes public native void setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value) throws ErrnoException; 80c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes public native void setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value) throws ErrnoException; 81c63f0d4e80a9fd3bdf99cd438d108b750226736aElliott Hughes public native void setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value) throws ErrnoException; 8259e4744d27231f260271dbbca406e0cc39768116Elliott Hughes public native void shutdown(FileDescriptor fd, int how) throws ErrnoException; 83454a95f6a28855aa3c88d168b15a45bf315efc99Elliott Hughes public native FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException; 8447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes public native StructStat stat(String path) throws ErrnoException; 8559fa7163774d6930a174bc038414a4b780581957Elliott Hughes public native StructStatFs statfs(String path) throws ErrnoException; 86ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes public native String strerror(int errno); 87a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes public native void symlink(String oldPath, String newPath) throws ErrnoException; 886fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes public native long sysconf(int name); 897341b9ed7157a1e37a3e69a0974676da358b735aElliott Hughes public native StructUtsname uname() throws ErrnoException; 9078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 9178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes if (buffer.isDirect()) { 9278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes return writeDirectBuffer(fd, buffer, buffer.position(), buffer.remaining()); 9378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes } 944af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes return write(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining()); 9578c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes } 9678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes private native int writeDirectBuffer(FileDescriptor fd, ByteBuffer buffer, int position, int remaining) throws ErrnoException; 9778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes public native int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException; 98bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes public native int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException; 99ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes} 100