blob: a00c5ad6add7b7d9f25cfe27d7dfdf3766b6b3d6 [file] [log] [blame]
Dan Pasanend1952662015-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 <sys/mount.h>
18
19#include <android-base/logging.h>
20#include <android-base/stringprintf.h>
21
22#include <logwrap/logwrap.h>
23
24#include "Ntfs.h"
25#include "Utils.h"
26
27using android::base::StringPrintf;
28
29namespace android {
30namespace vold {
31namespace ntfs {
32
33static const char* kMkfsPath = "/system/bin/mkfs.ntfs";
34static const char* kFsckPath = "/system/bin/fsck.ntfs";
35static const char* kMountPath = "/system/bin/mount.ntfs";
36
37bool IsSupported() {
38 return access(kMkfsPath, X_OK) == 0
39 && access(kFsckPath, X_OK) == 0
40 && access(kMountPath, X_OK) == 0
41 && IsFilesystemSupported("ntfs");
42}
43
44status_t Check(const std::string& source) {
45 std::vector<std::string> cmd;
46 cmd.push_back(kFsckPath);
47 cmd.push_back("-n");
48 cmd.push_back(source);
49
50 int rc = ForkExecvp(cmd, sFsckUntrustedContext);
51 if (rc == 0) {
52 LOG(INFO) << "Check OK";
53 return 0;
54 } else {
55 LOG(ERROR) << "Check failed (code " << rc << ")";
56 errno = EIO;
57 return -1;
58 }
59}
60
61status_t Mount(const std::string& source, const std::string& target, int ownerUid, int ownerGid,
62 int permMask) {
63 auto mountData = android::base::StringPrintf("utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,"
64 "shortname=mixed,nodev,nosuid,dirsync,noatime,"
65 "noexec", ownerUid, ownerGid, permMask, permMask);
66
67 std::vector<std::string> cmd;
68 cmd.push_back(kMountPath);
69 cmd.push_back("-o");
70 cmd.push_back(mountData.c_str());
71 cmd.push_back(source.c_str());
72 cmd.push_back(target.c_str());
73
74 int rc = ForkExecvp(cmd);
75 if (rc == 0) {
76 LOG(INFO) << "Mount OK";
77 return 0;
78 } else {
79 LOG(ERROR) << "Mount failed (code " << rc << ")";
80 errno = EIO;
81 return -1;
82 }
83}
84
85status_t Format(const std::string& source) {
86 std::vector<std::string> cmd;
87 cmd.push_back(kMkfsPath);
88 cmd.push_back(source);
89
90 int rc = ForkExecvp(cmd);
91 if (rc == 0) {
92 LOG(INFO) << "Format OK";
93 return 0;
94 } else {
95 LOG(ERROR) << "Format failed (code " << rc << ")";
96 errno = EIO;
97 return -1;
98 }
99 return 0;
100}
101
102} // namespace ntfs
103} // namespace vold
104} // namespace android