libcore_io_Linux.cpp revision 7e13c0f05ac9e7c55682d10e953dd4cbd5e6107c
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 Hughes#define LOG_TAG "Posix"
18ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
19ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JNIHelp.h"
20ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "JniConstants.h"
21ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "JniException.h"
22ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include "ScopedUtfChars.h"
23ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include "toStringArray.h"
24ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
25ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes#include <errno.h>
26ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes#include <stdlib.h>
2752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes#include <unistd.h>
287e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes#include <sys/mman.h>
2947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes#include <sys/stat.h>
30ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
317e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void throwErrnoException(JNIEnv* env, const char* name) {
327e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int errnum = errno;
337e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
34ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jthrowable cause = NULL;
35ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (env->ExceptionCheck()) {
36ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        cause = env->ExceptionOccurred();
37ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        env->ExceptionClear();
38ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
39ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
40f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    ScopedLocalRef<jstring> javaName(env, env->NewStringUTF(name));
41f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    if (javaName.get() == NULL) {
42f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // Not really much we can do here. We're probably dead in the water,
43f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        // but let's try to stumble on...
44f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        env->ExceptionClear();
45f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    }
46f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
47ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    jobject exception;
48ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (cause != NULL) {
49f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        static jmethodID ctor = env->GetMethodID(JniConstants::errnoExceptionClass, "<init>",
50f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                "(Ljava/lang/String;ILjava/lang/Throwable;)V");
51f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        exception = env->NewObject(JniConstants::errnoExceptionClass, ctor,
52f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                javaName.get(), errnum, cause);
53ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    } else {
54f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        static jmethodID ctor = env->GetMethodID(JniConstants::errnoExceptionClass, "<init>",
55f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                "(Ljava/lang/String;I)V");
56f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        exception = env->NewObject(JniConstants::errnoExceptionClass, ctor, javaName.get(), errnum);
57ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
58ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    env->Throw(reinterpret_cast<jthrowable>(exception));
59dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
60dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
61dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughestemplate <typename rc_t>
627e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) {
63dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == rc_t(-1)) {
647e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, name);
65dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
66dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    return rc;
6747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
6847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
6947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject makeStructStat(JNIEnv* env, const struct stat& sb) {
7047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    static jmethodID ctor = env->GetMethodID(JniConstants::structStatClass, "<init>",
7147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            "(JJIJIIJJJJJJJ)V");
7247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return env->NewObject(JniConstants::structStatClass, ctor,
7347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            jlong(sb.st_dev), jlong(sb.st_ino), jint(sb.st_mode), jlong(sb.st_nlink),
7447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            jint(sb.st_uid), jint(sb.st_gid), jlong(sb.st_rdev), jlong(sb.st_size),
7547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            jlong(sb.st_atime), jlong(sb.st_mtime), jlong(sb.st_ctime),
7647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            jlong(sb.st_blksize), jlong(sb.st_blocks));
7747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
7847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
7947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) {
8047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    ScopedUtfChars path(env, javaPath);
8147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    if (path.c_str() == NULL) {
8247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
8347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
8447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
8547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = isLstat ? TEMP_FAILURE_RETRY(lstat(path.c_str(), &sb))
8647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes                     : TEMP_FAILURE_RETRY(stat(path.c_str(), &sb));
87dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
887e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, isLstat ? "lstat" : "stat");
8947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
9047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
9147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
92ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
93ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
94ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jboolean Posix_access(JNIEnv* env, jobject, jstring javaPath, jint mode) {
95ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    ScopedUtfChars path(env, javaPath);
96ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    if (path.c_str() == NULL) {
97ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes        return JNI_FALSE;
98ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    }
9947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(access(path.c_str(), mode));
100dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
1017e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "access");
102dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    }
103ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return (rc == 0);
104ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
105ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
106ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jobjectArray Posix_environ(JNIEnv* env, jobject) {
107ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    extern char** environ; // Standard, but not in any header file.
108ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return toStringArray(env, environ);
109ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
110ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
11152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fdatasync(JNIEnv* env, jobject, jobject javaFd) {
11252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1137e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fdatasync", TEMP_FAILURE_RETRY(fdatasync(fd)));
11452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
11552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
11647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_fstat(JNIEnv* env, jobject, jobject javaFd) {
11747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
11847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    struct stat sb;
11947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(fstat(fd, &sb));
120dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    if (rc == -1) {
1217e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "fstat");
12247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        return NULL;
12347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    }
12447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return makeStructStat(env, sb);
12547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
12647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
12752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesstatic void Posix_fsync(JNIEnv* env, jobject, jobject javaFd) {
12852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1297e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "fsync", TEMP_FAILURE_RETRY(fsync(fd)));
130f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes}
131f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
132f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughesstatic void Posix_ftruncate(JNIEnv* env, jobject, jobject javaFd, jlong length) {
133f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1347e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "ftruncate", TEMP_FAILURE_RETRY(ftruncate64(fd, length)));
13552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
13652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
137ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic jstring Posix_getenv(JNIEnv* env, jobject, jstring javaName) {
138ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    ScopedUtfChars name(env, javaName);
139ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    if (name.c_str() == NULL) {
140ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes        return NULL;
141ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    }
142ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return env->NewStringUTF(getenv(name.c_str()));
143ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
144ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes
1459a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughesstatic jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) {
1469a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1479a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    return TEMP_FAILURE_RETRY(isatty(fd)) == 0;
1489a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes}
1499a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes
150dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughesstatic jlong Posix_lseek(JNIEnv* env, jobject, jobject javaFd, jlong offset, jint whence) {
151dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1527e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)));
153dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes}
154dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes
15547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_lstat(JNIEnv* env, jobject, jstring javaPath) {
15647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, true);
15747cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
15847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
1597e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_mlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
1607e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
1617e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "mlock", TEMP_FAILURE_RETRY(mlock(ptr, byteCount)));
1627e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
1637e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
1647e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic jlong Posix_mmap(JNIEnv* env, jobject, jlong address, jlong byteCount, jint prot, jint flags, jobject javaFd, jlong offset) {
1657e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    int fd = jniGetFDFromFileDescriptor(env, javaFd);
1667e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* suggestedPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
1677e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = mmap(suggestedPtr, byteCount, prot, flags, fd, offset);
1687e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    if (ptr == MAP_FAILED) {
1697e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "mmap");
1707e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    }
1717e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr));
1727e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
1737e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
1747e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_msync(JNIEnv* env, jobject, jlong address, jlong byteCount, jint flags) {
1757e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
1767e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "msync", TEMP_FAILURE_RETRY(msync(ptr, byteCount, flags)));
1777e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
1787e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
1797e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughesstatic void Posix_munlock(JNIEnv* env, jobject, jlong address, jlong byteCount) {
1807e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
1817e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    throwIfMinusOne(env, "munlock", TEMP_FAILURE_RETRY(munlock(ptr, byteCount)));
1827e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes}
1837e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes
1847e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughesstatic void Posix_munmap(JNIEnv* env, jobject, jlong address, jlong byteCount) {
1857e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    void* ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(address));
1867e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    throwIfMinusOne(env, "munmap", TEMP_FAILURE_RETRY(munmap(ptr, byteCount)));
1877e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes}
1887e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes
18947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughesstatic jobject Posix_stat(JNIEnv* env, jobject, jstring javaPath) {
19047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    return doStat(env, javaPath, false);
19147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes}
19247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes
193ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughesstatic jstring Posix_strerror(JNIEnv* env, jobject, jint errnum) {
19452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    char buffer[BUFSIZ];
195ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    const char* message = jniStrError(errnum, buffer, sizeof(buffer));
196ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    return env->NewStringUTF(message);
197ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes}
198ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes
1996fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughesstatic jlong Posix_sysconf(JNIEnv* env, jobject, jint name) {
2006fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    // Since -1 is a valid result from sysconf(3), detecting failure is a little more awkward.
2016fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    errno = 0;
2026fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    long result = sysconf(name);
2036fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    if (result == -1L && errno == EINVAL) {
2047e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        throwErrnoException(env, "sysconf");
2056fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    }
2066fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    return result;
2076fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes}
2086fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes
209ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesstatic JNINativeMethod gMethods[] = {
210ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"),
211ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, environ, "()[Ljava/lang/String;"),
21252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fdatasync, "(Ljava/io/FileDescriptor;)V"),
21347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, fstat, "(Ljava/io/FileDescriptor;)Llibcore/io/StructStat;"),
21452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"),
215f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"),
216ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"),
2179a3f363523000704205df288f8b6f2f48c0d8563Elliott Hughes    NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"),
218dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes    NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"),
21947cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, lstat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
2207e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, mlock, "(JJ)V"),
2217e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, mmap, "(JJIILjava/io/FileDescriptor;J)J"),
2227e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, msync, "(JJI)V"),
2237e13c0f05ac9e7c55682d10e953dd4cbd5e6107cElliott Hughes    NATIVE_METHOD(Posix, munlock, "(JJ)V"),
2247e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes    NATIVE_METHOD(Posix, munmap, "(JJ)V"),
22547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes    NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
226ddfdbb9d172fe9b72e08e8d7deab0aa3b8acf044Elliott Hughes    NATIVE_METHOD(Posix, strerror, "(I)Ljava/lang/String;"),
2276fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes    NATIVE_METHOD(Posix, sysconf, "(I)J"),
228ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes};
229ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughesint register_libcore_io_Posix(JNIEnv* env) {
230ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes    return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods));
231ec617e2cb4a374f0fd8fbda4a633214cf23a59a9Elliott Hughes}
232