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 #include "oh_file_uri.h"
16
17 #include <cstring>
18
19 #include "b_resources/b_constants.h"
20 #include "file_uri.h"
21 #include "log.h"
22 #include "securec.h"
23
24 using namespace std;
GetValue(std::string_view resultStr,char ** result)25 static FileManagement_ErrCode GetValue(std::string_view resultStr, char **result)
26 {
27 size_t count = resultStr.length();
28 if (count == 0) {
29 return ERR_UNKNOWN;
30 }
31 if (count > OHOS::FileManagement::Backup::BConstants::PARAM_STARING_MAX_MEMORY) {
32 LOGE("The param resultStr is too big");
33 return ERR_PARAMS;
34 }
35 *result = static_cast<char *>(malloc(sizeof(char) * (count + 1)));
36 if (*result == nullptr) {
37 LOGE("malloc is feiled!");
38 return ERR_ENOMEM;
39 }
40 int ret = strncpy_s(*result, count + 1, resultStr.data(), count);
41 if (ret != 0) {
42 LOGE("strncpy_s is feiled!");
43 free(*result);
44 *result = nullptr;
45 return ERR_ENOMEM;
46 }
47 return ERR_OK;
48 }
49
OH_FileUri_GetUriFromPath(const char * path,unsigned int length,char ** result)50 FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result)
51 {
52 if (path == nullptr || strlen(path) != length || result == nullptr) {
53 return ERR_PARAMS;
54 }
55 std::string pathStr(path, length);
56 OHOS::AppFileService::ModuleFileUri::FileUri fileUri(pathStr);
57 return GetValue(fileUri.ToString(), result);
58 }
59
OH_FileUri_GetPathFromUri(const char * uri,unsigned int length,char ** result)60 FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result)
61 {
62 if (uri == nullptr || strlen(uri) != length || result == nullptr) {
63 return ERR_PARAMS;
64 }
65 std::string uriStr(uri, length);
66 OHOS::AppFileService::ModuleFileUri::FileUri fileUri(uriStr);
67 return GetValue(fileUri.GetRealPath(), result);
68 }
69
OH_FileUri_GetFullDirectoryUri(const char * uri,unsigned int length,char ** result)70 FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result)
71 {
72 if (uri == nullptr || strlen(uri) != length || result == nullptr) {
73 return ERR_PARAMS;
74 }
75 std::string uriStr(uri, length);
76 OHOS::AppFileService::ModuleFileUri::FileUri fileUri(uriStr);
77 std::string resultStr = fileUri.GetFullDirectoryUri();
78 if (resultStr.length() == 0) {
79 return ERR_ENOENT;
80 }
81 return GetValue(resultStr, result);
82 }
83
OH_FileUri_IsValidUri(const char * uri,unsigned int length)84 bool OH_FileUri_IsValidUri(const char *uri, unsigned int length)
85 {
86 if (uri == nullptr || strlen(uri) != length) {
87 return false;
88 }
89 std::string uriStr(uri, length);
90 OHOS::AppFileService::ModuleFileUri::FileUri fileUri(uriStr);
91 return fileUri.CheckUriFormat(uriStr);
92 }
93
OH_FileUri_GetFileName(const char * uri,unsigned int length,char ** result)94 FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result)
95 {
96 if (uri == nullptr || strlen(uri) != length || result == nullptr) {
97 return ERR_PARAMS;
98 }
99 std::string uriStr(uri, length);
100 OHOS::AppFileService::ModuleFileUri::FileUri fileUri(uriStr);
101 std::string resultStr = fileUri.GetName();
102 size_t count = resultStr.length();
103 if (count == 0) {
104 return ERR_PARAMS;
105 }
106 return GetValue(resultStr, result);
107 }
108