Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <nativehelper/ScopedUtfChars.h> |
| 18 | #include "jni.h" |
| 19 | |
| 20 | #include "core_jni_helpers.h" |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | static jint com_android_internal_os_AtomicDirectory_getDirectoryFd(JNIEnv* env, |
| 25 | jobject /*clazz*/, jstring path) { |
| 26 | ScopedUtfChars path8(env, path); |
| 27 | if (path8.c_str() == NULL) { |
| 28 | ALOGE("Invalid path: %s", path8.c_str()); |
| 29 | return -1; |
| 30 | } |
| 31 | int fd; |
Nick Kralevich | 5ca3af3 | 2019-01-28 10:34:43 -0800 | [diff] [blame] | 32 | if ((fd = TEMP_FAILURE_RETRY(open(path8.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC))) == -1) { |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 33 | ALOGE("Cannot open directory %s, error: %s\n", path8.c_str(), strerror(errno)); |
| 34 | return -1; |
| 35 | } |
| 36 | return fd; |
| 37 | } |
| 38 | |
| 39 | static void com_android_internal_os_AtomicDirectory_fsyncDirectoryFd(JNIEnv* env, |
| 40 | jobject /*clazz*/, jint fd) { |
| 41 | if (TEMP_FAILURE_RETRY(fsync(fd)) == -1) { |
| 42 | ALOGE("Cannot fsync directory %d, error: %s\n", fd, strerror(errno)); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * JNI registration. |
| 48 | */ |
| 49 | static const JNINativeMethod gRegisterMethods[] = { |
| 50 | /* name, signature, funcPtr */ |
| 51 | { "fsyncDirectoryFd", |
| 52 | "(I)V", |
| 53 | (void*) com_android_internal_os_AtomicDirectory_fsyncDirectoryFd |
| 54 | }, |
| 55 | { "getDirectoryFd", |
| 56 | "(Ljava/lang/String;)I", |
| 57 | (void*) com_android_internal_os_AtomicDirectory_getDirectoryFd |
| 58 | }, |
| 59 | }; |
| 60 | |
| 61 | int register_com_android_internal_os_AtomicDirectory(JNIEnv* env) { |
| 62 | return RegisterMethodsOrDie(env, "com/android/internal/os/AtomicDirectory", |
| 63 | gRegisterMethods, NELEM(gRegisterMethods)); |
| 64 | } |
| 65 | |
| 66 | }; // namespace android |