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.
18 #define LOG_TAG "FuseDaemonJNI"
19 
20 #include <nativehelper/scoped_utf_chars.h>
21 
22 #include <string>
23 
24 #include "FuseDaemon.h"
25 #include "MediaProviderWrapper.h"
26 #include "android-base/logging.h"
27 #include "android-base/unique_fd.h"
28 
29 namespace mediaprovider {
30 namespace {
31 
32 constexpr const char* CLASS_NAME = "com/android/providers/media/fuse/FuseDaemon";
33 static jclass gFuseDaemonClass;
34 
com_android_providers_media_FuseDaemon_new(JNIEnv * env,jobject self,jobject media_provider)35 jlong com_android_providers_media_FuseDaemon_new(JNIEnv* env, jobject self,
36                                                  jobject media_provider) {
37     LOG(DEBUG) << "Creating the FUSE daemon...";
38     return reinterpret_cast<jlong>(new fuse::FuseDaemon(env, media_provider));
39 }
40 
com_android_providers_media_FuseDaemon_start(JNIEnv * env,jobject self,jlong java_daemon,jint fd,jstring java_path)41 void com_android_providers_media_FuseDaemon_start(JNIEnv* env, jobject self, jlong java_daemon,
42                                                   jint fd, jstring java_path) {
43     LOG(DEBUG) << "Starting the FUSE daemon...";
44     fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
45 
46     android::base::unique_fd ufd(fd);
47 
48     ScopedUtfChars utf_chars_path(env, java_path);
49     if (!utf_chars_path.c_str()) {
50         return;
51     }
52 
53     daemon->Start(std::move(ufd), utf_chars_path.c_str());
54 }
55 
com_android_providers_media_FuseDaemon_is_started(JNIEnv * env,jobject self,jlong java_daemon)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 
com_android_providers_media_FuseDaemon_delete(JNIEnv * env,jobject self,jlong java_daemon)63 void com_android_providers_media_FuseDaemon_delete(JNIEnv* env, jobject self, jlong java_daemon) {
64     LOG(DEBUG) << "Destroying the FUSE daemon...";
65     fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
66     delete daemon;
67 }
68 
com_android_providers_media_FuseDaemon_should_open_with_fuse(JNIEnv * env,jobject self,jlong java_daemon,jstring java_path,jboolean for_read,jint fd)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 
com_android_providers_media_FuseDaemon_invalidate_fuse_dentry_cache(JNIEnv * env,jobject self,jlong java_daemon,jstring java_path)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 
98         CHECK(pthread_getspecific(fuse::MediaProviderWrapper::gJniEnvKey) == nullptr);
99         daemon->InvalidateFuseDentryCache(utf_chars_path.c_str());
100     }
101     // TODO(b/145741152): Throw exception
102 }
103 
com_android_providers_media_FuseDaemon_get_original_media_format_file_path(JNIEnv * env,jobject self,jlong java_daemon,jint fd)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 
com_android_providers_media_FuseDaemon_initialize_device_id(JNIEnv * env,jobject self,jlong java_daemon,jstring java_path)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 
com_android_providers_media_FuseDaemon_is_fuse_thread(JNIEnv * env,jclass clazz)123 bool com_android_providers_media_FuseDaemon_is_fuse_thread(JNIEnv* env, jclass clazz) {
124     return pthread_getspecific(fuse::MediaProviderWrapper::gJniEnvKey) != nullptr;
125 }
126 
127 const JNINativeMethod methods[] = {
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)},
132         {"native_delete", "(J)V",
133          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete)},
134         {"native_should_open_with_fuse", "(JLjava/lang/String;ZI)Z",
135          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_should_open_with_fuse)},
136         {"native_is_fuse_thread", "()Z",
137          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_is_fuse_thread)},
138         {"native_is_started", "(J)Z",
139          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_is_started)},
140         {"native_invalidate_fuse_dentry_cache", "(JLjava/lang/String;)V",
141          reinterpret_cast<void*>(
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)}};
148 }  // namespace
149 
register_android_providers_media_FuseDaemon(JavaVM * vm,JNIEnv * env)150 void register_android_providers_media_FuseDaemon(JavaVM* vm, JNIEnv* env) {
151     gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME)));
152 
153     if (gFuseDaemonClass == nullptr) {
154         LOG(FATAL) << "Unable to find class : " << CLASS_NAME;
155     }
156 
157     if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) {
158         LOG(FATAL) << "Unable to register native methods";
159     }
160 
161     fuse::MediaProviderWrapper::OneTimeInit(vm);
162 }
163 }  // namespace mediaprovider
164