blob: 87861cae650a6cbbaf5fee240238178745eae7fc [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"
25#include "android-base/logging.h"
Zim3e45d9b2019-08-19 21:14:14 +010026
27namespace mediaprovider {
28namespace {
29
30constexpr const char* CLASS_NAME = "com/android/providers/media/fuse/FuseDaemon";
31static jclass gFuseDaemonClass;
32
Zimedbe69e2019-12-13 18:49:36 +000033jlong com_android_providers_media_FuseDaemon_new(JNIEnv* env, jobject self,
34 jobject media_provider) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010035 LOG(DEBUG) << "Creating the FUSE daemon...";
Zimedbe69e2019-12-13 18:49:36 +000036 return reinterpret_cast<jlong>(new fuse::FuseDaemon(env, media_provider));
Zim3e45d9b2019-08-19 21:14:14 +010037}
38
shafik8b57cd52019-09-06 10:51:29 +010039void com_android_providers_media_FuseDaemon_start(JNIEnv* env, jobject self, jlong java_daemon,
Zim7c8712d2019-10-03 21:01:26 +010040 jint fd, jstring java_path) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010041 LOG(DEBUG) << "Starting the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010042 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
43
Zim7c8712d2019-10-03 21:01:26 +010044 ScopedUtfChars utf_chars_path(env, java_path);
45 if (!utf_chars_path.c_str()) {
Zim3e45d9b2019-08-19 21:14:14 +010046 return;
47 }
Zim3e45d9b2019-08-19 21:14:14 +010048
Zimedbe69e2019-12-13 18:49:36 +000049 daemon->Start(fd, utf_chars_path.c_str());
Zim3e45d9b2019-08-19 21:14:14 +010050}
51
shafik8b57cd52019-09-06 10:51:29 +010052void com_android_providers_media_FuseDaemon_delete(JNIEnv* env, jobject self, jlong java_daemon) {
Narayan Kamath88203dc2019-08-30 17:19:38 +010053 LOG(DEBUG) << "Destroying the FUSE daemon...";
Zim3e45d9b2019-08-19 21:14:14 +010054 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
55 delete daemon;
56}
57
Zimedbe69e2019-12-13 18:49:36 +000058jboolean com_android_providers_media_FuseDaemon_should_open_with_fuse(JNIEnv* env, jobject self,
59 jlong java_daemon,
60 jstring java_path,
61 jboolean for_read, jint fd) {
62 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
63 if (daemon) {
64 ScopedUtfChars utf_chars_path(env, java_path);
65 if (!utf_chars_path.c_str()) {
66 // TODO(b/145741852): Throw exception
67 return JNI_FALSE;
68 }
69
70 return daemon->ShouldOpenWithFuse(fd, for_read, utf_chars_path.c_str());
71 }
72 // TODO(b/145741852): Throw exception
73 return JNI_FALSE;
74}
75
Zima76c3492020-02-19 01:23:26 +000076void com_android_providers_media_FuseDaemon_invalidate_fuse_dentry_cache(JNIEnv* env, jobject self,
77 jlong java_daemon,
78 jstring java_path) {
79 fuse::FuseDaemon* const daemon = reinterpret_cast<fuse::FuseDaemon*>(java_daemon);
80 if (daemon) {
81 ScopedUtfChars utf_chars_path(env, java_path);
82 if (!utf_chars_path.c_str()) {
83 // TODO(b/145741152): Throw exception
84 return;
85 }
86
87 daemon->InvalidateFuseDentryCache(utf_chars_path.c_str());
88 }
89 // TODO(b/145741152): Throw exception
90}
91
Zim3e45d9b2019-08-19 21:14:14 +010092const JNINativeMethod methods[] = {
Zim7c8712d2019-10-03 21:01:26 +010093 {"native_new", "(Lcom/android/providers/media/MediaProvider;)J",
94 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_new)},
95 {"native_start", "(JILjava/lang/String;)V",
96 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_start)},
Zim7c8712d2019-10-03 21:01:26 +010097 {"native_delete", "(J)V",
Zimedbe69e2019-12-13 18:49:36 +000098 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_delete)},
99 {"native_should_open_with_fuse", "(JLjava/lang/String;ZI)Z",
Zima76c3492020-02-19 01:23:26 +0000100 reinterpret_cast<void*>(com_android_providers_media_FuseDaemon_should_open_with_fuse)},
101 {"native_invalidate_fuse_dentry_cache", "(JLjava/lang/String;)V",
102 reinterpret_cast<void*>(
103 com_android_providers_media_FuseDaemon_invalidate_fuse_dentry_cache)}};
Zim3e45d9b2019-08-19 21:14:14 +0100104} // namespace
105
106void register_android_providers_media_FuseDaemon(JNIEnv* env) {
107 gFuseDaemonClass = static_cast<jclass>(env->NewGlobalRef(env->FindClass(CLASS_NAME)));
Narayan Kamath88203dc2019-08-30 17:19:38 +0100108
109 if (gFuseDaemonClass == nullptr) {
110 LOG(FATAL) << "Unable to find class : " << CLASS_NAME;
111 }
112
shafik8b57cd52019-09-06 10:51:29 +0100113 if (env->RegisterNatives(gFuseDaemonClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) {
Narayan Kamath88203dc2019-08-30 17:19:38 +0100114 LOG(FATAL) << "Unable to register native methods";
115 }
Zim3e45d9b2019-08-19 21:14:14 +0100116}
117} // namespace mediaprovider