• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/common/device-side/util/src/com/android/compatibility/common/util/FileUtils.java
44  *
45  * Copied from hidden API: frameworks/base/core/jni/android_FileUtils.cpp
46  */
47 
android_cts_FileUtils_getFileStatus(JNIEnv * env,jobject thiz,jstring path,jobject fileStatus,jboolean statLinks)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 
android_cts_FileUtils_getUserName(JNIEnv * env,jobject thiz,jint uid)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 
android_cts_FileUtils_getGroupName(JNIEnv * env,jobject thiz,jint gid)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 
android_cts_FileUtils_setPermissions(JNIEnv * env,jobject clazz,jstring file,jint mode)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",
114             "(Ljava/lang/String;Lcom/android/compatibility/common/util/FileUtils$FileStatus;Z)Z",
115             (void *) android_cts_FileUtils_getFileStatus  },
116     {  "getUserName", "(I)Ljava/lang/String;",
117             (void *) android_cts_FileUtils_getUserName  },
118     {  "getGroupName", "(I)Ljava/lang/String;",
119             (void *) android_cts_FileUtils_getGroupName  },
120     {  "setPermissions", "(Ljava/lang/String;I)I",
121             (void *) android_cts_FileUtils_setPermissions },
122 };
123 
register_android_cts_FileUtils(JNIEnv * env)124 int register_android_cts_FileUtils(JNIEnv* env)
125 {
126     jclass clazz = env->FindClass("com/android/compatibility/common/util/FileUtils");
127     assert(clazz != null);
128 
129     gFileStatusClass = env->FindClass(
130             "com/android/compatibility/common/util/FileUtils$FileStatus");
131     assert(gFileStatusClass != null);
132     gFileStatusDevFieldID = env->GetFieldID(gFileStatusClass, "dev", "I");
133     gFileStatusInoFieldID = env->GetFieldID(gFileStatusClass, "ino", "I");
134     gFileStatusModeFieldID = env->GetFieldID(gFileStatusClass, "mode", "I");
135     gFileStatusNlinkFieldID = env->GetFieldID(gFileStatusClass, "nlink", "I");
136     gFileStatusUidFieldID = env->GetFieldID(gFileStatusClass, "uid", "I");
137     gFileStatusGidFieldID = env->GetFieldID(gFileStatusClass, "gid", "I");
138     gFileStatusSizeFieldID = env->GetFieldID(gFileStatusClass, "size", "J");
139     gFileStatusBlksizeFieldID = env->GetFieldID(gFileStatusClass, "blksize", "I");
140     gFileStatusBlocksFieldID = env->GetFieldID(gFileStatusClass, "blocks", "J");
141     gFileStatusAtimeFieldID = env->GetFieldID(gFileStatusClass, "atime", "J");
142     gFileStatusMtimeFieldID = env->GetFieldID(gFileStatusClass, "mtime", "J");
143     gFileStatusCtimeFieldID = env->GetFieldID(gFileStatusClass, "ctime", "J");
144 
145     return env->RegisterNatives(clazz, gMethods,
146             sizeof(gMethods) / sizeof(JNINativeMethod));
147 }
148