Martijn Coenen | 6caa2d1 | 2020-01-30 15:13:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #define LOG_TAG "StorageManager" |
| 18 | #include <android-base/logging.h> |
| 19 | #include <android-base/unique_fd.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <linux/fs.h> |
| 22 | |
| 23 | #include <nativehelper/JNIHelp.h> |
| 24 | #include "core_jni_helpers.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | jboolean android_os_storage_StorageManager_setQuotaProjectId(JNIEnv* env, jobject self, |
| 29 | jstring path, jlong projectId) { |
| 30 | struct fsxattr fsx; |
| 31 | ScopedUtfChars utf_chars_path(env, path); |
| 32 | |
| 33 | if (projectId > UINT32_MAX) { |
| 34 | LOG(ERROR) << "Invalid project id: " << projectId; |
| 35 | return JNI_FALSE; |
| 36 | } |
| 37 | |
| 38 | android::base::unique_fd fd( |
| 39 | TEMP_FAILURE_RETRY(open(utf_chars_path.c_str(), O_RDONLY | O_CLOEXEC))); |
| 40 | if (fd == -1) { |
| 41 | PLOG(ERROR) << "Failed to open " << utf_chars_path.c_str() << " to set project id."; |
| 42 | return JNI_FALSE; |
| 43 | } |
| 44 | |
| 45 | int ret = ioctl(fd, FS_IOC_FSGETXATTR, &fsx); |
| 46 | if (ret == -1) { |
| 47 | PLOG(ERROR) << "Failed to get extended attributes for " << utf_chars_path.c_str() |
| 48 | << " to get project id."; |
| 49 | return JNI_FALSE; |
| 50 | } |
| 51 | |
| 52 | fsx.fsx_projid = projectId; |
| 53 | ret = ioctl(fd, FS_IOC_FSSETXATTR, &fsx); |
| 54 | if (ret == -1) { |
| 55 | PLOG(ERROR) << "Failed to set extended attributes for " << utf_chars_path.c_str() |
| 56 | << " to set project id."; |
| 57 | return JNI_FALSE; |
| 58 | } |
| 59 | |
| 60 | return JNI_TRUE; |
| 61 | } |
| 62 | |
| 63 | // ---------------------------------------------------------------------------- |
| 64 | |
| 65 | static const JNINativeMethod gStorageManagerMethods[] = { |
| 66 | {"setQuotaProjectId", "(Ljava/lang/String;J)Z", |
| 67 | (void*)android_os_storage_StorageManager_setQuotaProjectId}, |
| 68 | }; |
| 69 | |
| 70 | const char* const kStorageManagerPathName = "android/os/storage/StorageManager"; |
| 71 | |
| 72 | int register_android_os_storage_StorageManager(JNIEnv* env) { |
| 73 | return RegisterMethodsOrDie(env, kStorageManagerPathName, gStorageManagerMethods, |
| 74 | NELEM(gStorageManagerMethods)); |
| 75 | } |
| 76 | |
| 77 | }; // namespace android |