1 /*
2 * Copyright (C) 2021 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 MLOG_TAG "Distributed"
16
17 #include <map>
18
19 #include "medialibrary_device_operations.h"
20 #include "media_file_utils.h"
21 #include "media_log.h"
22 #include "medialibrary_errno.h"
23 #include "result_set_utils.h"
24 #include "media_column.h"
25
26 using namespace std;
27 using namespace OHOS::NativeRdb;
28
29 namespace OHOS {
30 namespace Media {
31 static const int64_t AGING_DEVICE_INTERVAL = 14 * 24 * 60 * 60LL;
32
33 static const map<string, string> TABLE_SYNC_STATUS_MAP = {
34 { MEDIALIBRARY_TABLE, DEVICE_DB_SYNC_STATUS },
35 { PhotoColumn::PHOTOS_TABLE, DEVICE_DB_PHOTO_SYNC_STATUS }
36 // AudiosTable and DocumentsTable is not synchronized yet
37 };
38
InsertDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const OHOS::Media::MediaLibraryDeviceInfo & deviceInfo,const std::string & bundleName)39 bool MediaLibraryDeviceOperations::InsertDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
40 const OHOS::Media::MediaLibraryDeviceInfo &deviceInfo, const std::string &bundleName)
41 {
42 if (rdbStore == nullptr) {
43 MEDIA_ERR_LOG("rdbstore is nullptr");
44 return false;
45 }
46 std::vector<std::string> columns;
47 AbsRdbPredicates absPredDevice(DEVICE_TABLE);
48 absPredDevice.EqualTo(DEVICE_DB_UDID, deviceInfo.deviceUdid);
49 auto queryResultSet = rdbStore->QueryByStep(absPredDevice, columns);
50
51 auto count = 0;
52 auto ret = queryResultSet->GetRowCount(count);
53 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::InsertDeviceInfo ret = %{public}d, count = %{public}d", ret, count);
54 if (ret == NativeRdb::E_OK) {
55 if (count > 0) {
56 // 更新数据库
57 ValuesBucket valuesBucket;
58 valuesBucket.PutString(DEVICE_DB_UDID, deviceInfo.deviceUdid);
59 valuesBucket.PutString(DEVICE_DB_NETWORK_ID, deviceInfo.networkId);
60 valuesBucket.PutInt(DEVICE_DB_SYNC_STATUS, 0);
61 valuesBucket.PutInt(DEVICE_DB_PHOTO_SYNC_STATUS, 0);
62 valuesBucket.PutLong(DEVICE_DB_DATE_MODIFIED, 0);
63 return MediaLibraryDeviceDb::UpdateDeviceInfo(valuesBucket, rdbStore) == E_SUCCESS;
64 } else {
65 // 插入数据库
66 ValuesBucket valuesBucket;
67 valuesBucket.PutString(DEVICE_DB_UDID, deviceInfo.deviceUdid);
68 valuesBucket.PutString(DEVICE_DB_NETWORK_ID, deviceInfo.networkId);
69 valuesBucket.PutString(DEVICE_DB_NAME, deviceInfo.deviceName);
70 valuesBucket.PutString(DEVICE_DB_IP, std::string());
71 valuesBucket.PutString(DEVICE_DB_SELF_ID, deviceInfo.selfId);
72 valuesBucket.PutInt(DEVICE_DB_TYPE, (int32_t) deviceInfo.deviceTypeId);
73 valuesBucket.PutString(DEVICE_DB_PREPATH, std::string());
74 int64_t now = MediaFileUtils::UTCTimeSeconds();
75 valuesBucket.PutLong(DEVICE_DB_DATE_ADDED, now);
76 return MediaLibraryDeviceDb::InsertDeviceInfo(valuesBucket, rdbStore) >= 0;
77 }
78 }
79 return false;
80 }
81
UpdateDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const OHOS::Media::MediaLibraryDeviceInfo & deviceInfo,const std::string & bundleName)82 bool MediaLibraryDeviceOperations::UpdateDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
83 const OHOS::Media::MediaLibraryDeviceInfo &deviceInfo, const std::string &bundleName)
84 {
85 std::vector<std::string> columns;
86 AbsRdbPredicates absPredDevice(DEVICE_TABLE);
87 absPredDevice.EqualTo(DEVICE_DB_UDID, deviceInfo.deviceUdid);
88 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::UpdateDeviceInfo dev");
89 auto queryResultSet = rdbStore->QueryByStep(absPredDevice, columns);
90
91 auto count = 0;
92 auto ret = queryResultSet->GetRowCount(count);
93 if (ret == NativeRdb::E_OK) {
94 if (count > 0) {
95 // 更新数据库
96 ValuesBucket valuesBucket;
97 valuesBucket.PutString(DEVICE_DB_UDID, deviceInfo.deviceUdid);
98 valuesBucket.PutString(DEVICE_DB_NETWORK_ID, deviceInfo.networkId);
99 int idx = -1;
100 ret = queryResultSet->GoToFirstRow();
101 if (ret != NativeRdb::E_OK) {
102 return false;
103 }
104
105 ret = queryResultSet->GetColumnIndex(DEVICE_DB_DATE_MODIFIED, idx);
106 if (ret != NativeRdb::E_OK) {
107 return false;
108 }
109
110 int64_t modifiedTime = 0;
111 queryResultSet->GetLong(idx, modifiedTime);
112 if (modifiedTime == 0) {
113 int64_t now = MediaFileUtils::UTCTimeSeconds();
114 valuesBucket.PutLong(DEVICE_DB_DATE_MODIFIED, now);
115 }
116 return MediaLibraryDeviceDb::UpdateDeviceInfo(valuesBucket, rdbStore) == E_SUCCESS;
117 }
118 }
119 return false;
120 }
121
DeleteDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const std::string & udid)122 bool MediaLibraryDeviceOperations::DeleteDeviceInfo(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
123 const std::string &udid)
124 {
125 return MediaLibraryDeviceDb::DeleteDeviceInfo(udid, rdbStore);
126 }
127
UpdateSyncStatus(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const std::string & tableName,const std::string & udid,int32_t syncStatus)128 bool MediaLibraryDeviceOperations::UpdateSyncStatus(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
129 const std::string &tableName, const std::string &udid, int32_t syncStatus)
130 {
131 std::vector<std::string> columns;
132 AbsRdbPredicates absPredDevice(DEVICE_TABLE);
133
134 absPredDevice.EqualTo(DEVICE_DB_UDID, udid);
135 auto queryResultSet = rdbStore->QueryByStep(absPredDevice, columns);
136
137 auto count = 0;
138 auto ret = queryResultSet->GetRowCount(count);
139 if (ret == NativeRdb::E_OK) {
140 if (count > 0) {
141 // 更新数据库
142 ValuesBucket valuesBucket;
143 valuesBucket.PutString(DEVICE_DB_UDID, udid);
144 if (TABLE_SYNC_STATUS_MAP.find(tableName) != TABLE_SYNC_STATUS_MAP.end()) {
145 valuesBucket.PutInt(TABLE_SYNC_STATUS_MAP.at(tableName), syncStatus);
146 }
147 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::UpdateSyncStatus");
148 return MediaLibraryDeviceDb::UpdateDeviceInfo(valuesBucket, rdbStore) == E_SUCCESS;
149 }
150 }
151 return false;
152 }
153
GetSyncStatusById(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,const std::string & udid,const std::string & table,int32_t & syncStatus)154 bool MediaLibraryDeviceOperations::GetSyncStatusById(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
155 const std::string &udid, const std::string &table, int32_t &syncStatus)
156 {
157 std::vector<std::string> columns;
158 AbsRdbPredicates absPredDevice(DEVICE_TABLE);
159
160 absPredDevice.EqualTo(DEVICE_DB_UDID, udid);
161 auto queryResultSet = rdbStore->QueryByStep(absPredDevice, columns);
162 if (queryResultSet == nullptr || queryResultSet->GoToFirstRow() != NativeRdb::E_OK) {
163 MEDIA_ERR_LOG("Query device by udid failed");
164 return false;
165 }
166
167 if (TABLE_SYNC_STATUS_MAP.find(table) != TABLE_SYNC_STATUS_MAP.end()) {
168 int32_t columnIndexId = 0;
169 queryResultSet->GetColumnIndex(TABLE_SYNC_STATUS_MAP.at(table), columnIndexId);
170 queryResultSet->GetInt(columnIndexId, syncStatus);
171 } else {
172 for (const auto &iter : TABLE_SYNC_STATUS_MAP) {
173 int32_t columnIndexId = 0;
174 queryResultSet->GetColumnIndex(iter.second, columnIndexId);
175 queryResultSet->GetInt(columnIndexId, syncStatus);
176 if (syncStatus == DEVICE_SYNCSTATUSING) {
177 break;
178 }
179 }
180 }
181
182 MEDIA_INFO_LOG("MediaLibraryDeviceOperations::GetSyncStatusById syncStatus = %{public}d", syncStatus);
183 return true;
184 }
185
QueryDeviceTable(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,std::map<std::string,std::set<int>> & excludeMap)186 bool MediaLibraryDeviceOperations::QueryDeviceTable(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
187 std::map<std::string, std::set<int>> &excludeMap)
188 {
189 const int SHORT_UDID_LEN = 8;
190 std::vector<std::string> columns;
191 AbsRdbPredicates absPredDevice(DEVICE_TABLE);
192 auto queryResultSet = rdbStore->QueryByStep(absPredDevice, columns);
193 if (queryResultSet == nullptr) {
194 MEDIA_ERR_LOG("MediaLibraryDeviceOperations::QueryDeviceTable fail");
195 return false;
196 }
197
198 if (queryResultSet->GoToNextRow() == NativeRdb::E_OK) {
199 int32_t columnIndexId;
200 std::string selfId;
201 queryResultSet->GetColumnIndex(DEVICE_DB_SELF_ID, columnIndexId);
202 queryResultSet->GetString(columnIndexId, selfId);
203 if (selfId.length() > SHORT_UDID_LEN) {
204 std::string shortUdid = selfId.substr(0, SHORT_UDID_LEN);
205 std::string randNumber = selfId.substr(SHORT_UDID_LEN);
206 if (!randNumber.empty()) {
207 auto &data = excludeMap[shortUdid];
208 data.insert(atoi(randNumber.c_str()));
209 }
210 }
211 }
212 return true;
213 }
214
GetAllDeviceData(const std::shared_ptr<NativeRdb::RdbStore> & rdbStore,vector<MediaLibraryDeviceInfo> & outDeviceList)215 bool MediaLibraryDeviceOperations::GetAllDeviceData(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
216 vector<MediaLibraryDeviceInfo> &outDeviceList)
217 {
218 std::vector<std::string> columns;
219 AbsRdbPredicates absPredDevice(DEVICE_TABLE);
220 auto queryResultSet = rdbStore->QueryByStep(absPredDevice, columns);
221 if (queryResultSet == nullptr) {
222 MEDIA_ERR_LOG("MediaLibraryDeviceOperations::GetAllDeviceData fail");
223 return false;
224 }
225
226 while (queryResultSet->GoToNextRow() == NativeRdb::E_OK) {
227 MediaLibraryDeviceInfo deviceInfo;
228 deviceInfo.networkId = get<string>(ResultSetUtils::GetValFromColumn(DEVICE_DB_NETWORK_ID, queryResultSet,
229 TYPE_STRING));
230 deviceInfo.deviceTypeId = static_cast<DistributedHardware::DmDeviceType>(get<int32_t>(
231 ResultSetUtils::GetValFromColumn(DEVICE_DB_TYPE, queryResultSet, TYPE_INT32)));
232 deviceInfo.deviceUdid = get<string>(ResultSetUtils::GetValFromColumn(DEVICE_DB_UDID, queryResultSet,
233 TYPE_STRING));
234 deviceInfo.selfId = get<string>(ResultSetUtils::GetValFromColumn(DEVICE_DB_SELF_ID, queryResultSet,
235 TYPE_STRING));
236 outDeviceList.push_back(deviceInfo);
237 }
238 return true;
239 }
240
GetAgingDeviceData(const shared_ptr<RdbStore> & rdbStore,vector<MediaLibraryDeviceInfo> & outDeviceList)241 bool MediaLibraryDeviceOperations::GetAgingDeviceData(const shared_ptr<RdbStore> &rdbStore,
242 vector<MediaLibraryDeviceInfo> &outDeviceList)
243 {
244 vector<string> columns;
245 int64_t agingTime = MediaFileUtils::UTCTimeSeconds() - AGING_DEVICE_INTERVAL;
246
247 MEDIA_INFO_LOG("GetAgingDeviceData less than %{public}" PRId64, agingTime);
248 AbsRdbPredicates absPredevice(DEVICE_TABLE);
249 absPredevice.GreaterThan(DEVICE_DB_DATE_MODIFIED, to_string(0))->And()->
250 LessThan(DEVICE_DB_DATE_MODIFIED, to_string(agingTime));
251 auto queryResultSet = rdbStore->QueryByStep(absPredevice, columns);
252 if (queryResultSet == nullptr) {
253 MEDIA_ERR_LOG("GetAgingDeviceData fail");
254 return false;
255 }
256 auto ret = queryResultSet->GoToFirstRow();
257 if (ret != NativeRdb::E_OK) {
258 MEDIA_ERR_LOG("Failed to fetch first record, ret = %{public}d", ret);
259 return false;
260 }
261
262 MediaLibraryDeviceInfo deviceInfo;
263 do {
264 deviceInfo.networkId = get<string>(ResultSetUtils::GetValFromColumn(DEVICE_DB_NETWORK_ID, queryResultSet,
265 TYPE_STRING));
266 deviceInfo.deviceTypeId = (DistributedHardware::DmDeviceType)(get<int32_t>(ResultSetUtils::GetValFromColumn(
267 DEVICE_DB_TYPE, queryResultSet, TYPE_INT32)));
268 deviceInfo.deviceUdid = get<string>(ResultSetUtils::GetValFromColumn(DEVICE_DB_UDID, queryResultSet,
269 TYPE_STRING));
270 deviceInfo.selfId = get<string>(ResultSetUtils::GetValFromColumn(DEVICE_DB_SELF_ID, queryResultSet,
271 TYPE_STRING));
272 outDeviceList.push_back(deviceInfo);
273 } while (queryResultSet->GoToNextRow() == NativeRdb::E_OK);
274
275 MEDIA_ERR_LOG("GetAgingDeviceData OUT, deviceSize = %{public}d", static_cast<int>(outDeviceList.size()));
276 return true;
277 }
278
GetAllDeviceUdid(const shared_ptr<RdbStore> & rdbStore,vector<string> & deviceUdids)279 bool MediaLibraryDeviceOperations::GetAllDeviceUdid(const shared_ptr<RdbStore> &rdbStore,
280 vector<string> &deviceUdids)
281 {
282 vector<string> columns;
283 AbsRdbPredicates absPreDevice(DEVICE_TABLE);
284 auto queryResultSet = rdbStore->QueryByStep(absPreDevice, columns);
285 if (queryResultSet == nullptr) {
286 MEDIA_ERR_LOG("MediaLibraryDeviceOperations::GetAllDeviceUdid fail");
287 return false;
288 }
289 auto ret = queryResultSet->GoToFirstRow();
290 if (ret != NativeRdb::E_OK) {
291 return true;
292 }
293 do {
294 string udid = get<string>(ResultSetUtils::GetValFromColumn(DEVICE_DB_UDID, queryResultSet,
295 TYPE_STRING));
296 deviceUdids.push_back(udid);
297 } while (queryResultSet->GoToNextRow() == NativeRdb::E_OK);
298
299 MEDIA_DEBUG_LOG("MediaLibraryDeviceOperations::GetAllDeviceUdid OUT");
300 return true;
301 }
302 } // namespace Media
303 } // namespace OHOS
304