• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "volume/external_volume_info.h"
17 
18 #include <algorithm>
19 #include <cerrno>
20 #include <csignal>
21 #include <cstdlib>
22 #include <cstring>
23 #include <sys/mount.h>
24 #include <sys/stat.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 
28 #include "storage_service_errno.h"
29 #include "storage_service_log.h"
30 #include "utils/disk_utils.h"
31 #include "utils/file_utils.h"
32 #include "utils/string_utils.h"
33 #include "volume/process.h"
34 
35 using namespace std;
36 namespace OHOS {
37 namespace StorageDaemon {
ReadMetadata()38 int32_t ExternalVolumeInfo::ReadMetadata()
39 {
40     int32_t ret = OHOS::StorageDaemon::ReadMetadata(devPath_, fsUuid_, fsType_, fsLabel_);
41     if (fsType_ == "ntfs") {
42         std::vector<std::string> cmd;
43         cmd = {
44             "ntfslabel",
45             devPath_
46         };
47         fsLabel_ = GetBlkidDataByCmd(cmd);
48     }
49     return ret;
50 }
51 
GetFsType()52 int32_t ExternalVolumeInfo::GetFsType()
53 {
54     for (uint32_t i = 0; i < supportMountType_.size(); i++) {
55         if (supportMountType_[i].compare(fsType_) == 0) {
56             return i;
57         }
58     }
59     return -1;
60 }
61 
GetFsUuid()62 std::string ExternalVolumeInfo::GetFsUuid()
63 {
64     return fsUuid_;
65 }
66 
GetFsLabel()67 std::string ExternalVolumeInfo::GetFsLabel()
68 {
69     return fsLabel_;
70 }
71 
GetMountPath()72 std::string ExternalVolumeInfo::GetMountPath()
73 {
74     return mountPath_;
75 }
76 
DoCreate(dev_t dev)77 int32_t ExternalVolumeInfo::DoCreate(dev_t dev)
78 {
79     int32_t ret = 0;
80     string id = VolumeInfo::GetVolumeId();
81 
82     device_ = dev;
83     devPath_ = StringPrintf(devPathDir_.c_str(), (id).c_str());
84 
85     ret = mknod(devPath_.c_str(), S_IFBLK, dev);
86     if (ret) {
87         LOGE("External volume DoCreate error.");
88         return E_ERR;
89     }
90 
91     return E_OK;
92 }
93 
DoDestroy()94 int32_t ExternalVolumeInfo::DoDestroy()
95 {
96     int err = remove(devPath_.c_str());
97     if (err) {
98         LOGE("External volume DoDestroy error.");
99         return E_ERR;
100     }
101     return E_OK;
102 }
103 
DoMount4Ext(uint32_t mountFlags)104 int32_t ExternalVolumeInfo::DoMount4Ext(uint32_t mountFlags)
105 {
106     mode_t mode = 0777;
107     int32_t ret = mount(devPath_.c_str(), mountPath_.c_str(), fsType_.c_str(), mountFlags, "");
108     if (!ret) {
109         TravelChmod(mountPath_, mode);
110     }
111     return ret;
112 }
113 
DoMount4Ntfs(uint32_t mountFlags)114 int32_t ExternalVolumeInfo::DoMount4Ntfs(uint32_t mountFlags)
115 {
116     auto mountData = StringPrintf("rw,uid=%d,gid=%d,dmask=0007,fmask=0007", UID_FILE_MANAGER, UID_FILE_MANAGER);
117     if (mountFlags & MS_RDONLY) {
118         mountData = StringPrintf("ro,uid=%d,gid=%d,dmask=0007,fmask=0007", UID_FILE_MANAGER, UID_FILE_MANAGER);
119     }
120 
121     std::vector<std::string> cmd = {
122         "mount.ntfs",
123         devPath_,
124         mountPath_,
125         "-o",
126         mountData.c_str()
127     };
128     return ForkExec(cmd);
129 }
130 
DoMount4Exfat(uint32_t mountFlags)131 int32_t ExternalVolumeInfo::DoMount4Exfat(uint32_t mountFlags)
132 {
133     auto mountData = StringPrintf("rw,uid=%d,gid=%d,dmask=0007,fmask=0007", UID_FILE_MANAGER, UID_FILE_MANAGER);
134     if (mountFlags & MS_RDONLY) {
135         mountData = StringPrintf("ro,uid=%d,gid=%d,dmask=0007,fmask=0007", UID_FILE_MANAGER, UID_FILE_MANAGER);
136     }
137 
138     std::vector<std::string> cmd = {
139         "mount.exfat",
140         "-o",
141         mountData.c_str(),
142         devPath_,
143         mountPath_,
144     };
145     return ForkExec(cmd);
146 }
147 
DoMount4OtherType(uint32_t mountFlags)148 int32_t ExternalVolumeInfo::DoMount4OtherType(uint32_t mountFlags)
149 {
150     mountFlags |= MS_MGC_VAL;
151     auto mountData = StringPrintf("uid=%d,gid=%d,dmask=0007,fmask=0007", UID_FILE_MANAGER, UID_FILE_MANAGER);
152     return mount(devPath_.c_str(), mountPath_.c_str(), fsType_.c_str(), mountFlags, mountData.c_str());
153 }
154 
DoMount(uint32_t mountFlags)155 int32_t ExternalVolumeInfo::DoMount(uint32_t mountFlags)
156 {
157     int32_t ret = DoCheck();
158     if (ret != E_OK) {
159         LOGE("External volume uuid=%{public}s check failed.", GetAnonyString(GetFsUuid()).c_str());
160         return ret;
161     }
162 
163     struct stat statbuf;
164     mountPath_ = StringPrintf(mountPathDir_.c_str(), fsUuid_.c_str());
165     if (!lstat(mountPath_.c_str(), &statbuf)) {
166         LOGE("volume mount path %{public}s exists, please remove first", GetMountPath().c_str());
167         return E_MOUNT;
168     }
169     ret = mkdir(mountPath_.c_str(), S_IRWXU | S_IRWXG | S_IXOTH);
170     if (ret) {
171         LOGE("the volume %{public}s create mount file %{public}s failed",
172             GetVolumeId().c_str(), GetMountPath().c_str());
173         return E_MOUNT;
174     }
175 
176     LOGI("Ready to mount: external volume fstype is %{public}s, mountflag is %{public}d", fsType_.c_str(), mountFlags);
177     if (fsType_ == "ext2" || fsType_ == "ext3" || fsType_ == "ext4") {
178         ret = DoMount4Ext(mountFlags);
179     } else if (fsType_ == "ntfs") {
180         ret = DoMount4Ntfs(mountFlags);
181     } else if (fsType_ == "exfat") {
182         ret = DoMount4Exfat(mountFlags);
183     } else {
184         ret = DoMount4OtherType(mountFlags);
185     }
186 
187     if (ret) {
188         LOGE("External volume DoMount error, errno = %{public}d", errno);
189         remove(mountPath_.c_str());
190         return E_MOUNT;
191     }
192 
193     return E_OK;
194 }
195 
DoUMount(bool force)196 int32_t ExternalVolumeInfo::DoUMount(bool force)
197 {
198     if (force) {
199         LOGI("External volume start force to unmount.");
200         Process ps(mountPath_);
201         ps.UpdatePidByPath();
202         ps.KillProcess(SIGKILL);
203         umount2(mountPath_.c_str(), MNT_DETACH);
204         remove(mountPath_.c_str());
205         LOGI("External volume force to unmount success.");
206         return E_OK;
207     }
208     LOGI("External volume start to unmount.");
209     int ret = umount(mountPath_.c_str());
210     int err = remove(mountPath_.c_str());
211     if (err && ret) {
212         LOGE("External volume DoUmount error.");
213         return E_UMOUNT;
214     }
215 
216     if (err) {
217         LOGE("failed to call remove(%{public}s) error, errno = %{public}d", mountPath_.c_str(), errno);
218         return E_SYS_CALL;
219     }
220     LOGI("External volume unmount success.");
221     return E_OK;
222 }
223 
DoCheck()224 int32_t ExternalVolumeInfo::DoCheck()
225 {
226     int32_t ret = ExternalVolumeInfo::ReadMetadata();
227     if (ret) {
228         LOGE("External volume uuid=%{public}s DoCheck failed.", GetAnonyString(GetFsUuid()).c_str());
229         return E_ERR;
230     }
231 
232     // check fstype
233     if (GetFsType() == -1) {
234         LOGE("External Volume type not support.");
235         return E_NOT_SUPPORT;
236     }
237     return E_OK;
238 }
239 
DoFormat(std::string type)240 int32_t ExternalVolumeInfo::DoFormat(std::string type)
241 {
242     int32_t err = 0;
243     std::map<std::string, std::string>::iterator iter = supportFormatType_.find(type);
244     if (iter == supportFormatType_.end()) {
245         LOGE("External volume format not support.");
246         return E_NOT_SUPPORT;
247     }
248 
249     if (type == "vfat") {
250         std::vector<std::string> cmd = {
251             iter->second,
252             "-A",
253             devPath_
254         };
255         err = ForkExec(cmd);
256     } else {
257         std::vector<std::string> cmd = {
258             iter->second,
259             devPath_
260         };
261         err = ForkExec(cmd);
262     }
263 
264     if (err == E_NO_CHILD) {
265         err = E_OK;
266     }
267 
268     ReadMetadata();
269     return err;
270 }
271 
DoSetVolDesc(std::string description)272 int32_t ExternalVolumeInfo::DoSetVolDesc(std::string description)
273 {
274     int32_t err = 0;
275     if (fsType_ == "ntfs") {
276         std::vector<std::string> cmd = {
277             "ntfslabel",
278             devPath_,
279             description
280         };
281         err = ForkExec(cmd);
282     } else if (fsType_ == "exfat") {
283         std::vector<std::string> cmd = {
284             "exfatlabel",
285             devPath_,
286             description
287         };
288         err = ForkExec(cmd);
289     } else {
290         LOGE("SetVolumeDescription fsType not support.");
291         return E_NOT_SUPPORT;
292     }
293 
294     ReadMetadata();
295     return err;
296 }
297 } // StorageDaemon
298 } // OHOS
299