4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Garima Shrivastava<garima.s@samsung.com>
7 * Jyotsna Dhumale <jyotsna.a@samsung.com>
8 * Venkatesha Sarpangala <sarpangala.v@samsung.com>
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <app2sd_internals.h>
25 #include <app2sd_interface.h>
27 #include <sys/types.h>
29 #include <pkgmgr-info.h>
31 static int __app2sd_create_app2sd_directories(uid_t uid)
34 char app2sd_user_path[FILENAME_MAX] = { 0, };
35 mode_t mode = DIR_PERMS;
37 ret = mkdir(APP2SD_PATH, mode);
39 if (errno != EEXIST) {
40 _E("create directory failed," \
41 " error no is (%d)", errno);
42 return APP2EXT_ERROR_CREATE_DIRECTORY;
46 if (!_is_global(uid)) {
47 tzplatform_set_user(uid);
48 snprintf(app2sd_user_path, FILENAME_MAX - 1, "%s/%s",
49 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME));
50 tzplatform_reset_user();
52 ret = mkdir(app2sd_user_path, mode);
54 if (errno != EEXIST) {
55 _E("create directory failed," \
56 " error no is (%d)", errno);
57 return APP2EXT_ERROR_CREATE_DIRECTORY;
62 return APP2EXT_SUCCESS;
65 int app2sd_usr_pre_app_install(const char *pkgid, GList* dir_list, int size, uid_t uid)
69 char *device_node = NULL;
72 char application_path[FILENAME_MAX] = { 0, };
73 char loopback_device[FILENAME_MAX] = { 0, };
74 int reqd_disk_size = size + ceil(size * 0.2);
76 /* validate the function parameter recieved */
77 if (pkgid == NULL || dir_list == NULL || size <= 0) {
78 _E("invalid function arguments");
79 return APP2EXT_ERROR_INVALID_ARGUMENTS;
82 /* check whether MMC is present or not */
83 ret = _app2sd_check_mmc_status();
85 _E("MMC not preset OR Not ready (%d)", ret);
86 return APP2EXT_ERROR_MMC_STATUS;
89 /* find available free memory in the MMC card */
90 ret = _app2sd_get_available_free_memory(MMC_PATH, &free_mmc_mem);
92 _E("unable to get available free memory in MMC (%d)",
94 return APP2EXT_ERROR_MMC_STATUS;
96 _D("size details for application installation:" \
97 " size=(%d)MB, reqd_disk_size=(%d)MB, free_mmc_size=(%d)MB",
98 size, reqd_disk_size, free_mmc_mem);
100 /* if avaialalbe free memory in MMC is less than required size + 5MB,
103 if ((reqd_disk_size + PKG_BUF_SIZE + MEM_BUF_SIZE) > free_mmc_mem) {
104 _E("insufficient memory in MMC for"
105 " application installation (%d)", ret);
106 return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
109 if (_is_global(uid)) {
110 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
111 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
112 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
115 tzplatform_set_user(uid);
116 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
117 tzplatform_getenv(TZ_USER_APP), pkgid);
118 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
119 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
120 tzplatform_reset_user();
122 _D("application_path = (%s)", application_path);
123 _D("loopback_device = (%s)", loopback_device);
125 ret = __app2sd_create_app2sd_directories(uid);
127 _E("failed to create app2sd dirs");
131 /* check same loopback_device existence */
132 result = (char *)_app2sd_find_associated_device(loopback_device);
133 if (result != NULL) {
134 _E("there is same associated File (%s)", loopback_device);
135 return APP2EXT_ERROR_SAME_LOOPBACK_DEVICE_EXISTS;
138 /* create a loopback device */
139 ret = _app2sd_create_loopback_device(pkgid, loopback_device,
140 (reqd_disk_size + PKG_BUF_SIZE));
142 _W("package already present, delete app directory");
143 ret = _app2sd_delete_directory(application_path);
145 _E("unable to delete the directory (%s)",
151 /* perform loopback encryption setup */
152 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
155 _E("loopback encryption setup failed");
156 _app2sd_delete_loopback_device(loopback_device);
157 return APP2EXT_ERROR_DO_LOSETUP;
160 /* check whether loopback device is associated
161 * with device node or not
163 devi = _app2sd_find_associated_device_node(loopback_device);
165 _E("finding associated device node failed");
166 ret = APP2EXT_ERROR_DO_LOSETUP;
170 /* format the loopback file system */
171 ret = _app2sd_create_file_system(device_node);
173 _E("creating FS failed failed");
174 ret = APP2EXT_ERROR_CREATE_FS;
178 /* mount the loopback encrypted pseudo device on application
179 * installation path as with Read Write permission
181 ret =_app2sd_mount_app_content(application_path, pkgid,
182 device_node, MOUNT_TYPE_RW, dir_list,
183 APP2SD_PRE_INSTALL, uid);
185 _E("mounting dev path to app install path failed");
186 ret = APP2EXT_ERROR_MOUNT_PATH;
191 ret = APP2EXT_SUCCESS;
196 result = _app2sd_detach_loop_device(device_node);
201 _app2sd_delete_loopback_device(loopback_device);
218 int app2sd_usr_post_app_install(const char *pkgid,
219 app2ext_status install_status, uid_t uid)
221 char *device_name = NULL;
222 char application_path[FILENAME_MAX] = { 0, };
223 char loopback_device[FILENAME_MAX] = { 0, };
224 int ret = APP2EXT_SUCCESS;
227 /* validate the function parameter recieved */
228 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
229 || install_status > APP2EXT_STATUS_SUCCESS) {
230 _E("invalid func parameters");
231 return APP2EXT_ERROR_INVALID_ARGUMENTS;
234 /* check whether MMC is present or not */
235 ret = _app2sd_check_mmc_status();
237 _E("MMC not present OR Not ready (%d)", ret);
238 return APP2EXT_ERROR_MMC_STATUS;
242 if (_is_global(uid)) {
243 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
244 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
245 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
248 tzplatform_set_user(uid);
249 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
250 tzplatform_getenv(TZ_USER_APP), pkgid);
251 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
252 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
253 tzplatform_reset_user();
255 _D("application_path = (%s)", application_path);
256 _D("loopback_device = (%s)", loopback_device);
258 /* get the associated device node for SD card applicationer */
259 device_name = _app2sd_find_associated_device_node(loopback_device);
260 if (NULL == device_name) {
261 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
264 ret = _app2sd_unmount_app_content(application_path);
270 _E("unable to unmount the app content (%d)", ret);
271 return APP2EXT_ERROR_UNMOUNT;
274 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
280 _E("unable to detach the loopback encryption setup" \
281 " for the application");
282 return APP2EXT_ERROR_UNMOUNT;
290 /* take appropriate action based on
291 * installation status of application package
293 if (install_status == APP2EXT_STATUS_FAILED) {
294 /* delete the loopback device from the SD card */
295 ret = _app2sd_delete_loopback_device(loopback_device);
297 _E("unable to delete the loopback device from the SD Card");
298 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
300 ret = _app2sd_remove_password_from_db(pkgid);
303 _E("unable to delete the password");
305 ret = _app2sd_delete_directory(application_path);
308 _E("unable to delete the directory (%s)", application_path);
310 /* if the status is success, then update installed storage
311 * to pkgmgr_parser db
313 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
314 INSTALL_EXTERNAL, uid);
315 if (pkgmgr_ret < 0) {
316 _E("fail to update installed location " \
317 "to db[%s, %d] of uid(%d), pkgmgr ret(%d)",
318 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
319 return APP2EXT_ERROR_PKGMGR_ERROR;
326 int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
328 int ret = APP2EXT_SUCCESS;
329 char application_path[FILENAME_MAX] = { 0, };
330 char loopback_device[FILENAME_MAX] = { 0, };
331 char *device_node = NULL;
335 /* validate the function parameter recieved */
337 _E("invalid function arguments to app launch setup");
338 return APP2EXT_ERROR_INVALID_ARGUMENTS;
341 /* check whether MMC is present or not */
342 ret = _app2sd_check_mmc_status();
344 _E("MMC not preset OR Not ready (%d)", ret);
345 return APP2EXT_ERROR_MMC_STATUS;
348 /* check app entry is there in sd card or not. */
349 if (_is_global(uid)) {
350 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
351 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
352 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
355 tzplatform_set_user(uid);
356 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
357 tzplatform_getenv(TZ_USER_APP), pkgid);
358 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
359 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
360 tzplatform_reset_user();
362 _D("application_path = (%s)", application_path);
363 _D("loopback_device = (%s)", loopback_device);
365 fp = fopen(loopback_device, "r+");
367 _E("app entry is not present in SD Card");
368 return APP2EXT_ERROR_INVALID_PACKAGE;
372 result = (char *)_app2sd_find_associated_device(loopback_device);
373 /* process the string */
374 if ((result != NULL) && strstr(result, "/dev") != NULL) {
375 _E("already associated");
378 return APP2EXT_ERROR_ALREADY_MOUNTED;
381 /* do loopback setup */
382 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
384 if (device_node == NULL) {
385 _E("loopback encryption setup failed");
386 return APP2EXT_ERROR_DO_LOSETUP;
390 ret = _app2sd_mount_app_content(application_path, pkgid,
391 device_node, MOUNT_TYPE_RD, NULL, APP2SD_APP_LAUNCH, uid);
398 return APP2EXT_ERROR_MOUNT_PATH;
409 int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
411 int ret = APP2EXT_SUCCESS;
412 char application_path[FILENAME_MAX] = { 0, };
413 char loopback_device[FILENAME_MAX] = { 0, };
416 /* validate the function parameter recieved */
418 _E("invalid function arguments to app launch setup");
419 return APP2EXT_ERROR_INVALID_ARGUMENTS;
422 /* check whether MMC is present or not */
423 ret = _app2sd_check_mmc_status();
425 _E("MMC not preset OR Not ready (%d)", ret);
426 return APP2EXT_ERROR_MMC_STATUS;
429 /* check app entry is there in sd card or not. */
430 if (_is_global(uid)) {
431 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
432 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
433 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
436 tzplatform_set_user(uid);
437 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
438 tzplatform_getenv(TZ_USER_APP), pkgid);
439 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
440 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
441 tzplatform_reset_user();
443 _D("application_path = (%s)", application_path);
444 _D("loopback_device = (%s)", loopback_device);
446 fp = fopen(loopback_device, "r+");
448 _E("app entry is not present in SD Card");
449 return APP2EXT_ERROR_INVALID_PACKAGE;
453 ret = _app2sd_unmount_app_content(application_path);
455 _E("unable to unmount the SD application");
456 return APP2EXT_ERROR_UNMOUNT;
459 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
461 _E("unable to remove loopback setup");
462 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
468 int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
470 int ret = APP2EXT_SUCCESS;
471 char application_path[FILENAME_MAX] = { 0, };
472 char loopback_device[FILENAME_MAX] = { 0, };
473 char *device_node = NULL;
476 /* validate the function parameter recieved */
478 _E("invalid function arguments to app launch setup");
479 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
483 /* check whether MMC is present or not */
484 ret = _app2sd_check_mmc_status();
486 _E("MMC not preset OR Not ready (%d)", ret);
487 ret = APP2EXT_ERROR_MMC_STATUS;
491 if (_is_global(uid)) {
492 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
493 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
494 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
497 tzplatform_set_user(uid);
498 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
499 tzplatform_getenv(TZ_USER_APP), pkgid);
500 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
501 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
502 tzplatform_reset_user();
504 _D("application_path = (%s)", application_path);
505 _D("loopback_device = (%s)", loopback_device);
507 /* check app entry is there in sd card or not. */
508 fp = fopen(loopback_device, "r+");
510 _E("app entry is not present in SD Card");
511 ret = APP2EXT_ERROR_INVALID_PACKAGE;
516 /* get the associated device node for SD card applicationer */
517 device_node = _app2sd_find_associated_device_node(loopback_device);
518 if (NULL == device_node) {
519 /* do loopback setup */
520 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
522 if (device_node == NULL) {
523 _E("loopback encryption setup failed");
524 ret = APP2EXT_ERROR_DO_LOSETUP;
528 ret = _app2sd_mount_app_content(application_path, pkgid,
529 device_node, MOUNT_TYPE_RW, NULL,
530 APP2SD_PRE_UNINSTALL, uid);
537 ret = APP2EXT_ERROR_MOUNT_PATH;
542 ret = _app2sd_mount_app_content(application_path, pkgid,
543 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
544 APP2SD_PRE_UNINSTALL, uid);
546 _E("remount failed");
551 ret = APP2EXT_ERROR_MOUNT_PATH;
564 int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
566 char application_path[FILENAME_MAX] = { 0, };
567 char loopback_device[FILENAME_MAX] = { 0, };
568 int ret = APP2EXT_SUCCESS;
570 /* validate the function parameter recieved */
572 _E("invalid function arguments");
573 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
577 /* check whether MMC is present or not */
578 ret = _app2sd_check_mmc_status();
580 _E("MMC not preset OR Not ready (%d)", ret);
581 ret = APP2EXT_ERROR_MMC_STATUS;
585 if (_is_global(uid)) {
586 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
587 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
588 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
591 tzplatform_set_user(uid);
592 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
593 tzplatform_getenv(TZ_USER_APP), pkgid);
594 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
595 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
596 tzplatform_reset_user();
598 _D("application_path = (%s)", application_path);
599 _D("loopback_device = (%s)", loopback_device);
601 /* unmount the loopback encrypted pseudo device from
602 * the application installation path
604 ret = _app2sd_unmount_app_content(application_path);
606 _E("unable to unmount the app content (%d)", ret);
607 ret = APP2EXT_ERROR_UNMOUNT;
610 /* detach the loopback encryption setup for the application */
611 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
613 _E("unable to Detach the loopback encryption setup" \
614 " for the application");
615 ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
619 /* delete the loopback device from the SD card */
620 ret = _app2sd_delete_loopback_device(loopback_device);
622 _E("unable to delete the " \
623 "loopback device from the SD Card");
624 ret = APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
628 ret = _app2sd_delete_directory(application_path);
630 _E("unable to delete the directory (%s)",
635 /* remove encryption password from DB */
636 ret = _app2sd_initialize_db();
638 _E("app2sd db initialize failed");
639 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
643 ret = _app2sd_remove_password_from_db(pkgid);
645 _E("cannot remove password from db");
646 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
654 int app2sd_usr_move_installed_app(const char *pkgid, GList* dir_list,
655 app2ext_move_type move_type, uid_t uid)
660 /* validate function arguments */
661 if (pkgid == NULL || dir_list == NULL
662 || move_type < APP2EXT_MOVE_TO_EXT
663 || move_type > APP2EXT_MOVE_TO_PHONE) {
664 _E("invalid function arguments");
665 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
669 pkgmgrinfo_pkginfo_h info_handle = NULL;
670 pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
671 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &info_handle);
672 if (pkgmgr_ret < 0) {
673 _E("failed to get pkginfo for pkg(%s), uid(%d), pkgmgr_ret(%d)",
674 pkgid, uid, pkgmgr_ret);
676 pkgmgr_ret = pkgmgrinfo_pkginfo_get_installed_storage(info_handle, &storage);
677 if (pkgmgr_ret < 0) {
678 _E("failed to get installed storage for pkg(%s) of uid(%d), pkgmgr_ret(%d)",
679 pkgid, uid, pkgmgr_ret);
680 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
684 if ((move_type == APP2EXT_MOVE_TO_EXT && storage == PMINFO_EXTERNAL_STORAGE)
685 || (move_type == APP2EXT_MOVE_TO_PHONE && storage == PMINFO_INTERNAL_STORAGE)) {
686 ret = APP2EXT_ERROR_PKG_EXISTS;
687 _E("PKG_EXISTS in [%d] STORAGE", storage);
688 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
691 _D("pkgid[%s] move to STORAGE [%d]", pkgid, storage);
693 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
695 ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid);
697 _D("unable to move application");
701 /* if move is completed, then update installed storage to pkgmgr_parser db */
702 if (move_type == APP2EXT_MOVE_TO_EXT) {
703 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
704 INSTALL_EXTERNAL, uid);
705 if (pkgmgr_ret < 0) {
706 _E("failed to update installed location to db " \
707 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
708 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
709 return APP2EXT_ERROR_PKGMGR_ERROR;
712 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
713 INSTALL_INTERNAL, uid);
714 if (pkgmgr_ret < 0) {
715 _E("failed to update installed location to db " \
716 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
717 pkgid, INSTALL_INTERNAL, uid, pkgmgr_ret);
718 return APP2EXT_ERROR_PKGMGR_ERROR;
723 _app2sd_make_result_info_file((char*)pkgid, ret, uid);
728 int app2sd_usr_pre_app_upgrade(const char *pkgid, GList* dir_list,
731 int ret = APP2EXT_SUCCESS;
732 char loopback_device[FILENAME_MAX] = { 0, };
733 char application_path[FILENAME_MAX] = { 0, };
734 char temp_pkgid[FILENAME_MAX] = { 0, };
735 char temp_loopback_device[FILENAME_MAX] = { 0, };
736 char temp_application_path[FILENAME_MAX] = { 0, };
737 char *device_node = NULL;
738 unsigned long long curr_size = 0;
740 int reqd_disk_size = size + ceil(size * 0.2);
742 /* validate function arguments*/
743 if (pkgid == NULL || dir_list == NULL || size<=0) {
744 _E("invalid function arguments");
745 return APP2EXT_ERROR_INVALID_ARGUMENTS;
748 /* check whether MMC is present or not */
749 ret = _app2sd_check_mmc_status();
751 _E("MMC not preset OR Not ready (%d)", ret);
752 return APP2EXT_ERROR_MMC_STATUS;
755 if (_is_global(uid)) {
756 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
757 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
758 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
761 tzplatform_set_user(uid);
762 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
763 tzplatform_getenv(TZ_USER_APP), pkgid);
764 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
765 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
766 tzplatform_reset_user();
768 _D("application_path = (%s)", application_path);
769 _D("loopback_device = (%s)", loopback_device);
771 /* check app entry is there in sd card or not. */
772 fp = fopen(loopback_device, "r+");
774 _E("app entry is not present in SD Card");
775 return APP2EXT_ERROR_INVALID_PACKAGE;
779 /* get installed app size*/
780 curr_size = _app2sd_calculate_file_size(loopback_device);
781 curr_size = (curr_size) / (1024 * 1024);
782 if (curr_size == 0) {
783 _E("app entry is not present in SD Card");
784 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
786 if ((int)curr_size < reqd_disk_size) {
787 snprintf(temp_pkgid, FILENAME_MAX - 1, "%s.new", pkgid);
788 if (_is_global(uid)) {
789 snprintf(temp_application_path, FILENAME_MAX - 1,
790 "%s/%s", tzplatform_getenv(TZ_SYS_RW_APP), temp_pkgid);
791 snprintf(temp_loopback_device, FILENAME_MAX - 1,
792 "%s/%s", APP2SD_PATH, temp_pkgid);
794 tzplatform_set_user(uid);
795 snprintf(temp_application_path, FILENAME_MAX - 1,
796 "%s/%s", tzplatform_getenv(TZ_USER_APP), temp_pkgid);
797 snprintf(temp_loopback_device, FILENAME_MAX - 1,
798 "%s/%s/%s", APP2SD_PATH,
799 tzplatform_getenv(TZ_USER_NAME), temp_pkgid);
800 tzplatform_reset_user();
802 ret = _app2sd_update_loopback_device_size(pkgid,
803 loopback_device, application_path, temp_pkgid,
804 temp_loopback_device, temp_application_path,
805 reqd_disk_size, dir_list, uid);
806 if (APP2EXT_SUCCESS != ret) {
807 _E("failed to update loopback device size");
812 /* get the associated device node for SD card applicationer */
813 device_node = _app2sd_find_associated_device_node(loopback_device);
814 if (NULL == device_node) {
815 /* do loopback setup */
816 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
818 if (device_node == NULL) {
819 _E("loopback encryption setup failed");
820 return APP2EXT_ERROR_DO_LOSETUP;
824 ret = _app2sd_mount_app_content(application_path, pkgid,
825 device_node, MOUNT_TYPE_RW, dir_list,
826 APP2SD_PRE_UPGRADE, uid);
833 return APP2EXT_ERROR_MOUNT_PATH;
837 ret = _app2sd_mount_app_content(application_path, pkgid,
838 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
839 APP2SD_PRE_UPGRADE, uid);
841 _E("remount failed");
846 return APP2EXT_ERROR_MOUNT_PATH;
857 int app2sd_usr_post_app_upgrade(const char *pkgid,
858 app2ext_status install_status, uid_t uid)
860 char *device_name = NULL;
861 char loopback_device[FILENAME_MAX] = { 0, };
862 char application_path[FILENAME_MAX] = { 0, };
863 int ret = APP2EXT_SUCCESS;
865 /* validate the function parameter recieved */
866 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
867 || install_status > APP2EXT_STATUS_SUCCESS) {
868 _E("invalid func parameters");
869 return APP2EXT_ERROR_INVALID_ARGUMENTS;
872 /* check whether MMC is present or not */
873 ret = _app2sd_check_mmc_status();
875 _E("MMC not preset OR Not ready (%d)", ret);
876 return APP2EXT_ERROR_MMC_STATUS;
879 if (_is_global(uid)) {
880 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
881 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
882 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
885 tzplatform_set_user(uid);
886 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
887 tzplatform_getenv(TZ_USER_APP), pkgid);
888 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
889 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
890 tzplatform_reset_user();
892 _D("application_path = (%s)", application_path);
893 _D("loopback_device = (%s)", loopback_device);
895 /* get the associated device node for SD card applicationer */
896 device_name = _app2sd_find_associated_device_node(loopback_device);
897 if (NULL == device_name) {
898 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
901 ret = _app2sd_unmount_app_content(application_path);
907 _E("unable to unmount the app content (%d)", ret);
908 return APP2EXT_ERROR_UNMOUNT;
911 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
917 _E("unable to detach the loopback encryption " \
918 "setup for the application");
919 return APP2EXT_ERROR_UNMOUNT;
930 int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
932 char loopback_device[FILENAME_MAX] = { 0, };
933 char application_path[FILENAME_MAX] = { 0, };
934 int ret = APP2EXT_SUCCESS;
936 _D("start force_clean [%s]", pkgid);
938 /* validate the function parameter recieved */
940 _E("invalid func parameters");
941 return APP2EXT_ERROR_INVALID_ARGUMENTS;
946 if (_is_global(uid)) {
947 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
948 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
949 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
952 tzplatform_set_user(uid);
953 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
954 tzplatform_getenv(TZ_USER_APP), pkgid);
955 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
956 APP2SD_PATH, tzplatform_getenv(TZ_USER_NAME), pkgid);
957 tzplatform_reset_user();
959 _D("application_path = (%s)", application_path);
960 _D("loopback_device = (%s)", loopback_device);
962 /* unmount the loopback encrypted pseudo device from the application installation path */
963 ret = _app2sd_unmount_app_content(application_path);
965 _E("unable to unmount the app content (%d)", ret);
968 /* detach the loopback encryption setup for the application */
969 ret = _app2sd_remove_all_loopback_encryption_setups(pkgid);
971 _E("unable to detach the loopback encryption setup for the application");
974 /* delete the loopback device from the SD card */
975 ret = _app2sd_delete_loopback_device(loopback_device);
977 _E("unable to detach the loopback encryption setup for the application");
981 _app2sd_delete_symlink(application_path);
983 /* remove passwrd from DB */
984 ret = _app2sd_initialize_db();
986 _E("app2sd db initialize failed");
988 ret = _app2sd_remove_password_from_db(pkgid);
990 _E("cannot remove password from db");
993 _D("finish force_clean");