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 mode_t mode = DIR_PERMS;
36 ret = mkdir(APP2SD_PATH, mode);
38 if (errno != EEXIST) {
39 _E("create directory failed," \
40 " error no is (%d)", errno);
41 return APP2EXT_ERROR_CREATE_DIRECTORY;
45 return APP2EXT_SUCCESS;
48 int app2sd_usr_pre_app_install(const char *pkgid, GList* dir_list, int size, uid_t uid)
52 char *device_node = NULL;
55 char application_path[FILENAME_MAX] = { 0, };
56 char loopback_device[FILENAME_MAX] = { 0, };
57 char *encoded_id = NULL;
58 int reqd_disk_size = size + ceil(size * 0.2);
60 /* validate the function parameter recieved */
61 if (pkgid == NULL || dir_list == NULL || size <= 0) {
62 _E("invalid function arguments");
63 return APP2EXT_ERROR_INVALID_ARGUMENTS;
66 /* check whether MMC is present or not */
67 ret = _app2sd_check_mmc_status();
69 _E("MMC not preset OR Not ready (%d)", ret);
70 return APP2EXT_ERROR_MMC_STATUS;
73 /* find available free memory in the MMC card */
74 ret = _app2sd_get_available_free_memory(MMC_PATH, &free_mmc_mem);
76 _E("unable to get available free memory in MMC (%d)",
78 return APP2EXT_ERROR_MMC_STATUS;
80 _D("size details for application installation:" \
81 " size=(%d)MB, reqd_disk_size=(%d)MB, free_mmc_size=(%d)MB",
82 size, reqd_disk_size, free_mmc_mem);
84 /* if avaialalbe free memory in MMC is less than required size + 5MB,
87 if ((reqd_disk_size + PKG_BUF_SIZE + MEM_BUF_SIZE) > free_mmc_mem) {
88 _E("insufficient memory in MMC for"
89 " application installation (%d)", ret);
90 return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
93 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
94 if (encoded_id == NULL) {
95 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
97 if (_is_global(uid)) {
98 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
99 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
100 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
101 APP2SD_PATH, encoded_id);
103 tzplatform_set_user(uid);
104 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
105 tzplatform_getenv(TZ_USER_APP), pkgid);
106 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
107 APP2SD_PATH, encoded_id);
108 tzplatform_reset_user();
111 _D("application_path = (%s)", application_path);
112 _D("loopback_device = (%s)", loopback_device);
114 ret = __app2sd_create_app2sd_directories(uid);
116 _E("failed to create app2sd dirs");
120 /* check same loopback_device existence */
121 result = (char *)_app2sd_find_associated_device(loopback_device);
122 if (result != NULL) {
123 _E("there is same associated File (%s)", loopback_device);
124 return APP2EXT_ERROR_SAME_LOOPBACK_DEVICE_EXISTS;
127 /* create a loopback device */
128 ret = _app2sd_create_loopback_device(pkgid, loopback_device,
129 (reqd_disk_size + PKG_BUF_SIZE));
131 _W("package already present, delete app directory");
132 ret = _app2sd_delete_directory(application_path);
134 _E("unable to delete the directory (%s)",
140 /* perform loopback encryption setup */
141 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
142 loopback_device, uid);
144 _E("loopback encryption setup failed");
145 _app2sd_delete_loopback_device(loopback_device);
146 return APP2EXT_ERROR_DO_LOSETUP;
149 /* check whether loopback device is associated
150 * with device node or not
152 devi = _app2sd_find_associated_device_node(loopback_device);
154 _E("finding associated device node failed");
155 ret = APP2EXT_ERROR_DO_LOSETUP;
159 /* format the loopback file system */
160 ret = _app2sd_create_file_system(device_node);
162 _E("creating FS failed failed");
163 ret = APP2EXT_ERROR_CREATE_FS;
167 /* mount the loopback encrypted pseudo device on application
168 * installation path as with Read Write permission
170 ret =_app2sd_mount_app_content(application_path, pkgid,
171 device_node, MOUNT_TYPE_RW, dir_list,
172 APP2SD_PRE_INSTALL, uid);
174 _E("mounting dev path to app install path failed");
175 ret = APP2EXT_ERROR_MOUNT_PATH;
180 ret = APP2EXT_SUCCESS;
185 result = _app2sd_detach_loop_device(device_node);
190 _app2sd_delete_loopback_device(loopback_device);
207 int app2sd_usr_post_app_install(const char *pkgid,
208 app2ext_status install_status, uid_t uid)
210 char *device_name = NULL;
211 char application_path[FILENAME_MAX] = { 0, };
212 char loopback_device[FILENAME_MAX] = { 0, };
213 char *encoded_id = NULL;
214 int ret = APP2EXT_SUCCESS;
217 /* validate the function parameter recieved */
218 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
219 || install_status > APP2EXT_STATUS_SUCCESS) {
220 _E("invalid func parameters");
221 return APP2EXT_ERROR_INVALID_ARGUMENTS;
224 /* check whether MMC is present or not */
225 ret = _app2sd_check_mmc_status();
227 _E("MMC not present OR Not ready (%d)", ret);
228 return APP2EXT_ERROR_MMC_STATUS;
232 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
233 if (encoded_id == NULL) {
234 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
236 if (_is_global(uid)) {
237 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
238 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
239 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
240 APP2SD_PATH, encoded_id);
242 tzplatform_set_user(uid);
243 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
244 tzplatform_getenv(TZ_USER_APP), pkgid);
245 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
246 APP2SD_PATH, encoded_id);
247 tzplatform_reset_user();
250 _D("application_path = (%s)", application_path);
251 _D("loopback_device = (%s)", loopback_device);
253 /* get the associated device node for SD card applicationer */
254 device_name = _app2sd_find_associated_device_node(loopback_device);
255 if (NULL == device_name) {
256 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
259 ret = _app2sd_unmount_app_content(application_path);
265 _E("unable to unmount the app content (%d)", ret);
266 return APP2EXT_ERROR_UNMOUNT;
269 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
275 _E("unable to detach the loopback encryption setup" \
276 " for the application");
277 return APP2EXT_ERROR_UNMOUNT;
285 /* take appropriate action based on
286 * installation status of application package
288 if (install_status == APP2EXT_STATUS_FAILED) {
289 /* delete the loopback device from the SD card */
290 ret = _app2sd_delete_loopback_device(loopback_device);
292 _E("unable to delete the loopback device from the SD Card");
293 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
295 ret = _app2sd_remove_password_from_db(pkgid);
298 _E("unable to delete the password");
300 ret = _app2sd_delete_directory(application_path);
303 _E("unable to delete the directory (%s)", application_path);
305 /* if the status is success, then update installed storage
306 * to pkgmgr_parser db
308 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
309 INSTALL_EXTERNAL, uid);
310 if (pkgmgr_ret < 0) {
311 _E("fail to update installed location " \
312 "to db[%s, %d] of uid(%d), pkgmgr ret(%d)",
313 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
314 return APP2EXT_ERROR_PKGMGR_ERROR;
321 int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
323 int ret = APP2EXT_SUCCESS;
324 char application_path[FILENAME_MAX] = { 0, };
325 char loopback_device[FILENAME_MAX] = { 0, };
326 char *encoded_id = NULL;
327 char *device_node = NULL;
331 /* validate the function parameter recieved */
333 _E("invalid function arguments to app launch setup");
334 return APP2EXT_ERROR_INVALID_ARGUMENTS;
337 /* check whether MMC is present or not */
338 ret = _app2sd_check_mmc_status();
340 _E("MMC not preset OR Not ready (%d)", ret);
341 return APP2EXT_ERROR_MMC_STATUS;
344 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
345 if (encoded_id == NULL) {
346 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
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",
353 APP2SD_PATH, encoded_id);
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",
359 APP2SD_PATH, encoded_id);
360 tzplatform_reset_user();
363 _D("application_path = (%s)", application_path);
364 _D("loopback_device = (%s)", loopback_device);
366 fp = fopen(loopback_device, "r+");
368 _E("app entry is not present in SD Card");
369 return APP2EXT_ERROR_INVALID_PACKAGE;
373 result = (char *)_app2sd_find_associated_device(loopback_device);
374 /* process the string */
375 if ((result != NULL) && strstr(result, "/dev") != NULL) {
376 _E("already associated");
379 return APP2EXT_ERROR_ALREADY_MOUNTED;
382 /* do loopback setup */
383 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
384 loopback_device, uid);
385 if (device_node == NULL) {
386 _E("loopback encryption setup failed");
387 return APP2EXT_ERROR_DO_LOSETUP;
391 ret = _app2sd_mount_app_content(application_path, pkgid,
392 device_node, MOUNT_TYPE_RD, NULL, APP2SD_APP_LAUNCH, uid);
399 return APP2EXT_ERROR_MOUNT_PATH;
410 int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
412 int ret = APP2EXT_SUCCESS;
413 char application_path[FILENAME_MAX] = { 0, };
414 char loopback_device[FILENAME_MAX] = { 0, };
415 char *encoded_id = NULL;
418 /* validate the function parameter recieved */
420 _E("invalid function arguments to app launch setup");
421 return APP2EXT_ERROR_INVALID_ARGUMENTS;
424 /* check whether MMC is present or not */
425 ret = _app2sd_check_mmc_status();
427 _E("MMC not preset OR Not ready (%d)", ret);
428 return APP2EXT_ERROR_MMC_STATUS;
431 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
432 if (encoded_id == NULL) {
433 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
435 /* check app entry is there in sd card or not. */
436 if (_is_global(uid)) {
437 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
438 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
439 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
440 APP2SD_PATH, encoded_id);
442 tzplatform_set_user(uid);
443 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
444 tzplatform_getenv(TZ_USER_APP), pkgid);
445 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
446 APP2SD_PATH, encoded_id);
447 tzplatform_reset_user();
450 _D("application_path = (%s)", application_path);
451 _D("loopback_device = (%s)", loopback_device);
453 fp = fopen(loopback_device, "r+");
455 _E("app entry is not present in SD Card");
456 return APP2EXT_ERROR_INVALID_PACKAGE;
460 ret = _app2sd_unmount_app_content(application_path);
462 _E("unable to unmount the SD application");
463 return APP2EXT_ERROR_UNMOUNT;
466 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
468 _E("unable to remove loopback setup");
469 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
475 int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
477 int ret = APP2EXT_SUCCESS;
478 char application_path[FILENAME_MAX] = { 0, };
479 char loopback_device[FILENAME_MAX] = { 0, };
480 char *encoded_id = NULL;
481 char *device_node = NULL;
484 /* validate the function parameter recieved */
486 _E("invalid function arguments to app launch setup");
487 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
491 /* check whether MMC is present or not */
492 ret = _app2sd_check_mmc_status();
494 _E("MMC not preset OR Not ready (%d)", ret);
495 ret = APP2EXT_ERROR_MMC_STATUS;
499 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
500 if (encoded_id == NULL) {
501 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
503 if (_is_global(uid)) {
504 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
505 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
506 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
507 APP2SD_PATH, encoded_id);
509 tzplatform_set_user(uid);
510 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
511 tzplatform_getenv(TZ_USER_APP), pkgid);
512 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
513 APP2SD_PATH, encoded_id);
514 tzplatform_reset_user();
517 _D("application_path = (%s)", application_path);
518 _D("loopback_device = (%s)", loopback_device);
520 /* check app entry is there in sd card or not. */
521 fp = fopen(loopback_device, "r+");
523 _E("app entry is not present in SD Card");
524 ret = APP2EXT_ERROR_INVALID_PACKAGE;
529 /* get the associated device node for SD card applicationer */
530 device_node = _app2sd_find_associated_device_node(loopback_device);
531 if (NULL == device_node) {
532 /* do loopback setup */
533 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
534 loopback_device, uid);
535 if (device_node == NULL) {
536 _E("loopback encryption setup failed");
537 ret = APP2EXT_ERROR_DO_LOSETUP;
541 ret = _app2sd_mount_app_content(application_path, pkgid,
542 device_node, MOUNT_TYPE_RW, NULL,
543 APP2SD_PRE_UNINSTALL, uid);
550 ret = APP2EXT_ERROR_MOUNT_PATH;
555 ret = _app2sd_mount_app_content(application_path, pkgid,
556 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
557 APP2SD_PRE_UNINSTALL, uid);
559 _E("remount failed");
564 ret = APP2EXT_ERROR_MOUNT_PATH;
577 int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
579 char application_path[FILENAME_MAX] = { 0, };
580 char loopback_device[FILENAME_MAX] = { 0, };
581 char *encoded_id = NULL;
582 int ret = APP2EXT_SUCCESS;
584 /* validate the function parameter recieved */
586 _E("invalid function arguments");
587 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
591 /* check whether MMC is present or not */
592 ret = _app2sd_check_mmc_status();
594 _E("MMC not preset OR Not ready (%d)", ret);
595 ret = APP2EXT_ERROR_MMC_STATUS;
599 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
600 if (encoded_id == NULL) {
601 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
603 if (_is_global(uid)) {
604 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
605 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
606 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
607 APP2SD_PATH, encoded_id);
609 tzplatform_set_user(uid);
610 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
611 tzplatform_getenv(TZ_USER_APP), pkgid);
612 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
613 APP2SD_PATH, encoded_id);
614 tzplatform_reset_user();
617 _D("application_path = (%s)", application_path);
618 _D("loopback_device = (%s)", loopback_device);
620 /* unmount the loopback encrypted pseudo device from
621 * the application installation path
623 ret = _app2sd_unmount_app_content(application_path);
625 _E("unable to unmount the app content (%d)", ret);
626 ret = APP2EXT_ERROR_UNMOUNT;
629 /* detach the loopback encryption setup for the application */
630 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
632 _E("unable to Detach the loopback encryption setup" \
633 " for the application");
634 ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
638 /* delete the loopback device from the SD card */
639 ret = _app2sd_delete_loopback_device(loopback_device);
641 _E("unable to delete the " \
642 "loopback device from the SD Card");
643 ret = APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
647 ret = _app2sd_delete_directory(application_path);
649 _E("unable to delete the directory (%s)",
654 /* remove encryption password from DB */
655 ret = _app2sd_initialize_db();
657 _E("app2sd db initialize failed");
658 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
662 ret = _app2sd_remove_password_from_db(pkgid);
664 _E("cannot remove password from db");
665 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
673 int app2sd_usr_move_installed_app(const char *pkgid, GList* dir_list,
674 app2ext_move_type move_type, uid_t uid)
679 /* validate function arguments */
680 if (pkgid == NULL || dir_list == NULL
681 || move_type < APP2EXT_MOVE_TO_EXT
682 || move_type > APP2EXT_MOVE_TO_PHONE) {
683 _E("invalid function arguments");
684 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
688 pkgmgrinfo_pkginfo_h info_handle = NULL;
689 pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
690 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &info_handle);
691 if (pkgmgr_ret < 0) {
692 _E("failed to get pkginfo for pkg(%s), uid(%d), pkgmgr_ret(%d)",
693 pkgid, uid, pkgmgr_ret);
695 pkgmgr_ret = pkgmgrinfo_pkginfo_get_installed_storage(info_handle, &storage);
696 if (pkgmgr_ret < 0) {
697 _E("failed to get installed storage for pkg(%s) of uid(%d), pkgmgr_ret(%d)",
698 pkgid, uid, pkgmgr_ret);
699 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
703 if ((move_type == APP2EXT_MOVE_TO_EXT && storage == PMINFO_EXTERNAL_STORAGE)
704 || (move_type == APP2EXT_MOVE_TO_PHONE && storage == PMINFO_INTERNAL_STORAGE)) {
705 ret = APP2EXT_ERROR_PKG_EXISTS;
706 _E("PKG_EXISTS in [%d] STORAGE", storage);
707 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
710 _D("pkgid[%s] move to STORAGE [%d]", pkgid, storage);
712 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
714 ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid);
716 _D("unable to move application");
720 /* if move is completed, then update installed storage to pkgmgr_parser db */
721 if (move_type == APP2EXT_MOVE_TO_EXT) {
722 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
723 INSTALL_EXTERNAL, uid);
724 if (pkgmgr_ret < 0) {
725 _E("failed to update installed location to db " \
726 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
727 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
728 return APP2EXT_ERROR_PKGMGR_ERROR;
731 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
732 INSTALL_INTERNAL, uid);
733 if (pkgmgr_ret < 0) {
734 _E("failed to update installed location to db " \
735 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
736 pkgid, INSTALL_INTERNAL, uid, pkgmgr_ret);
737 return APP2EXT_ERROR_PKGMGR_ERROR;
742 _app2sd_make_result_info_file((char*)pkgid, ret, uid);
747 int app2sd_usr_pre_app_upgrade(const char *pkgid, GList* dir_list,
750 int ret = APP2EXT_SUCCESS;
751 char loopback_device[FILENAME_MAX] = { 0, };
752 char application_path[FILENAME_MAX] = { 0, };
753 char temp_uid[32] = { 0, };
754 char *temp_pkgid = NULL;
755 char *temp_loopback_device = NULL;
756 char *temp_application_path = NULL;
757 char *device_node = NULL;
758 char *encoded_id = NULL;
759 char *temp_encoded_id = NULL;
761 unsigned long long curr_size = 0;
763 int reqd_disk_size = size + ceil(size * 0.2);
765 /* validate function arguments*/
766 if (pkgid == NULL || dir_list == NULL || size<=0) {
767 _E("invalid function arguments");
768 return APP2EXT_ERROR_INVALID_ARGUMENTS;
771 /* check whether MMC is present or not */
772 ret = _app2sd_check_mmc_status();
774 _E("MMC not preset OR Not ready (%d)", ret);
775 return APP2EXT_ERROR_MMC_STATUS;
778 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
779 if (encoded_id == NULL) {
780 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
782 if (_is_global(uid)) {
783 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
784 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
785 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
786 APP2SD_PATH, encoded_id);
788 tzplatform_set_user(uid);
789 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
790 tzplatform_getenv(TZ_USER_APP), pkgid);
791 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
792 APP2SD_PATH, encoded_id);
793 tzplatform_reset_user();
796 _D("application_path = (%s)", application_path);
797 _D("loopback_device = (%s)", loopback_device);
799 /* check app entry is there in sd card or not. */
800 fp = fopen(loopback_device, "r+");
802 _E("app entry is not present in SD Card");
803 return APP2EXT_ERROR_INVALID_PACKAGE;
807 /* get installed app size*/
808 curr_size = _app2sd_calculate_file_size(loopback_device);
809 curr_size = (curr_size) / (1024 * 1024);
810 if (curr_size == 0) {
811 _E("app entry is not present in SD Card");
812 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
814 if ((int)curr_size < reqd_disk_size) {
815 len = strlen(pkgid) + strlen(".new");
816 temp_pkgid = calloc(len + 1, sizeof(char));
817 if (temp_pkgid == NULL) {
818 _E("memory alloc failed");
819 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
821 snprintf(temp_pkgid, len, "%s.new", pkgid);
823 if (_is_global(uid)) {
824 len = strlen(tzplatform_getenv(TZ_SYS_RW_APP)) + strlen(temp_pkgid) + 1;
825 temp_application_path = calloc(len + 1, sizeof(char));
826 if (temp_application_path == NULL) {
827 _E("memory alloc failed");
829 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
831 snprintf(temp_application_path, len, "%s/%s",
832 tzplatform_getenv(TZ_SYS_RW_APP), temp_pkgid);
834 temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
835 if (temp_encoded_id == NULL) {
837 free(temp_application_path);
838 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
840 len = strlen(APP2SD_PATH) + strlen(temp_encoded_id) + 1;
841 temp_loopback_device = calloc(len + 1, sizeof(char));
842 if (temp_loopback_device == NULL) {
843 _E("memory alloc failed");
845 free(temp_application_path);
846 free(temp_encoded_id);
847 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
849 snprintf(temp_loopback_device, len, "%s/%s",
850 APP2SD_PATH, temp_encoded_id);
851 free(temp_encoded_id);
853 tzplatform_set_user(uid);
854 len = strlen(tzplatform_getenv(TZ_USER_APP)) + strlen(temp_pkgid) + 1;
855 temp_application_path = calloc(len + 1, sizeof(char));
856 if (temp_application_path == NULL) {
857 _E("memory alloc failed");
859 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
861 snprintf(temp_application_path, len, "%s/%s",
862 tzplatform_getenv(TZ_USER_APP), temp_pkgid);
864 temp_encoded_id = _app2sd_get_encoded_name((const char*)temp_pkgid, uid);
865 if (temp_encoded_id == NULL) {
867 free(temp_application_path);
868 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
870 snprintf(temp_uid, 32, "%d", uid);
871 len = strlen(APP2SD_PATH) + strlen(temp_uid) + strlen(temp_encoded_id) + 2;
872 temp_loopback_device = calloc(len + 1, sizeof(char));
873 if (temp_loopback_device == NULL) {
874 _E("memory alloc failed");
876 free(temp_application_path);
877 free(temp_encoded_id);
878 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
880 snprintf(temp_loopback_device, len, "%s/%s",
881 APP2SD_PATH, temp_encoded_id);
882 free(temp_encoded_id);
883 tzplatform_reset_user();
885 ret = _app2sd_update_loopback_device_size(pkgid,
886 loopback_device, application_path, temp_pkgid,
887 temp_loopback_device, temp_application_path,
888 reqd_disk_size, dir_list, uid);
890 free(temp_application_path);
891 free(temp_loopback_device);
892 if (APP2EXT_SUCCESS != ret) {
893 _E("failed to update loopback device size");
898 /* get the associated device node for SD card applicationer */
899 device_node = _app2sd_find_associated_device_node(loopback_device);
900 if (NULL == device_node) {
901 /* do loopback setup */
902 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
903 loopback_device, uid);
904 if (device_node == NULL) {
905 _E("loopback encryption setup failed");
906 return APP2EXT_ERROR_DO_LOSETUP;
910 ret = _app2sd_mount_app_content(application_path, pkgid,
911 device_node, MOUNT_TYPE_RW, dir_list,
912 APP2SD_PRE_UPGRADE, uid);
919 return APP2EXT_ERROR_MOUNT_PATH;
923 ret = _app2sd_mount_app_content(application_path, pkgid,
924 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
925 APP2SD_PRE_UPGRADE, uid);
927 _E("remount failed");
932 return APP2EXT_ERROR_MOUNT_PATH;
943 int app2sd_usr_post_app_upgrade(const char *pkgid,
944 app2ext_status install_status, uid_t uid)
946 char *device_name = NULL;
947 char loopback_device[FILENAME_MAX] = { 0, };
948 char application_path[FILENAME_MAX] = { 0, };
949 char *encoded_id = NULL;
950 int ret = APP2EXT_SUCCESS;
952 /* validate the function parameter recieved */
953 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
954 || install_status > APP2EXT_STATUS_SUCCESS) {
955 _E("invalid func parameters");
956 return APP2EXT_ERROR_INVALID_ARGUMENTS;
959 /* check whether MMC is present or not */
960 ret = _app2sd_check_mmc_status();
962 _E("MMC not preset OR Not ready (%d)", ret);
963 return APP2EXT_ERROR_MMC_STATUS;
966 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
967 if (encoded_id == NULL) {
968 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
970 if (_is_global(uid)) {
971 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
972 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
973 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
974 APP2SD_PATH, encoded_id);
976 tzplatform_set_user(uid);
977 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
978 tzplatform_getenv(TZ_USER_APP), pkgid);
979 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
980 APP2SD_PATH, encoded_id);
981 tzplatform_reset_user();
984 _D("application_path = (%s)", application_path);
985 _D("loopback_device = (%s)", loopback_device);
987 /* get the associated device node for SD card applicationer */
988 device_name = _app2sd_find_associated_device_node(loopback_device);
989 if (NULL == device_name) {
990 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
993 ret = _app2sd_unmount_app_content(application_path);
999 _E("unable to unmount the app content (%d)", ret);
1000 return APP2EXT_ERROR_UNMOUNT;
1003 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
1009 _E("unable to detach the loopback encryption " \
1010 "setup for the application");
1011 return APP2EXT_ERROR_UNMOUNT;
1022 int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
1024 char loopback_device[FILENAME_MAX] = { 0, };
1025 char application_path[FILENAME_MAX] = { 0, };
1026 char *encoded_id = NULL;
1027 int ret = APP2EXT_SUCCESS;
1029 _D("start force_clean [%s]", pkgid);
1031 /* validate the function parameter recieved */
1032 if (pkgid == NULL) {
1033 _E("invalid func parameters");
1034 return APP2EXT_ERROR_INVALID_ARGUMENTS;
1039 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1040 if (encoded_id == NULL) {
1041 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1043 if (_is_global(uid)) {
1044 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1045 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1046 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1047 APP2SD_PATH, encoded_id);
1049 tzplatform_set_user(uid);
1050 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1051 tzplatform_getenv(TZ_USER_APP), pkgid);
1052 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1053 APP2SD_PATH, encoded_id);
1054 tzplatform_reset_user();
1057 _D("application_path = (%s)", application_path);
1058 _D("loopback_device = (%s)", loopback_device);
1060 /* unmount the loopback encrypted pseudo device from the application installation path */
1061 ret = _app2sd_unmount_app_content(application_path);
1063 _E("unable to unmount the app content (%d)", ret);
1066 /* detach the loopback encryption setup for the application */
1067 ret = _app2sd_remove_all_loopback_encryption_setups(pkgid);
1069 _E("unable to detach the loopback encryption setup for the application");
1072 /* delete the loopback device from the SD card */
1073 ret = _app2sd_delete_loopback_device(loopback_device);
1075 _E("unable to detach the loopback encryption setup for the application");
1078 /* delete symlink */
1079 _app2sd_delete_symlink(application_path);
1081 /* remove passwrd from DB */
1082 ret = _app2sd_initialize_db();
1084 _E("app2sd db initialize failed");
1086 ret = _app2sd_remove_password_from_db(pkgid);
1088 _E("cannot remove password from db");
1091 _D("finish force_clean");