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