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 "b_resources/b_constants.h"
17 #include "file_fileuri_ffi.h"
18 #include "file_uri.h"
19 #include "macro.h"
20 #include "common_func.h"
21 #include "securec.h"
22 #include <cinttypes>
23 
24 using namespace OHOS::FFI;
25 
26 namespace OHOS {
27 namespace CJSystemapi {
28 namespace FileUri {
29 
30 
31 extern "C" {
MallocCString(const std::string & origin)32 char* MallocCString(const std::string& origin)
33 {
34     if (origin.empty()) {
35         return nullptr;
36     }
37     if (origin.length() > OHOS::FileManagement::Backup::BConstants::PARAM_STARING_MAX_MEMORY) {
38         LOGE("The param origin is too big");
39         return nullptr;
40     }
41     auto length = origin.length() + 1;
42     char* res = static_cast<char*>(malloc(sizeof(char) * length));
43     if (res == nullptr) {
44         return nullptr;
45     }
46     return std::char_traits<char>::copy(res, origin.c_str(), length);
47 }
48 
FfiOHOSFILEUriCreateUri(const char * uriOrPath)49 int64_t FfiOHOSFILEUriCreateUri(const char* uriOrPath)
50 {
51     LOGI("FILEURI_TEST::FfiOHOSFILEUriCreateUri");
52     auto nativeUri = FFIData::Create<FileUriImpl>(uriOrPath);
53     if (!nativeUri) {
54         return ERR_INVALID_INSTANCE_CODE;
55     }
56     int64_t id = nativeUri->GetID();
57     LOGI("FILEURI_TEST::FfiOHOSFILEUriCreateUri %{public}" PRId64, id);
58     return id;
59 }
60 
FfiOHOSFILEUriGetPath(int64_t id)61 char* FfiOHOSFILEUriGetPath(int64_t id)
62 {
63     LOGI("FILEURI_TEST::FfiOHOSFILEUriGetPath");
64     auto instance = FFIData::GetData<FileUriImpl>(id);
65     if (!instance) {
66         return nullptr;
67     }
68     auto path = instance->GetPath();
69     char* result = MallocCString(path);
70     return result;
71 }
72 
FfiOHOSFILEUriGetName(int64_t id)73 char* FfiOHOSFILEUriGetName(int64_t id)
74 {
75     LOGI("FILEURI_TEST::FfiOHOSFILEUriGetName");
76     auto instance = FFIData::GetData<FileUriImpl>(id);
77     if (!instance) {
78         return nullptr;
79     }
80     auto name = instance->GetName();
81     char* result = MallocCString(name);
82     return result;
83 }
84 
FfiOHOSFILEUriToString(int64_t id)85 char* FfiOHOSFILEUriToString(int64_t id)
86 {
87     LOGI("FILEURI_TEST::FfiOHOSFILEUriToString");
88     auto instance = FFIData::GetData<FileUriImpl>(id);
89     if (!instance) {
90         return nullptr;
91     }
92     auto str = instance->ToString();
93     char* result = MallocCString(str);
94     return result;
95 }
96 
FfiOHOSFILEUriGetUriFromPath(const char * path)97 char* FfiOHOSFILEUriGetUriFromPath(const char* path)
98 {
99     auto uri = AppFileService::CommonFunc::GetUriFromPath(path);
100     char* result = MallocCString(uri);
101     return result;
102 }
103 }
104 }
105 } // namespace CJSystemapi
106 } // namespace OHOS