Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "Ntfs.h" |
| 18 | #include "Utils.h" |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/stringprintf.h> |
| 22 | |
| 23 | #include <vector> |
| 24 | #include <string> |
| 25 | |
| 26 | #include <sys/mount.h> |
| 27 | |
| 28 | using android::base::StringPrintf; |
| 29 | |
| 30 | namespace android { |
| 31 | namespace vold { |
| 32 | namespace ntfs { |
| 33 | |
| 34 | static const char* kMkfsPath = "/system/bin/mkfs.ntfs"; |
| 35 | static const char* kFsckPath = "/system/bin/fsck.ntfs"; |
dhacker29 | ac2ed12 | 2015-12-06 05:32:30 -0500 | [diff] [blame] | 36 | #ifdef CONFIG_KERNEL_HAVE_NTFS |
| 37 | static const char* kMountPath = "/system/bin/mount"; |
| 38 | #else |
Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 39 | static const char* kMountPath = "/system/bin/mount.ntfs"; |
dhacker29 | ac2ed12 | 2015-12-06 05:32:30 -0500 | [diff] [blame] | 40 | #endif |
Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 41 | |
| 42 | bool IsSupported() { |
| 43 | return access(kMkfsPath, X_OK) == 0 |
| 44 | && access(kFsckPath, X_OK) == 0 |
| 45 | && access(kMountPath, X_OK) == 0 |
| 46 | && IsFilesystemSupported("ntfs"); |
| 47 | } |
| 48 | |
| 49 | status_t Check(const std::string& source) { |
| 50 | std::vector<std::string> cmd; |
| 51 | cmd.push_back(kFsckPath); |
| 52 | cmd.push_back("-n"); |
| 53 | cmd.push_back(source); |
| 54 | |
Michael Bestas | 7e0bc98 | 2015-12-06 23:53:55 +0200 | [diff] [blame] | 55 | // Ntfs devices are currently always untrusted |
| 56 | return ForkExecvp(cmd, sFsckUntrustedContext); |
Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | status_t Mount(const std::string& source, const std::string& target, bool ro, |
| 60 | bool remount, bool executable, int ownerUid, int ownerGid, int permMask, |
| 61 | bool createLost) { |
| 62 | char mountData[255]; |
| 63 | |
| 64 | const char* c_source = source.c_str(); |
| 65 | const char* c_target = target.c_str(); |
| 66 | |
| 67 | sprintf(mountData, |
dhacker29 | ac2ed12 | 2015-12-06 05:32:30 -0500 | [diff] [blame] | 68 | #ifdef CONFIG_KERNEL_HAVE_NTFS |
| 69 | "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,nodev,nosuid", |
| 70 | #else |
Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 71 | "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o," |
| 72 | "shortname=mixed,nodev,nosuid,dirsync", |
dhacker29 | ac2ed12 | 2015-12-06 05:32:30 -0500 | [diff] [blame] | 73 | #endif |
Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 74 | ownerUid, ownerGid, permMask, permMask); |
| 75 | |
| 76 | if (!executable) |
Rashed Abdel-Tawab | 4ac9032 | 2017-10-26 22:24:22 -0700 | [diff] [blame] | 77 | strlcat(mountData, ",noexec", sizeof(mountData)); |
Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 78 | if (ro) |
Rashed Abdel-Tawab | 4ac9032 | 2017-10-26 22:24:22 -0700 | [diff] [blame] | 79 | strlcat(mountData, ",ro", sizeof(mountData)); |
Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 80 | if (remount) |
Rashed Abdel-Tawab | 4ac9032 | 2017-10-26 22:24:22 -0700 | [diff] [blame] | 81 | strlcat(mountData, ",remount", sizeof(mountData)); |
Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 82 | |
| 83 | std::vector<std::string> cmd; |
| 84 | cmd.push_back(kMountPath); |
dhacker29 | ac2ed12 | 2015-12-06 05:32:30 -0500 | [diff] [blame] | 85 | #ifdef CONFIG_KERNEL_HAVE_NTFS |
| 86 | cmd.push_back("-t"); |
| 87 | cmd.push_back("ntfs"); |
| 88 | #endif |
Dan Pasanen | d250961 | 2015-10-27 22:52:37 -0500 | [diff] [blame] | 89 | cmd.push_back("-o"); |
| 90 | cmd.push_back(mountData); |
| 91 | cmd.push_back(c_source); |
| 92 | cmd.push_back(c_target); |
| 93 | |
| 94 | return ForkExecvp(cmd); |
| 95 | } |
| 96 | |
| 97 | status_t Format(const std::string& source, bool wipe) { |
| 98 | std::vector<std::string> cmd; |
| 99 | cmd.push_back(kMkfsPath); |
| 100 | if (wipe) |
| 101 | cmd.push_back("-f"); |
| 102 | cmd.push_back(source); |
| 103 | |
| 104 | return ForkExecvp(cmd); |
| 105 | } |
| 106 | |
| 107 | } // namespace ntfs |
| 108 | } // namespace vold |
| 109 | } // namespace android |