[Internal: merge sync-agent]
[platform/core/system/sync-agent.git] / src / framework / data-adapter / agent_manager.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vconf.h>
19
20 #include "initialization/parser.h"
21
22 #include "utility/fw_alloc.h"
23 #include "utility/sync_util.h"
24 #include "utility/fw_file.h"
25
26 #include "plugin/data_connector_plugin.h"
27 #include "plugin/account_plugin.h"
28
29 #include "account/manager.h"
30 #include "account/util_internal.h"
31
32 #include "data-adapter/changelog.h"
33 #include "data-adapter/luid.h"
34
35 #include "data-adapter/agent_handler_manager.h"
36 #include "data-adapter/agent_handler_manager_internal.h"
37
38 #include "data-adapter/agent_manager.h"
39 #include "data-adapter/agent_manager_internal.h"
40
41 #include "data-adapter/interface_service_item.h"
42
43 #ifndef EXPORT_API
44 #define EXPORT_API __attribute__ ((visibility("default")))
45 #endif
46
47 #ifndef SYNC_AGENT_LOG
48 #undef LOG_TAG
49 #define LOG_TAG "AF_DACI"
50 #endif
51
52 /*
53  * Key          : db/Apps/AgentFramework/[Agent Name]/ServiceChangePoint/[FW AccountID]/[Service_Type]
54  * Value                : Change Point (TimeStamp)
55  */
56 #define VCONF_KEY_SERIVCE_CHANGEPOINT   ".%s_service_change_point_%d_%d"
57
58 static void _set_serivce_change_point(int fw_account_id, int service_type, int change_point);
59
60 static int _get_serivce_change_point(int fw_account_id, int service_type);
61
62 static sync_agent_da_return_e _construct_change_log_tbl_from_service(int fw_account_id, int service_type);
63
64 EXPORT_API sync_agent_da_return_e sync_agent_construct_item_tbl_from_service(int fw_account_id, int service_type)
65 {
66         _EXTERN_FUNC_ENTER;
67         sync_agent_da_return_e err_code = SYNC_AGENT_DA_SUCCESS;
68         err_code = _construct_change_log_tbl_from_service(fw_account_id, service_type);
69         _EXTERN_FUNC_EXIT;
70         return err_code;
71 }
72
73 EXPORT_API sync_agent_da_return_e sync_agent_construct_folder_tbl_from_service(int service_type)
74 {
75         _EXTERN_FUNC_ENTER;
76
77         int fw_account_cnt = 0;
78         int *fw_account_id_list = da_get_account_id_list(&fw_account_cnt);
79
80         if (fw_account_id_list == NULL || fw_account_cnt == 0) {
81                 if (fw_account_id_list != NULL) {
82                         free(fw_account_id_list);
83                         fw_account_id_list = NULL;
84                 }
85                 return SYNC_AGENT_DA_SUCCESS;
86         }
87
88         int *service_account_id_list = 0;
89         if (fw_account_cnt != 0) {
90                 service_account_id_list = (int *)calloc(fw_account_cnt, sizeof(int));
91                 if (service_account_id_list == NULL) {
92                         if (fw_account_id_list != NULL) {
93                                 free(fw_account_id_list);
94                                 fw_account_id_list = NULL;
95                         }
96                         return SYNC_AGENT_DA_ERR_MEMORY_FULL;
97                 }
98
99                 int i = 0;
100                 for (; i < fw_account_cnt; i++) {
101                         service_account_id_list[i] = acc_get_service_account_id(service_type, fw_account_id_list[i]);
102                         _DEBUG_INFO("service_account_id_list[%d] : %d", i, service_account_id_list[i]);
103                 }
104         }
105
106         int service_account_id_cnt = fw_account_cnt;
107         _DEBUG_INFO("service_account_id_cnt : %d", service_account_id_cnt);
108
109         int i = 0;
110         for (; i < service_account_id_cnt; i++) {
111                 int fw_account_id = acc_get_fw_account_id(service_type, service_account_id_list[i], 0);
112                 _DEBUG_INFO("fw_account_id[%d] : %d", i, fw_account_id);
113
114                 plugin_get_folder_id_list_cb func_get_folder_id_list = plugin_get_function_get_folder_id_list(service_type);
115                 if (func_get_folder_id_list == NULL) {
116                         _DEBUG_ERROR("func_get_folder_id_list is NULL !!");
117                         if (fw_account_id_list != NULL) {
118                                 free(fw_account_id_list);
119                         }
120 //                      if (service_account_id_list != NULL) {
121                         free(service_account_id_list);
122 //                      }
123                         return SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
124                 }
125
126                 int service_folder_cnt = 0;
127                 int *service_folder_type_list = 0;
128                 char **service_folder_list = func_get_folder_id_list(service_account_id_list[i], &service_folder_cnt, &service_folder_type_list);
129                 if (service_folder_list == NULL) {
130                         _DEBUG_ERROR("service folder list is NULL !!");
131                         if (fw_account_id_list != NULL) {
132                                 free(fw_account_id_list);
133                                 fw_account_id_list = NULL;
134                         }
135 //                      if (service_account_id_list != NULL) {
136                         free(service_account_id_list);
137 //                      }
138                         return SYNC_AGENT_DA_ERRORS;
139                 }
140
141                 _DEBUG_INFO("service_folder_cnt  : %d", service_folder_cnt);
142
143 /*
144                  //Hard Coding - If account has one folder, consider folder' state complete.
145
146                 t_sync_agent_da_id_list_s_t *foder_id_list_info = T_DACI_Get_Folder_Folder_Id_List_By_Account_Id(fw_account_id);
147
148                 if (foder_id_list_info != NULL) {
149                         if (foder_id_list_info->count > 0) {
150                                 _DEBUG_INFO("foder_id_list_info->count  : %d", foder_id_list_info->count);
151                                 return SYNC_AGENT_DA_SUCCESS;
152                         }
153                 }*/
154
155                 int i = 0;
156                 for (; i < service_folder_cnt; i++) {
157
158                         /* checking whether the service_id exist in Folder table */
159                         char *temp_folder_id = da_get_folder_id_by_service_id_wrapper(fw_account_id, service_type, service_folder_list[i], service_folder_type_list[i]);
160
161                         _DEBUG_INFO("fw_account_id  : %d", fw_account_id);
162                         _DEBUG_INFO("service_folder_list[i]  : %s", service_folder_list[i]);
163                         _DEBUG_INFO("service_folder_type_list[i]  : %d", service_folder_type_list[i]);
164                         _DEBUG_INFO("temp_folder_id  : %s", temp_folder_id);
165                         if (temp_folder_id != NULL) {
166                                 free(temp_folder_id);
167                         } else {
168                                 /* Add Agent Folder table */
169                                 sync_agent_da_folder_s folder;
170                                 folder.account_id = fw_account_id;
171                                 folder.folder_id = sync_agent_generate_folder_luid();
172                                 folder.parent_folder_id = "0";
173                                 folder.data_store_id = service_type;
174                                 folder.folder_type_id = service_folder_type_list[i];
175                                 folder.service_id = service_folder_list[i];
176                                 folder.access_name = "DACI";
177
178                                 da_add_folder_internal(&folder, 1);
179                         }
180                 }
181         }
182
183         if (fw_account_id_list != NULL) {
184                 free(fw_account_id_list);
185                 fw_account_id_list = NULL;
186         }
187         if (service_account_id_list != NULL) {
188                 free(service_account_id_list);
189                 service_account_id_list = NULL;
190         }
191
192         _EXTERN_FUNC_EXIT;
193
194         return SYNC_AGENT_DA_SUCCESS;
195 }
196
197 EXPORT_API sync_agent_da_return_e sync_agent_refresh_item_tbl_from_service(int fw_account_id, int service_type)
198 {
199         _EXTERN_FUNC_ENTER;
200
201         /*
202          * todo Delete folder table
203          */
204
205         /*
206          * Delete Changelog table
207          */
208         da_delete_item_changelog_by_item_type_id_internal(fw_account_id, service_type);
209
210         /*
211          * Delete item table
212          */
213         da_delete_item_by_item_type_id_internal(fw_account_id, service_type);
214
215         /*
216          *      todo Insert folder table
217          */
218
219         /*
220          * insert item table
221          */
222         plugin_get_changed_item_for_folder_add_cb func_get_changed_item_for_folder_add = plugin_get_function_get_changed_item_for_folder_add(service_type);
223         plugin_get_changed_item_for_folder_update_cb func_get_changed_item_for_folder_update = plugin_get_function_get_changed_item_for_folder_update(service_type);
224
225         int service_account_id = acc_get_service_account_id(service_type, fw_account_id);
226         _DEBUG_INFO("service_account_id : %d", service_account_id);
227
228         sync_agent_da_id_list_s *fw_folder_id_list_info = da_get_folder_id_list_by_item_type_id_internal(fw_account_id, service_type);
229
230         retvm_if(fw_folder_id_list_info == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_id_list_s is NULL !!");
231
232         char **fw_folder_id_list = 0;
233         int fw_folder_id_cnt = 0;
234         if (fw_folder_id_list_info != NULL) {
235                 fw_folder_id_list = fw_folder_id_list_info->id;
236                 fw_folder_id_cnt = fw_folder_id_list_info->count;
237
238                 _DEBUG_INFO("fw_folder_id_cnt : %d", fw_folder_id_cnt);
239                 _DEBUG_INFO("fw_folder_id_list[0] : %s", fw_folder_id_list_info->id[0]);
240                 _DEBUG_INFO("fw_folder_id_list[0] : %s", fw_folder_id_list[0]);
241         }
242
243         int i = 0;
244         for (; i < fw_folder_id_cnt; i++) {
245                 _DEBUG_INFO("fw_folder_id_list[%d] : %s", i, fw_folder_id_list[i]);
246
247                 char *service_folder_id = sync_agent_get_service_folder_id(fw_folder_id_list[i]);
248
249                 _DEBUG_INFO("service_folder_id : %s", service_folder_id);
250
251                 int added_item_cnt = 0;
252                 sync_agent_plugin_item_node_s *added_service_item_id_list = func_get_changed_item_for_folder_add(service_account_id, service_folder_id, 0, &added_item_cnt);
253                 sync_agent_plugin_item_node_s *cursor_ptr = added_service_item_id_list;
254
255                 _DEBUG_INFO("added_item_cnt : %d", added_item_cnt);
256
257                 /*
258                  * Process Add Item
259                  */
260                 sync_agent_begin_transaction();
261                 char **fw_item_id_list = sync_agent_generate_item_luid(1, added_item_cnt);
262                 if (fw_item_id_list != NULL) {
263                         int y = 0;
264                         for (; y < added_item_cnt; y++) {
265                                 sync_agent_da_item_s item;
266                                 item.account_id = fw_account_id;
267                                 item.item_id = fw_item_id_list[y];
268                                 item.folder_id = fw_folder_id_list[i];
269                                 item.data_store_id = service_type;
270                                 item.service_id = cursor_ptr->item_id;
271                                 item.parent_service_id = cursor_ptr->parent_item_id;
272                                 item.access_name = "DACI";
273
274                                 sync_agent_da_return_e ret = da_add_item_internal(&item, 1);
275                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
276                                         _DEBUG_ERROR("add item failed return : %d", ret);
277                                 }
278
279                                 sync_agent_plugin_item_node_s *temp = cursor_ptr;
280                                 cursor_ptr = cursor_ptr->next;
281                                 free(temp->item_id);
282                                 free(temp->parent_item_id);
283                                 free(temp);
284
285                                 if (fw_item_id_list[y] != NULL) {
286                                         free(fw_item_id_list[y]);
287                                 }
288                         }
289                         free(fw_item_id_list);
290                 }
291                 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
292
293                 /*
294                  * Process Update Item
295                  */
296                 int updated_item_cnt = 0;
297                 sync_agent_plugin_item_node_s *updated_service_item_id_list = func_get_changed_item_for_folder_update(service_account_id, service_folder_id, 0, &updated_item_cnt);
298                 cursor_ptr = updated_service_item_id_list;
299
300                 _DEBUG_INFO("updated_item_cnt : %d", updated_item_cnt);
301
302                 sync_agent_begin_transaction();
303                 fw_item_id_list = sync_agent_generate_item_luid(1, updated_item_cnt);
304                 if (fw_item_id_list != NULL) {
305                         int y = 0;
306                         for (; y < updated_item_cnt; y++) {
307                                 sync_agent_da_item_s item;
308                                 item.account_id = fw_account_id;
309                                 item.item_id = fw_item_id_list[y];
310                                 item.folder_id = fw_folder_id_list[i];
311                                 item.data_store_id = service_type;
312                                 item.service_id = cursor_ptr->item_id;
313                                 item.parent_service_id = cursor_ptr->parent_item_id;
314                                 item.access_name = "DACI";
315
316                                 sync_agent_da_return_e ret = da_add_item_internal(&item, 1);
317                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
318                                         _DEBUG_ERROR("add item failed return : %d", ret);
319                                 }
320
321                                 sync_agent_plugin_item_node_s *temp = cursor_ptr;
322                                 cursor_ptr = cursor_ptr->next;
323                                 free(temp->item_id);
324                                 free(temp->parent_item_id);
325                                 free(temp);
326
327                                 if (fw_item_id_list[y] != NULL) {
328                                         free(fw_item_id_list[y]);
329                                 }
330                         }
331                         free(fw_item_id_list);
332                 }
333                 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
334
335                 if (fw_folder_id_list[i] != NULL) {
336                         free(fw_folder_id_list[i]);
337                 }
338
339                 if (service_folder_id != NULL) {
340                         free(service_folder_id);
341                 }
342         }
343
344         if (fw_folder_id_list_info != NULL) {
345                 if (fw_folder_id_list_info->id != NULL) {
346                         free(fw_folder_id_list_info->id);
347                 }
348                 free(fw_folder_id_list_info);
349         }
350
351         _EXTERN_FUNC_EXIT;
352
353         return SYNC_AGENT_DA_SUCCESS;
354 }
355
356 sync_agent_da_return_e da_refresh_changelog_tbl_from_service(int fw_account_id, int service_type)
357 {
358         _EXTERN_FUNC_ENTER;
359
360         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
361
362         /*
363          * Delete Changelog table
364          */
365         ret = da_delete_item_changelog_by_item_type_id_internal(fw_account_id, service_type);
366
367         /*
368          * Delete item table
369          */
370         ret = da_delete_item_by_item_type_id_internal(fw_account_id, service_type);
371
372         /*
373          * Construct Item & changelog table
374          */
375         plugin_get_changed_item_for_folder_add_cb func_get_changed_item_for_folder_add = plugin_get_function_get_changed_item_for_folder_add(service_type);
376
377         int service_account_id = acc_get_service_account_id(service_type, fw_account_id);
378         _DEBUG_INFO("service_account_id : %d", service_account_id);
379
380         sync_agent_da_id_list_s *fw_folder_id_list_info = da_get_folder_id_list_by_item_type_id_internal(fw_account_id, service_type);
381
382         retvm_if(fw_folder_id_list_info == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_id_list_s is NULL !!");
383
384         char **fw_folder_id_list = 0;
385         int fw_folder_id_cnt = 0;
386         if (fw_folder_id_list_info != NULL) {
387                 fw_folder_id_list = fw_folder_id_list_info->id;
388                 fw_folder_id_cnt = fw_folder_id_list_info->count;
389
390                 _DEBUG_INFO("[RJW] fw_folder_id_cnt : %d", fw_folder_id_cnt);
391                 _DEBUG_INFO("[RJW] fw_folder_id_list[0] : %s", fw_folder_id_list_info->id[0]);
392                 _DEBUG_INFO("[RJW] fw_folder_id_list[0] : %s", fw_folder_id_list[0]);
393         }
394
395         int i = 0;
396         for (; i < fw_folder_id_cnt; i++) {
397                 _DEBUG_INFO("fw_folder_id_list[%d] : %s", i, fw_folder_id_list[i]);
398
399                 char *service_folder_id = sync_agent_get_service_folder_id(fw_folder_id_list[i]);
400
401                 _DEBUG_INFO("service_folder_id : %s", service_folder_id);
402
403                 int item_cnt = 0;
404                 sync_agent_plugin_item_node_s *service_item_id_list = func_get_changed_item_for_folder_add(service_account_id, service_folder_id, 0, &item_cnt);
405                 sync_agent_plugin_item_node_s *cursor_ptr = service_item_id_list;
406
407                 _DEBUG_INFO("item_cnt : %d", item_cnt);
408
409                 sync_agent_begin_transaction();
410                 char **fw_item_id_list = sync_agent_generate_item_luid(1, item_cnt);
411                 if (fw_item_id_list != NULL) {
412                         int k = 0;
413                         for (; k < item_cnt; k++) {
414                                 sync_agent_da_item_s item;
415                                 char *service_item_id = cursor_ptr->item_id;
416                                 _DEBUG_INFO("service_item_id[%d] : %s", k, service_item_id);
417                                 _DEBUG_INFO("fw_item_id : %s", fw_item_id_list[k]);
418
419                                 item.item_id = fw_item_id_list[k];
420                                 item.data_store_id = service_type;
421                                 item.account_id = fw_account_id;
422                                 item.folder_id = fw_folder_id_list[i];
423                                 item.service_id = cursor_ptr->item_id;
424                                 item.parent_service_id = cursor_ptr->parent_item_id;
425                                 da_changelog_add_item_internal(&item);
426
427                                 sync_agent_plugin_item_node_s *temp = cursor_ptr;
428                                 cursor_ptr = cursor_ptr->next;
429                                 free(temp->item_id);
430                                 free(temp);
431                                 if (fw_item_id_list[k] != NULL) {
432                                         free(fw_item_id_list[k]);
433                                 }
434                         }
435                         free(fw_item_id_list);
436                 }
437                 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
438         }
439
440         if (fw_folder_id_list_info != NULL) {
441                 if (fw_folder_id_list_info->id != NULL) {
442                         free(fw_folder_id_list_info->id);
443                 }
444                 free(fw_folder_id_list_info);
445         }
446
447         _EXTERN_FUNC_EXIT;
448
449         return ret;
450 }
451
452 EXPORT_API sync_agent_da_return_e sync_agent_set_service_change_point(int fw_account_id, int service_type)
453 {
454         _EXTERN_FUNC_ENTER;
455
456         plugin_get_last_change_point_cb func_get_last_changepoint = plugin_get_function_get_last_change_point(service_type);
457
458         int new_last_change_point = func_get_last_changepoint();
459
460         _DEBUG_INFO("new_last_change_point : %d", new_last_change_point);
461
462         _set_serivce_change_point(fw_account_id, service_type, new_last_change_point);
463
464         _EXTERN_FUNC_EXIT;
465
466         return SYNC_AGENT_DA_SUCCESS;
467 }
468
469 /********************************************* static function ****************************/
470
471 static void _set_serivce_change_point(int fw_account_id, int service_type, int change_point)
472 {
473         _INNER_FUNC_ENTER;
474
475         const char *agent_key = init_get_agent_key();
476
477         char vconf_key[100];
478         snprintf(vconf_key, sizeof(vconf_key), VCONF_KEY_SERIVCE_CHANGEPOINT, agent_key, fw_account_id, service_type);
479
480         _DEBUG_TRACE("vConfKey : %s", vconf_key);
481
482         if (!sync_agent_set_int_into_file(vconf_key, change_point)) {
483                 _DEBUG_ERROR("vconf_set_id FAIL");
484         } else {
485                 _DEBUG_TRACE("Sucess Vconf Set Id");
486         }
487
488         _INNER_FUNC_EXIT;
489 }
490
491 static int _get_serivce_change_point(int fw_account_id, int service_type)
492 {
493         _INNER_FUNC_ENTER;
494
495         const char *agent_key = init_get_agent_key();
496
497         char vconf_key[100];
498         snprintf(vconf_key, sizeof(vconf_key), VCONF_KEY_SERIVCE_CHANGEPOINT, agent_key, fw_account_id, service_type);
499
500         int change_point = 0;
501         if (!sync_agent_get_int_from_file(vconf_key, &change_point)) {
502                 _DEBUG_TRACE("vconf_set_id FAIL - First attempt??");
503                 return 0;
504         }
505
506         _INNER_FUNC_EXIT;
507
508         return change_point;
509 }
510
511 sync_agent_da_return_e _construct_change_log_tbl_from_service(int fw_account_id, int service_type)
512 {
513         _INNER_FUNC_ENTER;
514
515         int service_account_id = acc_get_service_account_id(service_type, fw_account_id);
516         _DEBUG_TRACE("service_account_id : %d", service_account_id);
517
518         int changed_flag = 0;
519         int fw_change_point = _get_serivce_change_point(fw_account_id, service_type);
520         _DEBUG_TRACE("fw_last_change_point : %d", fw_change_point);
521
522         plugin_get_changed_item_for_folder_add_cb func_get_changed_item_for_folder_add = plugin_get_function_get_changed_item_for_folder_add(service_type);
523         plugin_get_changed_item_for_folder_delete_cb func_get_changed_item_for_folder_del = plugin_get_function_get_changed_item_for_folder_delete(service_type);
524         plugin_get_changed_item_for_folder_update_cb func_get_changed_item_for_folder_update = plugin_get_function_get_changed_item_for_folder_update(service_type);
525
526         /*
527          * todo Get fw_folder_id list in the service
528          */
529         sync_agent_da_id_list_s *fw_folder_id_list_info = da_get_folder_id_list_by_item_type_id_internal(fw_account_id, service_type);
530
531         retvm_if(fw_folder_id_list_info == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_id_list_s is NULL !!");
532
533         char **fw_folder_id_list = 0;
534         int fw_folder_id_cnt = 0;
535         if (fw_folder_id_list_info != NULL) {
536                 fw_folder_id_list = fw_folder_id_list_info->id;
537                 fw_folder_id_cnt = fw_folder_id_list_info->count;
538
539                 _DEBUG_TRACE("fw_folder_id_cnt : %d", fw_folder_id_cnt);
540                 _DEBUG_TRACE("fw_folder_id_list[0] : %s", fw_folder_id_list_info->id[0]);
541                 _DEBUG_TRACE("fw_folder_id_list[0] : %s", fw_folder_id_list[0]);
542         }
543
544         int i = 0;
545         for (; i < fw_folder_id_cnt; i++) {
546                 _DEBUG_TRACE("fw_folder_id_list[%d] : %s", i, fw_folder_id_list[i]);
547
548                 char *service_folder_id = sync_agent_get_service_folder_id(fw_folder_id_list[i]);
549                 _DEBUG_TRACE("service_folder_id : %s", service_folder_id);
550
551                 /*
552                  * 1. Process Add Item
553                  */
554                 int added_item_cnt = 0;
555                 sync_agent_plugin_item_node_s *added_service_item_id_list = func_get_changed_item_for_folder_add(service_account_id, service_folder_id, fw_change_point, &added_item_cnt);
556                 sync_agent_plugin_item_node_s *cursor_ptr = added_service_item_id_list;
557
558                 _DEBUG_TRACE("added_item_cnt : %d", added_item_cnt);
559                 if (added_item_cnt > 0) {
560                         changed_flag = 1;
561                 }
562
563                 char **fw_item_id_list = sync_agent_generate_item_luid(1, added_item_cnt);
564                 int y = 0;
565                 if (fw_item_id_list != NULL) {
566                         for (; y < added_item_cnt; y++) {
567                                 sync_agent_da_item_s item;
568                                 item.item_id = fw_item_id_list[y];
569                                 item.data_store_id = service_type;
570                                 item.account_id = fw_account_id;
571                                 item.folder_id = fw_folder_id_list[i];
572                                 item.service_id = cursor_ptr->item_id;
573                                 item.parent_service_id = cursor_ptr->parent_item_id;
574                                 da_changelog_add_item_internal(&item);
575                                 sync_agent_plugin_item_node_s *temp = cursor_ptr;
576                                 cursor_ptr = cursor_ptr->next;
577                                 free(temp->item_id);
578                                 free(temp);
579                                 if (fw_item_id_list[y] != NULL) {
580                                         free(fw_item_id_list[y]);
581                                 }
582                         }
583
584                         free(fw_item_id_list);
585                 }
586
587                 /*
588                  * 2. Process Delete Item
589                  */
590                 int deleted_item_cnt = 0;
591                 sync_agent_plugin_item_node_s *deleted_service_item_id_list = func_get_changed_item_for_folder_del(service_account_id, service_folder_id, fw_change_point, &deleted_item_cnt);
592                 cursor_ptr = deleted_service_item_id_list;
593
594                 _DEBUG_TRACE("deleted_item_cnt : %d", deleted_item_cnt);
595                 if (deleted_item_cnt > 0) {
596                         changed_flag = 1;
597                 }
598
599                 y = 0;
600                 for (; y < deleted_item_cnt; y++) {
601                         char *fw_item_id = da_get_item_id_internal(fw_account_id, cursor_ptr->item_id, service_type);
602                         sync_agent_plugin_item_node_s *temp = cursor_ptr;
603                         cursor_ptr = cursor_ptr->next;
604                         free(temp->item_id);
605                         free(temp);
606
607                         da_changelog_delete_item_internal(fw_account_id, fw_item_id, service_type);
608
609                         free(fw_item_id);
610                 }
611
612                 /*
613                  * 3. Process Update Item
614                  */
615                 int updated_item_cnt = 0;
616                 sync_agent_plugin_item_node_s *updated_service_item_id_list = func_get_changed_item_for_folder_update(service_account_id, service_folder_id, fw_change_point, &updated_item_cnt);
617                 cursor_ptr = updated_service_item_id_list;
618
619                 _DEBUG_TRACE("updated_item_cnt : %d", updated_item_cnt);
620                 if (updated_item_cnt > 0) {
621                         changed_flag = 1;
622                 }
623
624                 fw_item_id_list = sync_agent_generate_item_luid(1, updated_item_cnt);
625                 if (fw_item_id_list != NULL) {
626                         y = 0;
627                         for (; y < updated_item_cnt; y++) {
628                                 char *fw_item_id = da_get_item_id_internal(fw_account_id, cursor_ptr->item_id, service_type);
629
630                                 if (fw_item_id == NULL) {
631                                         sync_agent_da_item_s item;
632                                         item.item_id = fw_item_id_list[y];
633                                         item.data_store_id = service_type;
634                                         item.account_id = fw_account_id;
635                                         item.folder_id = fw_folder_id_list[i];
636                                         item.service_id = cursor_ptr->item_id;
637                                         item.parent_service_id = cursor_ptr->parent_item_id;
638                                         da_changelog_add_item_internal(&item);
639                                 } else {
640                                         da_changelog_update_item_internal(fw_account_id, fw_item_id, service_type);
641                                 }
642
643                                 sync_agent_plugin_item_node_s *temp = cursor_ptr;
644                                 cursor_ptr = cursor_ptr->next;
645
646                                 free(temp->item_id);
647                                 free(temp);
648
649                                 if (fw_item_id_list[y] != NULL) {
650                                         free(fw_item_id_list[y]);
651                                 }
652
653                                 if (fw_item_id != NULL) {
654                                         free(fw_item_id);
655                                 }
656                         }
657
658                         free(fw_item_id_list);
659                 }
660
661                 if (fw_folder_id_list[i] != NULL)
662                         free(fw_folder_id_list[i]);
663
664                 if (service_folder_id != NULL)
665                         free(service_folder_id);
666         }
667
668         if (fw_folder_id_list_info != NULL) {
669                 if (fw_folder_id_list_info->id != NULL) {
670                         free(fw_folder_id_list_info->id);
671                 }
672                 free(fw_folder_id_list_info);
673         }
674
675         _INNER_FUNC_EXIT;
676
677         return SYNC_AGENT_DA_SUCCESS;
678 }