1 /*
2  * Copyright (c) 2024 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 "environment_native.h"
17 
18 #include <string>
19 #include <unistd.h>
20 
21 #include "accesstoken_kit.h"
22 #include "filemgmt_libhilog.h"
23 #include "ipc_skeleton.h"
24 #include "parameter.h"
25 #include "securec.h"
26 #include "tokenid_kit.h"
27 
28 using namespace OHOS;
29 using namespace OHOS::FileManagement;
30 namespace {
31 const std::string STORAGE_PATH = "/storage/Users/";
32 const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY";
33 const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY";
34 const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY";
35 const std::string DOWNLOAD_PATH = "/Download";
36 const std::string DESKTOP_PATH = "/Desktop";
37 const std::string DOCUMENTS_PATH = "/Documents";
38 const std::string DEFAULT_USERNAME = "currentUser";
39 const std::string FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable";
40 const int DEVICE_NOT_SUPPORTED = 801;
41 
GetUserName()42 static std::string GetUserName()
43 {
44     return DEFAULT_USERNAME;
45 }
46 
GetPublicPath(const std::string & directoryName)47 static std::string GetPublicPath(const std::string &directoryName)
48 {
49     return STORAGE_PATH + GetUserName() + directoryName;
50 }
51 
CheckFileManagerFullMountEnable()52 static bool CheckFileManagerFullMountEnable()
53 {
54     char value[] = "false";
55     int retSystem = GetParameter(FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER.c_str(), "false", value, sizeof(value));
56     return (retSystem > 0) && (!std::strcmp(value, "true"));
57 }
58 }
59 
GetUserDir(char * path,char ** result)60 int GetUserDir(char *path, char **result)
61 {
62     if (!CheckFileManagerFullMountEnable()) {
63         HILOGD("Failed to enable the parameter: %{public}s", FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER.c_str());
64         return -DEVICE_NOT_SUPPORTED;
65     }
66     std::string dirPath = "";
67     dirPath = GetPublicPath(path);
68     *result = (char *) malloc((dirPath.length() + 1) * sizeof(char));
69     if (*result == nullptr) {
70         return -ENOMEM;
71     }
72     int ret = strcpy_s(*result, (dirPath.length() + 1) * sizeof(char), dirPath.c_str());
73     if (ret != 0) {
74         HILOGE("Failed to copy memory");
75         return -ENOMEM;
76     }
77     return ret;
78 }
79