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 <pkgmgr-info.h>
26 #include "app2sd_internals.h"
28 static int __app2sd_create_app2sd_directories(uid_t uid)
31 mode_t mode = DIR_PERMS;
33 ret = mkdir(APP2SD_PATH, mode);
35 if (errno != EEXIST) {
36 _E("create directory failed," \
37 " error no is (%d)", errno);
38 return APP2EXT_ERROR_CREATE_DIRECTORY;
42 return APP2EXT_SUCCESS;
45 int app2sd_usr_pre_app_install(const char *pkgid, GList* dir_list, int size, uid_t uid)
49 char *device_node = NULL;
52 char application_path[FILENAME_MAX] = { 0, };
53 char loopback_device[FILENAME_MAX] = { 0, };
54 char *encoded_id = NULL;
55 int reqd_disk_size = size + ceil(size * 0.2);
57 /* validate the function parameter recieved */
58 if (pkgid == NULL || dir_list == NULL || size <= 0) {
59 _E("invalid function arguments");
60 return APP2EXT_ERROR_INVALID_ARGUMENTS;
63 /* check whether MMC is present or not */
64 ret = _app2sd_check_mmc_status();
66 _E("MMC not preset OR Not ready (%d)", ret);
67 return APP2EXT_ERROR_MMC_STATUS;
70 /* find available free memory in the MMC card */
71 ret = _app2sd_get_available_free_memory(MMC_PATH, &free_mmc_mem);
73 _E("unable to get available free memory in MMC (%d)",
75 return APP2EXT_ERROR_MMC_STATUS;
77 _D("size details for application installation:" \
78 " size=(%d)MB, reqd_disk_size=(%d)MB, free_mmc_size=(%d)MB",
79 size, reqd_disk_size, free_mmc_mem);
81 /* if avaialalbe free memory in MMC is less than required size + 5MB,
84 if ((reqd_disk_size + PKG_BUF_SIZE + MEM_BUF_SIZE) > free_mmc_mem) {
85 _E("insufficient memory in MMC for"
86 " application installation (%d)", ret);
87 return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
90 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
91 if (encoded_id == NULL) {
92 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
94 if (_is_global(uid)) {
95 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
96 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
97 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
98 APP2SD_PATH, encoded_id);
100 tzplatform_set_user(uid);
101 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
102 tzplatform_getenv(TZ_USER_APP), pkgid);
103 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
104 APP2SD_PATH, encoded_id);
105 tzplatform_reset_user();
109 ret = __app2sd_create_app2sd_directories(uid);
111 _E("failed to create app2sd dirs");
115 /* check same loopback_device existence */
116 result = (char *)_app2sd_find_associated_device(loopback_device);
117 if (result != NULL) {
118 _E("there is same associated File (%s)", loopback_device);
119 return APP2EXT_ERROR_SAME_LOOPBACK_DEVICE_EXISTS;
122 /* create a loopback device */
123 ret = _app2sd_create_loopback_device(pkgid, loopback_device,
124 (reqd_disk_size + PKG_BUF_SIZE));
126 _W("package already present, delete app directory");
127 ret = _app2sd_delete_directory(application_path);
129 _E("unable to delete the directory (%s)",
135 /* perform loopback encryption setup */
136 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
137 loopback_device, uid);
139 _E("loopback encryption setup failed");
140 _app2sd_delete_loopback_device(loopback_device);
141 return APP2EXT_ERROR_DO_LOSETUP;
144 /* check whether loopback device is associated
145 * with device node or not
147 devi = _app2sd_find_associated_device_node(loopback_device);
149 _E("finding associated device node failed");
150 ret = APP2EXT_ERROR_DO_LOSETUP;
154 /* format the loopback file system */
155 ret = _app2sd_create_file_system(device_node);
157 _E("creating FS failed failed");
158 ret = APP2EXT_ERROR_CREATE_FS;
162 /* mount the loopback encrypted pseudo device on application
163 * installation path as with Read Write permission
165 ret =_app2sd_mount_app_content(application_path, pkgid,
166 device_node, MOUNT_TYPE_RW, dir_list,
167 APP2SD_PRE_INSTALL, uid);
169 _E("mounting dev path to app install path failed");
170 ret = APP2EXT_ERROR_MOUNT_PATH;
175 ret = APP2EXT_SUCCESS;
180 result = _app2sd_detach_loop_device(device_node);
185 _app2sd_delete_loopback_device(loopback_device);
202 int app2sd_usr_post_app_install(const char *pkgid,
203 app2ext_status install_status, uid_t uid)
205 char *device_name = NULL;
206 char application_path[FILENAME_MAX] = { 0, };
207 char loopback_device[FILENAME_MAX] = { 0, };
208 char *encoded_id = NULL;
209 int ret = APP2EXT_SUCCESS;
212 /* validate the function parameter recieved */
213 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
214 || install_status > APP2EXT_STATUS_SUCCESS) {
215 _E("invalid func parameters");
216 return APP2EXT_ERROR_INVALID_ARGUMENTS;
219 /* check whether MMC is present or not */
220 ret = _app2sd_check_mmc_status();
222 _E("MMC not present OR Not ready (%d)", ret);
223 return APP2EXT_ERROR_MMC_STATUS;
227 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
228 if (encoded_id == NULL) {
229 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
231 if (_is_global(uid)) {
232 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
233 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
234 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
235 APP2SD_PATH, encoded_id);
237 tzplatform_set_user(uid);
238 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
239 tzplatform_getenv(TZ_USER_APP), pkgid);
240 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
241 APP2SD_PATH, encoded_id);
242 tzplatform_reset_user();
246 /* get the associated device node for SD card applicationer */
247 device_name = _app2sd_find_associated_device_node(loopback_device);
248 if (NULL == device_name) {
249 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
252 ret = _app2sd_unmount_app_content(application_path);
258 _E("unable to unmount the app content (%d)", ret);
259 return APP2EXT_ERROR_UNMOUNT;
262 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
268 _E("unable to detach the loopback encryption setup" \
269 " for the application");
270 return APP2EXT_ERROR_UNMOUNT;
278 /* take appropriate action based on
279 * installation status of application package
281 if (install_status == APP2EXT_STATUS_FAILED) {
282 /* delete the loopback device from the SD card */
283 ret = _app2sd_delete_loopback_device(loopback_device);
285 _E("unable to delete the loopback device from the SD Card");
286 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
288 ret = _app2sd_remove_password_from_db(pkgid);
291 _E("unable to delete the password");
293 ret = _app2sd_delete_directory(application_path);
296 _E("unable to delete the directory (%s)", application_path);
298 /* if the status is success, then update installed storage
299 * to pkgmgr_parser db
301 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
302 INSTALL_EXTERNAL, uid);
303 if (pkgmgr_ret < 0) {
304 _E("fail to update installed location " \
305 "to db[%s, %d] of uid(%d), pkgmgr ret(%d)",
306 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
307 return APP2EXT_ERROR_PKGMGR_ERROR;
314 int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
316 int ret = APP2EXT_SUCCESS;
317 char application_path[FILENAME_MAX] = { 0, };
318 char loopback_device[FILENAME_MAX] = { 0, };
319 char *encoded_id = NULL;
320 char *device_node = NULL;
324 /* validate the function parameter recieved */
326 _E("invalid function arguments to app launch setup");
327 return APP2EXT_ERROR_INVALID_ARGUMENTS;
330 /* check whether MMC is present or not */
331 ret = _app2sd_check_mmc_status();
333 _E("MMC not preset OR Not ready (%d)", ret);
334 return APP2EXT_ERROR_MMC_STATUS;
337 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
338 if (encoded_id == NULL) {
339 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
341 /* check app entry is there in sd card or not. */
342 if (_is_global(uid)) {
343 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
344 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
345 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
346 APP2SD_PATH, encoded_id);
348 tzplatform_set_user(uid);
349 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
350 tzplatform_getenv(TZ_USER_APP), pkgid);
351 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
352 APP2SD_PATH, encoded_id);
353 tzplatform_reset_user();
357 fp = fopen(loopback_device, "r+");
359 _E("app entry is not present in SD Card");
360 return APP2EXT_ERROR_INVALID_PACKAGE;
364 result = (char *)_app2sd_find_associated_device(loopback_device);
365 /* process the string */
366 if ((result != NULL) && strstr(result, "/dev") != NULL) {
367 _E("already associated");
370 return APP2EXT_ERROR_ALREADY_MOUNTED;
373 /* do loopback setup */
374 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
375 loopback_device, uid);
376 if (device_node == NULL) {
377 _E("loopback encryption setup failed");
378 return APP2EXT_ERROR_DO_LOSETUP;
382 ret = _app2sd_mount_app_content(application_path, pkgid,
383 device_node, MOUNT_TYPE_RD, NULL, APP2SD_APP_LAUNCH, uid);
390 return APP2EXT_ERROR_MOUNT_PATH;
401 int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
403 int ret = APP2EXT_SUCCESS;
404 char application_path[FILENAME_MAX] = { 0, };
405 char loopback_device[FILENAME_MAX] = { 0, };
406 char *encoded_id = NULL;
409 /* validate the function parameter recieved */
411 _E("invalid function arguments to app launch setup");
412 return APP2EXT_ERROR_INVALID_ARGUMENTS;
415 /* check whether MMC is present or not */
416 ret = _app2sd_check_mmc_status();
418 _E("MMC not preset OR Not ready (%d)", ret);
419 return APP2EXT_ERROR_MMC_STATUS;
422 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
423 if (encoded_id == NULL) {
424 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
426 /* check app entry is there in sd card or not. */
427 if (_is_global(uid)) {
428 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
429 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
430 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
431 APP2SD_PATH, encoded_id);
433 tzplatform_set_user(uid);
434 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
435 tzplatform_getenv(TZ_USER_APP), pkgid);
436 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
437 APP2SD_PATH, encoded_id);
438 tzplatform_reset_user();
442 fp = fopen(loopback_device, "r+");
444 _E("app entry is not present in SD Card");
445 return APP2EXT_ERROR_INVALID_PACKAGE;
449 ret = _app2sd_unmount_app_content(application_path);
451 _E("unable to unmount the SD application");
452 return APP2EXT_ERROR_UNMOUNT;
455 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
457 _E("unable to remove loopback setup");
458 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
464 int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
466 int ret = APP2EXT_SUCCESS;
467 char application_path[FILENAME_MAX] = { 0, };
468 char loopback_device[FILENAME_MAX] = { 0, };
469 char *encoded_id = NULL;
470 char *device_node = NULL;
473 /* validate the function parameter recieved */
475 _E("invalid function arguments to app launch setup");
476 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
480 /* check whether MMC is present or not */
481 ret = _app2sd_check_mmc_status();
483 _E("MMC not preset OR Not ready (%d)", ret);
484 ret = APP2EXT_ERROR_MMC_STATUS;
488 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
489 if (encoded_id == NULL) {
490 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
492 if (_is_global(uid)) {
493 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
494 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
495 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
496 APP2SD_PATH, encoded_id);
498 tzplatform_set_user(uid);
499 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
500 tzplatform_getenv(TZ_USER_APP), pkgid);
501 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
502 APP2SD_PATH, encoded_id);
503 tzplatform_reset_user();
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,
521 loopback_device, uid);
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 char *encoded_id = NULL;
569 int ret = APP2EXT_SUCCESS;
571 /* validate the function parameter recieved */
573 _E("invalid function arguments");
574 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
578 /* check whether MMC is present or not */
579 ret = _app2sd_check_mmc_status();
581 _E("MMC not preset OR Not ready (%d)", ret);
582 ret = APP2EXT_ERROR_MMC_STATUS;
586 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
587 if (encoded_id == NULL) {
588 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
590 if (_is_global(uid)) {
591 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
592 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
593 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
594 APP2SD_PATH, encoded_id);
596 tzplatform_set_user(uid);
597 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
598 tzplatform_getenv(TZ_USER_APP), pkgid);
599 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
600 APP2SD_PATH, encoded_id);
601 tzplatform_reset_user();
605 /* unmount the loopback encrypted pseudo device from
606 * the application installation path
608 ret = _app2sd_unmount_app_content(application_path);
610 _E("unable to unmount the app content (%d)", ret);
611 ret = APP2EXT_ERROR_UNMOUNT;
614 /* detach the loopback encryption setup for the application */
615 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
617 _E("unable to Detach the loopback encryption setup" \
618 " for the application");
619 ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
623 /* delete the loopback device from the SD card */
624 ret = _app2sd_delete_loopback_device(loopback_device);
626 _E("unable to delete the " \
627 "loopback device from the SD Card");
628 ret = APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
632 ret = _app2sd_delete_directory(application_path);
634 _E("unable to delete the directory (%s)",
639 /* remove encryption password from DB */
640 ret = _app2sd_initialize_db();
642 _E("app2sd db initialize failed");
643 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
647 ret = _app2sd_remove_password_from_db(pkgid);
649 _E("cannot remove password from db");
650 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
658 int app2sd_usr_move_installed_app(const char *pkgid, GList* dir_list,
659 app2ext_move_type move_type, uid_t uid)
664 /* validate function arguments */
665 if (pkgid == NULL || dir_list == NULL
666 || move_type < APP2EXT_MOVE_TO_EXT
667 || move_type > APP2EXT_MOVE_TO_PHONE) {
668 _E("invalid function arguments");
669 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
673 pkgmgrinfo_pkginfo_h info_handle = NULL;
674 pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
675 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &info_handle);
676 if (pkgmgr_ret < 0) {
677 _E("failed to get pkginfo for pkg(%s), uid(%d), pkgmgr_ret(%d)",
678 pkgid, uid, pkgmgr_ret);
680 pkgmgr_ret = pkgmgrinfo_pkginfo_get_installed_storage(info_handle, &storage);
681 if (pkgmgr_ret < 0) {
682 _E("failed to get installed storage for pkg(%s) of uid(%d), pkgmgr_ret(%d)",
683 pkgid, uid, pkgmgr_ret);
684 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
688 if ((move_type == APP2EXT_MOVE_TO_EXT && storage == PMINFO_EXTERNAL_STORAGE)
689 || (move_type == APP2EXT_MOVE_TO_PHONE && storage == PMINFO_INTERNAL_STORAGE)) {
690 ret = APP2EXT_ERROR_PKG_EXISTS;
691 _E("PKG_EXISTS in [%d] STORAGE", storage);
692 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
695 _D("pkgid[%s] move to STORAGE [%d]", pkgid, storage);
697 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
699 ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid);
701 _D("unable to move application");
705 /* if move is completed, then update installed storage to pkgmgr_parser db */
706 if (move_type == APP2EXT_MOVE_TO_EXT) {
707 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
708 INSTALL_EXTERNAL, uid);
709 if (pkgmgr_ret < 0) {
710 _E("failed to update installed location to db " \
711 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
712 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
713 return APP2EXT_ERROR_PKGMGR_ERROR;
716 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
717 INSTALL_INTERNAL, uid);
718 if (pkgmgr_ret < 0) {
719 _E("failed to update installed location to db " \
720 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
721 pkgid, INSTALL_INTERNAL, uid, pkgmgr_ret);
722 return APP2EXT_ERROR_PKGMGR_ERROR;
727 _app2sd_make_result_info_file((char*)pkgid, ret, uid);
732 int app2sd_usr_pre_app_upgrade(const char *pkgid, GList* dir_list,
735 int ret = APP2EXT_SUCCESS;
736 char loopback_device[FILENAME_MAX] = { 0, };
737 char application_path[FILENAME_MAX] = { 0, };
738 char temp_uid[32] = { 0, };
739 char *temp_pkgid = NULL;
740 char *temp_loopback_device = NULL;
741 char *temp_application_path = NULL;
742 char *device_node = NULL;
743 char *encoded_id = NULL;
744 char *temp_encoded_id = NULL;
746 unsigned long long curr_size = 0;
748 int reqd_disk_size = size + ceil(size * 0.2);
750 /* validate function arguments*/
751 if (pkgid == NULL || dir_list == NULL || size<=0) {
752 _E("invalid function arguments");
753 return APP2EXT_ERROR_INVALID_ARGUMENTS;
756 /* check whether MMC is present or not */
757 ret = _app2sd_check_mmc_status();
759 _E("MMC not preset OR Not ready (%d)", ret);
760 return APP2EXT_ERROR_MMC_STATUS;
763 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
764 if (encoded_id == NULL) {
765 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
767 if (_is_global(uid)) {
768 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
769 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
770 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
771 APP2SD_PATH, encoded_id);
773 tzplatform_set_user(uid);
774 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
775 tzplatform_getenv(TZ_USER_APP), pkgid);
776 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
777 APP2SD_PATH, encoded_id);
778 tzplatform_reset_user();
782 /* check app entry is there in sd card or not. */
783 fp = fopen(loopback_device, "r+");
785 _E("app entry is not present in SD Card");
786 return APP2EXT_ERROR_INVALID_PACKAGE;
790 /* get installed app size*/
791 curr_size = _app2sd_calculate_file_size(loopback_device);
792 curr_size = (curr_size) / (1024 * 1024);
793 if (curr_size == 0) {
794 _E("app entry is not present in SD Card");
795 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
797 if ((int)curr_size < reqd_disk_size) {
798 len = strlen(pkgid) + strlen(".new");
799 temp_pkgid = calloc(len + 1, sizeof(char));
800 if (temp_pkgid == NULL) {
801 _E("memory alloc failed");
802 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
804 snprintf(temp_pkgid, len, "%s.new", pkgid);
806 if (_is_global(uid)) {
807 len = strlen(tzplatform_getenv(TZ_SYS_RW_APP)) + strlen(temp_pkgid) + 1;
808 temp_application_path = calloc(len + 1, sizeof(char));
809 if (temp_application_path == NULL) {
810 _E("memory alloc failed");
812 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
814 snprintf(temp_application_path, len, "%s/%s",
815 tzplatform_getenv(TZ_SYS_RW_APP), temp_pkgid);
817 temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
818 if (temp_encoded_id == NULL) {
820 free(temp_application_path);
821 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
823 len = strlen(APP2SD_PATH) + strlen(temp_encoded_id) + 1;
824 temp_loopback_device = calloc(len + 1, sizeof(char));
825 if (temp_loopback_device == NULL) {
826 _E("memory alloc failed");
828 free(temp_application_path);
829 free(temp_encoded_id);
830 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
832 snprintf(temp_loopback_device, len, "%s/%s",
833 APP2SD_PATH, temp_encoded_id);
834 free(temp_encoded_id);
836 tzplatform_set_user(uid);
837 len = strlen(tzplatform_getenv(TZ_USER_APP)) + strlen(temp_pkgid) + 1;
838 temp_application_path = calloc(len + 1, sizeof(char));
839 if (temp_application_path == NULL) {
840 _E("memory alloc failed");
842 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
844 snprintf(temp_application_path, len, "%s/%s",
845 tzplatform_getenv(TZ_USER_APP), temp_pkgid);
847 temp_encoded_id = _app2sd_get_encoded_name((const char*)temp_pkgid, uid);
848 if (temp_encoded_id == NULL) {
850 free(temp_application_path);
851 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
853 snprintf(temp_uid, 32, "%d", uid);
854 len = strlen(APP2SD_PATH) + strlen(temp_uid) + strlen(temp_encoded_id) + 2;
855 temp_loopback_device = calloc(len + 1, sizeof(char));
856 if (temp_loopback_device == NULL) {
857 _E("memory alloc failed");
859 free(temp_application_path);
860 free(temp_encoded_id);
861 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
863 snprintf(temp_loopback_device, len, "%s/%s",
864 APP2SD_PATH, temp_encoded_id);
865 free(temp_encoded_id);
866 tzplatform_reset_user();
868 ret = _app2sd_update_loopback_device_size(pkgid,
869 loopback_device, application_path, temp_pkgid,
870 temp_loopback_device, temp_application_path,
871 reqd_disk_size, dir_list, uid);
873 free(temp_application_path);
874 free(temp_loopback_device);
875 if (APP2EXT_SUCCESS != ret) {
876 _E("failed to update loopback device size");
881 /* get the associated device node for SD card applicationer */
882 device_node = _app2sd_find_associated_device_node(loopback_device);
883 if (NULL == device_node) {
884 /* do loopback setup */
885 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
886 loopback_device, uid);
887 if (device_node == NULL) {
888 _E("loopback encryption setup failed");
889 return APP2EXT_ERROR_DO_LOSETUP;
893 ret = _app2sd_mount_app_content(application_path, pkgid,
894 device_node, MOUNT_TYPE_RW, dir_list,
895 APP2SD_PRE_UPGRADE, uid);
902 return APP2EXT_ERROR_MOUNT_PATH;
906 ret = _app2sd_mount_app_content(application_path, pkgid,
907 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
908 APP2SD_PRE_UPGRADE, uid);
910 _E("remount failed");
915 return APP2EXT_ERROR_MOUNT_PATH;
926 int app2sd_usr_post_app_upgrade(const char *pkgid,
927 app2ext_status install_status, uid_t uid)
929 char *device_name = NULL;
930 char loopback_device[FILENAME_MAX] = { 0, };
931 char application_path[FILENAME_MAX] = { 0, };
932 char *encoded_id = NULL;
933 int ret = APP2EXT_SUCCESS;
935 /* validate the function parameter recieved */
936 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
937 || install_status > APP2EXT_STATUS_SUCCESS) {
938 _E("invalid func parameters");
939 return APP2EXT_ERROR_INVALID_ARGUMENTS;
942 /* check whether MMC is present or not */
943 ret = _app2sd_check_mmc_status();
945 _E("MMC not preset OR Not ready (%d)", ret);
946 return APP2EXT_ERROR_MMC_STATUS;
949 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
950 if (encoded_id == NULL) {
951 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
953 if (_is_global(uid)) {
954 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
955 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
956 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
957 APP2SD_PATH, encoded_id);
959 tzplatform_set_user(uid);
960 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
961 tzplatform_getenv(TZ_USER_APP), pkgid);
962 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
963 APP2SD_PATH, encoded_id);
964 tzplatform_reset_user();
968 /* get the associated device node for SD card applicationer */
969 device_name = _app2sd_find_associated_device_node(loopback_device);
970 if (NULL == device_name) {
971 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
974 ret = _app2sd_unmount_app_content(application_path);
980 _E("unable to unmount the app content (%d)", ret);
981 return APP2EXT_ERROR_UNMOUNT;
984 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
990 _E("unable to detach the loopback encryption " \
991 "setup for the application");
992 return APP2EXT_ERROR_UNMOUNT;
1003 int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
1005 char loopback_device[FILENAME_MAX] = { 0, };
1006 char application_path[FILENAME_MAX] = { 0, };
1007 char *encoded_id = NULL;
1008 int ret = APP2EXT_SUCCESS;
1010 /* validate the function parameter recieved */
1011 if (pkgid == NULL) {
1012 _E("invalid func parameters");
1013 return APP2EXT_ERROR_INVALID_ARGUMENTS;
1018 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1019 if (encoded_id == NULL) {
1020 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1022 if (_is_global(uid)) {
1023 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1024 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1025 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1026 APP2SD_PATH, encoded_id);
1028 tzplatform_set_user(uid);
1029 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1030 tzplatform_getenv(TZ_USER_APP), pkgid);
1031 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1032 APP2SD_PATH, encoded_id);
1033 tzplatform_reset_user();
1037 /* unmount the loopback encrypted pseudo device from the application installation path */
1038 ret = _app2sd_unmount_app_content(application_path);
1040 _E("unable to unmount the app content (%d)", ret);
1043 /* detach the loopback encryption setup for the application */
1044 ret = _app2sd_remove_all_loopback_encryption_setups(pkgid);
1046 _E("unable to detach the loopback encryption setup for the application");
1049 /* delete the loopback device from the SD card */
1050 ret = _app2sd_delete_loopback_device(loopback_device);
1052 _E("unable to detach the loopback encryption setup for the application");
1055 /* delete symlink */
1056 _app2sd_delete_symlink(application_path);
1058 /* remove passwrd from DB */
1059 ret = _app2sd_initialize_db();
1061 _E("app2sd db initialize failed");
1063 ret = _app2sd_remove_password_from_db(pkgid);
1065 _E("cannot remove password from db");