1/*
2 * Copyright (c) 2023 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 "NativeFileSystem.h"
17#include <dirent.h>
18#include <sys/stat.h>
19#include "PreviewerEngineLog.h"
20
21namespace OHOS::Ide {
22std::string NativeFileSystem::FindSubfolderByName(const std::string& parentFolderPath,
23    const std::string& subfolderName)
24{
25    DIR* dir = opendir(parentFolderPath.c_str());
26    if (dir == nullptr) {
27        ELOG("failed to open directory:%s.", parentFolderPath.c_str());
28        return "";
29    }
30    struct dirent* dirEntry;
31    while ((dirEntry = readdir(dir)) != nullptr) {
32        struct stat entryStat;
33        std::string filePath = parentFolderPath + "/" + dirEntry->d_name;
34        if (stat(filePath.c_str(), &entryStat) != -1 && S_ISDIR(entryStat.st_mode)) {
35            std::string dirName(dirEntry->d_name);
36            if (dirName.find(subfolderName) == 0) {
37                closedir(dir);
38                return filePath;
39            }
40        }
41    }
42    closedir(dir);
43    return "";
44}
45}