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