The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_util_Process.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 5 | ** 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 11 | ** 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | ** 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <sys/ioctl.h> |
| 32 | #include <linux/msdos_fs.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | jint android_os_FileUtils_setPermissions(JNIEnv* env, jobject clazz, |
| 37 | jstring file, jint mode, |
| 38 | jint uid, jint gid) |
| 39 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | jint android_os_FileUtils_getFatVolumeId(JNIEnv* env, jobject clazz, jstring path) |
| 59 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
| 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | static const JNINativeMethod methods[] = { |
| 80 | {"setPermissions", "(Ljava/lang/String;III)I", (void*)android_os_FileUtils_setPermissions}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | {"getFatVolumeId", "(Ljava/lang/String;)I", (void*)android_os_FileUtils_getFatVolumeId}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static const char* const kFileUtilsPathName = "android/os/FileUtils"; |
| 85 | |
| 86 | int register_android_os_FileUtils(JNIEnv* env) |
| 87 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | return AndroidRuntime::registerNativeMethods( |
| 89 | env, kFileUtilsPathName, |
| 90 | methods, NELEM(methods)); |
| 91 | } |
| 92 | |
| 93 | } |