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" |
Martijn Coenen | 083eb69 | 2020-04-24 09:39:58 +0200 | [diff] [blame] | 27 | #include "android-base/unique_fd.h" |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 28 | |
| 29 | namespace mediaprovider { |
| 30 | namespace { |
| 31 | |
| 32 | constexpr const char* CLASS_NAME = "com/android/providers/media/fuse/FuseDaemon"; |
| 33 | static jclass gFuseDaemonClass; |
| 34 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 35 | jlong com_android_providers_media_FuseDaemon_new(JNIEnv* env, jobject self, |
| 36 | jobject media_provider) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 37 | LOG(DEBUG) << "Creating the FUSE daemon..."; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 38 | return reinterpret_cast<jlong>(new fuse::FuseDaemon(env, media_provider)); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 39 | } |
| 40 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 41 | 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] | 42 | jint fd, jstring java_path) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 43 | LOG(DEBUG) << "Starting the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 44 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 45 | |
Martijn Coenen | 083eb69 | 2020-04-24 09:39:58 +0200 | [diff] [blame] | 46 | android::base::unique_fd ufd(fd); |
| 47 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 48 | ScopedUtfChars utf_chars_path(env, java_path); |
| 49 | if (!utf_chars_path.c_str()) { |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 50 | return; |
| 51 | } |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 52 | |
Martijn Coenen | 083eb69 | 2020-04-24 09:39:58 +0200 | [diff] [blame] | 53 | daemon->Start(std::move(ufd), utf_chars_path.c_str()); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Zim | d0435b2 | 2020-03-05 13:52:51 +0000 | [diff] [blame] | 56 | bool com_android_providers_media_FuseDaemon_is_started(JNIEnv* env, jobject self, |
| 57 | jlong java_daemon) { |
| 58 | LOG(DEBUG) << "Checking if FUSE daemon started..."; |
| 59 | const fuse::FuseDaemon* daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 60 | return daemon->IsStarted(); |
| 61 | } |
| 62 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 63 | 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] | 64 | LOG(DEBUG) << "Destroying the FUSE daemon..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 65 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 66 | delete daemon; |
| 67 | } |
| 68 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 69 | jboolean com_android_providers_media_FuseDaemon_should_open_with_fuse(JNIEnv* env, jobject self, |
| 70 | jlong java_daemon, |
| 71 | jstring java_path, |
| 72 | jboolean for_read, jint fd) { |
| 73 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 74 | if (daemon) { |
| 75 | ScopedUtfChars utf_chars_path(env, java_path); |
| 76 | if (!utf_chars_path.c_str()) { |
| 77 | // TODO(b/145741852): Throw exception |
| 78 | return JNI_FALSE; |
| 79 | } |
| 80 | |
| 81 | return daemon->ShouldOpenWithFuse(fd, for_read, utf_chars_path.c_str()); |
| 82 | } |
| 83 | // TODO(b/145741852): Throw exception |
| 84 | return JNI_FALSE; |
| 85 | } |
| 86 | |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 87 | void com_android_providers_media_FuseDaemon_invalidate_fuse_dentry_cache(JNIEnv* env, jobject self, |
| 88 | jlong java_daemon, |
| 89 | jstring java_path) { |
| 90 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 91 | if (daemon) { |
| 92 | ScopedUtfChars utf_chars_path(env, java_path); |
| 93 | if (!utf_chars_path.c_str()) { |
| 94 | // TODO(b/145741152): Throw exception |
| 95 | return; |
| 96 | } |
| 97 | |
Colin Cross | 915ad6a | 2020-04-03 18:19:07 -0700 | [diff] [blame] | 98 | CHECK(pthread_getspecific(fuse::MediaProviderWrapper::gJniEnvKey) == nullptr); |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 99 | daemon->InvalidateFuseDentryCache(utf_chars_path.c_str()); |
| 100 | } |
| 101 | // TODO(b/145741152): Throw exception |
| 102 | } |
| 103 | |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame^] | 104 | jstring com_android_providers_media_FuseDaemon_get_original_media_format_file_path( |
| 105 | JNIEnv* env, jobject self, jlong java_daemon, jint fd) { |
| 106 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 107 | const std::string path = daemon->GetOriginalMediaFormatFilePath(fd); |
| 108 | return env->NewStringUTF(path.c_str()); |
| 109 | } |
| 110 | |
| 111 | void com_android_providers_media_FuseDaemon_initialize_device_id(JNIEnv* env, jobject self, |
| 112 | jlong java_daemon, |
| 113 | jstring java_path) { |
| 114 | fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon); |
| 115 | ScopedUtfChars utf_chars_path(env, java_path); |
| 116 | if (!utf_chars_path.c_str()) { |
| 117 | LOG(WARNING) << "Couldn't initialise FUSE device id"; |
| 118 | return; |
| 119 | } |
| 120 | daemon->InitializeDeviceId(utf_chars_path.c_str()); |
| 121 | } |
| 122 | |
Zim | c0e65bd | 2020-03-09 15:22:59 +0000 | [diff] [blame] | 123 | bool com_android_providers_media_FuseDaemon_is_fuse_thread(JNIEnv* env, jclass clazz) { |
Zim | c0e65bd | 2020-03-09 15:22:59 +0000 | [diff] [blame] | 124 | return pthread_getspecific(fuse::MediaProviderWrapper::gJniEnvKey) != nullptr; |
| 125 | } |
| 126 | |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 127 | const JNINativeMethod methods[] = { |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 128 | {"native_new", "(Lcom/android/providers/media/MediaProvider;)J", |
| 129 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_new)}, |
| 130 | {"native_start", "(JILjava/lang/String;)V", |
| 131 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_start)}, |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 132 | {"native_delete", "(J)V", |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 133 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete)}, |
| 134 | {"native_should_open_with_fuse", "(JLjava/lang/String;ZI)Z", |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 135 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_should_open_with_fuse)}, |
Zim | c0e65bd | 2020-03-09 15:22:59 +0000 | [diff] [blame] | 136 | {"native_is_fuse_thread", "()Z", |
| 137 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_is_fuse_thread)}, |
Zim | d0435b2 | 2020-03-05 13:52:51 +0000 | [diff] [blame] | 138 | {"native_is_started", "(J)Z", |
| 139 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_is_started)}, |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 140 | {"native_invalidate_fuse_dentry_cache", "(JLjava/lang/String;)V", |
| 141 | reinterpret_cast<void*>( |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame^] | 142 | com_android_providers_media_FuseDaemon_invalidate_fuse_dentry_cache)}, |
| 143 | {"native_get_original_media_format_file_path", "(JI)Ljava/lang/String;", |
| 144 | reinterpret_cast<void*>( |
| 145 | com_android_providers_media_FuseDaemon_get_original_media_format_file_path)}, |
| 146 | {"native_initialize_device_id", "(JLjava/lang/String;)V", |
| 147 | reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_initialize_device_id)}}; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 148 | } // namespace |
| 149 | |
Narayan Kamath | de3fe17 | 2020-02-18 12:14:51 +0000 | [diff] [blame] | 150 | void register_android_providers_media_FuseDaemon(JavaVM* vm, JNIEnv* env) { |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 151 | gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME))); |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 152 | |
| 153 | if (gFuseDaemonClass == nullptr) { |
| 154 | LOG(FATAL) << "Unable to find class : " << CLASS_NAME; |
| 155 | } |
| 156 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 157 | if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) { |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 158 | LOG(FATAL) << "Unable to register native methods"; |
| 159 | } |
Narayan Kamath | de3fe17 | 2020-02-18 12:14:51 +0000 | [diff] [blame] | 160 | |
| 161 | fuse::MediaProviderWrapper::OneTimeInit(vm); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 162 | } |
| 163 | } // namespace mediaprovider |