Add multi-user support
[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 /* For multi-user support */
33 #include <tzplatform_config.h>
34
35 #define MAX_BUF_LEN     1024
36 #define APP2SD_TMP_PATH tzplatform_mkpath(TZ_USER_APP, "tmp")
37
38 int app2sd_pre_app_install(const char *pkgid, GList* dir_list,
39                                 int size)
40 {
41         int ret = 0;
42         int free_mmc_mem = 0;
43         char *device_node = NULL;
44         char *devi = NULL;
45         char *result = NULL;
46
47         /*Validate the function parameter recieved */
48         if (pkgid == NULL || dir_list == NULL || size <= 0) {
49                 app2ext_print("App2Sd Error : Invalid function arguments\n");
50                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
51         }
52         /*Check whether MMC is present or not */
53         ret = _app2sd_check_mmc_status();
54         if (ret) {
55                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
56                         ret);
57                 return APP2EXT_ERROR_MMC_STATUS;
58         }
59         /*Find available free memory in the MMC card */
60         ret = _app2sd_get_available_free_memory(MMC_PATH,
61                                                 &free_mmc_mem);
62         if (ret) {
63                 app2ext_print("App2Sd Error : Unable to get available free memory in MMC %d\n", ret);
64                 return APP2EXT_ERROR_MMC_STATUS;
65         }
66         /*If avaialalbe free memory in MMC is less than required size + 5MB , return error */
67         if ((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, (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                 pkgmgrinfo_pkgdbinfo_h handle = NULL;
224                 rt = pkgmgrinfo_create_pkgdbinfo(pkgid, &handle);
225                 if (rt < 0) {
226                         app2ext_print("pkgmgrinfo_create_pkgdbinfo[%s] fail.. \n", pkgid);
227                 }
228                 rt = pkgmgrinfo_set_installed_storage_to_pkgdbinfo(handle, INSTALL_EXTERNAL);
229                 if (rt < 0) {
230                         app2ext_print("fail to update installed location to db[%s, %s]\n", pkgid, INSTALL_EXTERNAL);
231                 }
232                 rt =pkgmgrinfo_save_pkgdbinfo(handle);
233                 if (rt < 0) {
234                         app2ext_print("pkgmgrinfo_save_pkgdbinfo[%s] failed\n", pkgid);
235                 }
236                 rt =pkgmgrinfo_destroy_pkgdbinfo(handle);
237                 if (rt < 0) {
238                         app2ext_print("pkgmgrinfo_destroy_pkgdbinfo[%s] failed\n", pkgid);
239                 }
240         }
241         return ret;
242 }
243
244 int app2sd_on_demand_setup_init(const char *pkgid)
245 {
246         int ret = APP2EXT_SUCCESS;
247         char app_path[FILENAME_MAX] = { 0, };
248         char *device_node = NULL;
249         char *result = NULL;
250         FILE *fp = NULL;
251
252         /*Validate the function parameter recieved */
253         if (pkgid == NULL) {
254                 app2ext_print
255                     ("App2Sd Error : Invalid function arguments to app launch setup\n");
256                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
257         }
258
259         /*Check whether MMC is present or not */
260         ret = _app2sd_check_mmc_status();
261         if (ret) {
262                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
263                              ret);
264                 return APP2EXT_ERROR_MMC_STATUS;
265         }
266
267         /*check app entry is there in sd card or not. */
268         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
269                  pkgid);
270         fp = fopen(app_path, "r+");
271         if (fp == NULL) {
272                 app2ext_print
273                     ("App2SD Error: App Entry is not present in SD Card\n");
274                 return APP2EXT_ERROR_INVALID_PACKAGE;
275         }
276         fclose(fp);
277         result = (char *)_app2sd_find_associated_device(app_path);
278         /*process the string */
279         if ((result!=NULL) && strstr(result, "/dev") != NULL) {
280                 app2ext_print("App2SD Error! Already associated\n");
281                 free(result);
282                 result = NULL;
283                 return APP2EXT_ERROR_ALREADY_MOUNTED;
284         }
285
286         /*Do loopback setup */
287         device_node = _app2sd_do_loopback_encryption_setup(pkgid);
288         if (device_node == NULL) {
289                 app2ext_print
290                     ("App2Sd Error : loopback encryption setup failed\n");
291                 return APP2EXT_ERROR_DO_LOSETUP;
292         }
293
294         /*Do  mounting */
295         ret =
296             _app2sd_mount_app_content(pkgid, device_node, MOUNT_TYPE_RD,
297                                 NULL, APP2SD_APP_LAUNCH);
298         if (ret) {
299                 app2ext_print("App2Sd Error : Re-mount failed\n");
300                 if (device_node) {
301                         free(device_node);
302                         device_node = NULL;
303                 }
304                 return APP2EXT_ERROR_MOUNT_PATH;
305         }
306         if (device_node) {
307                 free(device_node);
308                 device_node = NULL;
309         }
310         return ret;
311 }
312
313 int app2sd_on_demand_setup_exit(const char *pkgid)
314 {
315         int ret = APP2EXT_SUCCESS;
316         char app_path[FILENAME_MAX] = { 0, };
317         FILE *fp = NULL;
318
319         /*Validate the function parameter recieved */
320         if (pkgid == NULL) {
321                 app2ext_print
322                     ("App2Sd Error : Invalid function arguments to app launch setup\n");
323                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
324         }
325
326         /*Check whether MMC is present or not */
327         ret = _app2sd_check_mmc_status();
328         if (ret) {
329                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
330                              ret);
331                 return APP2EXT_ERROR_MMC_STATUS;
332         }
333         /*check app entry is there in sd card or not. */
334         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
335                  pkgid);
336         fp = fopen(app_path, "r+");
337         if (fp == NULL) {
338                 app2ext_print
339                     ("App2SD Error: App Entry is not present in SD Card\n");
340                 return APP2EXT_ERROR_INVALID_PACKAGE;
341         }
342         fclose(fp);
343         ret = _app2sd_unmount_app_content(pkgid);
344         if (ret) {
345                 app2ext_print
346                     ("App2SD Error: Unable to unmount the SD application\n");
347                 return APP2EXT_ERROR_UNMOUNT;
348         }
349         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
350         if (ret) {
351                 app2ext_print("App2SD Error: Unable to remove loopback setup\n");
352                 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
353         }
354         return ret;
355 }
356
357 int app2sd_pre_app_uninstall(const char *pkgid)
358 {
359         int ret = APP2EXT_SUCCESS;
360         char app_path[FILENAME_MAX] = { 0, };
361         char *device_node = NULL;
362         FILE*fp = NULL;
363
364         /*Validate the function parameter recieved */
365         if (pkgid == NULL) {
366                 app2ext_print
367                     ("App2Sd Error : Invalid function arguments to app launch setup\n");
368                 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
369                 goto END;
370         }
371         /*Check whether MMC is present or not */
372         ret = _app2sd_check_mmc_status();
373         if (ret) {
374                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
375                              ret);
376                 ret = APP2EXT_ERROR_MMC_STATUS;
377                 goto END;
378         }
379         /*check app entry is there in sd card or not. */
380         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
381         fp = fopen(app_path, "r+");
382         if (fp == NULL) {
383                 app2ext_print
384                     ("App2SD Error: App Entry is not present in SD Card\n");
385                 ret = APP2EXT_ERROR_INVALID_PACKAGE;
386                 goto END;
387         }
388         fclose(fp);
389
390         /*Get the associated device node for SD card applicationer */
391         device_node = _app2sd_find_associated_device_node(pkgid);
392         if (NULL == device_node) {
393                 /*Do loopback setup */
394                 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
395
396                 if (device_node == NULL) {
397                         app2ext_print
398                             ("App2Sd Error : loopback encryption setup failed\n");
399                         ret = APP2EXT_ERROR_DO_LOSETUP;
400                         goto END;
401                 }
402                 /*Do  mounting */
403                 ret =
404                     _app2sd_mount_app_content(pkgid, device_node,
405                                         MOUNT_TYPE_RW, NULL,
406                                         APP2SD_PRE_UNINSTALL);
407
408                 if (ret) {
409                         app2ext_print("App2Sd Error : RW-mount failed\n");
410                         if (device_node) {
411                                 free(device_node);
412                                 device_node = NULL;
413                         }
414                         ret = APP2EXT_ERROR_MOUNT_PATH;
415                         goto END;
416                 }
417         } else {
418                 /*Do  re-mounting */
419                 ret =
420                     _app2sd_mount_app_content(pkgid, device_node,
421                                         MOUNT_TYPE_RW_REMOUNT, NULL,
422                                         APP2SD_PRE_UNINSTALL);
423
424                 if (ret) {
425                         app2ext_print("App2Sd Error : Re-mount failed\n");
426                         if (device_node) {
427                                 free(device_node);
428                                 device_node = NULL;
429                         }
430                         ret = APP2EXT_ERROR_MOUNT_PATH;
431                         goto END;
432                 }
433         }
434         if (device_node) {
435                 free(device_node);
436                 device_node = NULL;
437         }
438
439 END:
440         if (ret != APP2EXT_SUCCESS)
441                 app2ext_print("App2Sd Error : app2sd has [%d]error, but return success for uninstallation\n", ret);
442         return APP2EXT_SUCCESS;
443 }
444
445 /*
446 * app2sd_post_app_uninstall_setup
447 * Uninstall Application and free all the allocated resources
448 * Called after dpkg remove, It deallocates dev node and loopback
449 */
450 int app2sd_post_app_uninstall(const char *pkgid)
451 {
452         char buf_dir[FILENAME_MAX] = { 0, };
453         int ret = APP2EXT_SUCCESS;
454         int ret1 = APP2EXT_SUCCESS;
455         /*Validate the function parameter recieved */
456         if (pkgid == NULL) {
457                 app2ext_print
458                     ("App2Sd Error : Invalid function arguments to Post Uninstall\n");
459                 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
460                 goto END;
461         }
462         /*Check whether MMC is present or not */
463         ret = _app2sd_check_mmc_status();
464         if (ret) {
465                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
466                              ret);
467                 ret = APP2EXT_ERROR_MMC_STATUS;
468                 goto END;
469         }
470         /*Unmount the loopback encrypted pseudo device from the application installation path */
471         ret = _app2sd_unmount_app_content(pkgid);
472         if (ret) {
473                 app2ext_print("Unable to unmount the app content %d\n", ret);
474                 ret = APP2EXT_ERROR_UNMOUNT;
475                 goto END;
476         }
477         /*Detach the loopback encryption setup for the application */
478         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
479         if (ret) {
480                 app2ext_print
481                     ("Unable to Detach the loopback encryption setup for the application");
482                 ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
483                 goto END;
484         }
485         /*Delete the loopback device from the SD card */
486         ret = _app2sd_delete_loopback_device(pkgid);
487         if (ret) {
488                 app2ext_print
489                     ("App2Sd Error : Unable to delete the loopback device from the SD Card\n");
490                 ret =  APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
491                 goto END;
492         }
493         memset((void *)&buf_dir, '\0', FILENAME_MAX);
494         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
495         ret1 = _app2sd_delete_directory(buf_dir);
496         if (ret1) {
497                 app2ext_print
498                     ("App2Sd Error : Unable to delete the directory %s\n",
499                      buf_dir);
500         }
501         /*remove encryption password from DB */
502         ret = _app2sd_initialize_db();
503         if (ret) {
504                 app2ext_print("\n app2sd db initialize failed");
505                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
506                 goto END;
507         }
508         ret = _app2sd_remove_password_from_db(pkgid);
509         if (ret) {
510                 app2ext_print("cannot remove password from db \n");
511                 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
512                 goto END;
513         }
514
515 END:
516         if (ret != APP2EXT_SUCCESS)
517                 app2ext_print("App2Sd Error : app2sd has [%d]error, but return success for uninstallation\n", ret);
518         return APP2EXT_SUCCESS;
519 }
520
521 int app2sd_move_installed_app(const char *pkgid, GList* dir_list,
522                         app2ext_move_type move_type)
523 {
524         int ret = 0;
525         int pkgmgrinfo_ret = 0;
526
527         /*Validate function arguments*/
528         if (pkgid == NULL || dir_list == NULL
529                 || move_type < APP2EXT_MOVE_TO_EXT
530                 || move_type > APP2EXT_MOVE_TO_PHONE) {
531                 app2ext_print("App2Sd Error : Invalid function arguments\n");
532                 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
533                 goto END;
534         }
535
536         /*If  move is completed, then update installed storage to pkgmgr_parser db*/
537         pkgmgrinfo_pkginfo_h info_handle = NULL;
538         pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
539         pkgmgrinfo_ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &info_handle);
540         if (pkgmgrinfo_ret < 0) {
541                 app2ext_print("App2Sd Error : pkgmgrinfo_pkginfo_get_pkginfo[%s] fail.. \n", pkgid);
542         }
543         pkgmgrinfo_ret = pkgmgrinfo_pkginfo_get_installed_storage(info_handle, &storage);
544         if (pkgmgrinfo_ret < 0) {
545                 app2ext_print("App2Sd Error : pkgmgrinfo_pkginfo_get_installed_storage[%s] fail.. \n", pkgid);
546         }
547
548         if ((move_type == APP2EXT_MOVE_TO_EXT && storage == PMINFO_EXTERNAL_STORAGE)
549                 || (move_type == APP2EXT_MOVE_TO_PHONE && storage == PMINFO_INTERNAL_STORAGE)) {
550                         ret = APP2EXT_ERROR_PKG_EXISTS;
551                         app2ext_print("App2Sd Error : PKG_EXISTS in [%d]STORAGE\n", storage);
552                         pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
553                         goto END;
554         } else {
555                 app2ext_print("App2Sd info : STORAGE Move[%d] is success\n", storage);
556         }
557         pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
558
559         ret = _app2sd_move_app(pkgid, move_type, dir_list);
560         if (ret) {
561                 app2ext_print("App2Sd Error : Unable to move application\n");
562                 goto END;
563         }
564
565         /*If  move is completed, then update installed storage to pkgmgr_parser db*/
566         pkgmgrinfo_pkgdbinfo_h handle = NULL;
567         pkgmgrinfo_ret = pkgmgrinfo_create_pkgdbinfo(pkgid, &handle);
568         if (pkgmgrinfo_ret < 0) {
569                 app2ext_print("App2Sd Error : pkgmgrinfo_create_pkgdbinfo[%s] fail.. \n", pkgid);
570         }
571
572         if (move_type == APP2EXT_MOVE_TO_EXT) {
573                 pkgmgrinfo_ret = pkgmgrinfo_set_installed_storage_to_pkgdbinfo(handle, INSTALL_EXTERNAL);
574                 if (pkgmgrinfo_ret < 0) {
575                         app2ext_print("App2Sd Error : fail to update installed location to db[%s, %s]\n", pkgid, INSTALL_EXTERNAL);
576                 }
577         } else {
578                 pkgmgrinfo_ret = pkgmgrinfo_set_installed_storage_to_pkgdbinfo(handle, INSTALL_INTERNAL);
579                 if (pkgmgrinfo_ret < 0) {
580                         app2ext_print("App2Sd Error : fail to update installed location to db[%s, %s]\n", pkgid, INSTALL_INTERNAL);
581                 }
582         }
583         pkgmgrinfo_ret =pkgmgrinfo_save_pkgdbinfo(handle);
584         if (pkgmgrinfo_ret < 0) {
585                 app2ext_print("pkgmgrinfo_save_pkgdbinfo[%s] failed\n", pkgid);
586         }
587         pkgmgrinfo_ret =pkgmgrinfo_destroy_pkgdbinfo(handle);
588         if (pkgmgrinfo_ret < 0) {
589                 app2ext_print("pkgmgrinfo_destroy_pkgdbinfo failed\n");
590         }
591
592 END:
593
594         vconf_set_int(VCONFKEY_PKGMGR_STATUS, ret);
595
596         return ret;
597 }
598
599 int app2sd_pre_app_upgrade(const char *pkgid, GList* dir_list,
600                         int size)
601 {
602         int ret = APP2EXT_SUCCESS;
603         char app_path[FILENAME_MAX] = { 0, };
604         char *device_node = NULL;
605         unsigned long long curr_size = 0;
606         FILE *fp = NULL;
607
608         /*Validate function arguments*/
609         if (pkgid == NULL || dir_list == NULL || size<=0) {
610                 app2ext_print
611                     ("App2Sd Error : Invalid function arguments \n");
612                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
613         }
614         /*Check whether MMC is present or not */
615         ret = _app2sd_check_mmc_status();
616         if (ret) {
617                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
618                              ret);
619                 return APP2EXT_ERROR_MMC_STATUS;
620         }
621         /*check app entry is there in sd card or not. */
622         snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
623                  pkgid);
624         app2ext_print("App2Sd Log : Checking path %s\n", app_path);
625         fp = fopen(app_path, "r+");
626         if (fp == NULL) {
627                 app2ext_print
628                     ("App2SD Error: App Entry is not present in SD Card\n");
629                 return APP2EXT_ERROR_INVALID_PACKAGE;
630         }
631         fclose(fp);
632         /*Get installed app size*/
633         curr_size = _app2sd_calculate_file_size(app_path);
634         curr_size = (curr_size/1024)/1024;
635
636         if (curr_size==0) {
637                 app2ext_print
638                     ("App2SD Error: App Entry is not present in SD Card\n");
639                 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
640         }
641         if (curr_size<size) {
642                 ret = _app2sd_update_loopback_device_size(pkgid, size, dir_list);
643                 if(APP2EXT_SUCCESS !=ret) {
644                         app2ext_print
645                             ("App2SD Error: _app2sd_update_loopback_device_size() failed\n");
646                         return ret;
647                 }
648         }
649
650         /*Get the associated device node for SD card applicationer */
651         device_node = _app2sd_find_associated_device_node(pkgid);
652         if (NULL == device_node) {
653                 /*Do loopback setup */
654                 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
655                 if (device_node == NULL) {
656                         app2ext_print
657                             ("App2Sd Error : loopback encryption setup failed\n");
658                         return APP2EXT_ERROR_DO_LOSETUP;
659                 }
660                 /*Do  mounting */
661                 ret =
662                     _app2sd_mount_app_content(pkgid, device_node,
663                                         MOUNT_TYPE_RW, NULL,
664                                         APP2SD_PRE_UPGRADE);
665                 if (ret) {
666                         app2ext_print("App2Sd Error : Re-mount failed\n");
667                         if (device_node) {
668                                 free(device_node);
669                                 device_node = NULL;
670                         }
671                         return APP2EXT_ERROR_MOUNT_PATH;
672                 }
673         } else {
674                 /*Do  re-mounting */
675                 ret =
676                     _app2sd_mount_app_content(pkgid, device_node,
677                                               MOUNT_TYPE_RW_REMOUNT, NULL,
678                                               APP2SD_PRE_UPGRADE);
679                 if (ret) {
680                         app2ext_print("App2Sd Error : Re-mount failed\n");
681                         if (device_node) {
682                                 free(device_node);
683                                 device_node = NULL;
684                         }
685                         return APP2EXT_ERROR_MOUNT_PATH;
686                 }
687         }
688
689         if (device_node) {
690                 free(device_node);
691                 device_node = NULL;
692         }
693         return ret;
694 }
695
696
697 int app2sd_post_app_upgrade(const char *pkgid,
698                         app2ext_status install_status)
699 {
700         char *device_name = NULL;
701         int ret = APP2EXT_SUCCESS;
702         /*Validate the function parameter recieved */
703         if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
704                 || install_status > APP2EXT_STATUS_SUCCESS) {
705                 app2ext_print("Invalid func parameters\n");
706                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
707         }
708         /*Check whether MMC is present or not */
709         ret = _app2sd_check_mmc_status();
710         if (ret) {
711                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
712                              ret);
713                 return APP2EXT_ERROR_MMC_STATUS;
714         }
715
716         /*Get the associated device node for SD card applicationer */
717         device_name = _app2sd_find_associated_device_node(pkgid);
718         if (NULL == device_name) {
719                 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
720         }
721         ret = _app2sd_unmount_app_content(pkgid);
722         if (ret) {
723                 if (device_name) {
724                         free(device_name);
725                         device_name = NULL;
726                 }
727                 app2ext_print("Unable to unmount the app content %d\n", ret);
728                 return APP2EXT_ERROR_UNMOUNT;
729         }
730         ret = _app2sd_remove_loopback_encryption_setup(pkgid);
731         if (ret) {
732                 if (device_name) {
733                         free(device_name);
734                         device_name = NULL;
735                 }
736                 app2ext_print
737                     ("Unable to Detach the loopback encryption setup for the application");
738                 return APP2EXT_ERROR_UNMOUNT;
739         }
740         if (device_name) {
741                 free(device_name);
742                 device_name = NULL;
743         }
744         return ret;
745 }
746
747 #if 0
748 /**
749  * Reserved API for forced cleanup
750  *
751  */
752 int app2sd_force_cleanup(const char *pkgid){
753         char *device_name = NULL;
754         char buf_dir[FILENAME_MAX] = { 0, };
755         int ret = APP2EXT_SUCCESS;
756         FILE *fp = NULL;
757
758         /*Validate the function parameter recieved */
759         if (pkgid == NULL) {
760                 app2ext_print("invalid func parameters\n");
761                 return APP2EXT_ERROR_INVALID_ARGUMENTS;
762         }
763         memset((void *)&buf_dir, '\0', FILENAME_MAX);
764         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
765         fp = fopen(buf_dir, "r+");
766         if (fp == NULL) {
767                 app2ext_print("\"%s\" not installed on SD Card\n", pkgid);
768                 return APP2EXT_ERROR_INVALID_PACKAGE;
769         }
770         fclose(fp);
771         /*Check whether MMC is present or not */
772         ret = _app2sd_check_mmc_status();
773         if (ret) {
774                 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
775                              ret);
776                 return APP2EXT_ERROR_MMC_STATUS;
777         }
778
779         /*Get the associated device node for SD card applicationer */
780         device_name = _app2sd_find_associated_device_node(pkgid);
781         if (NULL != device_name) {
782                 free(device_name);
783                 device_name = NULL;
784                 ret = _app2sd_unmount_app_content(pkgid);
785                 if (ret) {
786                         app2ext_print("Unable to unmount the app content %d\n", ret);
787                         return APP2EXT_ERROR_UNMOUNT;
788                 }
789                 ret = _app2sd_remove_loopback_encryption_setup(pkgid);
790                 if (ret) {
791                         app2ext_print
792                             ("Unable to Detach the loopback encryption setup for the application");
793                         return APP2EXT_ERROR_UNMOUNT;
794                 }
795         }
796
797         memset((void *)&buf_dir, '\0', FILENAME_MAX);
798         snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
799         ret = _app2sd_delete_directory(buf_dir);
800         if (ret) {
801                 app2ext_print
802                     ("App2Sd Error : Unable to delete the directory %s\n",
803                      buf_dir);
804         }
805
806         /*remove passwrd from DB*/
807         ret = _app2sd_initialize_db();
808         if (ret) {
809                 app2ext_print("\n app2sd db initialize failed");
810                 return APP2EXT_ERROR_SQLITE_REGISTRY;
811         }
812         ret = _app2sd_remove_password_from_db(pkgid);
813         if (ret) {
814                 app2ext_print("cannot remove password from db \n");
815                 return APP2EXT_ERROR_SQLITE_REGISTRY;
816         }
817         return ret;
818 }
819 #endif
820
821 /* This is the plug-in load function. The plugin has to bind its functions to function pointers of handle
822         @param[in/out]          st_interface    Specifies the storage interface.
823 */
824 void
825 app2ext_on_load(app2ext_interface *st_interface)
826 {
827         /*Plug-in Binding.*/
828         st_interface->pre_install= app2sd_pre_app_install;
829         st_interface->post_install= app2sd_post_app_install;
830         st_interface->pre_uninstall= app2sd_pre_app_uninstall;
831         st_interface->post_uninstall= app2sd_post_app_uninstall;
832         st_interface->pre_upgrade= app2sd_pre_app_upgrade;
833         st_interface->post_upgrade= app2sd_post_app_upgrade;
834         st_interface->move= app2sd_move_installed_app;
835         st_interface->enable= app2sd_on_demand_setup_init;
836         st_interface->disable= app2sd_on_demand_setup_exit;
837 }
838