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 specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "incremental_manager-jni"
18 
19 #include "core_jni_helpers.h"
20 #include "incfs_ndk.h"
21 #include "jni.h"
22 #include "nativehelper/JNIHelp.h"
23 
24 #include <iterator>
25 #include <memory>
26 
27 namespace android {
28 
nativeIsEnabled(JNIEnv * env,jobject clazz)29 static jboolean nativeIsEnabled(JNIEnv* env, jobject clazz) {
30     return IncFs_IsEnabled();
31 }
32 
nativeIsV2Available(JNIEnv * env,jobject clazz)33 static jboolean nativeIsV2Available(JNIEnv* env, jobject clazz) {
34     return !!(IncFs_Features() & INCFS_FEATURE_V2);
35 }
36 
nativeIsIncrementalPath(JNIEnv * env,jobject clazz,jstring javaPath)37 static jboolean nativeIsIncrementalPath(JNIEnv* env,
38                                     jobject clazz,
39                                     jstring javaPath) {
40     ScopedUtfChars path(env, javaPath);
41     return (jboolean)IncFs_IsIncFsPath(path.c_str());
42 }
43 
nativeIsIncrementalFd(JNIEnv * env,jobject clazz,jint fd)44 static jboolean nativeIsIncrementalFd(JNIEnv* env, jobject clazz, jint fd) {
45     return (jboolean)IncFs_IsIncFsFd(fd);
46 }
47 
nativeUnsafeGetFileSignature(JNIEnv * env,jobject clazz,jstring javaPath)48 static jbyteArray nativeUnsafeGetFileSignature(JNIEnv* env, jobject clazz, jstring javaPath) {
49     ScopedUtfChars path(env, javaPath);
50 
51     char signature[INCFS_MAX_SIGNATURE_SIZE];
52     size_t size = sizeof(signature);
53     if (IncFs_UnsafeGetSignatureByPath(path.c_str(), signature, &size) < 0) {
54         return nullptr;
55     }
56 
57     jbyteArray result = env->NewByteArray(size);
58     if (result != nullptr) {
59         env->SetByteArrayRegion(result, 0, size, (const jbyte*)signature);
60     }
61     return result;
62 }
63 
64 static const JNINativeMethod method_table[] =
65         {{"nativeIsEnabled", "()Z", (void*)nativeIsEnabled},
66          {"nativeIsV2Available", "()Z", (void*)nativeIsV2Available},
67          {"nativeIsIncrementalPath", "(Ljava/lang/String;)Z", (void*)nativeIsIncrementalPath},
68          {"nativeIsIncrementalFd", "(I)Z", (void*)nativeIsIncrementalFd},
69          {"nativeUnsafeGetFileSignature", "(Ljava/lang/String;)[B",
70           (void*)nativeUnsafeGetFileSignature}};
71 
register_android_os_incremental_IncrementalManager(JNIEnv * env)72 int register_android_os_incremental_IncrementalManager(JNIEnv* env) {
73     return jniRegisterNativeMethods(env, "android/os/incremental/IncrementalManager",
74                                     method_table, std::size(method_table));
75 }
76 
77 } // namespace android
78