com_android_server_PersistentDataBlockService.cpp revision 68d4acd205e8c2da524e62734ca42847306cc029
1/*
2 * Copyright (C) 2010 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 <android_runtime/AndroidRuntime.h>
18#include <JNIHelp.h>
19#include <jni.h>
20
21#include <utils/misc.h>
22#include <sys/ioctl.h>
23#include <sys/mount.h>
24
25#include <inttypes.h>
26#include <fcntl.h>
27
28namespace android {
29
30    uint64_t get_block_device_size(int fd)
31    {
32        uint64_t size = 0;
33        int ret;
34
35        ret = ioctl(fd, BLKGETSIZE64, &size);
36
37        if (ret)
38            return 0;
39
40        return size;
41    }
42
43    static jlong com_android_server_PeristentDataBlockService_getBlockDeviceSize(JNIEnv *env, jclass, jstring jpath) {
44        const char *path = env->GetStringUTFChars(jpath, 0);
45        int fd = open(path, O_RDONLY);
46
47        if (fd < 0)
48            return 0;
49
50        return get_block_device_size(fd);
51    }
52
53    static JNINativeMethod sMethods[] = {
54         /* name, signature, funcPtr */
55        {"getBlockDeviceSize", "(Ljava/lang/String;)J", (void*)com_android_server_PeristentDataBlockService_getBlockDeviceSize},
56    };
57
58    int register_android_server_PersistentDataBlockService(JNIEnv* env)
59    {
60        return jniRegisterNativeMethods(env, "com/android/server/PersistentDataBlockService",
61                                        sMethods, NELEM(sMethods));
62    }
63
64} /* namespace android */