blob: 0aaa2b158da8bfa865a235fd5f36df79a8800ba0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/libs/android_runtime/android_util_Process.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
Elliott Hughesdd66bcb2011-04-12 11:28:59 -07005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Elliott Hughesdd66bcb2011-04-12 11:28:59 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
18#define LOG_TAG "FileUtils"
19
20#include <utils/Log.h>
21
22#include <android_runtime/AndroidRuntime.h>
23
24#include "JNIHelp.h"
25
26#include <sys/errno.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <fcntl.h>
30#include <signal.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031#include <sys/ioctl.h>
32#include <linux/msdos_fs.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34namespace android {
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036jint android_os_FileUtils_setPermissions(JNIEnv* env, jobject clazz,
37 jstring file, jint mode,
38 jint uid, jint gid)
39{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 const jchar* str = env->GetStringCritical(file, 0);
41 String8 file8;
42 if (str) {
43 file8 = String8(str, env->GetStringLength(file));
44 env->ReleaseStringCritical(file, str);
45 }
46 if (file8.size() <= 0) {
47 return ENOENT;
48 }
49 if (uid >= 0 || gid >= 0) {
50 int res = chown(file8.string(), uid, gid);
51 if (res != 0) {
52 return errno;
53 }
54 }
55 return chmod(file8.string(), mode) == 0 ? 0 : errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056}
57
Dianne Hackborn053f61d2013-06-26 18:07:43 -070058jint android_os_FileUtils_getUid(JNIEnv* env, jobject clazz, jstring file)
59{
60 struct stat stats;
61 const jchar* str = env->GetStringCritical(file, 0);
62 String8 file8;
63 if (str) {
64 file8 = String8(str, env->GetStringLength(file));
65 env->ReleaseStringCritical(file, str);
66 }
67 if (file8.size() <= 0) {
68 return ENOENT;
69 }
70 if (stat(file8.string(), &stats) < 0) {
71 return -1;
72 }
73 return stats.st_uid;
74}
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076jint android_os_FileUtils_getFatVolumeId(JNIEnv* env, jobject clazz, jstring path)
77{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 if (path == NULL) {
79 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
80 return -1;
81 }
82 const char *pathStr = env->GetStringUTFChars(path, NULL);
83 int result = -1;
84 // only if our system supports this ioctl
85 #ifdef VFAT_IOCTL_GET_VOLUME_ID
86 int fd = open(pathStr, O_RDONLY);
87 if (fd >= 0) {
88 result = ioctl(fd, VFAT_IOCTL_GET_VOLUME_ID);
89 close(fd);
90 }
91 #endif
92
93 env->ReleaseStringUTFChars(path, pathStr);
94 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095}
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097static const JNINativeMethod methods[] = {
98 {"setPermissions", "(Ljava/lang/String;III)I", (void*)android_os_FileUtils_setPermissions},
Dianne Hackborn053f61d2013-06-26 18:07:43 -070099 {"getUid", "(Ljava/lang/String;)I", (void*)android_os_FileUtils_getUid},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 {"getFatVolumeId", "(Ljava/lang/String;)I", (void*)android_os_FileUtils_getFatVolumeId},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101};
102
103static const char* const kFileUtilsPathName = "android/os/FileUtils";
104
105int register_android_os_FileUtils(JNIEnv* env)
106{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 return AndroidRuntime::registerNativeMethods(
108 env, kFileUtilsPathName,
109 methods, NELEM(methods));
110}
111
112}