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