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