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