15bebb993Sopenharmony_ci/*
25bebb993Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
35bebb993Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45bebb993Sopenharmony_ci * you may not use this file except in compliance with the License.
55bebb993Sopenharmony_ci * You may obtain a copy of the License at
65bebb993Sopenharmony_ci *
75bebb993Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85bebb993Sopenharmony_ci *
95bebb993Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105bebb993Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115bebb993Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125bebb993Sopenharmony_ci * See the License for the specific language governing permissions and
135bebb993Sopenharmony_ci * limitations under the License.
145bebb993Sopenharmony_ci */
155bebb993Sopenharmony_ci
165bebb993Sopenharmony_ci#include "environment_native.h"
175bebb993Sopenharmony_ci
185bebb993Sopenharmony_ci#include <string>
195bebb993Sopenharmony_ci#include <unistd.h>
205bebb993Sopenharmony_ci
215bebb993Sopenharmony_ci#include "accesstoken_kit.h"
225bebb993Sopenharmony_ci#include "filemgmt_libhilog.h"
235bebb993Sopenharmony_ci#include "ipc_skeleton.h"
245bebb993Sopenharmony_ci#include "parameter.h"
255bebb993Sopenharmony_ci#include "securec.h"
265bebb993Sopenharmony_ci#include "tokenid_kit.h"
275bebb993Sopenharmony_ci
285bebb993Sopenharmony_ciusing namespace OHOS;
295bebb993Sopenharmony_ciusing namespace OHOS::FileManagement;
305bebb993Sopenharmony_cinamespace {
315bebb993Sopenharmony_ciconst std::string STORAGE_PATH = "/storage/Users/";
325bebb993Sopenharmony_ciconst std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY";
335bebb993Sopenharmony_ciconst std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY";
345bebb993Sopenharmony_ciconst std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY";
355bebb993Sopenharmony_ciconst std::string DOWNLOAD_PATH = "/Download";
365bebb993Sopenharmony_ciconst std::string DESKTOP_PATH = "/Desktop";
375bebb993Sopenharmony_ciconst std::string DOCUMENTS_PATH = "/Documents";
385bebb993Sopenharmony_ciconst std::string DEFAULT_USERNAME = "currentUser";
395bebb993Sopenharmony_ciconst std::string FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable";
405bebb993Sopenharmony_ciconst int DEVICE_NOT_SUPPORTED = 801;
415bebb993Sopenharmony_ci
425bebb993Sopenharmony_cistatic std::string GetUserName()
435bebb993Sopenharmony_ci{
445bebb993Sopenharmony_ci    return DEFAULT_USERNAME;
455bebb993Sopenharmony_ci}
465bebb993Sopenharmony_ci
475bebb993Sopenharmony_cistatic std::string GetPublicPath(const std::string &directoryName)
485bebb993Sopenharmony_ci{
495bebb993Sopenharmony_ci    return STORAGE_PATH + GetUserName() + directoryName;
505bebb993Sopenharmony_ci}
515bebb993Sopenharmony_ci
525bebb993Sopenharmony_cistatic bool CheckFileManagerFullMountEnable()
535bebb993Sopenharmony_ci{
545bebb993Sopenharmony_ci    char value[] = "false";
555bebb993Sopenharmony_ci    int retSystem = GetParameter(FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER.c_str(), "false", value, sizeof(value));
565bebb993Sopenharmony_ci    return (retSystem > 0) && (!std::strcmp(value, "true"));
575bebb993Sopenharmony_ci}
585bebb993Sopenharmony_ci}
595bebb993Sopenharmony_ci
605bebb993Sopenharmony_ciint GetUserDir(char *path, char **result)
615bebb993Sopenharmony_ci{
625bebb993Sopenharmony_ci    if (!CheckFileManagerFullMountEnable()) {
635bebb993Sopenharmony_ci        HILOGD("Failed to enable the parameter: %{public}s", FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER.c_str());
645bebb993Sopenharmony_ci        return -DEVICE_NOT_SUPPORTED;
655bebb993Sopenharmony_ci    }
665bebb993Sopenharmony_ci    std::string dirPath = "";
675bebb993Sopenharmony_ci    dirPath = GetPublicPath(path);
685bebb993Sopenharmony_ci    *result = (char *) malloc((dirPath.length() + 1) * sizeof(char));
695bebb993Sopenharmony_ci    if (*result == nullptr) {
705bebb993Sopenharmony_ci        return -ENOMEM;
715bebb993Sopenharmony_ci    }
725bebb993Sopenharmony_ci    int ret = strcpy_s(*result, (dirPath.length() + 1) * sizeof(char), dirPath.c_str());
735bebb993Sopenharmony_ci    if (ret != 0) {
745bebb993Sopenharmony_ci        HILOGE("Failed to copy memory");
755bebb993Sopenharmony_ci        return -ENOMEM;
765bebb993Sopenharmony_ci    }
775bebb993Sopenharmony_ci    return ret;
785bebb993Sopenharmony_ci}
79