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_is_fuse_thread(JNIEnv * env,jclass clazz)104 bool com_android_providers_media_FuseDaemon_is_fuse_thread(JNIEnv* env, jclass clazz) {
105     return pthread_getspecific(fuse::MediaProviderWrapper::gJniEnvKey) != nullptr;
106 }
107 
108 const JNINativeMethod methods[] = {
109         {"native_new", "(Lcom/android/providers/media/MediaProvider;)J",
110          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_new)},
111         {"native_start", "(JILjava/lang/String;)V",
112          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_start)},
113         {"native_delete", "(J)V",
114          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete)},
115         {"native_should_open_with_fuse", "(JLjava/lang/String;ZI)Z",
116          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_should_open_with_fuse)},
117         {"native_is_fuse_thread", "()Z",
118          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_is_fuse_thread)},
119         {"native_is_started", "(J)Z",
120          reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_is_started)},
121         {"native_invalidate_fuse_dentry_cache", "(JLjava/lang/String;)V",
122          reinterpret_cast<void*>(
123                  com_android_providers_media_FuseDaemon_invalidate_fuse_dentry_cache)}};
124 }  // namespace
125 
register_android_providers_media_FuseDaemon(JavaVM * vm,JNIEnv * env)126 void register_android_providers_media_FuseDaemon(JavaVM* vm, JNIEnv* env) {
127     gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME)));
128 
129     if (gFuseDaemonClass == nullptr) {
130         LOG(FATAL) << "Unable to find class : " << CLASS_NAME;
131     }
132 
133     if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) {
134         LOG(FATAL) << "Unable to register native methods";
135     }
136 
137     fuse::MediaProviderWrapper::OneTimeInit(vm);
138 }
139 }  // namespace mediaprovider
140