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