blob: 76b0fc16726411d87bbb9ce23b6e375305127823 [file] [log] [blame]
Svet Ganov8455ba22019-01-02 13:05:56 -08001/*
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
22namespace android {
23
24static 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 Kralevich5ca3af32019-01-28 10:34:43 -080032 if ((fd = TEMP_FAILURE_RETRY(open(path8.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC))) == -1) {
Svet Ganov8455ba22019-01-02 13:05:56 -080033 ALOGE("Cannot open directory %s, error: %s\n", path8.c_str(), strerror(errno));
34 return -1;
35 }
36 return fd;
37}
38
39static 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 */
49static 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
61int 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