1 /*
2 * Copyright (C) 2018 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 "Exfat.h"
25 #include "Utils.h"
26
27 using android::base::StringPrintf;
28
29 namespace android {
30 namespace vold {
31 namespace exfat {
32
33 static const char* kMkfsPath = "/system/bin/mkfs.exfat";
34 static const char* kFsckPath = "/system/bin/fsck.exfat";
35
IsSupported()36 bool IsSupported() {
37 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
38 IsFilesystemSupported("exfat");
39 }
40
Check(const std::string & source)41 status_t Check(const std::string& source) {
42 std::vector<std::string> cmd;
43 cmd.push_back(kFsckPath);
44 cmd.push_back(source);
45
46 int rc = ForkExecvp(cmd, sFsckUntrustedContext);
47 if (rc == 0) {
48 LOG(INFO) << "Check OK";
49 return 0;
50 } else {
51 LOG(ERROR) << "Check failed (code " << rc << ")";
52 errno = EIO;
53 return -1;
54 }
55 }
56
Mount(const std::string & source,const std::string & target,int ownerUid,int ownerGid,int permMask)57 status_t Mount(const std::string& source, const std::string& target, int ownerUid, int ownerGid,
58 int permMask) {
59 int mountFlags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME | MS_NOEXEC;
60 auto mountData = android::base::StringPrintf("uid=%d,gid=%d,fmask=%o,dmask=%o", ownerUid,
61 ownerGid, permMask, permMask);
62
63 if (mount(source.c_str(), target.c_str(), "exfat", mountFlags, mountData.c_str()) == 0) {
64 return 0;
65 }
66
67 PLOG(ERROR) << "Mount failed; attempting read-only";
68 mountFlags |= MS_RDONLY;
69 if (mount(source.c_str(), target.c_str(), "exfat", mountFlags, mountData.c_str()) == 0) {
70 return 0;
71 }
72
73 return -1;
74 }
75
Format(const std::string & source)76 status_t Format(const std::string& source) {
77 std::vector<std::string> cmd;
78 cmd.push_back(kMkfsPath);
79 cmd.push_back("-n");
80 cmd.push_back("android");
81 cmd.push_back(source);
82
83 int rc = ForkExecvp(cmd);
84 if (rc == 0) {
85 LOG(INFO) << "Format OK";
86 return 0;
87 } else {
88 LOG(ERROR) << "Format failed (code " << rc << ")";
89 errno = EIO;
90 return -1;
91 }
92 return 0;
93 }
94
95 } // namespace exfat
96 } // namespace vold
97 } // namespace android
98