blob: 76330b4f080e2593e18929116442dd0dac7e7b65 [file] [log] [blame]
Dan Pasanend2509612015-10-27 22:52:37 -05001/*
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
28using android::base::StringPrintf;
29
30namespace android {
31namespace vold {
32namespace ntfs {
33
34static const char* kMkfsPath = "/system/bin/mkfs.ntfs";
35static const char* kFsckPath = "/system/bin/fsck.ntfs";
dhacker29ac2ed122015-12-06 05:32:30 -050036#ifdef CONFIG_KERNEL_HAVE_NTFS
37static const char* kMountPath = "/system/bin/mount";
38#else
Dan Pasanend2509612015-10-27 22:52:37 -050039static const char* kMountPath = "/system/bin/mount.ntfs";
dhacker29ac2ed122015-12-06 05:32:30 -050040#endif
Dan Pasanend2509612015-10-27 22:52:37 -050041
42bool 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
49status_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 Bestas7e0bc982015-12-06 23:53:55 +020055 // Ntfs devices are currently always untrusted
56 return ForkExecvp(cmd, sFsckUntrustedContext);
Dan Pasanend2509612015-10-27 22:52:37 -050057}
58
59status_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,
dhacker29ac2ed122015-12-06 05:32:30 -050068#ifdef CONFIG_KERNEL_HAVE_NTFS
69 "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,nodev,nosuid",
70#else
Dan Pasanend2509612015-10-27 22:52:37 -050071 "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,"
72 "shortname=mixed,nodev,nosuid,dirsync",
dhacker29ac2ed122015-12-06 05:32:30 -050073#endif
Dan Pasanend2509612015-10-27 22:52:37 -050074 ownerUid, ownerGid, permMask, permMask);
75
76 if (!executable)
Rashed Abdel-Tawab4ac90322017-10-26 22:24:22 -070077 strlcat(mountData, ",noexec", sizeof(mountData));
Dan Pasanend2509612015-10-27 22:52:37 -050078 if (ro)
Rashed Abdel-Tawab4ac90322017-10-26 22:24:22 -070079 strlcat(mountData, ",ro", sizeof(mountData));
Dan Pasanend2509612015-10-27 22:52:37 -050080 if (remount)
Rashed Abdel-Tawab4ac90322017-10-26 22:24:22 -070081 strlcat(mountData, ",remount", sizeof(mountData));
Dan Pasanend2509612015-10-27 22:52:37 -050082
83 std::vector<std::string> cmd;
84 cmd.push_back(kMountPath);
dhacker29ac2ed122015-12-06 05:32:30 -050085#ifdef CONFIG_KERNEL_HAVE_NTFS
86 cmd.push_back("-t");
87 cmd.push_back("ntfs");
88#endif
Dan Pasanend2509612015-10-27 22:52:37 -050089 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
97status_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