blob: 64441784889e985d76d5357975de81e2de1f4d3d [file] [log] [blame]
Jeff Sharkey37ba1252018-01-19 10:55:18 +09001/*
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
27using android::base::StringPrintf;
28
29namespace android {
30namespace vold {
31namespace exfat {
32
33static const char* kMkfsPath = "/system/bin/mkfs.exfat";
34static const char* kFsckPath = "/system/bin/fsck.exfat";
35
36bool IsSupported() {
37 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
38 IsFilesystemSupported("exfat");
39}
40
41status_t Check(const std::string& source) {
42 std::vector<std::string> cmd;
43 cmd.push_back(kFsckPath);
LuK1337b8e07b22020-09-29 20:36:50 +020044 cmd.push_back("-y");
Jeff Sharkey37ba1252018-01-19 10:55:18 +090045 cmd.push_back(source);
46
Daniel Rosenbergd9261b12021-09-14 17:32:06 -070047 int rc = ForkExecvpTimeout(cmd, kUntrustedFsckSleepTime, sFsckUntrustedContext);
Jeff Sharkey37ba1252018-01-19 10:55:18 +090048 if (rc == 0) {
49 LOG(INFO) << "Check OK";
50 return 0;
Wen-Chih Lo5b9fd682023-06-08 13:21:21 +080051 } else if (rc == 1) {
52 LOG(INFO) << "Filesystem errors corrected";
53 return 0;
Jeff Sharkey37ba1252018-01-19 10:55:18 +090054 } else {
55 LOG(ERROR) << "Check failed (code " << rc << ")";
56 errno = EIO;
57 return -1;
58 }
59}
60
Daniel Rosenberg8c5dd6e2024-07-18 18:10:39 -070061status_t DoMount(const std::string& source, const std::string& target, int ownerUid, int ownerGid,
62 int permMask) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +090063 int mountFlags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME | MS_NOEXEC;
64 auto mountData = android::base::StringPrintf("uid=%d,gid=%d,fmask=%o,dmask=%o", ownerUid,
65 ownerGid, permMask, permMask);
66
67 if (mount(source.c_str(), target.c_str(), "exfat", mountFlags, mountData.c_str()) == 0) {
68 return 0;
69 }
70
71 PLOG(ERROR) << "Mount failed; attempting read-only";
72 mountFlags |= MS_RDONLY;
73 if (mount(source.c_str(), target.c_str(), "exfat", mountFlags, mountData.c_str()) == 0) {
74 return 0;
75 }
76
77 return -1;
78}
79
Daniel Rosenberg8c5dd6e2024-07-18 18:10:39 -070080struct mount_args {
81 const std::string& source;
82 const std::string& target;
83 int ownerUid;
84 int ownerGid;
85 int permMask;
86};
87
88int DoMountWrapper(void* args) {
89 struct mount_args* m_args = (struct mount_args*)args;
90
91 return DoMount(m_args->source, m_args->target, m_args->ownerUid, m_args->ownerGid,
92 m_args->permMask);
93}
94
95status_t Mount(const std::string& source, const std::string& target, int ownerUid, int ownerGid,
96 int permMask) {
97 struct mount_args args = {source, target, ownerUid, ownerGid, permMask};
98 return ForkTimeout(DoMountWrapper, &args, kUntrustedMountSleepTime);
99}
100
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900101status_t Format(const std::string& source) {
102 std::vector<std::string> cmd;
103 cmd.push_back(kMkfsPath);
104 cmd.push_back("-n");
“riyaghai”6b083c62024-10-18 08:24:02 +0000105 cmd.push_back("External");
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900106 cmd.push_back(source);
107
108 int rc = ForkExecvp(cmd);
109 if (rc == 0) {
110 LOG(INFO) << "Format OK";
111 return 0;
112 } else {
113 LOG(ERROR) << "Format failed (code " << rc << ")";
114 errno = EIO;
115 return -1;
116 }
117 return 0;
118}
119
120} // namespace exfat
121} // namespace vold
122} // namespace android