1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include "fs_manager/mount.h"
17fb299fa2Sopenharmony_ci#include <cerrno>
18fb299fa2Sopenharmony_ci#include <fcntl.h>
19fb299fa2Sopenharmony_ci#include <string>
20fb299fa2Sopenharmony_ci#include <sys/mount.h>
21fb299fa2Sopenharmony_ci#include <sys/stat.h>
22fb299fa2Sopenharmony_ci#include <sys/wait.h>
23fb299fa2Sopenharmony_ci#include <unistd.h>
24fb299fa2Sopenharmony_ci#include <vector>
25fb299fa2Sopenharmony_ci#include <linux/fs.h>
26fb299fa2Sopenharmony_ci#include "log/dump.h"
27fb299fa2Sopenharmony_ci#include "log/log.h"
28fb299fa2Sopenharmony_ci#include "utils.h"
29fb299fa2Sopenharmony_ci
30fb299fa2Sopenharmony_cinamespace Updater {
31fb299fa2Sopenharmony_ciusing Updater::Utils::SplitString;
32fb299fa2Sopenharmony_cistatic std::string g_defaultUpdaterFstab = "";
33fb299fa2Sopenharmony_cistatic Fstab *g_fstab = nullptr;
34fb299fa2Sopenharmony_cistatic const std::string PARTITION_PATH = "/dev/block/by-name";
35fb299fa2Sopenharmony_ci
36fb299fa2Sopenharmony_cistatic std::string GetFstabFile()
37fb299fa2Sopenharmony_ci{
38fb299fa2Sopenharmony_ci    /* check vendor fstab files from specific directory */
39fb299fa2Sopenharmony_ci    std::vector<const std::string> specificFstabFiles = {"/vendor/etc/fstab.updater"};
40fb299fa2Sopenharmony_ci    for (auto& fstabFile : specificFstabFiles) {
41fb299fa2Sopenharmony_ci        if (access(fstabFile.c_str(), F_OK) == 0) {
42fb299fa2Sopenharmony_ci            return fstabFile;
43fb299fa2Sopenharmony_ci        }
44fb299fa2Sopenharmony_ci    }
45fb299fa2Sopenharmony_ci    return "";
46fb299fa2Sopenharmony_ci}
47fb299fa2Sopenharmony_ci
48fb299fa2Sopenharmony_ci#ifndef UPDATE_PATCH_SHARED
49fb299fa2Sopenharmony_ciMountStatus GetMountStatusForPath(const std::string &path)
50fb299fa2Sopenharmony_ci{
51fb299fa2Sopenharmony_ci    FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
52fb299fa2Sopenharmony_ci    if (item == nullptr) {
53fb299fa2Sopenharmony_ci        return MountStatus::MOUNT_ERROR;
54fb299fa2Sopenharmony_ci    }
55fb299fa2Sopenharmony_ci    return GetMountStatusForMountPoint(item->mountPoint);
56fb299fa2Sopenharmony_ci}
57fb299fa2Sopenharmony_ci#endif
58fb299fa2Sopenharmony_ci
59fb299fa2Sopenharmony_civoid LoadFstab()
60fb299fa2Sopenharmony_ci{
61fb299fa2Sopenharmony_ci    std::string fstabFile = g_defaultUpdaterFstab;
62fb299fa2Sopenharmony_ci    if (fstabFile.empty()) {
63fb299fa2Sopenharmony_ci        fstabFile = GetFstabFile();
64fb299fa2Sopenharmony_ci        if (fstabFile.empty()) {
65fb299fa2Sopenharmony_ci            fstabFile = "/etc/fstab.updater";
66fb299fa2Sopenharmony_ci        }
67fb299fa2Sopenharmony_ci    }
68fb299fa2Sopenharmony_ci    if (g_fstab != nullptr) {
69fb299fa2Sopenharmony_ci        ReleaseFstab(g_fstab);
70fb299fa2Sopenharmony_ci        g_fstab = nullptr;
71fb299fa2Sopenharmony_ci    }
72fb299fa2Sopenharmony_ci    // Clear fstab before read fstab file.
73fb299fa2Sopenharmony_ci    if ((g_fstab = ReadFstabFromFile(fstabFile.c_str(), false)) == nullptr) {
74fb299fa2Sopenharmony_ci        LOG(WARNING) << "Read " << fstabFile << " failed";
75fb299fa2Sopenharmony_ci        return;
76fb299fa2Sopenharmony_ci    }
77fb299fa2Sopenharmony_ci
78fb299fa2Sopenharmony_ci    LOG(DEBUG) << "Updater filesystem config info:";
79fb299fa2Sopenharmony_ci    for (FstabItem *item = g_fstab->head; item != nullptr; item = item->next) {
80fb299fa2Sopenharmony_ci        LOG(DEBUG) << "\tDevice: " << item->deviceName;
81fb299fa2Sopenharmony_ci        LOG(DEBUG) << "\tMount point : " << item->mountPoint;
82fb299fa2Sopenharmony_ci        LOG(DEBUG) << "\tFs type : " << item->fsType;
83fb299fa2Sopenharmony_ci        LOG(DEBUG) << "\tMount options: " << item->mountOptions;
84fb299fa2Sopenharmony_ci    }
85fb299fa2Sopenharmony_ci}
86fb299fa2Sopenharmony_ci
87fb299fa2Sopenharmony_civoid LoadSpecificFstab(const std::string &fstabName)
88fb299fa2Sopenharmony_ci{
89fb299fa2Sopenharmony_ci    g_defaultUpdaterFstab = fstabName;
90fb299fa2Sopenharmony_ci    LoadFstab();
91fb299fa2Sopenharmony_ci    g_defaultUpdaterFstab = "";
92fb299fa2Sopenharmony_ci}
93fb299fa2Sopenharmony_ci
94fb299fa2Sopenharmony_ciint UmountForPath(const std::string& path)
95fb299fa2Sopenharmony_ci{
96fb299fa2Sopenharmony_ci    if (g_fstab == nullptr) {
97fb299fa2Sopenharmony_ci        LOG(ERROR) << "fstab is not loaded, g_fstab is null.";
98fb299fa2Sopenharmony_ci        return -1;
99fb299fa2Sopenharmony_ci    }
100fb299fa2Sopenharmony_ci
101fb299fa2Sopenharmony_ci    FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
102fb299fa2Sopenharmony_ci    if (item == nullptr) {
103fb299fa2Sopenharmony_ci        LOG(ERROR) << "Cannot find fstab item for " << path << " to umount.";
104fb299fa2Sopenharmony_ci        return -1;
105fb299fa2Sopenharmony_ci    }
106fb299fa2Sopenharmony_ci
107fb299fa2Sopenharmony_ci    LOG(DEBUG) << "Umount for path " << path;
108fb299fa2Sopenharmony_ci    MountStatus rc = GetMountStatusForMountPoint(item->mountPoint);
109fb299fa2Sopenharmony_ci    if (rc == MOUNT_ERROR) {
110fb299fa2Sopenharmony_ci        return -1;
111fb299fa2Sopenharmony_ci    } else if (rc == MOUNT_UMOUNTED) {
112fb299fa2Sopenharmony_ci        return 0;
113fb299fa2Sopenharmony_ci    } else {
114fb299fa2Sopenharmony_ci        if (path == "/data") {
115fb299fa2Sopenharmony_ci            Utils::SetParameter("updater.data.ready", "0");
116fb299fa2Sopenharmony_ci        }
117fb299fa2Sopenharmony_ci        int ret = umount(item->mountPoint);
118fb299fa2Sopenharmony_ci        if (ret == -1) {
119fb299fa2Sopenharmony_ci            LOG(ERROR) << "Umount " << item->mountPoint << "failed: " << errno;
120fb299fa2Sopenharmony_ci            return -1;
121fb299fa2Sopenharmony_ci        }
122fb299fa2Sopenharmony_ci    }
123fb299fa2Sopenharmony_ci    return 0;
124fb299fa2Sopenharmony_ci}
125fb299fa2Sopenharmony_ci
126fb299fa2Sopenharmony_cistatic int MountNtfsWithRetry(std::string source, std::string target)
127fb299fa2Sopenharmony_ci{
128fb299fa2Sopenharmony_ci    char *argv[] = {const_cast<char *>("system/bin/mount.ntfs"),
129fb299fa2Sopenharmony_ci        const_cast<char *>(source.c_str()), const_cast<char *>(target.c_str()), nullptr};
130fb299fa2Sopenharmony_ci    int num = 0;
131fb299fa2Sopenharmony_ci    do {
132fb299fa2Sopenharmony_ci        pid_t child = fork();
133fb299fa2Sopenharmony_ci        if (child == 0) {
134fb299fa2Sopenharmony_ci            if (execv(argv[0], argv)) {
135fb299fa2Sopenharmony_ci                _exit(-1);
136fb299fa2Sopenharmony_ci            }
137fb299fa2Sopenharmony_ci        }
138fb299fa2Sopenharmony_ci        int status = -1;
139fb299fa2Sopenharmony_ci        if (waitpid(child, &status, 0) < 0) {
140fb299fa2Sopenharmony_ci            LOG(ERROR) << "waitpid failed, " << child;
141fb299fa2Sopenharmony_ci        }
142fb299fa2Sopenharmony_ci        if (WIFEXITED(status)) {
143fb299fa2Sopenharmony_ci            LOG(ERROR) << "child terminated by exit " << WEXITSTATUS(status);
144fb299fa2Sopenharmony_ci        } else if (WIFSIGNALED(status)) {
145fb299fa2Sopenharmony_ci            LOG(ERROR) << "child terminated by signal " << WTERMSIG(status);
146fb299fa2Sopenharmony_ci        } else if (WIFSTOPPED(status)) {
147fb299fa2Sopenharmony_ci            LOG(ERROR) << "child stopped by signal " << WSTOPSIG(status);
148fb299fa2Sopenharmony_ci        }
149fb299fa2Sopenharmony_ci
150fb299fa2Sopenharmony_ci        if (status == 0) {
151fb299fa2Sopenharmony_ci            Utils::UsSleep(100); // 100 : Wait interval
152fb299fa2Sopenharmony_ci            LOG(INFO) << "success to mount " << source << " on " << target;
153fb299fa2Sopenharmony_ci            return 0;
154fb299fa2Sopenharmony_ci        } else {
155fb299fa2Sopenharmony_ci            if ((errno == ENOENT) || (errno == ENODEV) || (errno == ENOMEDIUM)) {
156fb299fa2Sopenharmony_ci                LOG(ERROR) << "SD card never insert, dont try again, failed to mount " << source << " on " << target;
157fb299fa2Sopenharmony_ci                return -1;
158fb299fa2Sopenharmony_ci            }
159fb299fa2Sopenharmony_ci        }
160fb299fa2Sopenharmony_ci        num++;
161fb299fa2Sopenharmony_ci        LOG(ERROR) << "failed to mount " << source << " on " << target << ", errno is " << errno;
162fb299fa2Sopenharmony_ci    } while (num < 3); // 3 : retry three times
163fb299fa2Sopenharmony_ci    return -1;
164fb299fa2Sopenharmony_ci}
165fb299fa2Sopenharmony_ci
166fb299fa2Sopenharmony_ciint MountSdcard(std::string &path, std::string &mountPoint)
167fb299fa2Sopenharmony_ci{
168fb299fa2Sopenharmony_ci    if (path.empty() || mountPoint.empty()) {
169fb299fa2Sopenharmony_ci        LOG(ERROR) << "path or mountPoint is null, mount fail";
170fb299fa2Sopenharmony_ci        return -1;
171fb299fa2Sopenharmony_ci    }
172fb299fa2Sopenharmony_ci    MountStatus rc = GetMountStatusForMountPoint(mountPoint.c_str());
173fb299fa2Sopenharmony_ci    if (rc == MountStatus::MOUNT_ERROR) {
174fb299fa2Sopenharmony_ci        return -1;
175fb299fa2Sopenharmony_ci    } else if (rc == MountStatus::MOUNT_MOUNTED) {
176fb299fa2Sopenharmony_ci        LOG(INFO) << path << " already mounted";
177fb299fa2Sopenharmony_ci        return 0;
178fb299fa2Sopenharmony_ci    }
179fb299fa2Sopenharmony_ci    const std::vector<const char *> fileSystemType = {"ext4", "vfat", "exfat"};
180fb299fa2Sopenharmony_ci    for (auto type : fileSystemType) {
181fb299fa2Sopenharmony_ci        if (mount(path.c_str(), mountPoint.c_str(), type, 0, nullptr) == 0) {
182fb299fa2Sopenharmony_ci            LOG(INFO) << "mount success, sdcard type is " << type;
183fb299fa2Sopenharmony_ci            return 0;
184fb299fa2Sopenharmony_ci        }
185fb299fa2Sopenharmony_ci    }
186fb299fa2Sopenharmony_ci    if (MountNtfsWithRetry(path, mountPoint) == 0) {
187fb299fa2Sopenharmony_ci        LOG(INFO) << "mount success, sdcard type is ntfs";
188fb299fa2Sopenharmony_ci        return 0;
189fb299fa2Sopenharmony_ci    }
190fb299fa2Sopenharmony_ci    return -1;
191fb299fa2Sopenharmony_ci}
192fb299fa2Sopenharmony_ci
193fb299fa2Sopenharmony_ciint MountForPath(const std::string &path)
194fb299fa2Sopenharmony_ci{
195fb299fa2Sopenharmony_ci    if (g_fstab == nullptr) {
196fb299fa2Sopenharmony_ci        LOG(ERROR) << "fstab is not loaded, g_fstab is null.";
197fb299fa2Sopenharmony_ci        return -1;
198fb299fa2Sopenharmony_ci    }
199fb299fa2Sopenharmony_ci
200fb299fa2Sopenharmony_ci    FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
201fb299fa2Sopenharmony_ci    int ret = -1;
202fb299fa2Sopenharmony_ci    if (item == nullptr) {
203fb299fa2Sopenharmony_ci        LOG(ERROR) << "Cannot find fstab item for " << path << " to mount.";
204fb299fa2Sopenharmony_ci        return -1;
205fb299fa2Sopenharmony_ci    }
206fb299fa2Sopenharmony_ci
207fb299fa2Sopenharmony_ci    LOG(DEBUG) << "Mount for path " << path;
208fb299fa2Sopenharmony_ci    MountStatus rc = GetMountStatusForMountPoint(item->mountPoint);
209fb299fa2Sopenharmony_ci    if (rc == MountStatus::MOUNT_ERROR) {
210fb299fa2Sopenharmony_ci        LOG(ERROR) << "GetMountStatusForMountPoint ret is MOUNT_ERROR";
211fb299fa2Sopenharmony_ci        ret = -1;
212fb299fa2Sopenharmony_ci    } else if (rc == MountStatus::MOUNT_MOUNTED) {
213fb299fa2Sopenharmony_ci        LOG(INFO) << path << " already mounted";
214fb299fa2Sopenharmony_ci        ret = 0;
215fb299fa2Sopenharmony_ci    } else {
216fb299fa2Sopenharmony_ci        ret = MountOneItem(item);
217fb299fa2Sopenharmony_ci    }
218fb299fa2Sopenharmony_ci    return ret;
219fb299fa2Sopenharmony_ci}
220fb299fa2Sopenharmony_ci
221fb299fa2Sopenharmony_civoid ErasePartition(const std::string &devPath)
222fb299fa2Sopenharmony_ci{
223fb299fa2Sopenharmony_ci    std::string realPath {};
224fb299fa2Sopenharmony_ci    if (!Utils::PathToRealPath(devPath, realPath)) {
225fb299fa2Sopenharmony_ci        LOG(ERROR) << "realpath failed:" << devPath;
226fb299fa2Sopenharmony_ci        return;
227fb299fa2Sopenharmony_ci    }
228fb299fa2Sopenharmony_ci    int fd = open(realPath.c_str(), O_RDWR | O_LARGEFILE);
229fb299fa2Sopenharmony_ci    if (fd == -1) {
230fb299fa2Sopenharmony_ci        LOG(ERROR) << "open failed:" << realPath;
231fb299fa2Sopenharmony_ci        return;
232fb299fa2Sopenharmony_ci    }
233fb299fa2Sopenharmony_ci
234fb299fa2Sopenharmony_ci    uint64_t size = 0;
235fb299fa2Sopenharmony_ci    int ret = ioctl(fd, BLKGETSIZE64, &size);
236fb299fa2Sopenharmony_ci    if (ret < 0) {
237fb299fa2Sopenharmony_ci        LOG(ERROR) << "get partition size failed:" << size;
238fb299fa2Sopenharmony_ci        close(fd);
239fb299fa2Sopenharmony_ci        return;
240fb299fa2Sopenharmony_ci    }
241fb299fa2Sopenharmony_ci
242fb299fa2Sopenharmony_ci    LOG(INFO) << "erase partition size:" << size;
243fb299fa2Sopenharmony_ci
244fb299fa2Sopenharmony_ci    uint64_t range[] { 0, size };
245fb299fa2Sopenharmony_ci    ret = ioctl(fd, BLKDISCARD, &range);
246fb299fa2Sopenharmony_ci    if (ret < 0) {
247fb299fa2Sopenharmony_ci        LOG(ERROR) << "erase partition failed";
248fb299fa2Sopenharmony_ci    }
249fb299fa2Sopenharmony_ci    close(fd);
250fb299fa2Sopenharmony_ci
251fb299fa2Sopenharmony_ci    return;
252fb299fa2Sopenharmony_ci}
253fb299fa2Sopenharmony_ci
254fb299fa2Sopenharmony_ciint FormatPartition(const std::string &path, bool isZeroErase)
255fb299fa2Sopenharmony_ci{
256fb299fa2Sopenharmony_ci    if (g_fstab == nullptr) {
257fb299fa2Sopenharmony_ci        LOG(ERROR) << "fstab is not loaded, g_fstab is null.";
258fb299fa2Sopenharmony_ci        return -1;
259fb299fa2Sopenharmony_ci    }
260fb299fa2Sopenharmony_ci
261fb299fa2Sopenharmony_ci    FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
262fb299fa2Sopenharmony_ci    if (item == nullptr) {
263fb299fa2Sopenharmony_ci        LOG(ERROR) << "Cannot find fstab item for " << path << " to format.";
264fb299fa2Sopenharmony_ci        return -1;
265fb299fa2Sopenharmony_ci    }
266fb299fa2Sopenharmony_ci
267fb299fa2Sopenharmony_ci    if (strcmp(item->mountPoint, "/") == 0) {
268fb299fa2Sopenharmony_ci        /* Can not format root */
269fb299fa2Sopenharmony_ci        return 0;
270fb299fa2Sopenharmony_ci    }
271fb299fa2Sopenharmony_ci
272fb299fa2Sopenharmony_ci    if (!IsSupportedFilesystem(item->fsType)) {
273fb299fa2Sopenharmony_ci        LOG(ERROR) << "Try to format " << item->mountPoint << " with unsupported file system type: " << item->fsType;
274fb299fa2Sopenharmony_ci        return -1;
275fb299fa2Sopenharmony_ci    }
276fb299fa2Sopenharmony_ci
277fb299fa2Sopenharmony_ci    // Umount first
278fb299fa2Sopenharmony_ci    if (UmountForPath(path) != 0) {
279fb299fa2Sopenharmony_ci        return -1;
280fb299fa2Sopenharmony_ci    }
281fb299fa2Sopenharmony_ci
282fb299fa2Sopenharmony_ci    if (isZeroErase) {
283fb299fa2Sopenharmony_ci        ErasePartition(item->deviceName);
284fb299fa2Sopenharmony_ci    }
285fb299fa2Sopenharmony_ci
286fb299fa2Sopenharmony_ci    int ret = DoFormat(item->deviceName, item->fsType);
287fb299fa2Sopenharmony_ci    if (ret != 0) {
288fb299fa2Sopenharmony_ci        LOG(ERROR) << "Format " << path << " failed";
289fb299fa2Sopenharmony_ci    }
290fb299fa2Sopenharmony_ci    return ((ret != 0) ? -1 : 0);
291fb299fa2Sopenharmony_ci}
292fb299fa2Sopenharmony_ci
293fb299fa2Sopenharmony_ciint SetupPartitions(bool isMountData)
294fb299fa2Sopenharmony_ci{
295fb299fa2Sopenharmony_ci    UPDATER_INIT_RECORD;
296fb299fa2Sopenharmony_ci    if (!Utils::IsUpdaterMode()) {
297fb299fa2Sopenharmony_ci        LOG(ERROR) << "live update mode";
298fb299fa2Sopenharmony_ci        return 0;
299fb299fa2Sopenharmony_ci    }
300fb299fa2Sopenharmony_ci
301fb299fa2Sopenharmony_ci    if (g_fstab == NULL || g_fstab->head == NULL) {
302fb299fa2Sopenharmony_ci        LOG(ERROR) << "Fstab is invalid";
303fb299fa2Sopenharmony_ci        UPDATER_LAST_WORD(-1);
304fb299fa2Sopenharmony_ci        return -1;
305fb299fa2Sopenharmony_ci    }
306fb299fa2Sopenharmony_ci    for (const FstabItem *item = g_fstab->head; item != nullptr; item = item->next) {
307fb299fa2Sopenharmony_ci        std::string mountPoint(item->mountPoint);
308fb299fa2Sopenharmony_ci        std::string fsType(item->fsType);
309fb299fa2Sopenharmony_ci        if (mountPoint == "/" || mountPoint == "/tmp" || fsType == "none" ||
310fb299fa2Sopenharmony_ci            mountPoint == "/sdcard") {
311fb299fa2Sopenharmony_ci            continue;
312fb299fa2Sopenharmony_ci        }
313fb299fa2Sopenharmony_ci
314fb299fa2Sopenharmony_ci        if (mountPoint == "/data" && isMountData) {
315fb299fa2Sopenharmony_ci            if (MountForPath(mountPoint) != 0) {
316fb299fa2Sopenharmony_ci                LOG(ERROR) << "Expected partition " << mountPoint << " is not mounted.";
317fb299fa2Sopenharmony_ci                UPDATER_LAST_WORD(-1);
318fb299fa2Sopenharmony_ci                return -1;
319fb299fa2Sopenharmony_ci            }
320fb299fa2Sopenharmony_ci            Utils::SetParameter("updater.data.ready", "1");
321fb299fa2Sopenharmony_ci            LOG(INFO) << "mount data not fail";
322fb299fa2Sopenharmony_ci            continue;
323fb299fa2Sopenharmony_ci        }
324fb299fa2Sopenharmony_ci        if (UmountForPath(mountPoint) != 0) {
325fb299fa2Sopenharmony_ci            LOG(ERROR) << "Umount " << mountPoint << " failed";
326fb299fa2Sopenharmony_ci            UPDATER_LAST_WORD(-1);
327fb299fa2Sopenharmony_ci            return -1;
328fb299fa2Sopenharmony_ci        }
329fb299fa2Sopenharmony_ci    }
330fb299fa2Sopenharmony_ci    return 0;
331fb299fa2Sopenharmony_ci}
332fb299fa2Sopenharmony_ci
333fb299fa2Sopenharmony_ciconst std::string GetBlockDeviceByMountPoint(const std::string &mountPoint)
334fb299fa2Sopenharmony_ci{
335fb299fa2Sopenharmony_ci    if (mountPoint.empty()) {
336fb299fa2Sopenharmony_ci        LOG(ERROR) << "mountPoint empty error.";
337fb299fa2Sopenharmony_ci        return "";
338fb299fa2Sopenharmony_ci    }
339fb299fa2Sopenharmony_ci    std::string blockDevice = PARTITION_PATH + mountPoint;
340fb299fa2Sopenharmony_ci    if (mountPoint[0] != '/') {
341fb299fa2Sopenharmony_ci        blockDevice = PARTITION_PATH + "/" + mountPoint;
342fb299fa2Sopenharmony_ci    }
343fb299fa2Sopenharmony_ci    if (g_fstab != nullptr) {
344fb299fa2Sopenharmony_ci        FstabItem *item = FindFstabItemForMountPoint(*g_fstab, mountPoint.c_str());
345fb299fa2Sopenharmony_ci        if (item != NULL) {
346fb299fa2Sopenharmony_ci            blockDevice = item->deviceName;
347fb299fa2Sopenharmony_ci        }
348fb299fa2Sopenharmony_ci    }
349fb299fa2Sopenharmony_ci    return blockDevice;
350fb299fa2Sopenharmony_ci}
351fb299fa2Sopenharmony_ci
352fb299fa2Sopenharmony_ciconst std::vector<std::string> GetBlockDevicesByMountPoint(const std::string &mountPoint)
353fb299fa2Sopenharmony_ci{
354fb299fa2Sopenharmony_ci    std::vector<std::string> blockDevices;
355fb299fa2Sopenharmony_ci    if (mountPoint.empty() || g_fstab == nullptr) {
356fb299fa2Sopenharmony_ci        LOG(ERROR) << "mountPoint or g_fstab empty error.";
357fb299fa2Sopenharmony_ci        return blockDevices;
358fb299fa2Sopenharmony_ci    }
359fb299fa2Sopenharmony_ci    for (FstabItem *item = g_fstab->head; item != NULL; item = item->next) {
360fb299fa2Sopenharmony_ci        if ((item->mountPoint != NULL) && item->mountPoint == mountPoint) {
361fb299fa2Sopenharmony_ci            blockDevices.push_back(item->deviceName);
362fb299fa2Sopenharmony_ci        }
363fb299fa2Sopenharmony_ci    }
364fb299fa2Sopenharmony_ci
365fb299fa2Sopenharmony_ci    if (blockDevices.empty()) {
366fb299fa2Sopenharmony_ci        std::string blockDevice = PARTITION_PATH + mountPoint;
367fb299fa2Sopenharmony_ci        if (mountPoint[0] != '/') {
368fb299fa2Sopenharmony_ci            blockDevice = PARTITION_PATH + "/" + mountPoint;
369fb299fa2Sopenharmony_ci        }
370fb299fa2Sopenharmony_ci        blockDevices.push_back(blockDevice);
371fb299fa2Sopenharmony_ci    }
372fb299fa2Sopenharmony_ci    return blockDevices;
373fb299fa2Sopenharmony_ci}
374fb299fa2Sopenharmony_ci} // updater
375