133eb0b6dSopenharmony_ci/*
233eb0b6dSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License.
533eb0b6dSopenharmony_ci * You may obtain a copy of the License at
633eb0b6dSopenharmony_ci *
733eb0b6dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
833eb0b6dSopenharmony_ci *
933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and
1333eb0b6dSopenharmony_ci * limitations under the License.
1433eb0b6dSopenharmony_ci */
1533eb0b6dSopenharmony_ci
1633eb0b6dSopenharmony_ci#include <cerrno>
1733eb0b6dSopenharmony_ci#include <cstring>
1833eb0b6dSopenharmony_ci#include <securec.h>
1933eb0b6dSopenharmony_ci
2033eb0b6dSopenharmony_ci#include "utils/file.h"
2133eb0b6dSopenharmony_ci#include "utils/log.h"
2233eb0b6dSopenharmony_ci
2333eb0b6dSopenharmony_cibool RealPath(const char *path, char *resolvedPath, size_t bufferSize)
2433eb0b6dSopenharmony_ci{
2533eb0b6dSopenharmony_ci    if (path == nullptr || resolvedPath == nullptr) {
2633eb0b6dSopenharmony_ci        return false;
2733eb0b6dSopenharmony_ci    }
2833eb0b6dSopenharmony_ci    size_t pathLen = strlen(path);
2933eb0b6dSopenharmony_ci    if (pathLen == 0 || pathLen > PATH_MAX) {
3033eb0b6dSopenharmony_ci        HILOG_WARN("File path is illeage");
3133eb0b6dSopenharmony_ci        return false;
3233eb0b6dSopenharmony_ci    }
3333eb0b6dSopenharmony_ci    char buffer[PATH_MAX + 1] = { 0 };
3433eb0b6dSopenharmony_ci    if (realpath(path, buffer) == NULL) {
3533eb0b6dSopenharmony_ci        // Maybe file does not exist.
3633eb0b6dSopenharmony_ci        HILOG_WARN("File path: %{public}s full path failure", path);
3733eb0b6dSopenharmony_ci        return false;
3833eb0b6dSopenharmony_ci    }
3933eb0b6dSopenharmony_ci    if (strcpy_s(resolvedPath, bufferSize - 1, buffer) != 0) {
4033eb0b6dSopenharmony_ci        return false;
4133eb0b6dSopenharmony_ci    }
4233eb0b6dSopenharmony_ci    return true;
4333eb0b6dSopenharmony_ci}