1 /* 2 * Copyright (C) 2011 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 <grp.h> 18 #include <jni.h> 19 #include <pwd.h> 20 #include <stdio.h> 21 #include <string.h> 22 #include <sys/errno.h> 23 #include <sys/types.h> 24 #include <sys/stat.h> 25 #include <assert.h> 26 27 static jclass gFileStatusClass; 28 static jfieldID gFileStatusDevFieldID; 29 static jfieldID gFileStatusInoFieldID; 30 static jfieldID gFileStatusModeFieldID; 31 static jfieldID gFileStatusNlinkFieldID; 32 static jfieldID gFileStatusUidFieldID; 33 static jfieldID gFileStatusGidFieldID; 34 static jfieldID gFileStatusSizeFieldID; 35 static jfieldID gFileStatusBlksizeFieldID; 36 static jfieldID gFileStatusBlocksFieldID; 37 static jfieldID gFileStatusAtimeFieldID; 38 static jfieldID gFileStatusMtimeFieldID; 39 static jfieldID gFileStatusCtimeFieldID; 40 41 /* 42 * Native methods used by 43 * cts/libs/deviceutil/src/android/cts/util/FileUtils.java 44 * 45 * Copied from hidden API: frameworks/base/core/jni/android_FileUtils.cpp 46 */ 47 48 jboolean android_cts_FileUtils_getFileStatus(JNIEnv* env, jobject thiz, 49 jstring path, jobject fileStatus, jboolean statLinks) 50 { 51 const char* pathStr = env->GetStringUTFChars(path, NULL); 52 jboolean ret = false; 53 struct stat s; 54 55 int res = statLinks == true ? lstat(pathStr, &s) : stat(pathStr, &s); 56 57 if (res == 0) { 58 ret = true; 59 if (fileStatus != NULL) { 60 env->SetIntField(fileStatus, gFileStatusDevFieldID, s.st_dev); 61 env->SetIntField(fileStatus, gFileStatusInoFieldID, s.st_ino); 62 env->SetIntField(fileStatus, gFileStatusModeFieldID, s.st_mode); 63 env->SetIntField(fileStatus, gFileStatusNlinkFieldID, s.st_nlink); 64 env->SetIntField(fileStatus, gFileStatusUidFieldID, s.st_uid); 65 env->SetIntField(fileStatus, gFileStatusGidFieldID, s.st_gid); 66 env->SetLongField(fileStatus, gFileStatusSizeFieldID, s.st_size); 67 env->SetIntField(fileStatus, gFileStatusBlksizeFieldID, s.st_blksize); 68 env->SetLongField(fileStatus, gFileStatusBlocksFieldID, s.st_blocks); 69 env->SetLongField(fileStatus, gFileStatusAtimeFieldID, s.st_atime); 70 env->SetLongField(fileStatus, gFileStatusMtimeFieldID, s.st_mtime); 71 env->SetLongField(fileStatus, gFileStatusCtimeFieldID, s.st_ctime); 72 } 73 } 74 75 env->ReleaseStringUTFChars(path, pathStr); 76 77 return ret; 78 } 79 80 jstring android_cts_FileUtils_getUserName(JNIEnv* env, jobject thiz, 81 jint uid) 82 { 83 struct passwd *pwd = getpwuid(uid); 84 return env->NewStringUTF(pwd->pw_name); 85 } 86 87 jstring android_cts_FileUtils_getGroupName(JNIEnv* env, jobject thiz, 88 jint gid) 89 { 90 struct group *grp = getgrgid(gid); 91 return env->NewStringUTF(grp->gr_name); 92 } 93 94 jint android_cts_FileUtils_setPermissions(JNIEnv* env, jobject clazz, 95 jstring file, jint mode) 96 { 97 const char *fileStr = env->GetStringUTFChars(file, NULL); 98 if (fileStr == NULL) { 99 return -1; 100 } 101 102 if (strlen(fileStr) <= 0) { 103 env->ReleaseStringUTFChars(file, fileStr); 104 return ENOENT; 105 } 106 107 jint returnValue = chmod(fileStr, mode) == 0 ? 0 : errno; 108 env->ReleaseStringUTFChars(file, fileStr); 109 return returnValue; 110 } 111 112 static JNINativeMethod gMethods[] = { 113 { "getFileStatus", "(Ljava/lang/String;Landroid/cts/util/FileUtils$FileStatus;Z)Z", 114 (void *) android_cts_FileUtils_getFileStatus }, 115 { "getUserName", "(I)Ljava/lang/String;", 116 (void *) android_cts_FileUtils_getUserName }, 117 { "getGroupName", "(I)Ljava/lang/String;", 118 (void *) android_cts_FileUtils_getGroupName }, 119 { "setPermissions", "(Ljava/lang/String;I)I", 120 (void *) android_cts_FileUtils_setPermissions }, 121 }; 122 123 int register_android_cts_FileUtils(JNIEnv* env) 124 { 125 jclass clazz = env->FindClass("android/cts/util/FileUtils"); 126 assert(clazz != null); 127 128 gFileStatusClass = env->FindClass("android/cts/util/FileUtils$FileStatus"); 129 assert(gFileStatusClass != null); 130 gFileStatusDevFieldID = env->GetFieldID(gFileStatusClass, "dev", "I"); 131 gFileStatusInoFieldID = env->GetFieldID(gFileStatusClass, "ino", "I"); 132 gFileStatusModeFieldID = env->GetFieldID(gFileStatusClass, "mode", "I"); 133 gFileStatusNlinkFieldID = env->GetFieldID(gFileStatusClass, "nlink", "I"); 134 gFileStatusUidFieldID = env->GetFieldID(gFileStatusClass, "uid", "I"); 135 gFileStatusGidFieldID = env->GetFieldID(gFileStatusClass, "gid", "I"); 136 gFileStatusSizeFieldID = env->GetFieldID(gFileStatusClass, "size", "J"); 137 gFileStatusBlksizeFieldID = env->GetFieldID(gFileStatusClass, "blksize", "I"); 138 gFileStatusBlocksFieldID = env->GetFieldID(gFileStatusClass, "blocks", "J"); 139 gFileStatusAtimeFieldID = env->GetFieldID(gFileStatusClass, "atime", "J"); 140 gFileStatusMtimeFieldID = env->GetFieldID(gFileStatusClass, "mtime", "J"); 141 gFileStatusCtimeFieldID = env->GetFieldID(gFileStatusClass, "ctime", "J"); 142 143 return env->RegisterNatives(clazz, gMethods, 144 sizeof(gMethods) / sizeof(JNINativeMethod)); 145 } 146