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