blob: a07f5b770b12da0b341da76b39312b23107e7165 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058jint android_os_FileUtils_getFatVolumeId(JNIEnv* env, jobject clazz, jstring path)
59{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 if (path == NULL) {
61 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
62 return -1;
63 }
64 const char *pathStr = env->GetStringUTFChars(path, NULL);
65 int result = -1;
66 // only if our system supports this ioctl
67 #ifdef VFAT_IOCTL_GET_VOLUME_ID
68 int fd = open(pathStr, O_RDONLY);
69 if (fd >= 0) {
70 result = ioctl(fd, VFAT_IOCTL_GET_VOLUME_ID);
71 close(fd);
72 }
73 #endif
74
75 env->ReleaseStringUTFChars(path, pathStr);
76 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077}
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079static const JNINativeMethod methods[] = {
80 {"setPermissions", "(Ljava/lang/String;III)I", (void*)android_os_FileUtils_setPermissions},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 {"getFatVolumeId", "(Ljava/lang/String;)I", (void*)android_os_FileUtils_getFatVolumeId},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082};
83
84static const char* const kFileUtilsPathName = "android/os/FileUtils";
85
86int register_android_os_FileUtils(JNIEnv* env)
87{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 return AndroidRuntime::registerNativeMethods(
89 env, kFileUtilsPathName,
90 methods, NELEM(methods));
91}
92
93}