1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "RelationalAsset"
16 #include "relational_asset.h"
17
18 #include <cstdlib>
19
20 #include "logger.h"
21 #include "relational_store_error_code.h"
22 #include "securec.h"
23
24 using namespace OHOS::RdbNdk;
25 constexpr int ASSET_TRANSFORM_BASE = 10;
OH_Data_Asset_SetName(Data_Asset * asset,const char * name)26 int OH_Data_Asset_SetName(Data_Asset *asset, const char *name)
27 {
28 if (asset == nullptr || name == nullptr) {
29 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
30 }
31 asset->asset_.name = name;
32 return OH_Rdb_ErrCode::RDB_OK;
33 }
34
OH_Data_Asset_SetUri(Data_Asset * asset,const char * uri)35 int OH_Data_Asset_SetUri(Data_Asset *asset, const char *uri)
36 {
37 if (asset == nullptr || uri == nullptr) {
38 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
39 }
40
41 asset->asset_.uri = uri;
42 return OH_Rdb_ErrCode::RDB_OK;
43 }
44
OH_Data_Asset_SetPath(Data_Asset * asset,const char * path)45 int OH_Data_Asset_SetPath(Data_Asset *asset, const char *path)
46 {
47 if (asset == nullptr || path == nullptr) {
48 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
49 }
50
51 asset->asset_.path = path;
52 return OH_Rdb_ErrCode::RDB_OK;
53 }
54
OH_Data_Asset_SetCreateTime(Data_Asset * asset,int64_t createTime)55 int OH_Data_Asset_SetCreateTime(Data_Asset *asset, int64_t createTime)
56 {
57 if (asset == nullptr) {
58 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
59 }
60
61 asset->asset_.createTime = std::to_string(createTime);
62 return OH_Rdb_ErrCode::RDB_OK;
63 }
64
OH_Data_Asset_SetModifyTime(Data_Asset * asset,int64_t modifyTime)65 int OH_Data_Asset_SetModifyTime(Data_Asset *asset, int64_t modifyTime)
66 {
67 if (asset == nullptr) {
68 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
69 }
70
71 asset->asset_.modifyTime = std::to_string(modifyTime);
72 return OH_Rdb_ErrCode::RDB_OK;
73 }
74
OH_Data_Asset_SetSize(Data_Asset * asset,size_t size)75 int OH_Data_Asset_SetSize(Data_Asset *asset, size_t size)
76 {
77 if (asset == nullptr) {
78 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
79 }
80
81 asset->asset_.size = std::to_string(size);
82 return OH_Rdb_ErrCode::RDB_OK;
83 }
84
OH_Data_Asset_SetStatus(Data_Asset * asset,Data_AssetStatus status)85 int OH_Data_Asset_SetStatus(Data_Asset *asset, Data_AssetStatus status)
86 {
87 if (asset == nullptr) {
88 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
89 }
90
91 asset->asset_.status = status;
92 return OH_Rdb_ErrCode::RDB_OK;
93 }
94
OH_Data_Asset_GetName(Data_Asset * asset,char * name,size_t * length)95 int OH_Data_Asset_GetName(Data_Asset *asset, char *name, size_t *length)
96 {
97 if (asset == nullptr) {
98 LOG_ERROR("Asset get name error: asset is NULL ? %{public}d.", (asset == nullptr));
99 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
100 }
101 size_t nameLength = asset->asset_.name.size();
102 if (nameLength >= *length) {
103 LOG_ERROR("Asset get name error: length is too small ? %{public}d.", (nameLength >= *length));
104 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
105 }
106 errno_t result = strcpy_s(name, *length, asset->asset_.name.c_str());
107 if (result != EOK) {
108 LOG_ERROR("strcpy_s failed, result is %{public}d", result);
109 return OH_Rdb_ErrCode::RDB_ERR;
110 }
111 *length = nameLength;
112 return OH_Rdb_ErrCode::RDB_OK;
113 }
114
OH_Data_Asset_GetUri(Data_Asset * asset,char * uri,size_t * length)115 int OH_Data_Asset_GetUri(Data_Asset *asset, char *uri, size_t *length)
116 {
117 if (asset == nullptr) {
118 LOG_ERROR("Asset get uri error: asset is NULL ? %{public}d.", (asset == nullptr));
119 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
120 }
121 size_t uriLength = asset->asset_.uri.size();
122 if (uriLength >= *length) {
123 LOG_ERROR("Asset get uri error: length is too small ? %{public}d.", (uriLength >= *length));
124 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
125 }
126
127 errno_t result = strcpy_s(uri, *length, asset->asset_.uri.c_str());
128 if (result != EOK) {
129 LOG_ERROR("strcpy_s failed, result is %{public}d", result);
130 return OH_Rdb_ErrCode::RDB_ERR;
131 }
132 *length = uriLength;
133 return OH_Rdb_ErrCode::RDB_OK;
134 }
135
OH_Data_Asset_GetPath(Data_Asset * asset,char * path,size_t * length)136 int OH_Data_Asset_GetPath(Data_Asset *asset, char *path, size_t *length)
137 {
138 if (asset == nullptr) {
139 LOG_ERROR("Asset get path error: asset is NULL ? %{public}d.", (asset == nullptr));
140 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
141 }
142 size_t pathLength = asset->asset_.path.size();
143 if (pathLength >= *length) {
144 LOG_ERROR("Asset get path error: length is too small ? %{public}d.", (pathLength >= *length));
145 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
146 }
147 errno_t result = strcpy_s(path, *length, asset->asset_.path.c_str());
148 if (result != EOK) {
149 LOG_ERROR("strcpy_s failed, result is %{public}d", result);
150 return OH_Rdb_ErrCode::RDB_ERR;
151 }
152 *length = pathLength;
153 return OH_Rdb_ErrCode::RDB_OK;
154 }
155
OH_Data_Asset_GetCreateTime(Data_Asset * asset,int64_t * createTime)156 int OH_Data_Asset_GetCreateTime(Data_Asset *asset, int64_t *createTime)
157 {
158 if (asset == nullptr) {
159 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
160 }
161 char *endPtr;
162 *createTime = strtol(asset->asset_.createTime.c_str(), &endPtr, ASSET_TRANSFORM_BASE);
163 if (*endPtr != '\0') {
164 LOG_ERROR("GetCreateTime failed.");
165 return OH_Rdb_ErrCode::RDB_ERR;
166 }
167 return OH_Rdb_ErrCode::RDB_OK;
168 }
169
OH_Data_Asset_GetModifyTime(Data_Asset * asset,int64_t * modifyTime)170 int OH_Data_Asset_GetModifyTime(Data_Asset *asset, int64_t *modifyTime)
171 {
172 if (asset == nullptr) {
173 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
174 }
175 char *endPtr;
176 *modifyTime = strtol(asset->asset_.modifyTime.c_str(), &endPtr, ASSET_TRANSFORM_BASE);
177 if (*endPtr != '\0') {
178 LOG_ERROR("GetModifyTime failed.");
179 return OH_Rdb_ErrCode::RDB_ERR;
180 }
181 return OH_Rdb_ErrCode::RDB_OK;
182 }
183
OH_Data_Asset_GetSize(Data_Asset * asset,size_t * size)184 int OH_Data_Asset_GetSize(Data_Asset *asset, size_t *size)
185 {
186 if (asset == nullptr) {
187 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
188 }
189 char *endPtr;
190 *size = strtol(asset->asset_.size.c_str(), &endPtr, ASSET_TRANSFORM_BASE);
191 if (*endPtr != '\0') {
192 LOG_ERROR("GetModifyTime failed.");
193 return OH_Rdb_ErrCode::RDB_ERR;
194 }
195 return OH_Rdb_ErrCode::RDB_OK;
196 }
197
OH_Data_Asset_GetStatus(Data_Asset * asset,Data_AssetStatus * status)198 int OH_Data_Asset_GetStatus(Data_Asset *asset, Data_AssetStatus *status)
199 {
200 if (asset == nullptr) {
201 return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
202 }
203 *status = static_cast<Data_AssetStatus>(asset->asset_.status);
204 return OH_Rdb_ErrCode::RDB_OK;
205 }
206
OH_Data_Asset_CreateOne()207 Data_Asset *OH_Data_Asset_CreateOne()
208 {
209 return new (std::nothrow) Data_Asset();
210 }
211
OH_Data_Asset_DestroyOne(Data_Asset * asset)212 int OH_Data_Asset_DestroyOne(Data_Asset *asset)
213 {
214 delete asset;
215 asset = nullptr;
216 return OH_Rdb_ErrCode::RDB_OK;
217 }
218
OH_Data_Asset_CreateMultiple(uint32_t count)219 Data_Asset **OH_Data_Asset_CreateMultiple(uint32_t count)
220 {
221 auto assets = new Data_Asset *[count];
222 for (uint32_t i = 0; i < count; ++i) {
223 assets[i] = new Data_Asset();
224 }
225 return assets;
226 }
227
OH_Data_Asset_DestroyMultiple(Data_Asset ** assets,uint32_t count)228 int OH_Data_Asset_DestroyMultiple(Data_Asset **assets, uint32_t count)
229 {
230 for (uint32_t i = 0; i < count; ++i) {
231 delete assets[i];
232 }
233 delete[] assets;
234 assets = nullptr;
235 return OH_Rdb_ErrCode::RDB_OK;
236 }
237