blob: d67123d03e350ec93c9472ea695071186e6327f9 [file] [log] [blame]
Zim3e45d9b2019-08-19 21:14:14 +01001/*
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.
Zim7e0d3142019-10-17 21:06:12 +010018#define LOG_TAG "FuseDaemonJNI"
Zim3e45d9b2019-08-19 21:14:14 +010019
shafik8b57cd52019-09-06 10:51:29 +010020#include <nativehelper/scoped_utf_chars.h>
Zim3e45d9b2019-08-19 21:14:14 +010021
22#include <string>
23
shafik8b57cd52019-09-06 10:51:29 +010024#include "FuseDaemon.h"
Narayan Kamathde3fe172020-02-18 12:14:51 +000025#include "MediaProviderWrapper.h"
shafik8b57cd52019-09-06 10:51:29 +010026#include "android-base/logging.h"
Martijn Coenen083eb692020-04-24 09:39:58 +020027#include "android-base/unique_fd.h"
Zim3e45d9b2019-08-19 21:14:14 +010028
29namespace mediaprovider {
30namespace {
31
32constexpr const char* CLASS_NAME = "com/android/providers/media/fuse/FuseDaemon";
33static jclass gFuseDaemonClass;
34
Zimedbe69e2019-12-13 18:49:36 +000035jlong com_android_providers_media_FuseDaemon_new(JNIEnv* env, jobject self,
36 jobject media_provider) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010037 LOG(DEBUG) << "Creating the FUSE daemon...";
Zimedbe69e2019-12-13 18:49:36 +000038 return reinterpret_cast<jlong>(new fuse::FuseDaemon(env, media_provider));
Zim3e45d9b2019-08-19 21:14:14 +010039}
40
shafik8b57cd52019-09-06 10:51:29 +010041void com_android_providers_media_FuseDaemon_start(JNIEnv* env, jobject self, jlong java_daemon,
Zim7c8712d2019-10-03 21:01:26 +010042 jint fd, jstring java_path) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010043 LOG(DEBUG) << "Starting the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010044 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
45
Martijn Coenen083eb692020-04-24 09:39:58 +020046 android::base::unique_fd ufd(fd);
47
Zim7c8712d2019-10-03 21:01:26 +010048 ScopedUtfChars utf_chars_path(env, java_path);
49 if (!utf_chars_path.c_str()) {
Zim3e45d9b2019-08-19 21:14:14 +010050 return;
51 }
Zim3e45d9b2019-08-19 21:14:14 +010052
Martijn Coenen083eb692020-04-24 09:39:58 +020053 daemon->Start(std::move(ufd), utf_chars_path.c_str());
Zim3e45d9b2019-08-19 21:14:14 +010054}
55
Zimd0435b22020-03-05 13:52:51 +000056bool 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
shafik8b57cd52019-09-06 10:51:29 +010063void com_android_providers_media_FuseDaemon_delete(JNIEnv* env, jobject self, jlong java_daemon) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010064 LOG(DEBUG) << "Destroying the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010065 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
66 delete daemon;
67}
68
Zimedbe69e2019-12-13 18:49:36 +000069jboolean 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
Zima76c3492020-02-19 01:23:26 +000087void 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 Cross915ad6a2020-04-03 18:19:07 -070098 CHECK(pthread_getspecific(fuse::MediaProviderWrapper::gJniEnvKey) == nullptr);
Zima76c3492020-02-19 01:23:26 +000099 daemon->InvalidateFuseDentryCache(utf_chars_path.c_str());
100 }
101 // TODO(b/145741152): Throw exception
102}
103
Biswarup Pal93f4ec12021-02-15 13:39:36 +0000104jstring 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
111void 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
Zimc0e65bd2020-03-09 15:22:59 +0000123bool com_android_providers_media_FuseDaemon_is_fuse_thread(JNIEnv* env, jclass clazz) {
Zimc0e65bd2020-03-09 15:22:59 +0000124 return pthread_getspecific(fuse::MediaProviderWrapper::gJniEnvKey) != nullptr;
125}
126
Zim3e45d9b2019-08-19 21:14:14 +0100127const JNINativeMethod methods[] = {
Zim7c8712d2019-10-03 21:01:26 +0100128 {"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)},
Zim7c8712d2019-10-03 21:01:26 +0100132 {"native_delete", "(J)V",
Zimedbe69e2019-12-13 18:49:36 +0000133 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete)},
134 {"native_should_open_with_fuse", "(JLjava/lang/String;ZI)Z",
Zima76c3492020-02-19 01:23:26 +0000135 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_should_open_with_fuse)},
Zimc0e65bd2020-03-09 15:22:59 +0000136 {"native_is_fuse_thread", "()Z",
137 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_is_fuse_thread)},
Zimd0435b22020-03-05 13:52:51 +0000138 {"native_is_started", "(J)Z",
139 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_is_started)},
Zima76c3492020-02-19 01:23:26 +0000140 {"native_invalidate_fuse_dentry_cache", "(JLjava/lang/String;)V",
141 reinterpret_cast<void*>(
Biswarup Pal93f4ec12021-02-15 13:39:36 +0000142 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)}};
Zim3e45d9b2019-08-19 21:14:14 +0100148} // namespace
149
Narayan Kamathde3fe172020-02-18 12:14:51 +0000150void register_android_providers_media_FuseDaemon(JavaVM* vm, JNIEnv* env) {
Zim3e45d9b2019-08-19 21:14:14 +0100151 gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME)));
Narayan Kamath88203dc2019-08-30 17:19:38 +0100152
153 if (gFuseDaemonClass == nullptr) {
154 LOG(FATAL) << "Unable to find class : " << CLASS_NAME;
155 }
156
shafik8b57cd52019-09-06 10:51:29 +0100157 if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) {
Narayan Kamath88203dc2019-08-30 17:19:38 +0100158 LOG(FATAL) << "Unable to register native methods";
159 }
Narayan Kamathde3fe172020-02-18 12:14:51 +0000160
161 fuse::MediaProviderWrapper::OneTimeInit(vm);
Zim3e45d9b2019-08-19 21:14:14 +0100162}
163} // namespace mediaprovider