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;
50 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
54 char application_path[FILENAME_MAX] = { 0, };
55 char loopback_device[FILENAME_MAX] = { 0, };
56 char *encoded_id = NULL;
57 int reqd_disk_size = size + ceil(size * 0.2);
59 /* validate the function parameter recieved */
60 if (pkgid == NULL || dir_list == NULL || size <= 0) {
61 _E("invalid function arguments");
62 return APP2EXT_ERROR_INVALID_ARGUMENTS;
65 /* check whether MMC is present or not */
66 ret = _app2sd_check_mmc_status();
68 _E("MMC not preset OR Not ready (%d)", ret);
69 return APP2EXT_ERROR_MMC_STATUS;
72 /* find available free memory in the MMC card */
73 ret = _app2sd_get_available_free_memory(MMC_PATH, &free_mmc_mem);
75 _E("unable to get available free memory in MMC (%d)",
77 return APP2EXT_ERROR_MMC_STATUS;
79 _D("size details for application installation:" \
80 " size=(%d)MB, reqd_disk_size=(%d)MB, free_mmc_size=(%d)MB",
81 size, reqd_disk_size, free_mmc_mem);
83 /* if avaialalbe free memory in MMC is less than required size + 5MB,
86 if ((reqd_disk_size + PKG_BUF_SIZE + MEM_BUF_SIZE) > free_mmc_mem) {
87 _E("insufficient memory in MMC for"
88 " application installation (%d)", ret);
89 return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
92 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
93 if (encoded_id == NULL)
94 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
96 if (_is_global(uid)) {
97 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
98 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
99 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
100 APP2SD_PATH, encoded_id);
102 tzplatform_set_user(uid);
103 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
104 tzplatform_getenv(TZ_USER_APP), pkgid);
105 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
106 APP2SD_PATH, encoded_id);
107 tzplatform_reset_user();
111 ret = __app2sd_create_app2sd_directories(uid);
113 _E("failed to create app2sd dirs");
117 /* check same loopback_device existence */
118 result = (char *)_app2sd_find_associated_device(loopback_device);
119 if (result != NULL) {
120 _E("there is same associated File (%s)", loopback_device);
121 return APP2EXT_ERROR_SAME_LOOPBACK_DEVICE_EXISTS;
124 /* create a loopback device */
125 ret = _app2sd_create_loopback_device(pkgid, loopback_device,
126 (reqd_disk_size + PKG_BUF_SIZE));
128 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
129 ret = _app2sd_dmcrypt_setup_device(pkgid, loopback_device, false, uid);
131 _E("dmcrypt setup device error");
132 return APP2EXT_ERROR_SETUP_DMCRYPT_DEVICE;
135 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
136 false, uid, &device_node);
138 _E("dmcrypt open device error");
139 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
142 /* perform loopback encryption setup */
143 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
144 loopback_device, uid);
146 _E("loopback encryption setup failed");
147 _app2sd_delete_loopback_device(loopback_device);
148 return APP2EXT_ERROR_DO_LOSETUP;
151 /* check whether loopback device is associated
152 * with device node or not
154 devi = _app2sd_find_associated_device_node(loopback_device);
156 _E("finding associated device node failed");
157 ret = APP2EXT_ERROR_DO_LOSETUP;
162 /* format the loopback file system */
163 ret = _app2sd_create_file_system(device_node);
165 _E("creating FS failed failed");
166 ret = APP2EXT_ERROR_CREATE_FS;
170 /* mount the loopback encrypted pseudo device on application
171 * installation path as with Read Write permission
173 ret = _app2sd_mount_app_content(application_path, pkgid,
174 device_node, MOUNT_TYPE_RW, dir_list,
175 APP2SD_PRE_INSTALL, uid);
177 _E("mounting dev path to app install path failed");
178 ret = APP2EXT_ERROR_MOUNT_PATH;
183 ret = APP2EXT_SUCCESS;
188 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
189 ret = _app2sd_dmcrypt_close_device(pkgid, uid);
191 _E("close dmcrypt device error(%d)", ret);
192 _app2sd_delete_loopback_device(loopback_device);
194 result = _app2sd_detach_loop_device(device_node);
199 _app2sd_delete_loopback_device(loopback_device);
209 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
219 int app2sd_usr_post_app_install(const char *pkgid,
220 app2ext_status install_status, uid_t uid)
222 char *device_name = NULL;
223 char application_path[FILENAME_MAX] = { 0, };
224 char loopback_device[FILENAME_MAX] = { 0, };
225 char *encoded_id = NULL;
226 int ret = APP2EXT_SUCCESS;
229 /* validate the function parameter recieved */
230 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
231 || install_status > APP2EXT_STATUS_SUCCESS) {
232 _E("invalid func parameters");
233 return APP2EXT_ERROR_INVALID_ARGUMENTS;
236 /* check whether MMC is present or not */
237 ret = _app2sd_check_mmc_status();
239 _E("MMC not present OR Not ready (%d)", ret);
240 return APP2EXT_ERROR_MMC_STATUS;
244 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
245 if (encoded_id == NULL)
246 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
248 if (_is_global(uid)) {
249 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
250 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
251 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
252 APP2SD_PATH, encoded_id);
254 tzplatform_set_user(uid);
255 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
256 tzplatform_getenv(TZ_USER_APP), pkgid);
257 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
258 APP2SD_PATH, encoded_id);
259 tzplatform_reset_user();
263 /* get the associated device node for SD card applicationer */
264 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
266 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
268 return APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE;
270 device_name = _app2sd_find_associated_device_node(loopback_device);
271 if (NULL == device_name)
272 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
275 ret = _app2sd_unmount_app_content(application_path);
281 _E("unable to unmount the app content (%d)", ret);
282 return APP2EXT_ERROR_UNMOUNT;
285 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
286 ret = _app2sd_dmcrypt_close_device(pkgid, uid);
292 _E("close dmcrypt device error(%d)", ret);
296 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
302 _E("unable to detach the loopback encryption setup" \
303 " for the application");
304 return APP2EXT_ERROR_UNMOUNT;
313 /* take appropriate action based on
314 * installation status of application package
316 if (install_status == APP2EXT_STATUS_FAILED) {
317 /* delete the loopback device from the SD card */
318 ret = _app2sd_delete_loopback_device(loopback_device);
320 _E("unable to delete the loopback device from the SD Card");
321 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
323 ret = _app2sd_remove_info_from_db(pkgid, uid);
325 _E("unable to delete info");
327 ret = _app2sd_delete_directory(application_path);
329 _E("unable to delete the directory (%s)", application_path);
331 /* if the status is success, then update installed storage
332 * to pkgmgr_parser db
334 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
335 INSTALL_EXTERNAL, uid);
336 if (pkgmgr_ret < 0) {
337 _E("fail to update installed location " \
338 "to db[%s, %d] of uid(%d), pkgmgr ret(%d)",
339 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
340 return APP2EXT_ERROR_PKGMGR_ERROR;
347 int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
349 int ret = APP2EXT_SUCCESS;
350 char application_path[FILENAME_MAX] = { 0, };
351 char loopback_device[FILENAME_MAX] = { 0, };
352 char *encoded_id = NULL;
353 char *device_node = NULL;
354 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
359 /* validate the function parameter recieved */
361 _E("invalid function arguments to app launch setup");
362 return APP2EXT_ERROR_INVALID_ARGUMENTS;
365 /* check whether MMC is present or not */
366 ret = _app2sd_check_mmc_status();
368 _E("MMC not preset OR Not ready (%d)", ret);
369 return APP2EXT_ERROR_MMC_STATUS;
372 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
373 if (encoded_id == NULL)
374 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
376 /* check app entry is there in sd card or not. */
377 if (_is_global(uid)) {
378 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
379 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
380 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
381 APP2SD_PATH, encoded_id);
383 tzplatform_set_user(uid);
384 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
385 tzplatform_getenv(TZ_USER_APP), pkgid);
386 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
387 APP2SD_PATH, encoded_id);
388 tzplatform_reset_user();
392 fp = fopen(loopback_device, "r+");
394 _E("app entry is not present in SD Card");
395 return APP2EXT_ERROR_INVALID_PACKAGE;
399 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
401 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
403 _E("device_node(%s_%d) already associated", pkgid, uid);
404 return APP2EXT_ERROR_ALREADY_MOUNTED;
407 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
408 false, uid, &device_node);
410 _E("dmcrypt open device error(%d)", ret);
411 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
414 result = (char *)_app2sd_find_associated_device(loopback_device);
415 /* process the string */
416 if ((result != NULL) && strstr(result, "/dev") != NULL) {
417 _E("already associated");
420 return APP2EXT_ERROR_ALREADY_MOUNTED;
423 /* do loopback setup */
424 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
425 loopback_device, uid);
426 if (device_node == NULL) {
427 _E("loopback encryption setup failed");
428 return APP2EXT_ERROR_DO_LOSETUP;
433 ret = _app2sd_mount_app_content(application_path, pkgid,
434 device_node, MOUNT_TYPE_RD, NULL, APP2SD_APP_LAUNCH, uid);
441 return APP2EXT_ERROR_MOUNT_PATH;
452 int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
454 int ret = APP2EXT_SUCCESS;
455 char application_path[FILENAME_MAX] = { 0, };
456 char loopback_device[FILENAME_MAX] = { 0, };
457 char *encoded_id = NULL;
460 /* validate the function parameter recieved */
462 _E("invalid function arguments to app launch setup");
463 return APP2EXT_ERROR_INVALID_ARGUMENTS;
466 /* check whether MMC is present or not */
467 ret = _app2sd_check_mmc_status();
469 _E("MMC not preset OR Not ready (%d)", ret);
470 return APP2EXT_ERROR_MMC_STATUS;
473 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
474 if (encoded_id == NULL)
475 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
477 /* check app entry is there in sd card or not. */
478 if (_is_global(uid)) {
479 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
480 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
481 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
482 APP2SD_PATH, encoded_id);
484 tzplatform_set_user(uid);
485 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
486 tzplatform_getenv(TZ_USER_APP), pkgid);
487 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
488 APP2SD_PATH, encoded_id);
489 tzplatform_reset_user();
493 fp = fopen(loopback_device, "r+");
495 _E("app entry is not present in SD Card");
496 return APP2EXT_ERROR_INVALID_PACKAGE;
500 ret = _app2sd_unmount_app_content(application_path);
502 _E("unable to unmount the SD application");
503 return APP2EXT_ERROR_UNMOUNT;
506 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
507 ret = _app2sd_dmcrypt_close_device(pkgid, uid);
509 _E("close dmcrypt device error(%d)", ret);
511 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
513 _E("unable to remove loopback setup");
514 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
521 int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
523 int ret = APP2EXT_SUCCESS;
524 char application_path[FILENAME_MAX] = { 0, };
525 char loopback_device[FILENAME_MAX] = { 0, };
526 char *encoded_id = NULL;
527 char *device_node = NULL;
530 /* validate the function parameter recieved */
532 _E("invalid function arguments to app launch setup");
533 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
537 /* check whether MMC is present or not */
538 ret = _app2sd_check_mmc_status();
540 _E("MMC not preset OR Not ready (%d)", ret);
541 ret = APP2EXT_ERROR_MMC_STATUS;
545 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
546 if (encoded_id == NULL)
547 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
549 if (_is_global(uid)) {
550 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
551 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
552 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
553 APP2SD_PATH, encoded_id);
555 tzplatform_set_user(uid);
556 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
557 tzplatform_getenv(TZ_USER_APP), pkgid);
558 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
559 APP2SD_PATH, encoded_id);
560 tzplatform_reset_user();
564 /* check app entry is there in sd card or not. */
565 fp = fopen(loopback_device, "r+");
567 _E("app entry is not present in SD Card");
568 ret = APP2EXT_ERROR_INVALID_PACKAGE;
573 /* get the associated device node for SD card applicationer */
574 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
576 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
578 device_node = _app2sd_find_associated_device_node(loopback_device);
580 if (NULL == device_node) {
581 /* do loopback setup */
582 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
583 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
584 false, uid, &device_node);
586 _E("dmcrypt open device error(%d)", ret);
587 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
590 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
591 loopback_device, uid);
592 if (device_node == NULL) {
593 _E("loopback encryption setup failed");
594 ret = APP2EXT_ERROR_DO_LOSETUP;
599 ret = _app2sd_mount_app_content(application_path, pkgid,
600 device_node, MOUNT_TYPE_RW, NULL,
601 APP2SD_PRE_UNINSTALL, uid);
608 ret = APP2EXT_ERROR_MOUNT_PATH;
613 ret = _app2sd_mount_app_content(application_path, pkgid,
614 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
615 APP2SD_PRE_UNINSTALL, uid);
617 _E("remount failed");
622 ret = APP2EXT_ERROR_MOUNT_PATH;
635 int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
637 char application_path[FILENAME_MAX] = { 0, };
638 char loopback_device[FILENAME_MAX] = { 0, };
639 char *encoded_id = NULL;
640 int ret = APP2EXT_SUCCESS;
642 /* validate the function parameter recieved */
644 _E("invalid function arguments");
645 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
649 /* check whether MMC is present or not */
650 ret = _app2sd_check_mmc_status();
652 _E("MMC not preset OR Not ready (%d)", ret);
653 ret = APP2EXT_ERROR_MMC_STATUS;
657 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
658 if (encoded_id == NULL)
659 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
661 if (_is_global(uid)) {
662 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
663 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
664 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
665 APP2SD_PATH, encoded_id);
667 tzplatform_set_user(uid);
668 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
669 tzplatform_getenv(TZ_USER_APP), pkgid);
670 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
671 APP2SD_PATH, encoded_id);
672 tzplatform_reset_user();
676 /* unmount the loopback encrypted pseudo device from
677 * the application installation path
679 ret = _app2sd_unmount_app_content(application_path);
681 _E("unable to unmount the app content (%d)", ret);
682 ret = APP2EXT_ERROR_UNMOUNT;
685 /* detach the loopback encryption setup for the application */
686 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
687 ret = _app2sd_dmcrypt_close_device(pkgid, uid);
689 _E("close dmcrypt device error(%d)", ret);
693 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
695 _E("unable to Detach the loopback encryption setup" \
696 " for the application");
697 ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
702 /* delete the loopback device from the SD card */
703 ret = _app2sd_delete_loopback_device(loopback_device);
705 _E("unable to delete the " \
706 "loopback device from the SD Card");
707 ret = APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
711 ret = _app2sd_delete_directory(application_path);
713 _E("unable to delete the directory (%s)",
718 /* remove encryption password from DB */
719 ret = _app2sd_initialize_db();
721 _E("app2sd db initialize failed");
722 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
726 ret = _app2sd_remove_info_from_db(pkgid, uid);
728 _E("cannot remove info from db");
729 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
737 int app2sd_usr_move_installed_app(const char *pkgid, GList *dir_list,
738 app2ext_move_type move_type, uid_t uid)
743 /* validate function arguments */
744 if (pkgid == NULL || dir_list == NULL
745 || move_type < APP2EXT_MOVE_TO_EXT
746 || move_type > APP2EXT_MOVE_TO_PHONE) {
747 _E("invalid function arguments");
748 return APP2EXT_ERROR_INVALID_ARGUMENTS;
751 pkgmgrinfo_pkginfo_h info_handle = NULL;
752 pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
753 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &info_handle);
754 if (pkgmgr_ret < 0) {
755 _E("failed to get pkginfo for pkg(%s), uid(%d), pkgmgr_ret(%d)",
756 pkgid, uid, pkgmgr_ret);
757 return APP2EXT_ERROR_PKGMGR_ERROR;
759 pkgmgr_ret = pkgmgrinfo_pkginfo_get_installed_storage(info_handle, &storage);
760 if (pkgmgr_ret < 0) {
761 _E("failed to get installed storage for pkg(%s) of uid(%d), pkgmgr_ret(%d)",
762 pkgid, uid, pkgmgr_ret);
763 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
764 return APP2EXT_ERROR_PKGMGR_ERROR;
767 if ((move_type == APP2EXT_MOVE_TO_EXT && storage == PMINFO_EXTERNAL_STORAGE)
768 || (move_type == APP2EXT_MOVE_TO_PHONE && storage == PMINFO_INTERNAL_STORAGE)) {
769 _E("PKG_EXISTS in [%d] STORAGE", storage);
770 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
771 return APP2EXT_ERROR_PKG_EXISTS;
773 _D("pkgid[%s] move to STORAGE [%d]", pkgid, storage);
775 pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
777 ret = __app2sd_create_app2sd_directories(uid);
779 _E("failed to create app2sd dirs");
783 ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid);
785 _D("unable to move application");
789 /* if move is completed, then update installed storage to pkgmgr_parser db */
790 if (move_type == APP2EXT_MOVE_TO_EXT) {
791 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
792 INSTALL_EXTERNAL, uid);
793 if (pkgmgr_ret < 0) {
794 _E("failed to update installed location to db " \
795 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
796 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
797 return APP2EXT_ERROR_PKGMGR_ERROR;
800 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
801 INSTALL_INTERNAL, uid);
802 if (pkgmgr_ret < 0) {
803 _E("failed to update installed location to db " \
804 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
805 pkgid, INSTALL_INTERNAL, uid, pkgmgr_ret);
806 return APP2EXT_ERROR_PKGMGR_ERROR;
810 return APP2EXT_SUCCESS;
813 int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
816 int ret = APP2EXT_SUCCESS;
817 char loopback_device[FILENAME_MAX] = { 0, };
818 char application_path[FILENAME_MAX] = { 0, };
819 char temp_uid[32] = { 0, };
820 char *temp_pkgid = NULL;
821 char *temp_loopback_device = NULL;
822 char *temp_application_path = NULL;
823 char *device_node = NULL;
824 char *encoded_id = NULL;
825 char *temp_encoded_id = NULL;
827 unsigned long long curr_size = 0;
829 int reqd_disk_size = size + ceil(size * 0.2);
831 /* validate function arguments*/
832 if (pkgid == NULL || dir_list == NULL || size <= 0) {
833 _E("invalid function arguments");
834 return APP2EXT_ERROR_INVALID_ARGUMENTS;
837 /* check whether MMC is present or not */
838 ret = _app2sd_check_mmc_status();
840 _E("MMC not preset OR Not ready (%d)", ret);
841 return APP2EXT_ERROR_MMC_STATUS;
844 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
845 if (encoded_id == NULL)
846 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
848 if (_is_global(uid)) {
849 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
850 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
851 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
852 APP2SD_PATH, encoded_id);
854 tzplatform_set_user(uid);
855 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
856 tzplatform_getenv(TZ_USER_APP), pkgid);
857 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
858 APP2SD_PATH, encoded_id);
859 tzplatform_reset_user();
863 /* check app entry is there in sd card or not. */
864 fp = fopen(loopback_device, "r+");
866 _E("app entry is not present in SD Card");
867 return APP2EXT_ERROR_INVALID_PACKAGE;
871 /* get installed app size*/
872 curr_size = _app2sd_calculate_file_size(loopback_device);
873 curr_size = (curr_size) / (1024 * 1024);
874 if (curr_size == 0) {
875 _E("app entry is not present in SD Card");
876 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
878 if ((int)curr_size < reqd_disk_size) {
879 len = strlen(pkgid) + strlen(".new");
880 temp_pkgid = calloc(len + 1, sizeof(char));
881 if (temp_pkgid == NULL) {
882 _E("memory alloc failed");
883 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
885 snprintf(temp_pkgid, len + 1, "%s.new", pkgid);
887 if (_is_global(uid)) {
888 len = strlen(tzplatform_getenv(TZ_SYS_RW_APP)) + strlen(temp_pkgid) + 1;
889 temp_application_path = calloc(len + 1, sizeof(char));
890 if (temp_application_path == NULL) {
891 _E("memory alloc failed");
893 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
895 snprintf(temp_application_path, len + 1, "%s/%s",
896 tzplatform_getenv(TZ_SYS_RW_APP), temp_pkgid);
898 temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
899 if (temp_encoded_id == NULL) {
901 free(temp_application_path);
902 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
904 len = strlen(APP2SD_PATH) + strlen(temp_encoded_id) + 1;
905 temp_loopback_device = calloc(len + 1, sizeof(char));
906 if (temp_loopback_device == NULL) {
907 _E("memory alloc failed");
909 free(temp_application_path);
910 free(temp_encoded_id);
911 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
913 snprintf(temp_loopback_device, len + 1, "%s/%s",
914 APP2SD_PATH, temp_encoded_id);
915 free(temp_encoded_id);
917 tzplatform_set_user(uid);
918 len = strlen(tzplatform_getenv(TZ_USER_APP)) + strlen(temp_pkgid) + 1;
919 temp_application_path = calloc(len + 1, sizeof(char));
920 if (temp_application_path == NULL) {
921 _E("memory alloc failed");
923 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
925 snprintf(temp_application_path, len + 1, "%s/%s",
926 tzplatform_getenv(TZ_USER_APP), temp_pkgid);
928 temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
929 if (temp_encoded_id == NULL) {
931 free(temp_application_path);
932 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
934 snprintf(temp_uid, 32, "%d", uid);
935 len = strlen(APP2SD_PATH) + strlen(temp_uid) + strlen(temp_encoded_id) + 2;
936 temp_loopback_device = calloc(len + 1, sizeof(char));
937 if (temp_loopback_device == NULL) {
938 _E("memory alloc failed");
940 free(temp_application_path);
941 free(temp_encoded_id);
942 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
944 snprintf(temp_loopback_device, len + 1, "%s/%s",
945 APP2SD_PATH, temp_encoded_id);
946 free(temp_encoded_id);
947 tzplatform_reset_user();
949 ret = _app2sd_update_loopback_device_size(pkgid,
950 loopback_device, application_path, temp_pkgid,
951 temp_loopback_device, temp_application_path,
952 reqd_disk_size, dir_list, uid);
954 free(temp_application_path);
955 free(temp_loopback_device);
956 if (APP2EXT_SUCCESS != ret) {
957 _E("failed to update loopback device size");
962 /* get the associated device node for SD card applicationer */
963 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
965 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
967 device_node = _app2sd_find_associated_device_node(loopback_device);
969 if (NULL == device_node) {
970 /* do loopback setup */
971 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
972 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
973 false, uid, &device_node);
975 _E("dmcrypt open device error");
976 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
979 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
980 loopback_device, uid);
981 if (device_node == NULL) {
982 _E("loopback encryption setup failed");
983 return APP2EXT_ERROR_DO_LOSETUP;
988 ret = _app2sd_mount_app_content(application_path, pkgid,
989 device_node, MOUNT_TYPE_RW, dir_list,
990 APP2SD_PRE_UPGRADE, uid);
997 return APP2EXT_ERROR_MOUNT_PATH;
1000 /* do re-mounting */
1001 ret = _app2sd_mount_app_content(application_path, pkgid,
1002 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
1003 APP2SD_PRE_UPGRADE, uid);
1005 _E("remount failed");
1010 return APP2EXT_ERROR_MOUNT_PATH;
1021 int app2sd_usr_post_app_upgrade(const char *pkgid,
1022 app2ext_status install_status, uid_t uid)
1024 char *device_name = NULL;
1025 char loopback_device[FILENAME_MAX] = { 0, };
1026 char application_path[FILENAME_MAX] = { 0, };
1027 char *encoded_id = NULL;
1028 int ret = APP2EXT_SUCCESS;
1030 /* validate the function parameter recieved */
1031 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
1032 || install_status > APP2EXT_STATUS_SUCCESS) {
1033 _E("invalid func parameters");
1034 return APP2EXT_ERROR_INVALID_ARGUMENTS;
1037 /* check whether MMC is present or not */
1038 ret = _app2sd_check_mmc_status();
1040 _E("MMC not preset OR Not ready (%d)", ret);
1041 return APP2EXT_ERROR_MMC_STATUS;
1044 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1045 if (encoded_id == NULL)
1046 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1048 if (_is_global(uid)) {
1049 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1050 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1051 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1052 APP2SD_PATH, encoded_id);
1054 tzplatform_set_user(uid);
1055 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1056 tzplatform_getenv(TZ_USER_APP), pkgid);
1057 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1058 APP2SD_PATH, encoded_id);
1059 tzplatform_reset_user();
1063 /* get the associated device node for SD card applicationer */
1064 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
1066 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
1068 _E("could not find associated dmcrypt device node" \
1069 " (%s_%d)", pkgid, uid);
1070 return APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE;
1073 device_name = _app2sd_find_associated_device_node(loopback_device);
1074 if (NULL == device_name)
1075 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
1078 ret = _app2sd_unmount_app_content(application_path);
1084 _E("unable to unmount the app content (%d)", ret);
1085 return APP2EXT_ERROR_UNMOUNT;
1088 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
1089 ret = _app2sd_dmcrypt_close_device(pkgid, uid);
1095 _E("close dmcrypt device error(%d)", ret);
1096 return APP2EXT_ERROR_CLOSE_DMCRYPT_DEVICE;
1099 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
1105 _E("unable to detach the loopback encryption " \
1106 "setup for the application");
1107 return APP2EXT_ERROR_UNMOUNT;
1119 int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
1121 char loopback_device[FILENAME_MAX] = { 0, };
1122 char application_path[FILENAME_MAX] = { 0, };
1123 char *encoded_id = NULL;
1124 int ret = APP2EXT_SUCCESS;
1126 /* validate the function parameter recieved */
1127 if (pkgid == NULL) {
1128 _E("invalid func parameters");
1129 return APP2EXT_ERROR_INVALID_ARGUMENTS;
1134 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1135 if (encoded_id == NULL)
1136 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1138 if (_is_global(uid)) {
1139 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1140 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1141 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1142 APP2SD_PATH, encoded_id);
1144 tzplatform_set_user(uid);
1145 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1146 tzplatform_getenv(TZ_USER_APP), pkgid);
1147 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1148 APP2SD_PATH, encoded_id);
1149 tzplatform_reset_user();
1153 ret = _app2sd_force_clean(pkgid, application_path, loopback_device, uid);
1158 int app2sd_enable_full_pkg(void)
1160 int ret = APP2EXT_SUCCESS;
1162 char buf[FILENAME_MAX] = { 0, };
1163 char loopback_device[FILENAME_MAX] = { 0, };
1165 struct dirent entry;
1166 struct dirent *result = NULL;
1170 dir = opendir(APP2SD_PATH);
1172 if (strerror_r(errno, buf, sizeof(buf)) == 0)
1173 _E("failed to opendir (%s)", buf);
1174 return APP2EXT_ERROR_OPEN_DIR;
1177 ret = _app2sd_initialize_db();
1179 _E("app2sd db initialize failed");
1181 return APP2EXT_ERROR_SQLITE_REGISTRY;
1184 for (rc = readdir_r(dir, &entry, &result);
1185 rc == 0 && result != NULL;
1186 rc = readdir_r(dir, &entry, &result)) {
1187 if (strcmp(entry.d_name, ".") == 0 ||
1188 strcmp(entry.d_name, "..") == 0)
1190 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1191 APP2SD_PATH, entry.d_name);
1192 ret = _app2sd_get_info_from_db(loopback_device,
1195 _E("failed to get info from db");
1198 if (pkgid && uid > 0) {
1199 _D("pkgid(%s), uid(%d)", pkgid, uid);
1200 ret = app2sd_usr_on_demand_setup_init(pkgid, uid);
1202 _E("error(%d)", ret);
1221 int app2sd_disable_full_pkg(void)
1223 int ret = APP2EXT_SUCCESS;
1225 char buf[FILENAME_MAX] = { 0, };
1226 char loopback_device[FILENAME_MAX] = { 0, };
1228 struct dirent entry;
1229 struct dirent *result = NULL;
1233 dir = opendir(APP2SD_PATH);
1235 if (strerror_r(errno, buf, sizeof(buf)) == 0)
1236 _E("failed to opendir (%s)", buf);
1237 return APP2EXT_ERROR_OPEN_DIR;
1240 ret = _app2sd_initialize_db();
1242 _E("app2sd db initialize failed");
1244 return APP2EXT_ERROR_SQLITE_REGISTRY;
1247 for (rc = readdir_r(dir, &entry, &result);
1248 rc == 0 && result != NULL;
1249 rc = readdir_r(dir, &entry, &result)) {
1250 if (strcmp(entry.d_name, ".") == 0 ||
1251 strcmp(entry.d_name, "..") == 0)
1253 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1254 APP2SD_PATH, entry.d_name);
1255 ret = _app2sd_get_info_from_db(loopback_device,
1258 _E("failed to get info from db");
1261 if (pkgid && uid > 0) {
1262 _D("pkgid(%s), uid(%d)", pkgid, uid);
1263 ret = app2sd_usr_on_demand_setup_exit(pkgid, uid);
1265 _E("error(%d)", ret);