Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2010 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 "DefContainer-JNI"
     18 
     19 #include <JNIHelp.h>
     20 
     21 #include <diskusage/dirsize.h>
     22 #include <utils/Log.h>
     23 
     24 #include <sys/types.h>
     25 #include <sys/stat.h>
     26 #include <errno.h>
     27 #include <fcntl.h>
     28 #include <string.h>
     29 
     30 namespace android {
     31 
     32 static jlong native_measureDirectory(JNIEnv* env, jobject clazz, jstring directory) {
     33     jlong ret = 0L;
     34 
     35     const char* path = env->GetStringUTFChars(directory, NULL);
     36     if (path == NULL) {
     37         return ret;
     38     }
     39 
     40     int dirfd = open(path, O_DIRECTORY, O_RDONLY);
     41     if (dirfd < 0) {
     42         ALOGI("error opening: %s: %s", path, strerror(errno));
     43     } else {
     44         ret = calculate_dir_size(dirfd);
     45         close(dirfd);
     46     }
     47 
     48     env->ReleaseStringUTFChars(directory, path);
     49 
     50     return ret;
     51 }
     52 
     53 static const JNINativeMethod g_methods[] = {
     54     { "native_measureDirectory", "(Ljava/lang/String;)J", (void*)native_measureDirectory },
     55 };
     56 
     57 int register_com_android_defcontainer(JNIEnv *env) {
     58     if (jniRegisterNativeMethods(
     59             env, "com/android/defcontainer/MeasurementUtils", g_methods, NELEM(g_methods)) < 0) {
     60         return JNI_ERR;
     61     }
     62 
     63     return JNI_VERSION_1_6;
     64 }
     65 
     66 } // namespace android
     67 
     68 int JNI_OnLoad(JavaVM *jvm, void* reserved) {
     69     JNIEnv *env;
     70 
     71     if (jvm->GetEnv((void**)&env, JNI_VERSION_1_6)) {
     72         return JNI_ERR;
     73     }
     74 
     75     return android::register_com_android_defcontainer(env);
     76 }
     77