Home | History | Annotate | Download | only in verifier
      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 #include <jni.h>
     18 #include <stdio.h>
     19 #include <sys/types.h>
     20 #include <sys/stat.h>
     21 #include <grp.h>
     22 #include <pwd.h>
     23 #include <unistd.h>
     24 
     25 static jfieldID gFileStatusDevFieldID;
     26 static jfieldID gFileStatusInoFieldID;
     27 static jfieldID gFileStatusModeFieldID;
     28 static jfieldID gFileStatusNlinkFieldID;
     29 static jfieldID gFileStatusUidFieldID;
     30 static jfieldID gFileStatusGidFieldID;
     31 static jfieldID gFileStatusSizeFieldID;
     32 static jfieldID gFileStatusBlksizeFieldID;
     33 static jfieldID gFileStatusBlocksFieldID;
     34 static jfieldID gFileStatusAtimeFieldID;
     35 static jfieldID gFileStatusMtimeFieldID;
     36 static jfieldID gFileStatusCtimeFieldID;
     37 static jfieldID gFileStatusExecutableID;
     38 
     39 /* Copied from hidden API: frameworks/base/core/jni/android_os_FileUtils.cpp */
     40 jboolean com_android_cts_verifier_os_FileUtils_getFileStatus(JNIEnv* env, jobject thiz,
     41         jstring path, jobject fileStatus, jboolean statLinks)
     42 {
     43     const char* pathStr = env->GetStringUTFChars(path, NULL);
     44     jboolean ret = false;
     45     struct stat s;
     46 
     47     int res = statLinks == true ? lstat(pathStr, &s) : stat(pathStr, &s);
     48 
     49     if (res == 0) {
     50         ret = true;
     51         if (fileStatus != NULL) {
     52             env->SetIntField(fileStatus, gFileStatusDevFieldID, s.st_dev);
     53             env->SetIntField(fileStatus, gFileStatusInoFieldID, s.st_ino);
     54             env->SetIntField(fileStatus, gFileStatusModeFieldID, s.st_mode);
     55             env->SetIntField(fileStatus, gFileStatusNlinkFieldID, s.st_nlink);
     56             env->SetIntField(fileStatus, gFileStatusUidFieldID, s.st_uid);
     57             env->SetIntField(fileStatus, gFileStatusGidFieldID, s.st_gid);
     58             env->SetLongField(fileStatus, gFileStatusSizeFieldID, s.st_size);
     59             env->SetIntField(fileStatus, gFileStatusBlksizeFieldID, s.st_blksize);
     60             env->SetLongField(fileStatus, gFileStatusBlocksFieldID, s.st_blocks);
     61             env->SetLongField(fileStatus, gFileStatusAtimeFieldID, s.st_atime);
     62             env->SetLongField(fileStatus, gFileStatusMtimeFieldID, s.st_mtime);
     63             env->SetLongField(fileStatus, gFileStatusCtimeFieldID, s.st_ctime);
     64         }
     65         if (access(pathStr, X_OK) == 0) {
     66             env->SetBooleanField(fileStatus, gFileStatusExecutableID, JNI_TRUE);
     67         } else {
     68             env->SetBooleanField(fileStatus, gFileStatusExecutableID, JNI_FALSE);
     69         }
     70     }
     71 
     72     env->ReleaseStringUTFChars(path, pathStr);
     73 
     74     return ret;
     75 }
     76 
     77 jstring com_android_cts_verifier_os_FileUtils_getUserName(JNIEnv* env, jobject thiz,
     78         jint uid)
     79 {
     80     struct passwd *pwd = getpwuid(uid);
     81     return env->NewStringUTF(pwd->pw_name);
     82 }
     83 
     84 jstring com_android_cts_verifier_os_FileUtils_getGroupName(JNIEnv* env, jobject thiz,
     85         jint gid)
     86 {
     87     struct group *grp = getgrgid(gid);
     88     return env->NewStringUTF(grp->gr_name);
     89 }
     90 
     91 static JNINativeMethod gMethods[] = {
     92     {  "getFileStatus", "(Ljava/lang/String;Lcom/android/cts/verifier/os/FileUtils$FileStatus;Z)Z",
     93             (void *) com_android_cts_verifier_os_FileUtils_getFileStatus  },
     94     {  "getUserName", "(I)Ljava/lang/String;",
     95             (void *) com_android_cts_verifier_os_FileUtils_getUserName  },
     96     {  "getGroupName", "(I)Ljava/lang/String;",
     97             (void *) com_android_cts_verifier_os_FileUtils_getGroupName  },
     98 };
     99 
    100 int register_com_android_cts_verifier_os_FileUtils(JNIEnv* env)
    101 {
    102     jclass clazz = env->FindClass("com/android/cts/verifier/os/FileUtils");
    103 
    104     jclass fileStatusClass = env->FindClass("com/android/cts/verifier/os/FileUtils$FileStatus");
    105     gFileStatusDevFieldID = env->GetFieldID(fileStatusClass, "dev", "I");
    106     gFileStatusInoFieldID = env->GetFieldID(fileStatusClass, "ino", "I");
    107     gFileStatusModeFieldID = env->GetFieldID(fileStatusClass, "mode", "I");
    108     gFileStatusNlinkFieldID = env->GetFieldID(fileStatusClass, "nlink", "I");
    109     gFileStatusUidFieldID = env->GetFieldID(fileStatusClass, "uid", "I");
    110     gFileStatusGidFieldID = env->GetFieldID(fileStatusClass, "gid", "I");
    111     gFileStatusSizeFieldID = env->GetFieldID(fileStatusClass, "size", "J");
    112     gFileStatusBlksizeFieldID = env->GetFieldID(fileStatusClass, "blksize", "I");
    113     gFileStatusBlocksFieldID = env->GetFieldID(fileStatusClass, "blocks", "J");
    114     gFileStatusAtimeFieldID = env->GetFieldID(fileStatusClass, "atime", "J");
    115     gFileStatusMtimeFieldID = env->GetFieldID(fileStatusClass, "mtime", "J");
    116     gFileStatusCtimeFieldID = env->GetFieldID(fileStatusClass, "ctime", "J");
    117     gFileStatusExecutableID = env->GetFieldID(fileStatusClass, "executable", "Z");
    118 
    119     return env->RegisterNatives(clazz, gMethods,
    120             sizeof(gMethods) / sizeof(JNINativeMethod));
    121 }
    122