7a168edf2c1b692ebb81c1910d7b6760e4c61c1e
[platform/core/system/upgrade.git] / src / delta-ua / engine / SS_UPI.c
1 /*
2  * delta-ua
3  *
4  * Copyright (c) 2017 - 2022 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 /*HEADER */
20
21 /*
22
23 Function Prototypes Mandatory
24
25 */
26
27 #include<stdio.h>
28 #include<ctype.h>
29 #include<stdlib.h>
30 #include<string.h>
31 #include<inttypes.h>
32 #include <stdbool.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <sys/statfs.h>
36 #include <mntent.h>
37 #include "ua_types.h"
38 #include "SS_Common.h"
39 #include "fota_common.h"
40 #include "SS_UPI.h"
41 #include "SS_PatchDelta.h"
42 #include "SS_Engine_Errors.h"
43 #include "SS_FSUpdate.h"
44
45 int gtotalFSCnt = 0;
46 int FS_UpgradeState = E_SS_FAILURE;
47 int gvalid_session = 0;          //used as fail-safe in case device is turmed off or battery removed during update
48 fs_list *headptr_list;
49 tar_Data_t *tar_cfg_data = NULL;
50
51 #ifdef MEM_PROFILING
52 /*
53         Description:
54                 Create script file , set it executable, execute script in child process
55                 Only works if valid delta.tar is present for upgrade
56         Summary:
57                 If MEM_PROFILING is activated,
58                 we can see the result of memory profiling after delta upgrade
59                 in file defined by macro - SS_MEMORY_PROFILING_SCRIPT
60 */
61 int mem_profiling_start = 0;
62 int SS_Do_Memory_Profiling()
63 {
64         int ret = -1;
65         pid_t pid;
66         char memory_usage_script[1024] = "#!/bin/bash\nlog_file=$1\npid=$2\nmaxmem=0\nwhile [[ -d \"/proc/${pid}\" ]]; do\n\
67                                           mem=`cat /proc/${pid}/smaps | grep Pss | grep -v Swap|awk '{print $2}'|awk '{s+=$1} END {print s}'`\n\
68                                           if [[ ${mem} -gt ${maxmem} ]]; then\nmaxmem=${mem}\n\
69                                                   echo -e \"Memory usage till now is: ${maxmem} KB.\" >> $log_file\n    fi\n    sleep 0.01\ndone\n\
70                                                           echo -e \"Max was : ${maxmem} KB.\" >> $log_file\n";
71         char cmd[1024] = { 0, };
72
73         //Open a file and write the script contents in it
74         FILE *fp = fopen(SS_MEMORY_PROFILING_SCRIPT, "w+");
75         fwrite(memory_usage_script, strlen(memory_usage_script), 1, fp);
76         fclose(fp);
77         //make the file executable        -        Octal 495 is 757 decimal
78         if (chmod(SS_MEMORY_PROFILING_SCRIPT, 495) < 0) {
79                 LOGE("Error in chmod(%s, 495) - %d (%s)\n", SS_MEMORY_PROFILING_SCRIPT, errno, strerror(errno));
80                 return E_SS_FAILURE;
81         }
82         //calling mem_use.sh
83         //Usage : <location of script> <location of log file to be created> <pid of the calling process>
84         pid = getpid();
85         snprintf(cmd, sizeof(cmd) - 1, "%s %s %d", SS_MEMORY_PROFILING_SCRIPT, SS_MEMORY_USAGE_LOG, pid);
86         ret = _system_cmd_nowait(cmd);
87         sleep(1);
88         LOG("ret for memory profiling cmd is %d\n", ret);
89         if (ret == 0) {
90                 mem_profiling_start = 1;
91                 return S_SS_SUCCESS;
92         } else {
93                 LOGE("Could not start Memory Profiling\n");
94                 return E_SS_FAILURE;
95         }
96 }
97 #endif
98 #ifdef TIME_PROFILING
99 static char ts1[256];
100 static double ts2;
101 double t1;
102 double t2;
103 double fast_tar_get_item_size_time = 0.0;
104 double SS_LoadFile_time = 0.0;
105 double SS_FSBuildNodes_time = 0.0;
106
107 static void get_time_stamp1(void)
108 {
109         struct timeval tv;
110         int sec, msec;
111
112         gettimeofday(&tv, NULL);
113         sec = (int)tv.tv_sec;
114         msec = (int)(tv.tv_usec / 1000);
115         snprintf(ts1, 256, "%06d.%03d", sec % 100000, msec);
116 }
117 #endif
118
119 //Check SS function if available
120 int file_exist(char *filename)
121 {
122         struct stat buf;
123         int ret = 0;
124
125         ret = lstat(filename, &buf);
126         if (ret < 0)
127                 ret = stat(filename, &buf);
128
129         return (ret >= 0) ? (1) : (0);
130 }
131
132 #ifdef POWER_FAIL_TEST
133
134 static int SS_Get_power_fail_flag(void)
135 {
136         int fd;
137         char buf[256];
138         char *ptr = NULL;
139         int result = 0;
140         int ret_val = 0;
141
142         if (file_exist(SS_POWER_FAIL_TEST_FLAG) == 0) {
143                 LOGE("No exist file!!\n");
144                 return -1;
145         }
146
147         fd = open(SS_POWER_FAIL_TEST_FLAG, O_RDWR, S_IRWXU);
148         if (fd == -1) {
149                 LOGE("Could not open status file!!\n");
150                 return -1;
151         }
152
153         result = SS_ReadFile(fd, 0, buf, sizeof(buf));
154         if (result != 0) {
155                 LOGE("SS_ReadFile failed!!\n");
156                 return -1;
157         }
158
159         ret_val = atoi(buf);
160
161         result = SS_CloseFile(fd);
162         if (result != 0)
163                 LOGE("SS_CloseFile failed!!\n");
164
165         return ret_val;
166 }
167
168 static void SS_Set_power_fail_flag(int del_type)
169 {
170         int fd;
171         int result = 0;
172         char num_str[16];
173         LOG("del_type:[%d]\n", del_type);
174
175         fd = open(SS_POWER_FAIL_TEST_FLAG, O_RDWR | O_CREAT, S_IRWXU);
176         if (fd == -1) {
177                 LOGE("Could not open status file!!\n");
178                 return;
179         }
180
181         sprintf(num_str, "%d", del_type);
182         result = SS_WriteFile(fd, 0, num_str, strlen(num_str));
183         if (result != 0)
184                 LOGE("SS_WriteFile failed!!\n");
185
186         result = SS_CloseFile(fd);
187         if (result != 0)
188                 LOGE("SS_CloseFile failed!!\n");
189
190         sync();
191
192 }
193
194 int SS_Do_Power_fail_test(int del_type)
195 {
196         int ret = -1;
197         char cmd[1024] = { 0, };
198
199         SS_Set_power_fail_flag(del_type);
200
201         snprintf(cmd, sizeof(cmd) - 1, "%s", "/usr/sbin/reboot fota");
202         ret = _system_cmd_nowait(cmd);
203         sleep(1);
204         LOG("ret for SS_Do_Power_fail_test cmd is %d\n", ret);
205         if (ret == 0) {
206                 return S_SS_SUCCESS;
207         } else {
208                 LOGE("Could not start Memory Profiling\n");
209                 return E_SS_FAILURE;
210         }
211 }
212 #endif
213
214 int SS_GetProgressResolution(int ultotalFSCnt)
215 {
216         if (ultotalFSCnt < DISPLAYRESOLUTION_SIZE)
217                 return 1;
218         else
219                 return (ultotalFSCnt / DISPLAYRESOLUTION_SIZE);
220 }
221
222 void SS_SetUpgradeState(int Val)
223 {
224         LOGE("FAILED to upgrade Cause:[0x%x]\n", Val);
225         FS_UpgradeState = Val;
226         return;
227 }
228
229 int SS_GetUpgradeState()
230 {
231         return FS_UpgradeState;
232 }
233
234 int SS_Get_last_update_status(int* last_update_status, int* del_type, char* status_path)
235 {
236         int fd;
237         unsigned char buf[257];
238         char *ptr = NULL;
239         char *saveptr = NULL;
240         int result = 0;
241
242         if (file_exist(status_path) == 0) {
243                 LOG("No exist file!! - File_path:[%s]\n", status_path);
244                 return -1;
245         }
246
247         fd = open(status_path, O_RDWR, S_IRWXU);
248         if (fd == -1) {
249                 LOGE("Could not open status file!!, File_path:[%s]\n", status_path);
250                 return -1;
251         }
252
253         result = SS_ReadFile(fd, 0, buf, sizeof(buf)-1);
254         if (result != 0) {
255                 LOGE("SS_ReadFile failed!!\n");
256                 result = SS_CloseFile(fd);
257                 if (result != 0)
258                         LOGE("SS_CloseFile failed!!\n");
259                 return -1;
260         }
261
262         ptr = strtok_r((char *)buf, " ", &saveptr);
263
264         if (ptr != NULL) {
265                 *last_update_status = atoi(ptr);
266                 ptr = strtok_r(NULL, " ", &saveptr);
267         }
268
269         if (ptr != NULL) {
270                 *del_type = atoi(ptr);
271         }
272
273         result = SS_CloseFile(fd);
274         if (result != 0)
275                 LOGE("SS_CloseFile failed!!\n");
276
277         return 0;
278 }
279
280 void SS_Set_last_update_status(int last_update_status, int del_type, char *status_path)
281 {
282         int fd;
283         int result = 0;
284         char num_str[16];
285         LOG("last_update_status:[%d], del_type:[%d]\n", last_update_status, del_type);
286
287         fd = open(status_path, O_RDWR | O_CREAT, S_IRWXU);
288         if (fd == -1) {
289                 LOGE("Could not open status file!!, File_path:[%s]\n", status_path);
290                 return;
291         }
292
293         snprintf(num_str, sizeof(num_str), "%d %d", last_update_status, del_type);
294         result = SS_WriteFile(fd, 0, (unsigned char *)num_str, strlen(num_str));
295         if (result != 0)
296                 LOGE("SS_WriteFile failed!!\n");
297
298         result = SS_CloseFile(fd);
299         if (result != 0)
300                 LOGE("SS_CloseFile failed!!\n");
301
302         sync();
303
304 }
305
306 int SS_rename1(const char *old_file_name, const char *new_file_name)
307 {
308         int result = E_SS_FAILURE;
309         char *temp_name = NULL;
310         temp_name = (char *)SS_Malloc(strlen(new_file_name) + 10);
311         if (temp_name == NULL)
312                 return E_SS_FAILURE;
313         snprintf(temp_name, strlen(new_file_name) + 10, "%s.temp", new_file_name);
314         result = rename(new_file_name, temp_name);
315         if (result != 0)
316                 goto Cleanup;
317         result = rename(old_file_name, new_file_name);
318         if (result != 0)
319                 goto Cleanup;
320         result = unlink(temp_name);
321  Cleanup:
322         if (temp_name)
323                 SS_Free(temp_name);
324         return result;
325 }
326
327 /*!
328  *********************************************************************************
329  *                                       SS_FSVerifyNode
330  *********************************************************************************
331  *
332  * @brief
333  *      This is to verify nodes being added to global control structure for diff and delete cases.
334  *      gvalid_session global is used to check if nodes are already verified.
335  *
336  *
337  *      @param
338  *
339  *      @return                         returns S_SS_SUCCESS and
340  *                                              returns E_SS_FAILURE in case any error occurs
341  *
342  *********************************************************************************
343  */
344 int SS_FSVerifyNode(const char *path, const char *patchname, const char *sha1src, int type,
345                                         char *patchpath_name, int *data_size, int *data_offset)
346 {
347         char patch[MAX_FILE_PATH] = { 0 };
348         FileInfo source_file;
349         uint8_t source_sha1[SHA_DIGEST_SIZE];
350
351         if (gvalid_session) {
352                 if ((type == DIFFS || type == DELETES || type == MOVES) && !file_exist((char *)path)) {
353                         LOGE("failed to verifyNodes [does not exist], Path : [%s] Type[%d]\n", path, type);
354                         SS_SetUpgradeState(E_SS_FSBADNODES);
355                         return E_SS_FAILURE;
356                 }
357                 snprintf(patch, MAX_FILE_PATH, "%s%s%s", patchpath_name, "/", patchname);
358                 if ((type == DIFFS/*||type == NEWFILES */)) {   // allowing size 0 also for folders
359                         if (tar_get_item_size_from_struct(&tar_cfg_data, patchname, data_size, data_offset)
360                                         != S_SS_SUCCESS) {
361                                 LOGE("failed to get item size from struct, Patch : [%s]\n", patchname);
362                                 SS_SetUpgradeState(E_SS_FSBADNODES);
363                                 return E_SS_FAILURE;
364                         }
365                         if (*data_size < 0) {
366                                 LOGE("failed to verifyNodes [delta absent], Patch : [%s] Type[%d]\n", patch, type);
367                                 SS_SetUpgradeState(E_SS_FSBADNODES);
368                                 return E_SS_FAILURE;
369                         }
370                 }
371                 //For other types (NEWs, SYMs, Folders), SHA check not required
372                 if ((type == DIFFS || type == MOVES/* ||type == DELETES */) &&
373                                 (strcmp(sha1src, SS_NULLENTRY) != 0)) {
374                         if (ParseSha1(sha1src, source_sha1) != 0) {
375                                 LOGE("Failed to parse Src-sha1 \"%s\"\n", sha1src);
376                                 return E_SS_FAILURE;
377                         }
378                         if (SS_LoadFile(path, &source_file) == 0) {
379                                 if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) != 0) {
380                                         SS_Free(source_file.data);
381                                         unsigned char actualShaBuffer[41] = { 0, };
382                                         hex_digest(source_file.sha1, actualShaBuffer, SHA_DIGEST_SIZE);
383                                         LOGE("SS_FSVerifyNode - SHA mismatch with SRC  - PATH [%s] Expected [%s] Actual [%s]\n",
384                                                 path, sha1src, actualShaBuffer);
385                                         SS_SetUpgradeState(E_SS_FSSRCCURRUPTED);        // Define other error
386                                         return E_SS_FAILURE;
387                                 }
388                         }
389                         SS_Free(source_file.data);
390                 }
391         } else {
392                 LOGL(LOG_SSENGINE, "FS partition Already verified - Filling only size and offset \n");
393                 snprintf(patch, MAX_FILE_PATH, "%s%s%s", patchpath_name, "/", patchname);
394                 if ((type == DIFFS/* ||type == NEWFILES */)) {   // allowing size 0 also for folders
395                         if (tar_get_item_size_from_struct(&tar_cfg_data, patchname, data_size, data_offset)
396                                         != S_SS_SUCCESS) {
397                                 LOGE("failed to get item size from struct, Patch : [%s] \n", patchname);
398                                 SS_SetUpgradeState(E_SS_FSBADNODES);
399                                 return E_SS_FAILURE;
400                         }
401                         if (*data_size < 0) {
402                                 LOGE("failed to verifyNodes [delta absent], Patch : [%s] Type[%d]\n", patch, type);
403                                 SS_SetUpgradeState(E_SS_FSBADNODES);
404                                 return E_SS_FAILURE;
405                         }
406                 }
407         }
408         return S_SS_SUCCESS;
409 }
410
411 /*!
412  *********************************************************************************
413  *                                       SS_AppendNode
414  *********************************************************************************
415  *
416  * @brief
417  *      This is to append node to the global control structure for delta files.
418  *
419  *
420  *      @param
421  *
422  *      @return                         returns S_SS_SUCCESS and
423  *                                              returns E_SS_FAILURE in case any error occurs
424  *
425  *********************************************************************************
426  */
427 int SS_AppendNode(const char *ubDeltaPath, fs_params ** headparam, fs_params ** tailparam, const char *old_path,
428                                   const char *new_path, const char *patchname, const char *sha1src, const char *sha1trg, int type,
429                                   char *patchpath_name)
430 {
431         fs_params *newnode = NULL;
432         int data_size = 0;
433         int data_offset = 0;
434
435         if (!ubDeltaPath || !old_path || !new_path || !patchname || !sha1src || !sha1trg || !patchpath_name) {
436                 LOGE("Bad Nodes, NULL params passed for Appending Nodes \n");
437                 SS_SetUpgradeState(E_SS_FSINVALIDNODEPARAMS);
438                 return E_SS_FAILURE;
439         }
440         if ((E_SS_FAILURE ==
441                  SS_FSVerifyNode(old_path, patchname, sha1src, type, patchpath_name, &data_size, &data_offset))) {
442                 LOGE("Bad Nodes, Failed to pass verification - [Delta Path - %s][OldPath - %s] [NewPath - %s] \n", ubDeltaPath,
443                          old_path, new_path);
444                 return E_SS_FAILURE;
445         }
446         newnode = (fs_params *) SS_Malloc(sizeof(fs_params));
447         if (!newnode)
448                 return E_SS_FAILURE;
449         strncpy(newnode->file_old_path, old_path, SS_MAX_NAMELENSUPPORTED);//wgid: 29483
450         strncpy(newnode->file_new_path, new_path, SS_MAX_NAMELENSUPPORTED);//wgid: 29482
451         strncpy(newnode->patch_name, patchname, SS_MAX_NAMELENSUPPORTED);//wgid: 28033
452         strncpy(newnode->sha1src, sha1src, sizeof(newnode->sha1src) -1);//wgid: 25282
453         strncpy(newnode->sha1trg, sha1trg, sizeof(newnode->sha1trg) - 1);//wgid: 25283
454         newnode->type = type;
455         newnode->data_size = data_size;
456         newnode->data_offset = data_offset;
457         newnode->nextnode = NULL;
458
459         //LOG("%s %s %d %s %s \n",newnode->file_path,newnode->patch_name,newnode->type, newnode->sha1src, newnode->sha1trg);
460
461         if (*headparam == NULL) {
462                 *headparam = newnode;
463                 *tailparam = newnode;
464         } else {
465                 (*tailparam)->nextnode = newnode;
466                 (*tailparam) = (*tailparam)->nextnode;
467         }
468         return S_SS_SUCCESS;
469
470 }
471
472 void SS_UpdateUIProgress(ua_dataSS_t * ua_dataSS, int ulTotalFsCnt, int ulDone)
473 {
474         static int ss_count = 1;
475         int res_val = SS_GetProgressResolution(ulTotalFsCnt);
476         if (!ua_dataSS) {
477                 LOGE("Error ua_dataSS\n");
478                 return;
479         }
480 //LOG("\nvalues are ss_count[%d] total_file_cnt[%d]",ss_count,ulTotalFsCnt);
481         if (ulDone == 1) {
482                 if (ua_dataSS->ui_progress)
483                         ua_dataSS->ui_progress(ua_dataSS, 100);
484                 ss_count = 1;
485         } else if (ss_count < ulTotalFsCnt) {
486                 if (ss_count % res_val == 0) { //Max 50 times display
487                         double data = (double)ss_count / (double)ulTotalFsCnt;
488                         if (ua_dataSS->ui_progress)
489                                 ua_dataSS->ui_progress(ua_dataSS, data * 100);
490                 }
491                 ss_count++;
492         } else {
493                 if (ua_dataSS->ui_progress)
494                         ua_dataSS->ui_progress(ua_dataSS, 100);
495                 ss_count = 1;
496         }
497
498 }
499
500 /*!
501  *********************************************************************************
502  *                                       SS_FSClearNodes
503  *********************************************************************************
504  *
505  * @brief
506  *      This is to clear the global control structure for delta files.
507  *
508  *
509  *      @param
510  *
511  *      @return
512  *
513  *********************************************************************************
514  */
515 void SS_FSClearNodes()
516 {
517         fs_params *local_temp = NULL;
518         fs_params *local_next = NULL;
519         LOGL(LOG_SSENGINE, "Free Nodes\n");
520         if (headptr_list) {
521                 if (headptr_list->del_ref) {
522                         local_temp = headptr_list->del_ref;
523                         while (local_temp) {
524                                 local_next = local_temp->nextnode;
525                                 SS_Free(local_temp);
526                                 local_temp = local_next;
527                         }
528                 }
529                 if (headptr_list->dif_ref) {
530                         local_temp = headptr_list->dif_ref;
531                         while (local_temp) {
532                                 local_next = local_temp->nextnode;
533                                 SS_Free(local_temp);
534                                 local_temp = local_next;
535                         }
536                 }
537                 if (headptr_list->move_ref) {
538                         local_temp = headptr_list->move_ref;
539                         while (local_temp) {
540                                 local_next = local_temp->nextnode;
541                                 SS_Free(local_temp);
542                                 local_temp = local_next;
543                         }
544                 }
545                 if (headptr_list->new_ref) {
546                         local_temp = headptr_list->new_ref;
547                         while (local_temp) {
548                                 local_next = local_temp->nextnode;
549                                 SS_Free(local_temp);
550                                 local_temp = local_next;
551                         }
552                 }
553                 if (headptr_list->sym_difref) {
554                         local_temp = headptr_list->sym_difref;
555                         while (local_temp) {
556                                 local_next = local_temp->nextnode;
557                                 SS_Free(local_temp);
558                                 local_temp = local_next;
559                         }
560                 }
561                 if (headptr_list->sym_newref) {
562                         local_temp = headptr_list->sym_newref;
563                         while (local_temp) {
564                                 local_next = local_temp->nextnode;
565                                 SS_Free(local_temp);
566                                 local_temp = local_next;
567                         }
568                 }
569                 if (headptr_list->hard_newref) {
570                         local_temp = headptr_list->hard_newref;
571                         while (local_temp) {
572                                 local_next = local_temp->nextnode;
573                                 SS_Free(local_temp);
574                                 local_temp = local_next;
575                         }
576                 }
577                 if (headptr_list->hard_difref) {
578                         local_temp = headptr_list->hard_difref;
579                         while (local_temp) {
580                                 local_next = local_temp->nextnode;
581                                 SS_Free(local_temp);
582                                 local_temp = local_next;
583                         }
584                 }
585                 SS_Free(headptr_list);
586                 headptr_list = NULL;
587         }
588 }
589
590 /*!
591  *********************************************************************************
592  *                                       SS_FSGetDeltaCount
593  *********************************************************************************
594  *
595  * @brief
596  *      This is to get the delta count for diffs , deletes etc.
597  *
598  *
599  *      @param
600  *
601  *      @return                         returns structure with delta count info
602  *                                              NULL in case of error
603  *
604  *********************************************************************************
605  */
606
607 struct details *SS_FSGetDeltaCount(char *ubDeltaPath, char *ubDeltaInfoFile, char *patchlist_backup_patch)
608 {
609         int size = 0, bckupsize = 0, ret = 1;
610         char *token = NULL;
611         char *FileData = NULL;
612         int data_size = -1;
613         char *line = NULL;
614         char *saveptr = NULL;
615         char buf[256];
616         struct details *refer_copy = NULL;
617         FILE *filename_bkup = NULL;
618
619         if (!(ubDeltaPath && ubDeltaInfoFile)) {
620                 LOGE("failed to Parse DELTA count information: \n");
621                 SS_SetUpgradeState(E_SS_BAD_PARAMS);
622                 return NULL;
623         }
624         refer_copy = (struct details *)SS_Malloc(sizeof(struct details));
625
626         if (refer_copy == NULL) {
627                 LOGE("failed to allocate memory\n");
628                 return NULL;
629         }
630
631         LOGL(LOG_SSENGINE, "Delta File Info =%s\n", ubDeltaInfoFile);
632
633         size = tar_get_item_size(ubDeltaPath, ubDeltaInfoFile);
634         if (size < 0) {
635                 LOGE("failed to Access DELTA info file DPath:[%s] File: [%s]\n", ubDeltaPath, ubDeltaInfoFile);
636                 SS_SetUpgradeState(E_SS_FSBADDELTA);
637                 ret = 0;
638                 goto cleanup;
639         }
640
641         FileData = SS_Malloc(size + 1);
642         if (FileData == NULL) {
643                 LOGE("Failed to Allocate Memory\n");
644                 SS_SetUpgradeState(E_SS_MALLOC_ERROR);
645                 ret = 0;
646                 goto cleanup;
647         }
648
649         memset(FileData, 0, size + 1);
650         data_size = tar_get_cfg_data(ubDeltaPath, ubDeltaInfoFile, FileData, size);
651         if (data_size <= 0) {      // == 0 is NOT okay??
652                 LOGE("Failed to read cfg data from Delta\n");
653                 SS_SetUpgradeState(E_SS_FSBADDELTA);
654                 ret = 0;
655                 goto cleanup;
656         }
657         filename_bkup = fopen(patchlist_backup_patch, "wb+");
658         if (filename_bkup == NULL) {
659                 strerror_r(errno, buf, sizeof(buf));
660                 LOGE("Failed to create BACKUP file Error:[%s]\n", buf);
661                 SS_SetUpgradeState(E_SS_FSFAILEDTOBACKUPPATCHINFO);
662                 ret = 0;
663                 goto cleanup;
664         }
665
666         bckupsize = fwrite(FileData, 1, data_size, filename_bkup);  //RECHECK SIZE 1
667         if (bckupsize <= 0) {
668                 SS_SetUpgradeState(E_SS_FSFAILEDTOBACKUPPATCHINFO);
669                 ret = 0;
670                 goto cleanup;
671         }
672         LOGL(LOG_SSENGINE, " Size [%d] DataSize [%d] BakUpSize [%d]\n", size, data_size, bckupsize);
673
674         line = strstr(FileData, SS_FSCOUNT_MAGIC_KEY);
675         if (line) {
676                 LOGL(LOG_SSENGINE, "SS_FSGetDeltaCount() last line %s \n", line);
677
678                 token = strtok_r(&line[SS_FSCOUNT_MAGIG_KEYLEN], SS_TOKEN_SPACE, &saveptr);
679                 if (token)
680                         refer_copy->diffs = atoi(token);
681                 else {
682                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT);
683                         ret = 0;
684                         goto cleanup;
685                 }
686                 token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr);
687                 if (token)
688                         refer_copy->moves = atoi(token);
689                 else {
690                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT);
691                         ret = 0;
692                         goto cleanup;
693                 }
694
695                 token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr);
696                 if (token)
697                         refer_copy->news = atoi(token);
698                 else {
699                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT);
700                         ret = 0;
701                         goto cleanup;
702                 }
703
704                 token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr);
705                 if (token)
706                         refer_copy->deletes = atoi(token);
707                 else {
708                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT);
709                         ret = 0;
710                         goto cleanup;
711                 }
712
713                 token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr);
714                 if (token)
715                         refer_copy->symdiffs = atoi(token);
716                 else {
717                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT);
718                         ret = 0;
719                         goto cleanup;
720                 }
721
722                 token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr);
723                 if (token)
724                         refer_copy->symnews = atoi(token);
725                 else {
726                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT);
727                         ret = 0;
728                         goto cleanup;
729                 }
730
731                 token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr);
732                 if (token)
733                         refer_copy->harddiffs = atoi(token);
734                 else {
735                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT);
736                         ret = 0;
737                         goto cleanup;
738                 }
739
740                 token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr);
741                 if (token)
742                         refer_copy->hardnews = atoi(token);
743                 else {
744                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT);
745                         ret = 0;
746                         goto cleanup;
747                 }
748
749                 gtotalFSCnt =
750                         refer_copy->diffs + refer_copy->moves + refer_copy->news + refer_copy->deletes + refer_copy->symdiffs +
751                         refer_copy->symnews + refer_copy->harddiffs + refer_copy->hardnews;
752                 LOG("SS_FSGetDeltaCount() total no of file %d\n", gtotalFSCnt);
753
754         } else {
755                 SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT);
756                 LOG("SS_FSGetDeltaCount() Failed to read last line\n");
757         }
758         if (gtotalFSCnt < 0) {
759                 ret = 0;
760                 goto cleanup;
761         }
762
763  cleanup:
764         if (ret) {
765                 SS_Free(FileData);
766                 if (filename_bkup)
767                         fclose(filename_bkup);
768                 return refer_copy;
769         } else {
770                 SS_Free(FileData);
771                 SS_Free(refer_copy);
772                 if (filename_bkup)
773                         fclose(filename_bkup);
774                 return NULL;
775         }
776
777 }
778
779 /*!
780  *********************************************************************************
781  *                                       SS_FSBuildNodes
782  *********************************************************************************
783  *
784  * @brief
785  *      This is used to build the gobal control structure for diffs, deletes etc.
786  *      For all the entries in config file (which has info) the information is parsed to the global control struct
787  *
788  *
789  *      @param
790  *
791  *      @return                         returns fs_list structure filled with details of all the files to be diff-ed ,deleted etc.
792  *                                              NULL in case of error
793  *
794  *********************************************************************************
795  */
796 fs_list *SS_FSBuildNodes(ua_dataSS_t * ua_dataSS)
797 {
798         FILE *fp = NULL;
799         char line[SS_TOKEN_MAXLINE_LEN] = { '\0' };
800         char patchlist_backup_patch[MAX_FILE_PATH];
801         snprintf(patchlist_backup_patch, MAX_FILE_PATH, "%s/%s",ua_dataSS->update_data->ua_delta_folder, SS_PATCHLIST_BKUPLOC);
802         char string_na[] = "NA";
803         char *patch_name = NULL;
804         char *source_name = NULL;
805         char *target_name = NULL;
806         char *sha1src = NULL;
807         char *sha1trg = NULL;
808         char *change_type = NULL;
809         char *file_type = NULL;
810         char *saveptr = NULL;
811         uint32_t ulPatchCount = 0, del_type = DELETES;
812         fs_params *fs_diffhead = NULL;
813         fs_params *fs_difftail = NULL;
814         fs_params *fs_movehead = NULL;
815         fs_params *fs_movetail = NULL;
816         fs_params *fs_newhead = NULL;
817         fs_params *fs_delhead = NULL;
818         fs_params *fs_deltail = NULL;
819         fs_params *fs_symlinkdiffhead = NULL;
820         fs_params *fs_symlinkdifftail = NULL;
821         fs_params *fs_symlinknewhead = NULL;
822         fs_params *fs_symlinknewtail = NULL;
823         fs_params *fs_hardlinkdiffhead = NULL;
824         fs_params *fs_hardlinkdifftail = NULL;
825         fs_params *fs_hardlinknewhead = NULL;
826         fs_params *fs_hardlinkdnewtail = NULL;
827
828         struct details *local = NULL;
829         fs_list *fs_head_node = NULL;
830         int i = 0, retval = 0;
831         if (!ua_dataSS) {
832                 LOGE("Bad structure ua_dataSS\n");
833                 SS_SetUpgradeState(E_SS_BAD_PARAMS);
834                 return NULL;
835         }
836         LOGL(LOG_SSENGINE, " Build Nodes Entry \n");
837         if (tar_cfg_data == NULL)
838                 tar_cfg_data = tar_build_cfg_table(ua_dataSS->update_data->ua_delta_path);
839         if (!tar_cfg_data) {
840                 LOGE(" tar_build_cfg_table  Failed \n");
841                 SS_SetUpgradeState(E_SS_BAD_PARAMS);
842                 return NULL;
843         }
844         local = SS_FSGetDeltaCount(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_patch_info, patchlist_backup_patch);
845         if (local == NULL) {
846                 LOGE(" Build Nodes Failed \n");
847                 if (tar_cfg_data)
848                         tar_free_cfg_table(&tar_cfg_data);
849                 SS_SetUpgradeState(E_SS_BAD_PARAMS);
850                 return NULL;
851         }
852
853         fp = fopen(patchlist_backup_patch, "r");
854         if (!fp) {
855                 SS_SetUpgradeState(E_SS_FSFAILEDTOOPENPATCHINFO);
856                 if (tar_cfg_data)
857                         tar_free_cfg_table(&tar_cfg_data);
858                 SS_Free(local);
859                 return NULL;
860         }
861
862         ulPatchCount = local->diffs + local->deletes + local->news + local->moves + local->symdiffs +
863                 local->symnews + local->harddiffs + local->hardnews;
864         LOG("Total FS count [%d].\n", ulPatchCount);
865 /*
866 ************************************************************************
867 Parsing logic implemented for patchlist
868 ************************************************************************
869 Sample entries in patchlist as below :
870 <CHANGE-TYPE>:<FILE-TYPE>:<OTHER-DETAILS...>
871 ************************************************************************
872 DIFF:REG:system/bin/vi:system/bin/vi:2f2f3dc6d3ee06af0080ac7975f22941660f2480:78b2d44af32d854c70f1cb7431a60c2682a320cc:diff1_vi.delta
873 DIFF:TPK:system/usr/packages/removable/com.samsung.calculator.tpk:system/usr/packages/removable/com.samsung.calculator.tpk:
874                                  96fc1bcde30d501ba65ef0038e05da46d255a7b3:fa1d5d9daa4097ac302b69244297f508577c3a01:diff1598_com.samsung.calculator.tpk.delta/
875 MOVE:REG:system/etc/smack/accesses.d/heremaps-engine-devel:system/usr/apps/com.samsung.contacts/res/temp:da39a3ee5e6b4b0d3255bfef95601890afd80709
876 DEL:REG:system/usr/ug/res/images/ug-phone/contacts/favorites_icon_remove.PNG:38ad8be378506d19b1c769d46be262cf100f6c59
877 DEL:SYM:system/usr/apps/com.samsung.message-lite/lib/libmsg-common.so
878 DEL:HARD:system/usr/apps/com.samsung.message-lite/lib/libmsg-common.so
879 SYM:DIFF:system/usr/lib/sync-agent/kies-private/libplugin-na-mobex.so.0:system/usr/lib/sync-agent/kies-private/libplugin-na-mobex.so.0:
880                                         libplugin-na-mobex.so.0.3.57
881 SYM:NEW:system/lib/firmware/vbc_eq:/opt/system/vbc_eq
882 HARD:DIFF:system/usr/lib/sync-agent/kies-private/libplugin-na-mobex.so.0:system/usr/lib/sync-agent/kies-private/libplugin-na-mobex.so.0:
883                                         libplugin-na-mobex.so.0.3.57
884 HARD:NEW:system/lib/firmware/vbc_eq:/opt/system/vbc_eq
885 ***********************************************************************
886 */
887         if (local && ((local->diffs) > 0 || (local->moves > 0))) {
888                 LOGL(LOG_SSENGINE, "%ss [%d] %ss [%d]\n", SS_STRING_DIFF, local->diffs, SS_STRING_MOVE, local->moves);
889                 for (i = 0; i < (local->diffs + local->moves); i++) {
890                         if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) {
891                                 SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
892                                 break;
893                         }
894                         //LOGL(LOG_SSENGINE, "DIFF LINE:[%d] [%s] \n",i+1,line);
895
896                         change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr);
897                         file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
898                         if (!file_type) {
899                                 LOGE("Unexpected null in strtok_r");
900                                 goto CleanUp;
901                         }
902
903                         if (change_type && strcmp(change_type, SS_STRING_MOVE) == 0) {   // && strcmp(file_type,"TPK") == 0){
904                                 source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
905                                 target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
906                                 sha1src = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr);
907                                 //LOGL(LOG_SSENGINE, "%s Index [%d]\n", SS_STRING_MOVE, i);
908
909                                 if (!source_name || !target_name || !sha1src) {
910                                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
911                                         //LOGE("Failed to extract Patch Info Type:DELETES \n");
912                                         LOGE("Failed to parse DIFFS - LINE:[%d] [%s] \n", i + 1, line);
913                                         goto CleanUp;
914                                 }
915                                 retval =
916                                         SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_movehead, &fs_movetail, source_name,
917                                                                   target_name, string_na, sha1src, string_na, MOVES,
918                                                                   ua_dataSS->update_delta->ua_patch_path);
919                                 if (retval == E_SS_FAILURE)      // ONLY test purpose, should enable this
920                                         goto CleanUp;
921
922                                 if (ua_dataSS->ua_operation == UI_OP_SCOUT) {
923                                         SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
924                                 }
925                         } else if (change_type && strcmp(change_type, SS_STRING_DIFF) == 0) {     // && strcmp(file_type,"TPK") == 0){
926                                 source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
927                                 target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
928                                 sha1src = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
929                                 sha1trg = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
930                                 patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr);
931                                 //LOGL(LOG_SSENGINE, "%s Index [%d]\n", SS_STRING_DIFF, i);
932
933                                 if (patch_name && (strlen(patch_name) <= SS_MAX_NAMELENSUPPORTED)) {
934                                         retval =
935                                                 SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_diffhead, &fs_difftail,
936                                                                           source_name, target_name, patch_name, sha1src, sha1trg, DIFFS,
937                                                                           ua_dataSS->update_delta->ua_patch_path);
938                                         if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this
939                                                 goto CleanUp;
940                                 } else {
941                                         SS_SetUpgradeState(E_SS_FILENAMELENERROR);
942                                         LOGE("File Name length Limitation Error File:[%s]\n", patch_name);
943                                         goto CleanUp;
944                                 }
945
946                                 if (ua_dataSS->ua_operation == UI_OP_SCOUT) {
947                                         SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
948                                 }
949                         } else {
950                                 SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
951                                 LOGE("Patch Name format Error File\n");
952                                 goto CleanUp;
953                         }
954                 }
955         }
956         if (local && (local->news) > 0) {   //check if new files archive is present in the delta
957                 char new_patch_path[MAX_FILE_PATH] = { 0, };
958                 snprintf(new_patch_path, MAX_FILE_PATH, "%s%s", ua_dataSS->parti_info->ua_subject_name, SS_COMPRESSED_FILE);
959                 if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, new_patch_path) <= 0) {
960                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
961                         LOGE("New files not present in Patch\n");
962                         goto CleanUp;
963                 }
964         }
965         if (local && (local->deletes) > 0) {            //this is to group to delete list
966                 LOGL(LOG_SSENGINE, "%ss [%d]\n", SS_STRING_DEL, local->deletes);
967                 for (i = 0; i < (local->deletes); i++) {
968                         if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) {
969                                 SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
970                                 break;
971                         }
972
973                         change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr);
974                         if (!change_type) {
975                                 LOGE("Unexpected null in strtok_r");
976                                 goto CleanUp;
977                         }
978                         file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
979
980                         if (file_type && strcmp(file_type, SS_STRING_REG) == 0) {
981                                 source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
982                                 sha1src = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr);
983                                 del_type = DELETES;
984                         } else if (file_type && ((strcmp(file_type, SS_STRING_SYM) == 0) || (strcmp(file_type, SS_STRING_HARD) == 0))) {
985                                 source_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr);
986                                 sha1src = string_na;
987                                 del_type = DELETES;
988                         } else if (file_type && strcmp(file_type, SS_STRING_END) == 0) {
989                                 source_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr);
990                                 sha1src = string_na;
991                                 del_type = DELETE_END;
992                         } else {
993                                 LOGE("Failed to parse DELETES - LINE:[%d] [%s] \n", i + 1, line);
994                                 goto CleanUp;
995                         }
996
997                         if (!source_name || !sha1src) {
998                                 SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
999                                 //LOGE("Failed to extract Patch Info Type:DELETES \n");
1000                                 LOGE("Failed to parse DELETES - LINE:[%d] [%s] \n", i + 1, line);
1001                                 goto CleanUp;
1002                         }
1003                         //LOGL(LOG_SSENGINE, "%s Index [%d]\n", SS_STRING_DEL, i);
1004                         retval =
1005                                 SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_delhead, &fs_deltail, source_name,
1006                                                           string_na, string_na, sha1src, string_na, del_type,
1007                                                           ua_dataSS->update_delta->ua_patch_path);
1008                         if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this
1009                                 goto CleanUp;
1010             if (ua_dataSS->ua_operation == UI_OP_SCOUT) {
1011                         SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1012             }
1013
1014                 }
1015         }                                                  //For symlink files
1016
1017         if (local && (local->symdiffs) > 0) {
1018                 LOGL(LOG_SSENGINE, "%s %ss [%d]\n", SS_STRING_SYM, SS_STRING_DIFF, local->symdiffs);
1019                 for (i = 0; i < (local->symdiffs); i++) {          //get the count from below function
1020                         if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) {
1021                                 SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
1022                                 break;
1023                         }
1024                         //LOGL(LOG_SSENGINE, "SYMDIFF LINE:[%d] [%s] \n",i+1,line);
1025
1026                         change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr);
1027                         file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1028
1029                         if ((change_type && file_type) &&
1030                                         strcmp(change_type, SS_STRING_SYM) == 0 && strcmp(file_type, SS_STRING_DIFF) == 0) {    // && strcmp(file_type,"TPK") == 0){
1031                                 source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1032                                 target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1033                                 patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr);
1034                                 //LOGL(LOG_SSENGINE, "%s %s Index [%d]\n", SS_STRING_SYM, SS_STRING_DIFF, i);
1035
1036                                 if (!source_name || !target_name || !patch_name) {
1037                                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
1038                                         //LOGE("Failed to extract Patch Info Type:DELETES \n");
1039                                         LOGE("Failed to parse SymDiffs - LINE:[%d] [%s] \n", i + 1, line);
1040                                         goto CleanUp;
1041                                 }
1042                                 retval =
1043                                         SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_symlinkdiffhead, &fs_symlinkdifftail,
1044                                                                   source_name, target_name, patch_name, string_na, string_na, SYMDIFFS,
1045                                                                   ua_dataSS->update_delta->ua_patch_path);
1046                                 if (retval == E_SS_FAILURE)      // ONLY test purpose, should enable this
1047                                         goto CleanUp;
1048                 if (ua_dataSS->ua_operation == UI_OP_SCOUT) {
1049                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1050                 }
1051                         }
1052                 }
1053         }
1054         if (local && (local->symnews) > 0) {
1055                 LOGL(LOG_SSENGINE, "%s %ss [%d]n", SS_STRING_SYM, SS_STRING_NEW, local->symnews);
1056                 for (i = 0; i < (local->symnews); i++) {
1057                         if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) {
1058                                 SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
1059                                 break;
1060                         }
1061                         //LOGL(LOG_SSENGINE, "SYMNEWS LINE:[%d] [%s] \n",i+1,line);
1062
1063                         change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr);
1064                         file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1065
1066                         if ((change_type && file_type) &&
1067                                         (strcmp(change_type, SS_STRING_SYM) == 0 && strcmp(file_type, SS_STRING_NEW) == 0)) {
1068                                 source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1069                                 patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr);
1070                                 //LOGL(LOG_SSENGINE, "%s %s Index [%d]\n", SS_STRING_SYM, SS_STRING_NEW, i);
1071
1072                                 if (!source_name || !patch_name) {
1073                                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
1074                                         //LOGE("Failed to extract Patch Info Type:DELETES \n");
1075                                         LOGE("Failed to parse SymNews - LINE:[%d] [%s] \n", i + 1, line);
1076                                         goto CleanUp;
1077                                 }
1078                                 retval =
1079                                         SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_symlinknewhead, &fs_symlinknewtail,
1080                                                                   source_name, string_na, patch_name, string_na, string_na, SYMNEWFILES,
1081                                                                   ua_dataSS->update_delta->ua_patch_path);
1082                                 if (retval == E_SS_FAILURE)      // ONLY test purpose, should enable this
1083                                         goto CleanUp;
1084                 if (ua_dataSS->ua_operation == UI_OP_SCOUT) {
1085                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1086                 }
1087                         }
1088                 }
1089         }                       // For hardlinks
1090         if (local && (local->hardnews) > 0) {
1091                 LOGL(LOG_SSENGINE, "%s %ss [%d]\n", SS_STRING_HARD, SS_STRING_NEW, local->hardnews);
1092                 for (i = 0; i < (local->hardnews); i++) {
1093                         if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) {
1094                                 SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
1095                                 break;
1096                         }
1097                         //LOGL(LOG_SSENGINE, "HARDNEWS LINE:[%d] [%s] \n",i+1,line);
1098
1099                         change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr);
1100                         file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1101
1102                         if ((change_type && file_type) &&
1103                                         (strcmp(change_type, SS_STRING_HARD) == 0 && strcmp(file_type, SS_STRING_NEW) == 0)) {
1104                                 source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1105                                 patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr);
1106                                 //LOGL(LOG_SSENGINE, "%s %s Index [%d]\n", SS_STRING_SYM, SS_STRING_NEW, i);
1107
1108                                 if (!source_name || !patch_name) {
1109                                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
1110                                         //LOGE("Failed to extract Patch Info Type:DELETES \n");
1111                                         LOGE("Failed to parse HardNews - LINE:[%d] [%s] \n", i + 1, line);
1112                                         goto CleanUp;
1113                                 }
1114                                 retval =
1115                                         SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_hardlinknewhead, &fs_hardlinkdnewtail,
1116                                                                   source_name, string_na, patch_name, string_na, string_na, HARDNEWFILES,
1117                                                                   ua_dataSS->update_delta->ua_patch_path);
1118                                 if (retval == E_SS_FAILURE)      // ONLY test purpose, should enable this
1119                                         goto CleanUp;
1120                 if (ua_dataSS->ua_operation == UI_OP_SCOUT) {
1121                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1122                 }
1123                         }
1124                 }
1125         }
1126
1127                 if (local && (local->harddiffs) > 0) {
1128                 LOGL(LOG_SSENGINE, "%s %ss [%d]\n", SS_STRING_HARD, SS_STRING_DIFF, local->harddiffs);
1129                 for (i = 0; i < (local->harddiffs); i++) {         //get the count from below function
1130                         if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) {
1131                                 SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
1132                                 break;
1133                         }
1134                         //LOGL(LOG_SSENGINE, "HARDIFFS LINE:[%d] [%s] \n",i+1,line);
1135
1136                         change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr);
1137                         file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1138
1139                         if ((change_type && file_type) &&
1140                                         strcmp(change_type, SS_STRING_HARD) == 0 && strcmp(file_type, SS_STRING_DIFF) == 0) {   // && strcmp(file_type,"TPK") == 0){
1141                                 source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1142                                 target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr);
1143                                 patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr);
1144                                 //LOGL(LOG_SSENGINE, "%s %s Index [%d]\n", SS_STRING_SYM, SS_STRING_DIFF, i);
1145
1146                                 if (!source_name || !target_name || !patch_name) {
1147                                         SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO);
1148                                         //LOGE("Failed to extract Patch Info Type:DELETES \n");
1149                                         LOGE("Failed to parse HardDiffs - LINE:[%d] [%s] \n", i + 1, line);
1150                                         goto CleanUp;
1151                                 }
1152                                 retval =
1153                                         SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_hardlinkdiffhead, &fs_hardlinkdifftail,
1154                                                                   source_name, target_name, patch_name, string_na, string_na, HARDDIFFS,
1155                                                                   ua_dataSS->update_delta->ua_patch_path);
1156                                 if (retval == E_SS_FAILURE)      // ONLY test purpose, should enable this
1157                                         goto CleanUp;
1158                 if (ua_dataSS->ua_operation == UI_OP_SCOUT) {
1159                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1160                 }
1161                         }
1162                 }
1163         }
1164
1165         fs_head_node = (fs_list *) SS_Malloc(sizeof(fs_list));
1166         if (!fs_head_node) {
1167                 SS_SetUpgradeState(E_SS_MALLOC_ERROR);
1168                 goto CleanUp;
1169         }
1170         fs_head_node->dif_ref = fs_diffhead;
1171         fs_head_node->move_ref = fs_movehead;
1172         fs_head_node->new_ref = fs_newhead;
1173         fs_head_node->del_ref = fs_delhead;
1174         fs_head_node->sym_difref = fs_symlinkdiffhead;
1175         fs_head_node->sym_newref = fs_symlinknewhead;
1176         fs_head_node->hard_difref = fs_hardlinkdiffhead;
1177         fs_head_node->hard_newref = fs_hardlinknewhead;
1178         fs_head_node->ulPatchCount = ulPatchCount;
1179
1180         if (ua_dataSS->ua_operation == UI_OP_SCOUT) {
1181         SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 1);
1182         }
1183
1184  CleanUp:
1185         fclose(fp);
1186         SS_Free(local);
1187         unlink(patchlist_backup_patch);
1188         if (retval == E_SS_FAILURE)
1189                 if (tar_cfg_data)
1190                         tar_free_cfg_table(&tar_cfg_data);
1191         return fs_head_node;
1192 }
1193
1194 void SS_GetPartition_LocDetails(ua_dataSS_t * ua_dataSS)
1195 {
1196         LOGL(LOG_SSENGINE, "PART NAME: [%s] \n", ua_dataSS->parti_info->ua_parti_name);
1197         snprintf(ua_dataSS->update_delta->ua_patch_path, MAX_FILE_PATH, "%s", ua_dataSS->parti_info->ua_subject_name);
1198         snprintf(ua_dataSS->update_delta->ua_patch_info, MAX_FILE_PATH, "%s%s%s", ua_dataSS->parti_info->ua_subject_name,
1199                          ua_dataSS->parti_info->ua_parti_name, SS_PATCHLISTFORMAT);
1200         snprintf(ua_dataSS->update_delta->ua_attrib_path, MAX_FILE_PATH, "%s%s%s", ua_dataSS->parti_info->ua_subject_name,
1201                          ua_dataSS->parti_info->ua_parti_name, SS_PATCH_ATTR_FORMAT);
1202         LOGL(LOG_SSENGINE, "PatchPath[%s] PatchInfo [%s] Attributes [%s]\n", ua_dataSS->update_delta->ua_patch_path,
1203                  ua_dataSS->update_delta->ua_patch_info, ua_dataSS->update_delta->ua_attrib_path);
1204
1205         return;
1206 }
1207
1208 //Support functions//Change Struct format details (Can include total file count also???)/*!
1209 /*
1210 ******************************************************************************** *
1211 SS_FSSetAttributes
1212         *********************************************************************************
1213         * *@brief
1214         * This is used to set the file attributes at the end of application of patches in FS
1215         *
1216         * *@param
1217         *
1218         *@return returns S_SS_SUCCESS
1219         * E_SS_FAILURE in case of error
1220         *
1221         *********************************************************************************
1222  */
1223 int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS)
1224 {
1225         char *pline = NULL;
1226         char *psaveptr = NULL;
1227         char *pfilePath = NULL;
1228         char *pfiletype = NULL;
1229         char *attributSize = NULL;
1230         char *pattribs = NULL;
1231         int ulAttribSize = 0;
1232         int result = S_SS_SUCCESS;
1233         int fail_cnt = 0;
1234
1235         if (!(ua_dataSS && ua_dataSS->update_delta && ua_dataSS->update_data->ua_delta_path)) {
1236                 LOGE("Bad params for SS_FSSetAttributes\n");
1237                 SS_SetUpgradeState(E_SS_BAD_PARAMS);
1238                 return E_SS_FAILURE;
1239         }
1240         LOGL(LOG_SSENGINE, "ATTRIB PATH: [%s] \n", ua_dataSS->update_delta->ua_attrib_path);
1241
1242         int item_size = tar_get_item_size(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_attrib_path);
1243
1244         if (item_size <= 0) {
1245                 LOGL(LOG_SSENGINE, "No Attributes to SET\n");
1246                 return S_SS_SUCCESS;    // Delta with ONLY deletes
1247         }
1248         char *item_data = SS_Malloc(item_size + 1);
1249         if (item_data == NULL) {
1250                 LOGE("SS_Malloc failed!! - item_data\n");
1251                 SS_SetUpgradeState(E_SS_MALLOC_ERROR);
1252                 return E_SS_FAILURE;
1253         }
1254         int read_data =
1255                 tar_get_cfg_data(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_attrib_path, item_data,
1256                                                  item_size);
1257         if (read_data <= 0) {
1258                 LOGE("read_data failed!!\n");
1259                 SS_SetUpgradeState(E_SS_FSBADDELTA);
1260                 if (item_data != NULL) {
1261                         SS_Free(item_data);
1262                         item_data = NULL;
1263                 }
1264                 return E_SS_FAILURE;
1265         }
1266         item_data[read_data] = '\0';
1267
1268         pline = strtok_r(item_data, "\n", &psaveptr);
1269         if (pline == NULL) {
1270                 LOGL(LOG_SSENGINE, "No Attributes to SET as no lines in file\n");
1271                 if (item_data != NULL) {
1272                         SS_Free(item_data);
1273                         item_data = NULL;
1274                 }
1275                 return E_SS_FAILURE;
1276         }
1277
1278         while (pline) {
1279                 char *saveptr_pline = NULL;
1280                 pfilePath = strtok_r(pline, "\"", &saveptr_pline);
1281
1282                 if (pfilePath && strcmp(pfilePath, SS_FWSLASH) == 0) {
1283                         LOGE("skip root: it is RO\n");
1284                         pline = strtok_r(NULL, SS_TOKEN_NEWLINE, &psaveptr);
1285                         continue;
1286                 }
1287
1288                 pfiletype = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr_pline);
1289                 attributSize = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr_pline);
1290                 pattribs = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr_pline);
1291                 //LOG("\nSS_FSSetAttributes [%s][%s][%s]", pfiletype, attributSize, pattribs);
1292                 if (pattribs && pfilePath && pfiletype && attributSize) {
1293                         ulAttribSize = strlen(pattribs);
1294                         //LOG("\nSS_SetFileAttributes [%s][%s][%d][%s]",pfilePath,pfiletype,ulAttribSize, pattribs );
1295                         //LOG("SS_SetFileAttributes [%s]\n", pfilePath);
1296
1297                         result = SS_SetFileAttributes(pfilePath, ulAttribSize, (const unsigned char *)pattribs);
1298                         if (result != S_SS_SUCCESS) {
1299                                 LOGE("Failed to set Attributes %s\n", pfilePath);
1300                                 SS_SetUpgradeState(E_SS_FSBADATTRIBUTES);
1301                                 if (item_data) {
1302                                         SS_Free(item_data);
1303                                         item_data = NULL;
1304                                 }
1305                                 fail_cnt++;
1306                         }
1307                 } else {
1308                         LOGE("Failed to Parse Attributes - LINE %s\n", pline);
1309                         SS_SetUpgradeState(E_SS_FSBADATTRIBUTES);
1310                         if (item_data) {
1311                                 SS_Free(item_data);
1312                                 item_data = NULL;
1313                         }
1314                         fail_cnt++;
1315                 }
1316                 pline = strtok_r(NULL, SS_TOKEN_NEWLINE, &psaveptr);
1317         }
1318
1319         if (item_data != NULL) {
1320                 SS_Free(item_data);
1321                 item_data = NULL;
1322         }
1323
1324         if (fail_cnt > 0)
1325                 result = E_SS_FAILURE;
1326         else
1327                 result = S_SS_SUCCESS;
1328
1329         return result;
1330 }
1331
1332 /*!
1333  *********************************************************************************
1334  *                                       SS_FSUpdateFile
1335  *********************************************************************************
1336  *
1337  * @brief
1338  *      This is used to update individual files on case basis
1339  *
1340  *
1341  *      @param
1342  *
1343  *      @return                         returns S_SS_SUCCESS
1344  *                                              E_SS_FAILURE in case of error
1345  *
1346  *********************************************************************************
1347  */
1348 int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, fs_params * pFsNode,
1349                                         const char *patch_path)
1350 {
1351         int ulFileIndex = 1;
1352         char ubPatch[SS_MAX_FILE_PATH] = { 0, };
1353         int ulReadCnt = 0;
1354         int ulResult = S_SS_SUCCESS;
1355         FileInfo source_file;
1356         FileInfo target_file;
1357         uint8_t target_sha1[SHA_DIGEST_SIZE] = { 0, };
1358         char new_compressed_path[MAX_FILE_PATH], patchfile_source_path[MAX_FILE_PATH];
1359         snprintf(new_compressed_path, MAX_FILE_PATH, "%s/%s", ua_dataSS->update_data->ua_delta_folder, SS_NEW_COMPRESSED_FILE);
1360         snprintf(patchfile_source_path, MAX_FILE_PATH, "%s/%s", ua_dataSS->update_data->ua_delta_folder, SS_PATCHFILE_SOURCE);
1361
1362         if (!patch_path) {
1363                 LOGE("Bad patch_path name\n");
1364                 return E_SS_FAILURE;
1365         }
1366         switch (ubFileType) {
1367         case DIFFS:
1368                 {
1369                         LOGL(LOG_SSENGINE, "DIFFS mode start\n");
1370 #ifdef TIME_PROFILING
1371                         get_time_stamp1();      //total time capturing
1372                         t1 = atof(ts1);
1373                         LOGL(LOG_SSENGINE, "DIFFS mode start time = [%lf]\n",
1374                              t1);
1375 #endif
1376                         tar_open(ua_dataSS->update_data->ua_delta_path);
1377                         while (pFsNode) {
1378                                 //LOGL(LOG_SSENGINE, "DIFFS update Index: [%d] \n", ulFileIndex++);
1379                                 snprintf(ubPatch, SS_MAX_FILE_PATH, "%s%s", patch_path, pFsNode->patch_name);
1380                                 //LOGL(LOG_SSENGINE, "DIFF list --- [File Name %s]\n [Patch Name %s]",pFsNode->file_path, ubPatch);
1381                                 if (pFsNode->data_size > 0) {
1382                                         ulReadCnt =
1383                                                 fast_tar_extract_file(ua_dataSS->update_data->ua_delta_path, ubPatch, patchfile_source_path,
1384                                                                                           pFsNode->data_size, pFsNode->data_offset);
1385                                         if (ulReadCnt < 0) {
1386                                                 ulResult = E_SS_FAILURE;
1387                                                 tar_close();
1388                                                 SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1389                                                 LOGE("Delta Read Failed\n");
1390                                                 break;
1391                                         }
1392                                         //LOGL(LOG_SSENGINE,"Updating [Item - %s]and size is[%d] Read Count[%d]\n",ubPatch, pFsNode->data_size, ulReadCnt);
1393
1394                                         if (ulReadCnt > 0)
1395                                                 ulResult =
1396                                                         SS_UpdateDeltaFS(pFsNode->file_old_path, pFsNode->file_new_path, pFsNode->sha1src,
1397                                                                                          pFsNode->sha1trg, pFsNode->data_size, ua_dataSS->update_data->ua_delta_folder);
1398                                         if (ulResult != S_SS_SUCCESS) {
1399                                                 LOGE("FS update Failed Result : [%d], [Item - %s]and size is[%d] Read Count[%d], index = [%d]\n", ulResult,
1400                                                          ubPatch, pFsNode->data_size, ulReadCnt, ulFileIndex);
1401                                                 tar_close();
1402                                                 SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1403                                                 break;
1404                                         }
1405                                         SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1406                                         pFsNode = pFsNode->nextnode;
1407                                 }
1408                                 else {
1409                                         ulResult = E_SS_FAILURE;
1410                                         tar_close();
1411                                         LOGE("size is invalid, index = [%d]\n",
1412                                              ulFileIndex);
1413                                         SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1414                                         break;
1415                                 }
1416                                 ulFileIndex++;
1417                         }
1418                         LOGL(LOG_SSENGINE, "DIFFS : Total index = [%d]\n",
1419                              ulFileIndex - 1);
1420 #ifdef TIME_PROFILING
1421                         get_time_stamp1();  //total time capturing
1422                         t2 = atof(ts1);
1423                         LOGL(LOG_SSENGINE,
1424                              "DIFFS mode end time = [%lf], diff = [%lf]\n", t2,
1425                              (t2 - t1));
1426 #endif
1427                         tar_close();
1428                         LOGL(LOG_SSENGINE, "DIFFS mode end\n");
1429                 }
1430                 break;
1431         case MOVES:
1432                 {
1433                         LOGL(LOG_SSENGINE, "MOVES mode start\n");
1434
1435 #ifdef TIME_PROFILING
1436                         get_time_stamp1();  //total time capturing
1437                         t1 = atof(ts1);
1438                         LOGL(LOG_SSENGINE, "MOVES mode start time = [%lf]\n",
1439                              t1);
1440 #endif
1441                         while (pFsNode) {
1442                                 //LOGL(LOG_SSENGINE, "MOVES update Index: [%d] \n", ulFileIndex++);
1443                                 int skip_flag = 0;
1444                                 if (SS_LoadFile(pFsNode->file_old_path, &source_file) == 0) {
1445                                         LOGL(LOG_SSENGINE, "Patch Can be applied\n");
1446                                         if (source_file.data)
1447                                                 SS_Free(source_file.data);
1448                                 } else if (SS_LoadFile(pFsNode->file_new_path, &target_file) == 0) {
1449                                         LOGL(LOG_SSENGINE, "source deleted!!, file_old_path: [%s]\n", pFsNode->file_old_path);
1450                                         if (ParseSha1(pFsNode->sha1src, target_sha1) != 0) {
1451                                                 LOGE("failed to parse sha1 \"%s\"\n", pFsNode->sha1src);
1452                                                 ulResult = E_SS_FAILURE;
1453                                                 SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1454                                                 if (target_file.data)
1455                                                         SS_Free(target_file.data);
1456                                                 break;
1457                                         }
1458                                         if (memcmp(target_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) {
1459                                                 LOGL(LOG_SSENGINE, "Patch already applied\n");
1460                                                 skip_flag = 1;
1461                                                 if (target_file.data)
1462                                                         SS_Free(target_file.data);
1463                                         } else {
1464                                                 LOGL(LOG_SSENGINE, "target_sha1 diff!!: [%s]\n", target_sha1);
1465                                                 ulResult = E_SS_FAILURE;
1466                                                 SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1467                                                 if (target_file.data)
1468                                                         SS_Free(target_file.data);
1469                                                 break;
1470                                         }
1471                                 } else {
1472                                         LOGE("No exist files. - file_old_path: [%s] file_new_path: [%s]\n", pFsNode->file_old_path, pFsNode->file_new_path);
1473                                         ulResult = E_SS_FAILURE;
1474                                         SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1475                                         break;
1476                                 }
1477
1478                                 if (skip_flag == 0) {
1479                                         ulResult = SS_MoveFile(pFsNode->file_old_path, pFsNode->file_new_path);
1480                                         if (ulResult != S_SS_SUCCESS) {
1481                                         LOGE("Move Failed for [%s] to [%s], result = [%d], index = [%d]\n", pFsNode->file_old_path, pFsNode->file_new_path, ulResult, ulFileIndex);
1482                                                 SS_SetUpgradeState(ulResult);
1483                                                 break;
1484                                         } else {
1485                                                 // Verification
1486                                                 if (SS_LoadFile(pFsNode->file_new_path, &target_file) == 0) {
1487                                                         if (ParseSha1(pFsNode->sha1src, target_sha1) != 0) {
1488                                                                 LOGE("failed to parse sha1 \"%s\"\n", pFsNode->sha1src);
1489                                                                 ulResult = E_SS_FAILURE;
1490                                                                 SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1491                                                                 if (target_file.data)
1492                                                                         SS_Free(target_file.data);
1493                                                                 break;
1494                                                         }
1495                                                         if (memcmp(target_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) {
1496                                                                 LOGL(LOG_SSENGINE, "Patch success!!\n");
1497                                                                 if (target_file.data)
1498                                                                         SS_Free(target_file.data);
1499                                                         } else {
1500                                                                 LOGL(LOG_SSENGINE, "target_sha1 diff!!: [%s]\n", target_sha1);
1501                                                                 ulResult = E_SS_FAILURE;
1502                                                                 SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1503                                                                 if (target_file.data)
1504                                                                         SS_Free(target_file.data);
1505                                                                 break;
1506                                                         }
1507                                                 }
1508                                         }
1509                                 }
1510                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1511                                 pFsNode = pFsNode->nextnode;
1512                                 ulFileIndex++;
1513                         }
1514                         LOGL(LOG_SSENGINE, "MOVES : Total index = [%d]\n",
1515                              ulFileIndex - 1);
1516 #ifdef TIME_PROFILING
1517                         get_time_stamp1();  //total time capturing
1518                         t2 = atof(ts1);
1519                         LOGL(LOG_SSENGINE,
1520                              "MOVES mode end time = [%lf], diff = [%lf]\n", t2,
1521                              (t2 - t1));
1522 #endif
1523                         LOGL(LOG_SSENGINE, "MOVES mode end\n");
1524                 }
1525                 break;
1526         case DELETES:
1527                 {
1528                         LOGL(LOG_SSENGINE, "DELETES mode start\n");
1529
1530                         int ulFiletype = 0;
1531 #ifdef TIME_PROFILING
1532                         get_time_stamp1();  //total time capturing
1533                         t1 = atof(ts1);
1534                         LOGL(LOG_SSENGINE, "DELETES mode start time = [%lf]\n",
1535                              t1);
1536 #endif
1537                         while (pFsNode) {
1538                                 if (pFsNode->type == DELETES) {
1539                                         //LOGL(LOG_SSENGINE, "DELETES update Index: [%d] \n", ulFileIndex++);
1540                                         SS_GetFileType(pFsNode->file_old_path, (enumFileType *) & ulFiletype);
1541                                         if (ulFiletype == 2)            //FT_FOLDER
1542                                                 ulResult = SS_DeleteFolder(pFsNode->file_old_path);
1543                                         else
1544                                                 ulResult = SS_DeleteFile(pFsNode->file_old_path);
1545                                         if (ulResult != S_SS_SUCCESS) {
1546                                                 LOGE("Delete Failed, result = [%d], index = [%d]\n", ulResult, ulFileIndex);
1547                                                 SS_SetUpgradeState(ulResult);
1548                                                 break;
1549                                         }
1550                                         ulFileIndex++;
1551                                 }
1552                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1553                                 pFsNode = pFsNode->nextnode;
1554                         }
1555                         LOGL(LOG_SSENGINE, "DELETES : Total index = [%d]\n",
1556                              ulFileIndex - 1);
1557 #ifdef TIME_PROFILING
1558                         get_time_stamp1();  //total time capturing
1559                         t2 = atof(ts1);
1560                         LOGL(LOG_SSENGINE,
1561                              "DELETES mode end time = [%lf], diff = [%lf]\n",
1562                              t2, (t2 - t1));
1563 #endif
1564                         LOGL(LOG_SSENGINE, "DELETES mode end\n");
1565                 }
1566                 break;
1567         case DELETE_END:
1568                 {
1569                         LOGL(LOG_SSENGINE, "DELETE_END mode start\n");
1570
1571                         int ulFiletype = 0;
1572 #ifdef TIME_PROFILING
1573                         get_time_stamp1();  //total time capturing
1574                         t1 = atof(ts1);
1575                         LOGL(LOG_SSENGINE,
1576                              "DELETE_END mode start time = [%lf]\n", t1);
1577 #endif
1578                         while (pFsNode) {
1579                                 if (pFsNode->type == DELETE_END) {
1580                                         LOGL(LOG_SSENGINE, "DELETE_END update Index: [%d] \n", ulFileIndex++);
1581                                         SS_GetFileType(pFsNode->file_old_path, (enumFileType *) & ulFiletype);
1582                                         if (ulFiletype == 2)            //FT_FOLDER
1583                                                 ulResult = SS_DeleteFolder(pFsNode->file_old_path);
1584                                         else
1585                                                 ulResult = SS_DeleteFile(pFsNode->file_old_path);
1586                                         if (ulResult != S_SS_SUCCESS) {
1587                                                 LOGE("Delete Failed, result = [%d], index = [%d]\n", ulResult, ulFileIndex);
1588                                                 SS_SetUpgradeState(ulResult);
1589                                                 break;
1590                                         }
1591                                         ulFileIndex++;
1592                                 }
1593                                 pFsNode = pFsNode->nextnode;
1594                         }
1595                         LOGL(LOG_SSENGINE, "DELETE_END : Total index = [%d]\n",
1596                              ulFileIndex - 1);
1597 #ifdef TIME_PROFILING
1598                         get_time_stamp1();      //total time capturing
1599                         t2 = atof(ts1);
1600                         LOGL(LOG_SSENGINE,
1601                              "DELETE_END mode start time = [%lf], diff = [%lf]\n",
1602                              t2, (t2 - t1));
1603 #endif
1604                         LOGL(LOG_SSENGINE, "DELETE_END mode end\n");
1605                 }
1606                 break;
1607
1608         case NEWFILES:
1609                 {
1610                         LOGL(LOG_SSENGINE, "NEWFILES mode start\n");
1611
1612 #ifdef TIME_PROFILING
1613                         get_time_stamp1();      //total time capturing
1614                         t1 = atof(ts1);
1615                         LOGL(LOG_SSENGINE, "NEWFILES mode start time = [%lf]\n",
1616                              t1);
1617 #endif
1618                         LOGL(LOG_SSENGINE, "Starting New file upgrade for   [%s]\n", patch_path);
1619                         if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, (char *)patch_path, new_compressed_path) >=
1620                                 0)
1621                                 if (_7zdecompress(new_compressed_path) == 0)
1622                                         LOGL(LOG_SSENGINE, "7zip extracted  successfully %s\n", ua_dataSS->parti_info->ua_parti_name);
1623                                 else
1624                                         LOGL(LOG_SSENGINE, "7zip extraction error for %s\n", ua_dataSS->parti_info->ua_parti_name);
1625                         else
1626                                 LOGL(LOG_SSENGINE, "tar extraction error for %s\n", ua_dataSS->parti_info->ua_parti_name);
1627                         SS_DeleteFile(new_compressed_path);
1628 #ifdef TIME_PROFILING
1629                         get_time_stamp1();      //total time capturing
1630                         t2 = atof(ts1);
1631                         LOGL(LOG_SSENGINE,
1632                              "NEWFILES mode end time = [%lf], diff = [%lf]\n",
1633                              t2, (t2 - t1));
1634 #endif
1635                         LOGL(LOG_SSENGINE, "NEWFILES mode end\n");
1636                 }
1637                 break;
1638         case SYMDIFFS:
1639                 {
1640                         LOGL(LOG_SSENGINE, "SYMDIFFS mode start\n");
1641
1642 #ifdef TIME_PROFILING
1643                         get_time_stamp1();      //total time capturing
1644                         t1 = atof(ts1);
1645                         LOGL(LOG_SSENGINE, "SYMDIFFS mode start time = [%lf]\n",
1646                              t1);
1647 #endif
1648                         while (pFsNode) {
1649                                 //LOGL(LOG_SSENGINE, "SYMDIFFS update Index: [%d] \n", ulFileIndex++);
1650                                 //LOG("Sym Diff file paths: [Linkname - %s] [reference file name- %s][]\n", pFsNode->file_path,pFsNode->patch_name);
1651                                 ulResult = SS_Unlink(pFsNode->file_old_path);
1652                                 if (ulResult == S_SS_SUCCESS) {
1653                                         ulResult = SS_Link(NULL, pFsNode->file_new_path, pFsNode->patch_name);
1654                                         if (ulResult != S_SS_SUCCESS) {
1655                                                 LOGE("SS_Link Failed, Linkname:[%s], reference file name, index = [%d]:[%s]\n",
1656                                                 pFsNode->file_new_path, ulFileIndex, pFsNode->patch_name);
1657                                         }
1658                                 } else {
1659                                         LOGE("Unlink Failed, result = [%d], index = [%d]\n", ulResult, ulFileIndex);
1660                                         SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1661                                         break;
1662                                 }
1663                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1664                                 pFsNode = pFsNode->nextnode;
1665                                 ulFileIndex++;
1666                         }
1667                         LOGL(LOG_SSENGINE, "SYMDIFFS : Total index = [%d]\n",
1668                              ulFileIndex - 1);
1669 #ifdef TIME_PROFILING
1670                         get_time_stamp1();      //total time capturing
1671                         t2 = atof(ts1);
1672                         LOGL(LOG_SSENGINE,
1673                              "SYMDIFFS mode end time = [%lf], diff = [%lf]\n",
1674                              t2, (t2 - t1));
1675 #endif
1676                         LOGL(LOG_SSENGINE, "SYMDIFFS mode end\n");
1677                 }
1678                 break;
1679         case SYMNEWFILES:
1680                 {
1681                         LOGL(LOG_SSENGINE, "SYMNEWFILES mode start\n");
1682
1683                         fs_params *head_node;
1684                         int retry_count = 0, do_retry = 0;
1685 #ifdef TIME_PROFILING
1686                         get_time_stamp1();      //total time capturing
1687                         t1 = atof(ts1);
1688                         LOGL(LOG_SSENGINE,
1689                              "SYMNEWFILES mode start time = [%lf]\n", t1);
1690 #endif
1691  SYMLINK_CREATE:
1692                         head_node = pFsNode;
1693                         while (head_node) {
1694                                 //LOGL(LOG_SSENGINE, "SYMNEWS update Index: [%d] \n", ulFileIndex++);
1695                                 snprintf(ubPatch, SS_MAX_FILE_PATH, "%s%s%s", patch_path, "/", head_node->patch_name);
1696                                 LOGL(LOG_SSENGINE, "Sym New file paths: [Linkname - %s] [reference file name- %s][]\n",
1697                                          head_node->file_old_path, head_node->patch_name);
1698                                 ulResult = SS_Link(NULL, head_node->file_old_path, head_node->patch_name);
1699                                 if (ulResult == E_SS_FAILURE) {
1700                                         LOGE("Link Failed, result = [%d], index = [%d]\n", ulResult, ulFileIndex);
1701                                         SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1702                                         break;
1703                                 } else if (ulResult == ENOENT) {  //to handle cases where new symlink points to a new symlink yet to be created
1704                                         do_retry = 1;      //we will retry the failed symlinks with error 2 (no file or dir) again after this cycle
1705                                         //SS_UpdateUIProgress(ua_dataSS,ulPatchCount);
1706                                         head_node = head_node->nextnode;
1707                                         continue;
1708                                 }
1709                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1710                                 head_node = head_node->nextnode;
1711                                 ulFileIndex++;
1712                         }
1713                         LOGL(LOG_SSENGINE, "SYMNEWFILES : Total index = [%d]\n",
1714                              ulFileIndex - 1);
1715                         if (do_retry && (retry_count < 4)) {
1716                                 retry_count++;
1717                                 ulFileIndex = 0;
1718                                 do_retry = 0;
1719                                 goto SYMLINK_CREATE;
1720                         } else if (do_retry && (retry_count >= 4)) {  //retry to be done maximum 4 times
1721                                 LOGE("Link Failed after %d retrys\n", retry_count);
1722                                 //SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1723                                 break;
1724                         }
1725 #ifdef TIME_PROFILING
1726                         get_time_stamp1();      //total time capturing
1727                         t2 = atof(ts1);
1728                         LOGL(LOG_SSENGINE,
1729                              "SYMNEWFILES mode end time = [%lf], diff = [%lf]\n",
1730                              t2, (t2 - t1));
1731 #endif
1732                         LOGL(LOG_SSENGINE, "SYMNEWFILES mode end\n");
1733                 }
1734                 break;
1735         case HARDDIFFS:
1736                 {
1737                         LOGL(LOG_SSENGINE, "HARDDIFFS mode start\n");
1738
1739 #ifdef TIME_PROFILING
1740                         get_time_stamp1();      //total time capturing
1741                         t1 = atof(ts1);
1742                         LOGL(LOG_SSENGINE, "HARDDIFFS mode start time = [%lf]\n",
1743                              t1);
1744 #endif
1745                         while (pFsNode) {
1746                                 //LOGL(LOG_SSENGINE, "SYMDIFFS update Index: [%d] \n", ulFileIndex++);
1747                                 //LOG("Sym Diff file paths: [Linkname - %s] [reference file name- %s][]\n", pFsNode->file_path,pFsNode->patch_name);
1748                                 ulResult = SS_DeleteFile(pFsNode->file_old_path);
1749                                 if (ulResult == S_SS_SUCCESS) {
1750                                         ulResult = SS_HardLink(pFsNode->file_new_path, pFsNode->patch_name);
1751                                         if (ulResult != S_SS_SUCCESS) {
1752                                                 LOGE("SS_HardLink Failed, Linkname:[%s], reference file name, index = [%d]:[%s]\n",
1753                                                 pFsNode->file_new_path, ulFileIndex, pFsNode->patch_name);
1754                                         }
1755                                 } else {
1756                                         LOGE("Removing old hardlink Failed, result = [%d], index = [%d]\n", ulResult, ulFileIndex);
1757                                         SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1758                                         break;
1759                                 }
1760                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1761                                 pFsNode = pFsNode->nextnode;
1762                                 ulFileIndex++;
1763                         }
1764                         LOGL(LOG_SSENGINE, "HARDDIFFS : Total index = [%d]\n",
1765                              ulFileIndex - 1);
1766 #ifdef TIME_PROFILING
1767                         get_time_stamp1();      //total time capturing
1768                         t2 = atof(ts1);
1769                         LOGL(LOG_SSENGINE,
1770                              "HARDDIFFS mode end time = [%lf], diff = [%lf]\n",
1771                              t2, (t2 - t1));
1772 #endif
1773                         LOGL(LOG_SSENGINE, "HARDDIFFS mode end\n");
1774                 }
1775                 break;
1776         case HARDNEWFILES:
1777                 {
1778                         LOGL(LOG_SSENGINE, "HARDNEWFILES mode start\n");
1779
1780 #ifdef TIME_PROFILING
1781                         get_time_stamp1();      //total time capturing
1782                         t1 = atof(ts1);
1783                         LOGL(LOG_SSENGINE,
1784                              "HARDNEWFILES mode start time = [%lf]\n", t1);
1785 #endif
1786             fs_params *head_node = pFsNode;
1787                         while (head_node) {
1788                                 //LOGL(LOG_SSENGINE, "SYMNEWS update Index: [%d] \n", ulFileIndex++);
1789                                 LOGL(LOG_SSENGINE, "Hardlink New file paths: [Linkname - %s] [reference file name- %s][]\n",
1790                                          head_node->file_old_path, head_node->patch_name);
1791                                 ulResult = SS_HardLink(head_node->file_old_path, head_node->patch_name);
1792                                 if (ulResult == E_SS_FAILURE) {
1793                                         LOGE("Link Failed, result = [%d], index = [%d]\n", ulResult, ulFileIndex);
1794                                         SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
1795                                         break;
1796                                 }
1797                                 SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0);
1798                                 head_node = head_node->nextnode;
1799                                 ulFileIndex++;
1800                         }
1801                         LOGL(LOG_SSENGINE, "HARDNEWFILES : Total index = [%d]\n",
1802                              ulFileIndex - 1);
1803
1804 #ifdef TIME_PROFILING
1805                         get_time_stamp1();      //total time capturing
1806                         t2 = atof(ts1);
1807                         LOGL(LOG_SSENGINE,
1808                              "HARDNEWFILES mode end time = [%lf], diff = [%lf]\n",
1809                              t2, (t2 - t1));
1810 #endif
1811                         LOGL(LOG_SSENGINE, "HARDNEWFILES mode end\n");
1812
1813         }
1814                 break;
1815         default:
1816                 break;
1817         }
1818         return ulResult;
1819 }
1820
1821 #ifdef MEM_PROFILING
1822 extern int max_mem;
1823 extern int cur_mem;
1824 #endif
1825 /*!
1826  *********************************************************************************
1827  *                                       SS_FSUpdatemain
1828  *********************************************************************************
1829  *
1830  * @brief
1831  *      This is the API exposed from the engine to update FS.
1832  *
1833  *
1834  *      @param
1835  *
1836  *      @return                         returns S_SS_SUCCESS
1837  *                                              E_SS_FAILURE in case of error
1838  *
1839  *********************************************************************************
1840  */
1841
1842 int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS)
1843 {
1844         int ulResult = S_SS_SUCCESS;
1845         int last_update_status = 0;
1846         int del_type = 0;
1847         fs_list *head_ptr_node = NULL;
1848         char new_patch_path[SS_MAX_FILE_PATH] = {
1849                 0
1850         };
1851         char status_path[MAX_FILE_PATH];
1852         snprintf(status_path, MAX_FILE_PATH, "%s/%s",ua_dataSS->update_data->ua_delta_folder, SS_UPDATE_STATUS_PATH);
1853
1854         if (!ua_dataSS)
1855                 return E_SS_BAD_PARAMS; // Set error ??
1856         head_ptr_node = headptr_list;
1857
1858         if (!head_ptr_node) {      //in case of power failure, try rebilding nodes again
1859                 SS_FSVerifyPartition(ua_dataSS);
1860                 head_ptr_node = headptr_list;
1861         }
1862
1863         if (!head_ptr_node)
1864                 return E_SS_FSBADNODES;
1865
1866         SS_GetPartition_LocDetails(ua_dataSS);
1867
1868         LOGL(LOG_SSENGINE, "FS Update Entry\n");
1869
1870 #ifdef POWER_FAIL_TEST
1871         int fail_test_flag = 0;
1872         fail_test_flag = SS_Get_power_fail_flag();
1873         LOGL(LOG_SSENGINE, "fail_test_flag: [%d]\n", fail_test_flag);
1874 #endif
1875
1876         if(SS_Get_last_update_status(&last_update_status, &del_type, status_path) == -1)
1877                 LOGE("SS_Get_last_update_status failed!!\n");
1878
1879         LOGL(LOG_SSENGINE, "last_update_status: [%d], del_type: [%d]\n", last_update_status, del_type);
1880
1881         // for now, we don't want to read last del_type as the logic doesn't work
1882         del_type = 0;
1883
1884         if (head_ptr_node->del_ref == NULL) {
1885                 LOGL(LOG_SSENGINE, "No DEL header\n");
1886         } else if (ulResult == S_SS_SUCCESS) {
1887                 if (del_type < DELETES) {
1888                         ulResult = SS_FSUpdateFile(DELETES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref,
1889                                         ua_dataSS->update_delta->ua_patch_path);
1890                         if (ulResult == S_SS_SUCCESS) {
1891                                 LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETES success!!\n");
1892
1893 #ifdef POWER_FAIL_TEST
1894                                 if (fail_test_flag < DELETES) {
1895                                         if (SS_Do_Power_fail_test(DELETES) == E_SS_FAILURE) {
1896                                                 LOGE("SS_Do_Power_fail_test failed!!\n");
1897                                         }
1898                                 }
1899 #endif
1900                                 SS_Set_last_update_status(0, DELETES, status_path);
1901                         }
1902                 } else {
1903                         LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETES already applied!!\n");
1904                 }
1905         }
1906
1907         if (head_ptr_node->dif_ref == NULL) {
1908                 LOGL(LOG_SSENGINE, "No DIFF header\n");
1909         } else if (ulResult == S_SS_SUCCESS) {
1910                 ulResult =
1911                         SS_FSUpdateFile(DIFFS, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->dif_ref,
1912                                                         ua_dataSS->update_delta->ua_patch_path);
1913                 if (ulResult == S_SS_SUCCESS) {
1914                         LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DIFFS success!!\n");
1915 #ifdef POWER_FAIL_TEST
1916                         if (fail_test_flag < DIFFS) {
1917                                 if (SS_Do_Power_fail_test(DIFFS) == E_SS_FAILURE) {
1918                                         LOGE("SS_Do_Power_fail_test failed!!\n");
1919                                 }
1920                         }
1921 #endif
1922                 }
1923         }
1924
1925         if (head_ptr_node->move_ref == NULL) {
1926                 LOGL(LOG_SSENGINE, "No MOVE header\n");
1927         } else if (ulResult == S_SS_SUCCESS) {
1928                 if (del_type < MOVES) {
1929                 ulResult =
1930                         SS_FSUpdateFile(MOVES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->move_ref,
1931                                                         ua_dataSS->update_delta->ua_patch_path);
1932                         if (ulResult == S_SS_SUCCESS) {
1933                                 LOGL(LOG_SSENGINE, "SS_FSUpdateFile - MOVES success!!\n");
1934 #ifdef POWER_FAIL_TEST
1935                                 if (fail_test_flag < MOVES) {
1936                                         if (SS_Do_Power_fail_test(MOVES) == E_SS_FAILURE) {
1937                                                 LOGE("SS_Do_Power_fail_test failed!!\n");
1938                                         }
1939                                 }
1940 #endif
1941                                 SS_Set_last_update_status(0, MOVES, status_path);
1942                         }
1943                 } else {
1944                         LOGL(LOG_SSENGINE, "SS_FSUpdateFile - MOVES already applied!!\n");
1945                 }
1946         }
1947
1948         if (head_ptr_node->del_ref == NULL) {
1949                 LOGL(LOG_SSENGINE, "No DEL header\n");
1950         } else if (ulResult == S_SS_SUCCESS) {
1951                 if (del_type < DELETE_END) {
1952                         ulResult = SS_FSUpdateFile(DELETE_END, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref,
1953                                         ua_dataSS->update_delta->ua_patch_path);
1954                         if (ulResult == S_SS_SUCCESS) {
1955                                 LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETE_END success!!\n");
1956 #ifdef POWER_FAIL_TEST
1957                                 if (fail_test_flag < DELETE_END) {
1958                                         if (SS_Do_Power_fail_test(DELETE_END) == E_SS_FAILURE) {
1959                                                 LOGE("SS_Do_Power_fail_test failed!!\n");
1960                                         }
1961                                 }
1962 #endif
1963                                 SS_Set_last_update_status(0, DELETE_END, status_path);
1964                         }
1965                 } else {
1966                         LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETE_END already applied!!\n");
1967                 }
1968         }
1969
1970          if (ulResult == S_SS_SUCCESS) {
1971                  //new file extraction start
1972                 snprintf(new_patch_path, SS_MAX_FILE_PATH, "%s%s", ua_dataSS->parti_info->ua_subject_name, SS_COMPRESSED_FILE);  // subject name wil have fw slash as part of cfg file
1973                 LOGL(LOG_SSENGINE, "File path created to extract new files : [%s]\n", new_patch_path);
1974                 ulResult =
1975                         SS_FSUpdateFile(NEWFILES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->new_ref, new_patch_path);
1976                 //new file extraction end
1977                 if (ulResult == S_SS_SUCCESS) {
1978                         LOGL(LOG_SSENGINE, "SS_FSUpdateFile - NEWFILES success!!\n");
1979                 }
1980         }
1981
1982         if (head_ptr_node->sym_difref == NULL) {
1983                 LOGL(LOG_SSENGINE, "No SYMDIFF header\n");
1984         } else if (ulResult == S_SS_SUCCESS) {
1985                 ulResult =
1986                         SS_FSUpdateFile(SYMDIFFS, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->sym_difref,
1987                                                         ua_dataSS->update_delta->ua_patch_path);
1988                 if (ulResult == S_SS_SUCCESS) {
1989                         LOGL(LOG_SSENGINE, "SS_FSUpdateFile - SYMDIFFS success!!\n");
1990 #ifdef POWER_FAIL_TEST
1991                         if (fail_test_flag < SYMDIFFS) {
1992                                 if (SS_Do_Power_fail_test(SYMDIFFS) == E_SS_FAILURE) {
1993                                         LOGE("SS_Do_Power_fail_test failed!!\n");
1994                                 }
1995                         }
1996 #endif
1997                 }
1998         }
1999
2000         if (head_ptr_node->sym_newref == NULL) {
2001                 LOGL(LOG_SSENGINE, "No SYMNEW header\n");
2002         } else if (ulResult == S_SS_SUCCESS) {
2003                 ulResult =
2004                         SS_FSUpdateFile(SYMNEWFILES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->sym_newref,
2005                                                         ua_dataSS->update_delta->ua_patch_path);
2006                 if (ulResult == S_SS_SUCCESS) {
2007                         LOGL(LOG_SSENGINE, "SS_FSUpdateFile - SYMNEWFILES success!!\n");
2008 #ifdef POWER_FAIL_TEST
2009                         if (fail_test_flag < SYMNEWFILES) {
2010                                 if (SS_Do_Power_fail_test(SYMNEWFILES) == E_SS_FAILURE) {
2011                                         LOGE("SS_Do_Power_fail_test failed!!\n");
2012                                 }
2013                         }
2014 #endif
2015                 }
2016         }
2017
2018         if (head_ptr_node->hard_newref == NULL) {
2019                 LOGL(LOG_SSENGINE, "No HARDNEW header\n");
2020         } else if (ulResult == S_SS_SUCCESS) {
2021                 ulResult =
2022                         SS_FSUpdateFile(HARDNEWFILES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->hard_newref,
2023                                                         ua_dataSS->update_delta->ua_patch_path);
2024                 if (ulResult == S_SS_SUCCESS) {
2025                         LOGL(LOG_SSENGINE, "SS_FSUpdateFile - HARDNEWFILES success!!\n");
2026 #ifdef POWER_FAIL_TEST
2027                         if (fail_test_flag < HARDNEWFILES) {
2028                                 if (SS_Do_Power_fail_test(HARDNEWFILES) == E_SS_FAILURE) {
2029                                         LOGE("SS_Do_Power_fail_test failed!!\n");
2030                                 }
2031                         }
2032 #endif
2033                 }
2034         }
2035
2036         if (head_ptr_node->hard_difref == NULL) {
2037                 LOGL(LOG_SSENGINE, "No HARDDIFF header\n");
2038         } else if (ulResult == S_SS_SUCCESS) {
2039                 ulResult =
2040                         SS_FSUpdateFile(HARDDIFFS, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->hard_difref,
2041                                                         ua_dataSS->update_delta->ua_patch_path);
2042                 if (ulResult == S_SS_SUCCESS) {
2043                         LOGL(LOG_SSENGINE, "SS_FSUpdateFile - HARDDIFFs success!!\n");
2044 #ifdef POWER_FAIL_TEST
2045                         if (fail_test_flag < HARDDIFFS) {
2046                                 if (SS_Do_Power_fail_test(HARDDIFFS) == E_SS_FAILURE) {
2047                                         LOGE("SS_Do_Power_fail_test failed!!\n");
2048                                 }
2049                         }
2050 #endif
2051                 }
2052         }
2053
2054         if (ulResult == S_SS_SUCCESS) {
2055                 ulResult = SS_FSSetAttributes(ua_dataSS);
2056         } else {
2057                 SS_FSSetAttributes(ua_dataSS);  // To prevent boot failures by smack.
2058         }
2059
2060         sync();
2061         sleep(1);
2062         SS_FSClearNodes();
2063
2064         if (ulResult == S_SS_SUCCESS) {
2065                 SS_UpdateUIProgress(ua_dataSS, 0, 1);   //fix WGID : 51963, When all updates are done to FS , patchcount is not needed, passing 1 to 3rd arg is enough
2066                 SS_Set_last_update_status(0, DEL_TYPE_MAX, status_path);
2067         }
2068
2069         LOGL(LOG_SSENGINE, "FS update Complete\n");
2070 #ifdef  MEM_PROFILING
2071         LOGL(LOG_SSENGINE, "Stats are : Cur Max : [%d] Global Max : [%d]\n", cur_mem, max_mem);
2072 #endif
2073 #ifdef POWER_FAIL_TEST
2074         unlink(SS_POWER_FAIL_TEST_FLAG);
2075 #endif
2076         if (ulResult == S_SS_SUCCESS)
2077                 return ulResult;
2078         else
2079                 return SS_GetUpgradeState();
2080 }
2081
2082 /*!
2083  *********************************************************************************
2084  *                                       SS_FSUpdatemain
2085  *********************************************************************************
2086  *
2087  * @brief
2088  *      This is the API exposed from the engine to update FS.
2089  *      FS entry function for updating FS partition. Should be invoked only after verification of the partition
2090  *
2091  *
2092  *      @param                          Requires common data structure having all details & Partition Index.
2093  *                                              (Used for getting right NODES information that built during verification)
2094  *                                              (Configuration, Delta info, Partition Info, UI link , Kind of operation.(Verify or Updates))
2095  *
2096  *      @return                         returns S_SS_SUCCESS
2097  *                                              E_SS_FAILURE in case of error
2098  *
2099  *********************************************************************************
2100  */
2101 size_t SS_FSAvailiableFreeSpace(char *block_name)
2102 {
2103
2104         struct mntent *ent;
2105         FILE *aFile;
2106         struct statfs sb;
2107         aFile = setmntent("/proc/mounts", "r");
2108         if (aFile == NULL) {
2109                 LOGE("setmntent error\n");
2110                 return E_SS_FAILURE;
2111         }
2112         while (NULL != (ent = getmntent(aFile))) {
2113                 if (strcmp(ent->mnt_fsname, block_name) == 0) {
2114                         if (statfs(ent->mnt_dir, &sb) == 0)
2115                                 LOGL(LOG_SSENGINE, "Total free space = %" PRIu64 ", blocks free = %" PRIu64 "\n", sb.f_bsize * sb.f_bavail, sb.f_bfree);
2116                 }
2117         }
2118         endmntent(aFile);
2119         return ((long long)sb.f_bsize * (long long)sb.f_bavail >= (long long)SIZE_4GB) ? SIZE_4GB : sb.f_bsize * sb.f_bavail ;
2120 }
2121
2122 int SS_FSVerifyPartition(ua_dataSS_t * ua_dataSS)
2123 {
2124         int ulResult = S_SS_SUCCESS;
2125         if (!ua_dataSS) {
2126                 LOGE("Wrong Param for fs verification\n");
2127                 SS_SetUpgradeState(E_SS_BAD_PARAMS);
2128                 return E_SS_FAILURE;
2129         }
2130
2131 #ifdef MEM_PROFILING
2132         if (!mem_profiling_start)
2133                 if (!(S_SS_SUCCESS == SS_Do_Memory_Profiling()))
2134                         return E_SS_FAILURE;
2135 #endif
2136         SS_GetPartition_LocDetails(ua_dataSS);
2137         LOGL(LOG_SSENGINE, "FS Verification Start\n");
2138         if (ua_dataSS->ua_operation == UI_OP_SCOUT)
2139                 gvalid_session = 1;  // (shd b true if called during verification)
2140         headptr_list = SS_FSBuildNodes(ua_dataSS);
2141 #ifdef TIME_PROFILING
2142         LOGL(LOG_SSENGINE, "fast_tar_get_item_size_time :[%lf]\n", fast_tar_get_item_size_time);
2143         LOGL(LOG_SSENGINE, "SS_LoadFile_time :[%lf]\n", SS_LoadFile_time);
2144         LOGL(LOG_SSENGINE, "SS_FSBuildNodes_time :[%lf]\n", SS_FSBuildNodes_time);
2145 #endif
2146         if (!headptr_list) {
2147                 LOGE("FS Verification Failed\n");
2148                 SS_FSClearNodes();
2149                 ulResult = E_SS_FAILURE;
2150         }
2151
2152         if (ulResult == S_SS_SUCCESS)
2153                 return ulResult;
2154         else
2155                 return SS_GetUpgradeState();
2156 }
2157
2158 //Should check if space is available????
2159 int SS_BackupSource(const char *source_filename, char *backup_path)
2160 {
2161         int ret = E_SS_FAILURE;
2162
2163         if (source_filename) {
2164                 ret = (int)SS_CopyFile(source_filename, backup_path);
2165                 if (ret != S_SS_SUCCESS) {
2166                         LOGE("failed to back up source file  Error [%d]\n", ret);
2167                         SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED);
2168                 }
2169         }
2170         return ret;
2171 }
2172
2173 int SS_BackupSourceClear(char *backup_path)
2174 {
2175         int ret = E_SS_FAILURE;
2176         ret = (int)SS_DeleteFile(backup_path);
2177         if (ret != S_SS_SUCCESS) {
2178                 LOGE("failed to delete BACKUP file\n");
2179                 SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED);
2180         }
2181         return ret;
2182 }
2183
2184 int SS_PatchSourceClear(char *patch_path)
2185 {
2186         int ret = E_SS_FAILURE;
2187         ret = (int)SS_DeleteFile(patch_path);
2188         if (ret != S_SS_SUCCESS) {
2189                 LOGE("failed to delete PATCHFILE file\n");
2190                 SS_SetUpgradeState(E_SS_PATCHFILE_DEL_ERROR);
2191         }
2192         return ret;
2193 }
2194
2195 long SS_GetUPIVersion(unsigned char *ver_str)
2196 {
2197         if (ver_str) {
2198                 strncpy((char *)ver_str, SS_TOTA_VERSION, MAX_PATH);
2199 #ifdef MEM_PROFILING
2200                 if (!mem_profiling_start)
2201                         if (!(S_SS_SUCCESS == SS_Do_Memory_Profiling()))
2202                                 LOGE("Unable to start Memory_Profiling\n");
2203 #endif
2204                 return S_SS_SUCCESS;//wgid: 2456
2205         } else
2206                 return E_SS_FAILURE;
2207 }