Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 specic language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Need to use LOGE_EX. |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 18 | #define LOG_TAG "FuseDaemonJNI" |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 19 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 20 | #include <nativehelper/scoped_utf_chars.h> |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 21 | |
| 22 | #include <string> |
| 23 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 24 | #include "FuseDaemon.h" |
Narayan Kamath | de3fe17 | 2020-02-18 12:14:51 +0000 | [diff] [blame^] | 25 | #include "MediaProviderWrapper.h" |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 26 | #include "android-base/logging.h" |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 27 | |
| 28 | namespace mediaprovider { |
| 29 | namespace { |
| 30 | |
| 31 | constexpr const char* CLASS_NAME = "com/android/providers/media/fuse/FuseDaemon"; |
| 32 | static jclass gFuseDaemonClass; |
| 33 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 34 | jlong com_android_providers_media_FuseDaemon_new(JNIEnv* env, jobject self, |
| 35 | jobject media_provider) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 36 | LOG(DEBUG) << "Creating the FUSE daemon..."; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 37 | return reinterpret_cast<jlong>(new fuse::FuseDaemon(env, media_provider)); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 38 | } |
| 39 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 40 | void com_android_providers_media_FuseDaemon_start(JNIEnv* env, jobject self, jlong java_daemon, |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 41 | jint fd, jstring java_path) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 42 | LOG(DEBUG) << "Starting the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 43 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 44 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 45 | ScopedUtfChars utf_chars_path(env, java_path); |
| 46 | if (!utf_chars_path.c_str()) { |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 47 | return; |
| 48 | } |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 49 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 50 | daemon->Start(fd, utf_chars_path.c_str()); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 51 | } |
| 52 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 53 | void com_android_providers_media_FuseDaemon_delete(JNIEnv* env, jobject self, jlong java_daemon) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 54 | LOG(DEBUG) << "Destroying the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 55 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 56 | delete daemon; |
| 57 | } |
| 58 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 59 | jboolean com_android_providers_media_FuseDaemon_should_open_with_fuse(JNIEnv* env, jobject self, |
| 60 | jlong java_daemon, |
| 61 | jstring java_path, |
| 62 | jboolean for_read, jint fd) { |
| 63 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 64 | if (daemon) { |
| 65 | ScopedUtfChars utf_chars_path(env, java_path); |
| 66 | if (!utf_chars_path.c_str()) { |
| 67 | // TODO(b/145741852): Throw exception |
| 68 | return JNI_FALSE; |
| 69 | } |
| 70 | |
| 71 | return daemon->ShouldOpenWithFuse(fd, for_read, utf_chars_path.c_str()); |
| 72 | } |
| 73 | // TODO(b/145741852): Throw exception |
| 74 | return JNI_FALSE; |
| 75 | } |
| 76 | |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 77 | void com_android_providers_media_FuseDaemon_invalidate_fuse_dentry_cache(JNIEnv* env, jobject self, |
| 78 | jlong java_daemon, |
| 79 | jstring java_path) { |
| 80 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 81 | if (daemon) { |
| 82 | ScopedUtfChars utf_chars_path(env, java_path); |
| 83 | if (!utf_chars_path.c_str()) { |
| 84 | // TODO(b/145741152): Throw exception |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | daemon->InvalidateFuseDentryCache(utf_chars_path.c_str()); |
| 89 | } |
| 90 | // TODO(b/145741152): Throw exception |
| 91 | } |
| 92 | |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 93 | const JNINativeMethod methods[] = { |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 94 | {"native_new", "(Lcom/android/providers/media/MediaProvider;)J", |
| 95 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_new)}, |
| 96 | {"native_start", "(JILjava/lang/String;)V", |
| 97 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_start)}, |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 98 | {"native_delete", "(J)V", |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 99 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete)}, |
| 100 | {"native_should_open_with_fuse", "(JLjava/lang/String;ZI)Z", |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 101 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_should_open_with_fuse)}, |
| 102 | {"native_invalidate_fuse_dentry_cache", "(JLjava/lang/String;)V", |
| 103 | reinterpret_cast<void*>( |
| 104 | com_android_providers_media_FuseDaemon_invalidate_fuse_dentry_cache)}}; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 105 | } // namespace |
| 106 | |
Narayan Kamath | de3fe17 | 2020-02-18 12:14:51 +0000 | [diff] [blame^] | 107 | void register_android_providers_media_FuseDaemon(JavaVM* vm, JNIEnv* env) { |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 108 | gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME))); |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 109 | |
| 110 | if (gFuseDaemonClass == nullptr) { |
| 111 | LOG(FATAL) << "Unable to find class : " << CLASS_NAME; |
| 112 | } |
| 113 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 114 | if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 115 | LOG(FATAL) << "Unable to register native methods"; |
| 116 | } |
Narayan Kamath | de3fe17 | 2020-02-18 12:14:51 +0000 | [diff] [blame^] | 117 | |
| 118 | fuse::MediaProviderWrapper::OneTimeInit(vm); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 119 | } |
| 120 | } // namespace mediaprovider |