tizen 2.3 release
[framework/appfw/app2sd.git] / plugin / app2sd / src / app2sd_interface.c
1 /*
2  * app2ext
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Garima Shrivastava<garima.s@samsung.com>
7  *      Jyotsna Dhumale <jyotsna.a@samsung.com>
8  *      Venkatesha Sarpangala <sarpangala.v@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <app2sd_internals.h>
25 #include <app2sd_interface.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <pkgmgr-info.h>
30 #include <vconf.h>
31
32 #define MAX_BUF_LEN     1024
33 #define APP2SD_TMP_PATH "/opt/usr/apps/tmp"
34
35 int app2sd_pre_app_install(const char *pkgid, GList* dir_list,
36                                 int size)
37 {
38         int ret = 0;
39         int free_mmc_mem = 0;
40         char *device_node = NULL;
41         char *devi = NULL;
42         char *result = NULL;
43
44         /*Validate the function parameter recieved */
45         if (pkgid == NULL || dir_list == NULL || size <= 0) {
46                 app2ext_print("App2Sd Error : Invalid function arguments\n");
47                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
48         }
49         /*Check whether MMC is present or not */
50         ret = _app2sd_check_mmc_status();
51         if (ret) {
52                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
53                         ret);
54                 return APP2EXT_ERROR_MMC_STATUS;
55         }
56         /*Find available free memory in the MMC card */
57         ret = _app2sd_get_available_free_memory(MMC_PATH,
58                                                 &free_mmc_mem);
59         if (ret) {
60                 app2ext_print("App2Sd Error : Unable to get available free memory in MMC %d\n", ret);
61                 return APP2EXT_ERROR_MMC_STATUS;
62         }
63         /*If avaialalbe free memory in MMC is less than required size + 5MB , return error */
64         if ((size + PKG_BUF_SIZE + MEM_BUF_SIZE) > free_mmc_mem) {
65                 app2ext_print("Insufficient memory in MMC for application installation %d\n", ret);
66                 return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
67         }
68         /*Create a loopback device */
69         ret = _app2sd_create_loopback_device(pkgid, (size+PKG_BUF_SIZE));
70         if (ret) {
71                 app2ext_print("App2Sd Error : Package already present\n");
72                 char buf_dir[FILENAME_MAX] = { 0, };
73                 memset((void *)&buf_dir, '\0', FILENAME_MAX);
74                 snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
75                 ret = _app2sd_delete_directory(buf_dir);
76                 if (ret) {
77                         app2ext_print
78                                 ("App2Sd Error : Unable to delete the directory %s\n",
79                                  buf_dir);
80                 }
81         }
82         /*Perform Loopback encryption setup */
83         device_node = _app2sd_do_loopback_encryption_setup(pkgid);
84         if (!device_node) {
85                 app2ext_print("App2Sd Error : Loopback encryption setup failed\n");
86                 _app2sd_delete_loopback_device(pkgid);
87                 return APP2EXT_ERROR_DO_LOSETUP;
88         }
89         /*Check whether loopback device is associated with device node or not */
90         devi = _app2sd_find_associated_device_node(pkgid);
91         if (devi == NULL) {
92                 app2ext_print("App2Sd Error : finding associated device node failed\n");
93                 ret = APP2EXT_ERROR_DO_LOSETUP;
94                 goto FINISH_OFF;
95         }
96
97         /*Format the loopback file system */
98         ret = _app2sd_create_file_system(device_node);
99         if (ret) {
100                 app2ext_print("App2Sd Error : creating FS failed failed\n");
101                 ret = APP2EXT_ERROR_CREATE_FS;
102                 goto FINISH_OFF;
103         }
104
105         /*Mount the loopback encrypted pseudo device on application installation path as with Read Write permission */
106         ret =_app2sd_mount_app_content(pkgid, device_node, MOUNT_TYPE_RW,
107                                         dir_list, APP2SD_PRE_INSTALL);
108         if (ret) {
109                 app2ext_print("App2Sd Error : mounting dev path to app install path failed\n");
110                 ret = APP2EXT_ERROR_MOUNT_PATH;
111                 goto FINISH_OFF;
112         }
113
114         /*Success */
115         ret = APP2EXT_SUCCESS;
116         goto END;
117
118 FINISH_OFF:
119
120         if (device_node) {
121                 result = _app2sd_detach_loop_device(device_node);
122                 if (result) {
123                         free(result);
124                         result = NULL;
125                 }
126                 _app2sd_delete_loopback_device(pkgid);
127         }
128 END:
129         if (device_node) {
130                 free(device_node);
131                 device_node = NULL;
132         }
133         if (devi) {
134                 free(devi);
135                 devi = NULL;
136         }
137         return ret;
138 }
139
140 int app2sd_post_app_install(const char *pkgid,
141                         app2ext_status install_status)
142 {
143         char *device_name = NULL;
144         char buf_dir[FILENAME_MAX] = { 0, };
145         int ret = APP2EXT_SUCCESS;
146         /*Validate the function parameter recieved */
147         if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
148                 || install_status > APP2EXT_STATUS_SUCCESS) {
149                 app2ext_print("Invalid func parameters\n");
150                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
151         }
152
153         /*Check whether MMC is present or not */
154         ret = _app2sd_check_mmc_status();
155         if (ret) {
156                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
157                              ret);
158                 return APP2EXT_ERROR_MMC_STATUS;
159         }
160         sync(); //2
161         /*Get the associated device node for SD card applicationer */
162         device_name = _app2sd_find_associated_device_node(pkgid);
163         if (NULL == device_name) {
164                 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
165         }
166         ret = _app2sd_unmount_app_content(pkgid);
167         if (ret) {
168                 if (device_name) {
169                         free(device_name);
170                         device_name = NULL;
171                 }
172                 app2ext_print("Unable to unmount the app content %d\n", ret);
173                 return APP2EXT_ERROR_UNMOUNT;
174         }
175         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
176         if (ret) {
177                 if (device_name) {
178                         free(device_name);
179                         device_name = NULL;
180                 }
181                 app2ext_print
182                     ("Unable to Detach the loopback encryption setup for the application");
183                 return APP2EXT_ERROR_UNMOUNT;
184         }
185         if (device_name) {
186                 free(device_name);
187                 device_name = NULL;
188         }
189
190         /*Take appropriate action based on installation
191         status of application package */
192         if (install_status == APP2EXT_STATUS_FAILED) {
193                 /*Delete the loopback device from the SD card */
194                 ret = _app2sd_delete_loopback_device(pkgid);
195                 if (ret) {
196                         app2ext_print
197                             ("App2Sd Error : Unable to delete the loopback device from the SD Card\n");
198                         return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
199                 }
200                 ret = _app2sd_remove_password_from_db(pkgid);
201
202                 if (ret) {
203                         app2ext_print
204                             ("App2Sd Error : Unable to delete the password\n");
205                 }
206
207                 snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
208
209                 ret = _app2sd_delete_directory(buf_dir);
210
211                 if (ret) {
212                         app2ext_print
213                             ("App2Sd Error : Unable to delete the directory %s\n",
214                              buf_dir);
215                 }
216
217         } else {
218                 /*If  the status is success, then update installed storage to pkgmgr_parser db*/
219                 int rt = 0;
220                 rt = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
221                 if (rt < 0) {
222                         app2ext_print("fail to update installed location to db[%s, %s]\n", pkgid, INSTALL_EXTERNAL);
223                 }
224         }
225         return ret;
226 }
227
228 int app2sd_on_demand_setup_init(const char *pkgid)
229 {
230         int ret = APP2EXT_SUCCESS;
231         char app_path[FILENAME_MAX] = { 0, };
232         char *device_node = NULL;
233         char *result = NULL;
234         FILE *fp = NULL;
235
236         /*Validate the function parameter recieved */
237         if (pkgid == NULL) {
238                 app2ext_print
239                     ("App2Sd Error : Invalid function arguments to app launch setup\n");
240                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
241         }
242
243         /*Check whether MMC is present or not */
244         ret = _app2sd_check_mmc_status();
245         if (ret) {
246                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
247                              ret);
248                 return APP2EXT_ERROR_MMC_STATUS;
249         }
250
251         /*check app entry is there in sd card or not. */
252         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
253                  pkgid);
254         fp = fopen(app_path, "r+");
255         if (fp == NULL) {
256                 app2ext_print
257                     ("App2SD Error: App Entry is not present in SD Card\n");
258                 return APP2EXT_ERROR_INVALID_PACKAGE;
259         }
260         fclose(fp);
261         result = (char *)_app2sd_find_associated_device(app_path);
262         /*process the string */
263         if ((result!=NULL) && strstr(result, "/dev") != NULL) {
264                 app2ext_print("App2SD Error! Already associated\n");
265                 free(result);
266                 result = NULL;
267                 return APP2EXT_ERROR_ALREADY_MOUNTED;
268         }
269
270         /*Do loopback setup */
271         device_node = _app2sd_do_loopback_encryption_setup(pkgid);
272         if (device_node == NULL) {
273                 app2ext_print
274                     ("App2Sd Error : loopback encryption setup failed\n");
275                 return APP2EXT_ERROR_DO_LOSETUP;
276         }
277
278         /*Do  mounting */
279         ret =
280             _app2sd_mount_app_content(pkgid, device_node, MOUNT_TYPE_RD,
281                                 NULL, APP2SD_APP_LAUNCH);
282         if (ret) {
283                 app2ext_print("App2Sd Error : Re-mount failed\n");
284                 if (device_node) {
285                         free(device_node);
286                         device_node = NULL;
287                 }
288                 return APP2EXT_ERROR_MOUNT_PATH;
289         }
290         if (device_node) {
291                 free(device_node);
292                 device_node = NULL;
293         }
294         return ret;
295 }
296
297 int app2sd_on_demand_setup_exit(const char *pkgid)
298 {
299         int ret = APP2EXT_SUCCESS;
300         char app_path[FILENAME_MAX] = { 0, };
301         FILE *fp = NULL;
302
303         /*Validate the function parameter recieved */
304         if (pkgid == NULL) {
305                 app2ext_print
306                     ("App2Sd Error : Invalid function arguments to app launch setup\n");
307                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
308         }
309
310         /*Check whether MMC is present or not */
311         ret = _app2sd_check_mmc_status();
312         if (ret) {
313                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
314                              ret);
315                 return APP2EXT_ERROR_MMC_STATUS;
316         }
317         /*check app entry is there in sd card or not. */
318         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
319                  pkgid);
320         fp = fopen(app_path, "r+");
321         if (fp == NULL) {
322                 app2ext_print
323                     ("App2SD Error: App Entry is not present in SD Card\n");
324                 return APP2EXT_ERROR_INVALID_PACKAGE;
325         }
326         fclose(fp);
327         ret = _app2sd_unmount_app_content(pkgid);
328         if (ret) {
329                 app2ext_print
330                     ("App2SD Error: Unable to unmount the SD application\n");
331                 return APP2EXT_ERROR_UNMOUNT;
332         }
333         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
334         if (ret) {
335                 app2ext_print("App2SD Error: Unable to remove loopback setup\n");
336                 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
337         }
338         return ret;
339 }
340
341 int app2sd_pre_app_uninstall(const char *pkgid)
342 {
343         int ret = APP2EXT_SUCCESS;
344         char app_path[FILENAME_MAX] = { 0, };
345         char *device_node = NULL;
346         FILE*fp = NULL;
347
348         /*Validate the function parameter recieved */
349         if (pkgid == NULL) {
350                 app2ext_print
351                     ("App2Sd Error : Invalid function arguments to app launch setup\n");
352                 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
353                 goto END;
354         }
355         /*Check whether MMC is present or not */
356         ret = _app2sd_check_mmc_status();
357         if (ret) {
358                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
359                              ret);
360                 ret = APP2EXT_ERROR_MMC_STATUS;
361                 goto END;
362         }
363         /*check app entry is there in sd card or not. */
364         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
365         fp = fopen(app_path, "r+");
366         if (fp == NULL) {
367                 app2ext_print
368                     ("App2SD Error: App Entry is not present in SD Card\n");
369                 ret = APP2EXT_ERROR_INVALID_PACKAGE;
370                 goto END;
371         }
372         fclose(fp);
373
374         /*Get the associated device node for SD card applicationer */
375         device_node = _app2sd_find_associated_device_node(pkgid);
376         if (NULL == device_node) {
377                 /*Do loopback setup */
378                 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
379
380                 if (device_node == NULL) {
381                         app2ext_print
382                             ("App2Sd Error : loopback encryption setup failed\n");
383                         ret = APP2EXT_ERROR_DO_LOSETUP;
384                         goto END;
385                 }
386                 /*Do  mounting */
387                 ret =
388                     _app2sd_mount_app_content(pkgid, device_node,
389                                         MOUNT_TYPE_RW, NULL,
390                                         APP2SD_PRE_UNINSTALL);
391
392                 if (ret) {
393                         app2ext_print("App2Sd Error : RW-mount failed\n");
394                         if (device_node) {
395                                 free(device_node);
396                                 device_node = NULL;
397                         }
398                         ret = APP2EXT_ERROR_MOUNT_PATH;
399                         goto END;
400                 }
401         } else {
402                 /*Do  re-mounting */
403                 ret =
404                     _app2sd_mount_app_content(pkgid, device_node,
405                                         MOUNT_TYPE_RW_REMOUNT, NULL,
406                                         APP2SD_PRE_UNINSTALL);
407
408                 if (ret) {
409                         app2ext_print("App2Sd Error : Re-mount failed\n");
410                         if (device_node) {
411                                 free(device_node);
412                                 device_node = NULL;
413                         }
414                         ret = APP2EXT_ERROR_MOUNT_PATH;
415                         goto END;
416                 }
417         }
418         if (device_node) {
419                 free(device_node);
420                 device_node = NULL;
421         }
422
423 END:
424         if (ret != APP2EXT_SUCCESS)
425                 app2ext_print("App2Sd Error : app2sd has [%d]error, but return success for uninstallation\n", ret);
426         return ret;
427 }
428
429 /*
430 * app2sd_post_app_uninstall_setup
431 * Uninstall Application and free all the allocated resources
432 * Called after dpkg remove, It deallocates dev node and loopback
433 */
434 int app2sd_post_app_uninstall(const char *pkgid)
435 {
436         char buf_dir[FILENAME_MAX] = { 0, };
437         int ret = APP2EXT_SUCCESS;
438         int ret1 = APP2EXT_SUCCESS;
439         /*Validate the function parameter recieved */
440         if (pkgid == NULL) {
441                 app2ext_print
442                     ("App2Sd Error : Invalid function arguments to Post Uninstall\n");
443                 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
444                 goto END;
445         }
446         /*Check whether MMC is present or not */
447         ret = _app2sd_check_mmc_status();
448         if (ret) {
449                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
450                              ret);
451                 ret = APP2EXT_ERROR_MMC_STATUS;
452                 goto END;
453         }
454         /*Unmount the loopback encrypted pseudo device from the application installation path */
455         ret = _app2sd_unmount_app_content(pkgid);
456         if (ret) {
457                 app2ext_print("Unable to unmount the app content %d\n", ret);
458                 ret = APP2EXT_ERROR_UNMOUNT;
459                 goto END;
460         }
461         /*Detach the loopback encryption setup for the application */
462         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
463         if (ret) {
464                 app2ext_print
465                     ("Unable to Detach the loopback encryption setup for the application");
466                 ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
467                 goto END;
468         }
469         /*Delete the loopback device from the SD card */
470         ret = _app2sd_delete_loopback_device(pkgid);
471         if (ret) {
472                 app2ext_print
473                     ("App2Sd Error : Unable to delete the loopback device from the SD Card\n");
474                 ret =  APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
475                 goto END;
476         }
477         memset((void *)&buf_dir, '\0', FILENAME_MAX);
478         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
479         ret1 = _app2sd_delete_directory(buf_dir);
480         if (ret1) {
481                 app2ext_print
482                     ("App2Sd Error : Unable to delete the directory %s\n",
483                      buf_dir);
484         }
485         /*remove encryption password from DB */
486         ret = _app2sd_initialize_db();
487         if (ret) {
488                 app2ext_print("\n app2sd db initialize failed");
489                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
490                 goto END;
491         }
492         ret = _app2sd_remove_password_from_db(pkgid);
493         if (ret) {
494                 app2ext_print("cannot remove password from db \n");
495                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
496                 goto END;
497         }
498
499 END:
500         if (ret != APP2EXT_SUCCESS)
501                 app2ext_print("App2Sd Error : app2sd has [%d]error, but return success for uninstallation\n", ret);
502         return ret;
503 }
504
505 int app2sd_move_installed_app(const char *pkgid, GList* dir_list,
506                         app2ext_move_type move_type)
507 {
508         int ret = 0;
509         int pkgmgrinfo_ret = 0;
510
511         /*Validate function arguments*/
512         if (pkgid == NULL || dir_list == NULL
513                 || move_type < APP2EXT_MOVE_TO_EXT
514                 || move_type > APP2EXT_MOVE_TO_PHONE) {
515                 app2ext_print("App2Sd Error : Invalid function arguments\n");
516                 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
517                 goto END;
518         }
519
520         /*If  move is completed, then update installed storage to pkgmgr_parser db*/
521         pkgmgrinfo_pkginfo_h info_handle = NULL;
522         pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
523         pkgmgrinfo_ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &info_handle);
524         if (pkgmgrinfo_ret < 0) {
525                 app2ext_print("App2Sd Error : pkgmgrinfo_pkginfo_get_pkginfo[%s] fail.. \n", pkgid);
526         }
527         pkgmgrinfo_ret = pkgmgrinfo_pkginfo_get_installed_storage(info_handle, &storage);
528         if (pkgmgrinfo_ret < 0) {
529                 app2ext_print("App2Sd Error : pkgmgrinfo_pkginfo_get_installed_storage[%s] fail.. \n", pkgid);
530         }
531
532         if ((move_type == APP2EXT_MOVE_TO_EXT && storage == PMINFO_EXTERNAL_STORAGE)
533                 || (move_type == APP2EXT_MOVE_TO_PHONE && storage == PMINFO_INTERNAL_STORAGE)) {
534                         ret = APP2EXT_ERROR_PKG_EXISTS;
535                         app2ext_print("App2Sd Error : PKG_EXISTS in [%d]STORAGE\n", storage);
536                         pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
537                         goto END;
538         } else {
539                 app2ext_print("App2Sd info : pkgid[%s] move to STORAGE[%d]\n", pkgid, storage);
540         }
541         pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
542
543         ret = _app2sd_move_app(pkgid, move_type, dir_list);
544         if (ret) {
545                 app2ext_print("App2Sd Error : Unable to move application\n");
546                 goto END;
547         }
548
549         /*If  move is completed, then update installed storage to pkgmgr_parser db*/
550         if (move_type == APP2EXT_MOVE_TO_EXT) {
551                 pkgmgrinfo_ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
552                 if (pkgmgrinfo_ret < 0) {
553                         app2ext_print("App2Sd Error : fail to update installed location to db[%s, %s]\n", pkgid, INSTALL_EXTERNAL);
554                 }
555         } else {
556                 pkgmgrinfo_ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_INTERNAL);
557                 if (pkgmgrinfo_ret < 0) {
558                         app2ext_print("App2Sd Error : fail to update installed location to db[%s, %s]\n", pkgid, INSTALL_INTERNAL);
559                 }
560         }
561 END:
562
563 //      vconf_set_int(VCONFKEY_PKGMGR_STATUS, ret);
564         _app2sd_make_result_info_file(pkgid, ret);
565
566         return ret;
567 }
568
569 int app2sd_pre_app_upgrade(const char *pkgid, GList* dir_list,
570                         int size)
571 {
572         int ret = APP2EXT_SUCCESS;
573         char app_path[FILENAME_MAX] = { 0, };
574         char *device_node = NULL;
575         unsigned long long curr_size = 0;
576         FILE *fp = NULL;
577
578         /*Validate function arguments*/
579         if (pkgid == NULL || dir_list == NULL || size<=0) {
580                 app2ext_print
581                     ("App2Sd Error : Invalid function arguments \n");
582                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
583         }
584         /*Check whether MMC is present or not */
585         ret = _app2sd_check_mmc_status();
586         if (ret) {
587                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
588                              ret);
589                 return APP2EXT_ERROR_MMC_STATUS;
590         }
591         /*check app entry is there in sd card or not. */
592         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
593                  pkgid);
594         app2ext_print("App2Sd Log : Checking path %s\n", app_path);
595         fp = fopen(app_path, "r+");
596         if (fp == NULL) {
597                 app2ext_print
598                     ("App2SD Error: App Entry is not present in SD Card\n");
599                 return APP2EXT_ERROR_INVALID_PACKAGE;
600         }
601         fclose(fp);
602         /*Get installed app size*/
603         curr_size = _app2sd_calculate_file_size(app_path);
604         curr_size = (curr_size/1024)/1024;
605
606         if (curr_size==0) {
607                 app2ext_print
608                     ("App2SD Error: App Entry is not present in SD Card\n");
609                 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
610         }
611         if (curr_size<size) {
612                 ret = _app2sd_update_loopback_device_size(pkgid, size, dir_list);
613                 if(APP2EXT_SUCCESS !=ret) {
614                         app2ext_print
615                             ("App2SD Error: _app2sd_update_loopback_device_size() failed\n");
616                         return ret;
617                 }
618         }
619
620         /*Get the associated device node for SD card applicationer */
621         device_node = _app2sd_find_associated_device_node(pkgid);
622         if (NULL == device_node) {
623                 /*Do loopback setup */
624                 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
625                 if (device_node == NULL) {
626                         app2ext_print
627                             ("App2Sd Error : loopback encryption setup failed\n");
628                         return APP2EXT_ERROR_DO_LOSETUP;
629                 }
630                 /*Do  mounting */
631                 ret =
632                     _app2sd_mount_app_content(pkgid, device_node,
633                                         MOUNT_TYPE_RW, NULL,
634                                         APP2SD_PRE_UPGRADE);
635                 if (ret) {
636                         app2ext_print("App2Sd Error : Re-mount failed\n");
637                         if (device_node) {
638                                 free(device_node);
639                                 device_node = NULL;
640                         }
641                         return APP2EXT_ERROR_MOUNT_PATH;
642                 }
643         } else {
644                 /*Do  re-mounting */
645                 ret =
646                     _app2sd_mount_app_content(pkgid, device_node,
647                                               MOUNT_TYPE_RW_REMOUNT, NULL,
648                                               APP2SD_PRE_UPGRADE);
649                 if (ret) {
650                         app2ext_print("App2Sd Error : Re-mount failed\n");
651                         if (device_node) {
652                                 free(device_node);
653                                 device_node = NULL;
654                         }
655                         return APP2EXT_ERROR_MOUNT_PATH;
656                 }
657         }
658
659         if (device_node) {
660                 free(device_node);
661                 device_node = NULL;
662         }
663         return ret;
664 }
665
666
667 int app2sd_post_app_upgrade(const char *pkgid,
668                         app2ext_status install_status)
669 {
670         char *device_name = NULL;
671         int ret = APP2EXT_SUCCESS;
672         /*Validate the function parameter recieved */
673         if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
674                 || install_status > APP2EXT_STATUS_SUCCESS) {
675                 app2ext_print("Invalid func parameters\n");
676                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
677         }
678         /*Check whether MMC is present or not */
679         ret = _app2sd_check_mmc_status();
680         if (ret) {
681                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
682                              ret);
683                 return APP2EXT_ERROR_MMC_STATUS;
684         }
685
686         /*Get the associated device node for SD card applicationer */
687         device_name = _app2sd_find_associated_device_node(pkgid);
688         if (NULL == device_name) {
689                 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
690         }
691         ret = _app2sd_unmount_app_content(pkgid);
692         if (ret) {
693                 if (device_name) {
694                         free(device_name);
695                         device_name = NULL;
696                 }
697                 app2ext_print("Unable to unmount the app content %d\n", ret);
698                 return APP2EXT_ERROR_UNMOUNT;
699         }
700         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
701         if (ret) {
702                 if (device_name) {
703                         free(device_name);
704                         device_name = NULL;
705                 }
706                 app2ext_print
707                     ("Unable to Detach the loopback encryption setup for the application");
708                 return APP2EXT_ERROR_UNMOUNT;
709         }
710         if (device_name) {
711                 free(device_name);
712                 device_name = NULL;
713         }
714         return ret;
715 }
716
717 #if 0
718 /**
719  * Reserved API for forced cleanup
720  *
721  */
722 int app2sd_force_cleanup(const char *pkgid){
723         char *device_name = NULL;
724         char buf_dir[FILENAME_MAX] = { 0, };
725         int ret = APP2EXT_SUCCESS;
726         FILE *fp = NULL;
727
728         /*Validate the function parameter recieved */
729         if (pkgid == NULL) {
730                 app2ext_print("invalid func parameters\n");
731                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
732         }
733         memset((void *)&buf_dir, '\0', FILENAME_MAX);
734         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
735         fp = fopen(buf_dir, "r+");
736         if (fp == NULL) {
737                 app2ext_print("\"%s\" not installed on SD Card\n", pkgid);
738                 return APP2EXT_ERROR_INVALID_PACKAGE;
739         }
740         fclose(fp);
741         /*Check whether MMC is present or not */
742         ret = _app2sd_check_mmc_status();
743         if (ret) {
744                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
745                              ret);
746                 return APP2EXT_ERROR_MMC_STATUS;
747         }
748
749         /*Get the associated device node for SD card applicationer */
750         device_name = _app2sd_find_associated_device_node(pkgid);
751         if (NULL != device_name) {
752                 free(device_name);
753                 device_name = NULL;
754                 ret = _app2sd_unmount_app_content(pkgid);
755                 if (ret) {
756                         app2ext_print("Unable to unmount the app content %d\n", ret);
757                         return APP2EXT_ERROR_UNMOUNT;
758                 }
759                 ret = _app2sd_remove_loopback_encryption_setup(pkgid);
760                 if (ret) {
761                         app2ext_print
762                             ("Unable to Detach the loopback encryption setup for the application");
763                         return APP2EXT_ERROR_UNMOUNT;
764                 }
765         }
766
767         memset((void *)&buf_dir, '\0', FILENAME_MAX);
768         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
769         ret = _app2sd_delete_directory(buf_dir);
770         if (ret) {
771                 app2ext_print
772                     ("App2Sd Error : Unable to delete the directory %s\n",
773                      buf_dir);
774         }
775
776         /*remove passwrd from DB*/
777         ret = _app2sd_initialize_db();
778         if (ret) {
779                 app2ext_print("\n app2sd db initialize failed");
780                 return APP2EXT_ERROR_SQLITE_REGISTRY;
781         }
782         ret = _app2sd_remove_password_from_db(pkgid);
783         if (ret) {
784                 app2ext_print("cannot remove password from db \n");
785                 return APP2EXT_ERROR_SQLITE_REGISTRY;
786         }
787         return ret;
788 }
789 #endif
790
791 int app2sd_force_clean(const char *pkgid)
792 {
793         char buf_dir[FILENAME_MAX] = { 0, };
794         int ret = APP2EXT_SUCCESS;
795
796         /*Validate the function parameter recieved */
797         if (pkgid == NULL) {
798                 app2ext_print("Invalid func parameters\n");
799                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
800         }
801         app2ext_print("star force_clean [%s]", pkgid);
802
803         sync(); //2
804         /*Unmount the loopback encrypted pseudo device from the application installation path */
805         ret = _app2sd_unmount_app_content(pkgid);
806         if (ret) {
807                 app2ext_print("Unable to unmount the app content %d\n", ret);
808         }
809
810         /*Detach the loopback encryption setup for the application */
811         ret = _app2sd_remove_all_loopback_encryption_setups(pkgid);
812         if (ret) {
813                 app2ext_print("Unable to Detach the loopback encryption setup for the application");
814         }
815
816         /*Delete the loopback device from the SD card */
817         ret = _app2sd_delete_loopback_device(pkgid);
818         if (ret) {
819                 app2ext_print("Unable to Detach the loopback encryption setup for the application");
820         }
821
822         /*Delete symlink*/
823         memset((void *)&buf_dir, '\0', FILENAME_MAX);
824         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
825
826         _app2sd_delete_symlink(buf_dir);
827
828         /*remove passwrd from DB*/
829         ret = _app2sd_initialize_db();
830         if (ret) {
831                 app2ext_print("\n app2sd db initialize failed");
832         }
833         ret = _app2sd_remove_password_from_db(pkgid);
834         if (ret) {
835                 app2ext_print("cannot remove password from db \n");
836         }
837
838         app2ext_print("finish force_clean");
839         return 0;
840 }
841
842 /* This is the plug-in load function. The plugin has to bind its functions to function pointers of handle
843         @param[in/out]          st_interface    Specifies the storage interface.
844 */
845 void
846 app2ext_on_load(app2ext_interface *st_interface)
847 {
848         /*Plug-in Binding.*/
849         st_interface->pre_install= app2sd_pre_app_install;
850         st_interface->post_install= app2sd_post_app_install;
851         st_interface->pre_uninstall= app2sd_pre_app_uninstall;
852         st_interface->post_uninstall= app2sd_post_app_uninstall;
853         st_interface->pre_upgrade= app2sd_pre_app_upgrade;
854         st_interface->post_upgrade= app2sd_post_app_upgrade;
855         st_interface->move= app2sd_move_installed_app;
856         st_interface->force_clean= app2sd_force_clean;
857         st_interface->enable= app2sd_on_demand_setup_init;
858         st_interface->disable= app2sd_on_demand_setup_exit;
859 }
860