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