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>
25 #include <package-manager.h>
28 #include "app2sd_internals.h"
30 static int __app2sd_create_app2sd_directories(uid_t uid, char *mmc_path)
33 char app2sd_path[FILENAME_MAX] = { 0, };
34 mode_t mode = DIR_PERMS;
36 snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
37 mmc_path, EXTIMG_DIR);
38 ret = mkdir(app2sd_path, mode);
40 if (errno != EEXIST) {
41 _E("create directory failed," \
42 " error no is (%d)", errno);
43 return APP2EXT_ERROR_CREATE_DIRECTORY;
47 return APP2EXT_SUCCESS;
50 static int __app2sd_finalize_device_setup(const char *pkgid,
51 const char *loopback_device, const char *application_path,
54 char *device_node = NULL;
55 int ret = APP2EXT_SUCCESS;
57 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
59 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
61 _E("failed to find device node");
62 return APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE;
65 device_node = _app2sd_find_associated_device_node(loopback_device);
66 if (NULL == device_node) {
67 _E("failed to find device node");
68 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
72 ret = _app2sd_unmount_app_content(application_path);
78 _E("unable to unmount the app content (%d)", ret);
79 return APP2EXT_ERROR_UNMOUNT;
82 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
83 ret = _app2sd_dmcrypt_close_device(pkgid, uid);
89 _E("close dmcrypt device error(%d)", ret);
93 ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
99 _E("unable to detach the loopback encryption setup" \
100 " for the application");
101 return APP2EXT_ERROR_UNMOUNT;
113 int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid_t uid)
116 int free_mmc_mem = 0;
118 char *device_node = NULL;
119 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
123 char mmc_path[FILENAME_MAX] = { 0, };
124 char application_path[FILENAME_MAX] = { 0, };
125 char loopback_device[FILENAME_MAX] = { 0, };
126 char *encoded_id = NULL;
127 int reqd_disk_size = size + ceil(size * 0.2);
129 /* validate the function parameter recieved */
130 if (pkgid == NULL || dir_list == NULL || size <= 0) {
131 _E("invalid function arguments");
132 return APP2EXT_ERROR_INVALID_ARGUMENTS;
135 /* check whether MMC is present or not */
136 ret = _app2sd_check_mmc_status(&sdpath);
138 _E("MMC not present OR Not ready (%d)", ret);
139 return APP2EXT_ERROR_MMC_STATUS;
141 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
145 /* find available free memory in the MMC card */
146 ret = _app2sd_get_available_free_memory(mmc_path, &free_mmc_mem);
148 _E("unable to get available free memory in MMC (%d)", ret);
149 return APP2EXT_ERROR_MMC_STATUS;
151 _D("size details for application installation:" \
152 " size=(%d)MB, reqd_disk_size=(%d)MB, free_mmc_size=(%d)MB",
153 size, reqd_disk_size, free_mmc_mem);
155 /* if avaialalbe free memory in MMC is less than required size + 5MB,
158 if ((reqd_disk_size + PKG_BUF_SIZE + MEM_BUF_SIZE) > free_mmc_mem) {
159 _E("insufficient memory in MMC for"
160 " application installation (%d)", ret);
161 return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
164 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
165 if (encoded_id == NULL)
166 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
168 if (_is_global(uid)) {
169 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
170 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
171 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
172 mmc_path, EXTIMG_DIR, encoded_id);
174 tzplatform_set_user(uid);
175 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
176 tzplatform_getenv(TZ_USER_APP), pkgid);
177 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
178 mmc_path, EXTIMG_DIR, encoded_id);
179 tzplatform_reset_user();
183 ret = __app2sd_create_app2sd_directories(uid, mmc_path);
185 _E("failed to create app2sd dirs");
189 /* create a loopback device */
190 ret = _app2sd_create_loopback_device(pkgid, loopback_device,
191 (reqd_disk_size + PKG_BUF_SIZE));
193 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
194 ret = _app2sd_dmcrypt_setup_device(pkgid, loopback_device, false, uid);
196 _E("dmcrypt setup device error");
197 return APP2EXT_ERROR_SETUP_DMCRYPT_DEVICE;
200 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
201 false, uid, &device_node);
203 _E("dmcrypt open device error");
204 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
207 /* perform loopback encryption setup */
208 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
209 loopback_device, uid);
211 _E("loopback encryption setup failed");
212 _app2sd_delete_loopback_device(loopback_device);
213 return APP2EXT_ERROR_DO_LOSETUP;
216 /* check whether loopback device is associated
217 * with device node or not
219 devi = _app2sd_find_associated_device_node(loopback_device);
221 _E("finding associated device node failed");
222 ret = APP2EXT_ERROR_DO_LOSETUP;
227 /* format the loopback file system */
228 ret = _app2sd_create_file_system(device_node);
230 _E("creating FS failed failed");
231 ret = APP2EXT_ERROR_CREATE_FS;
235 /* mount the loopback encrypted pseudo device on application
236 * installation path as with Read Write permission
238 ret = _app2sd_mount_app_content(application_path, pkgid,
239 device_node, MOUNT_TYPE_RW, dir_list,
240 APP2SD_PRE_INSTALL, uid);
242 _E("mounting dev path to app install path failed");
243 ret = APP2EXT_ERROR_MOUNT_PATH;
248 ret = APP2EXT_SUCCESS;
253 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
254 ret = _app2sd_dmcrypt_close_device(pkgid, uid);
256 _E("close dmcrypt device error(%d)", ret);
257 _app2sd_delete_loopback_device(loopback_device);
259 result = _app2sd_detach_loop_device(device_node);
264 _app2sd_delete_loopback_device(loopback_device);
274 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
285 int app2sd_usr_post_app_install(const char *pkgid,
286 app2ext_status install_status, uid_t uid)
289 char mmc_path[FILENAME_MAX] = { 0, };
290 char application_path[FILENAME_MAX] = { 0, };
291 char application_mmc_path[FILENAME_MAX] = { 0, };
292 char loopback_device[FILENAME_MAX] = { 0, };
293 char *encoded_id = NULL;
294 int ret = APP2EXT_SUCCESS;
297 /* validate the function parameter recieved */
298 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
299 || install_status > APP2EXT_STATUS_SUCCESS) {
300 _E("invalid func parameters");
301 return APP2EXT_ERROR_INVALID_ARGUMENTS;
304 /* check whether MMC is present or not */
305 ret = _app2sd_check_mmc_status(&sdpath);
307 _E("MMC not present OR Not ready (%d)", ret);
308 return APP2EXT_ERROR_MMC_STATUS;
310 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
314 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
315 if (encoded_id == NULL)
316 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
318 if (_is_global(uid)) {
319 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
320 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
321 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
322 mmc_path, EXTIMG_DIR, encoded_id);
324 tzplatform_set_user(uid);
325 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
326 tzplatform_getenv(TZ_USER_APP), pkgid);
327 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
328 mmc_path, EXTIMG_DIR, encoded_id);
329 tzplatform_reset_user();
333 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
334 application_path, uid);
336 _E("failed to finalize device setup");
340 /* take appropriate action based on
341 * installation status of application package
343 if (install_status == APP2EXT_STATUS_FAILED) {
344 /* delete the loopback device from the SD card */
345 ret = _app2sd_delete_loopback_device(loopback_device);
347 _E("unable to delete the loopback device from the SD Card");
348 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
350 ret = _app2sd_remove_info_from_db(pkgid, uid);
352 _E("unable to delete info");
354 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
356 ret = _app2sd_delete_directory(application_mmc_path);
358 _E("unable to delete the directory (%s)", application_path);
360 /* if the status is success, then update installed storage
361 * to pkgmgr_parser db
363 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
364 INSTALL_EXTERNAL, loopback_device, uid);
365 if (pkgmgr_ret < 0) {
366 _E("fail to update installed location " \
367 "to db[%s, %d] of uid(%d), pkgmgr ret(%d)",
368 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
369 return APP2EXT_ERROR_PKGMGR_ERROR;
377 int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
379 int ret = APP2EXT_SUCCESS;
380 char mmc_path[FILENAME_MAX] = { 0, };
381 char application_path[FILENAME_MAX] = { 0, };
382 char loopback_device[FILENAME_MAX] = { 0, };
384 char *encoded_id = NULL;
385 char *device_node = NULL;
386 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
391 /* validate the function parameter recieved */
393 _E("invalid function arguments to app launch setup");
394 return APP2EXT_ERROR_INVALID_ARGUMENTS;
397 /* check whether MMC is present or not */
398 ret = _app2sd_check_mmc_status(&sdpath);
400 _E("MMC not present OR Not ready (%d)", ret);
401 return APP2EXT_ERROR_MMC_STATUS;
403 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
406 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
407 if (encoded_id == NULL)
408 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
410 /* check app entry is there in sd card or not. */
411 if (_is_global(uid)) {
412 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
413 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
414 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
415 mmc_path, EXTIMG_DIR, encoded_id);
417 tzplatform_set_user(uid);
418 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
419 tzplatform_getenv(TZ_USER_APP), pkgid);
420 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
421 mmc_path, EXTIMG_DIR, encoded_id);
422 tzplatform_reset_user();
426 fp = fopen(loopback_device, "r+");
428 _E("app entry is not present in SD Card, (%s), errno(%d)",
429 loopback_device, errno);
430 return APP2EXT_ERROR_INVALID_PACKAGE;
434 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
436 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
438 _E("device_node(%s_%d) already associated", pkgid, uid);
440 return APP2EXT_ERROR_ALREADY_MOUNTED;
443 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
444 false, uid, &device_node);
446 _E("dmcrypt open device error(%d)", ret);
447 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
450 result = (char *)_app2sd_find_associated_device(loopback_device);
451 /* process the string */
452 if ((result != NULL) && strstr(result, "/dev") != NULL) {
453 _E("already associated");
456 return APP2EXT_ERROR_ALREADY_MOUNTED;
459 /* do loopback setup */
460 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
461 loopback_device, uid);
462 if (device_node == NULL) {
463 _E("loopback encryption setup failed");
464 return APP2EXT_ERROR_DO_LOSETUP;
469 ret = _app2sd_mount_app_content(application_path, pkgid,
470 device_node, MOUNT_TYPE_RD, NULL, APP2SD_APP_LAUNCH, uid);
477 return APP2EXT_ERROR_MOUNT_PATH;
488 static int _app2sd_application_handler(const pkgmgrinfo_appinfo_h handle, void *data)
493 uid_t uid = *(uid_t *)data;
495 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
497 _E("failed to get appid");
498 return APP2EXT_ERROR_PKGMGR_ERROR;
501 _D("appid(%s), uid(%d)", appid, uid);
503 ret = aul_app_is_running_for_uid(appid, uid);
505 return APP2EXT_SUCCESS;
507 pid = aul_app_get_pid_for_uid(appid, uid);
509 _E("failed to get pid");
510 return APP2EXT_ERROR_KILLAPP_ERROR;
513 ret = aul_terminate_pid_sync_without_restart_for_uid(pid, uid);
514 if (ret != AUL_R_OK) {
515 _E("failed to kill app");
516 return APP2EXT_ERROR_KILLAPP_ERROR;
519 return APP2EXT_SUCCESS;
522 static int _app2sd_kill_running_app(const char *pkgid, uid_t uid)
525 pkgmgrinfo_pkginfo_h handle;
526 pkgmgrinfo_appinfo_filter_h filter;
528 ret = pkgmgrinfo_pkginfo_get_usr_all_pkginfo(pkgid, uid, &handle);
530 _E("failed to get pkginfo");
531 return APP2EXT_ERROR_PKGMGR_ERROR;
534 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
536 ret = pkgmgrinfo_appinfo_filter_create(&filter);
538 _E("failed to create appinfo filter");
539 return APP2EXT_ERROR_PKGMGR_ERROR;
542 ret = pkgmgrinfo_appinfo_filter_add_string(filter,
543 PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid);
545 _E("failed to add pkgid to filter");
546 pkgmgrinfo_appinfo_filter_destroy(filter);
547 return APP2EXT_ERROR_PKGMGR_ERROR;
550 ret = pkgmgrinfo_appinfo_filter_add_bool(filter,
551 PMINFO_APPINFO_PROP_APP_CHECK_STORAGE, false);
553 _E("failed to add check_storage to filter");
554 pkgmgrinfo_appinfo_filter_destroy(filter);
555 return APP2EXT_ERROR_PKGMGR_ERROR;
558 ret = pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(filter,
559 _app2sd_application_handler, &uid, uid);
561 _E("failed to get filtered foreach appinfo");
562 pkgmgrinfo_appinfo_filter_destroy(filter);
563 return APP2EXT_ERROR_PKGMGR_ERROR;
566 pkgmgrinfo_appinfo_filter_destroy(filter);
568 return APP2EXT_SUCCESS;
571 int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
573 int ret = APP2EXT_SUCCESS;
574 char application_path[FILENAME_MAX] = { 0, };
575 char loopback_device[FILENAME_MAX] = { 0, };
576 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
577 char mmc_path[FILENAME_MAX] = { 0, };
579 char *encoded_id = NULL;
582 /* validate the function parameter recieved */
584 _E("invalid function arguments to app launch setup");
585 return APP2EXT_ERROR_INVALID_ARGUMENTS;
588 _app2sd_kill_running_app(pkgid, uid);
590 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
591 if (_is_global(uid)) {
592 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
593 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
595 tzplatform_set_user(uid);
596 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
597 tzplatform_getenv(TZ_USER_APP), pkgid);
598 tzplatform_reset_user();
601 /* check whether MMC is present or not */
602 ret = _app2sd_check_mmc_status(&sdpath);
604 _W("MMC not present OR Not ready (%d)", ret);
606 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
609 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
610 if (encoded_id == NULL)
611 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
613 if (_is_global(uid)) {
614 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
615 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
616 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
617 mmc_path, EXTIMG_DIR, encoded_id);
619 tzplatform_set_user(uid);
620 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
621 tzplatform_getenv(TZ_USER_APP), pkgid);
622 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
623 mmc_path, EXTIMG_DIR, encoded_id);
624 tzplatform_reset_user();
629 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
630 application_path, uid);
632 _E("failed to finalize device setup");
639 int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
641 int ret = APP2EXT_SUCCESS;
642 char mmc_path[FILENAME_MAX] = { 0, };
643 char application_path[FILENAME_MAX] = { 0, };
644 char loopback_device[FILENAME_MAX] = { 0, };
646 char *encoded_id = NULL;
647 char *device_node = NULL;
650 /* validate the function parameter recieved */
652 _E("invalid function arguments to app launch setup");
653 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
657 /* check whether MMC is present or not */
658 ret = _app2sd_check_mmc_status(&sdpath);
660 _E("MMC not present OR Not ready (%d)", ret);
661 ret = APP2EXT_ERROR_MMC_STATUS;
664 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
668 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
669 if (encoded_id == NULL)
670 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
672 if (_is_global(uid)) {
673 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
674 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
675 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
676 mmc_path, EXTIMG_DIR, encoded_id);
678 tzplatform_set_user(uid);
679 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
680 tzplatform_getenv(TZ_USER_APP), pkgid);
681 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
682 mmc_path, EXTIMG_DIR, encoded_id);
683 tzplatform_reset_user();
687 /* check app entry is there in sd card or not. */
688 fp = fopen(loopback_device, "r+");
690 _E("app entry is not present in SD Card");
691 ret = APP2EXT_ERROR_INVALID_PACKAGE;
696 /* get the associated device node for SD card applicationer */
697 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
699 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
701 device_node = _app2sd_find_associated_device_node(loopback_device);
703 if (NULL == device_node) {
704 /* do loopback setup */
705 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
706 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
707 false, uid, &device_node);
709 _E("dmcrypt open device error(%d)", ret);
710 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
713 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
714 loopback_device, uid);
715 if (device_node == NULL) {
716 _E("loopback encryption setup failed");
717 ret = APP2EXT_ERROR_DO_LOSETUP;
722 ret = _app2sd_mount_app_content(application_path, pkgid,
723 device_node, MOUNT_TYPE_RW, NULL,
724 APP2SD_PRE_UNINSTALL, uid);
731 ret = APP2EXT_ERROR_MOUNT_PATH;
736 ret = _app2sd_mount_app_content(application_path, pkgid,
737 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
738 APP2SD_PRE_UNINSTALL, uid);
740 _E("remount failed");
745 ret = APP2EXT_ERROR_MOUNT_PATH;
759 int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
761 char mmc_path[FILENAME_MAX] = { 0, };
762 char application_path[FILENAME_MAX] = { 0, };
763 char application_mmc_path[FILENAME_MAX] = { 0, };
764 char loopback_device[FILENAME_MAX] = { 0, };
766 char *encoded_id = NULL;
767 int ret = APP2EXT_SUCCESS;
769 /* validate the function parameter recieved */
771 _E("invalid function arguments");
772 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
776 /* check whether MMC is present or not */
777 ret = _app2sd_check_mmc_status(&sdpath);
779 _E("MMC not present OR Not ready (%d)", ret);
780 ret = APP2EXT_ERROR_MMC_STATUS;
783 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
787 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
788 if (encoded_id == NULL)
789 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
791 if (_is_global(uid)) {
792 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
793 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
794 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
795 mmc_path, EXTIMG_DIR, encoded_id);
797 tzplatform_set_user(uid);
798 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
799 tzplatform_getenv(TZ_USER_APP), pkgid);
800 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
801 mmc_path, EXTIMG_DIR, encoded_id);
802 tzplatform_reset_user();
806 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
807 application_path, uid);
809 _E("failed to finalize device setup");
813 /* delete the loopback device from the SD card */
814 ret = _app2sd_delete_loopback_device(loopback_device);
816 _E("unable to delete the " \
817 "loopback device from the SD Card");
818 ret = APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
822 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
824 ret = _app2sd_delete_directory(application_mmc_path);
826 _E("unable to delete the directory (%s)",
831 /* remove encryption password from DB */
832 ret = _app2sd_initialize_db();
834 _E("app2sd db initialize failed");
835 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
839 ret = _app2sd_remove_info_from_db(pkgid, uid);
841 _E("cannot remove info from db");
842 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
851 int app2sd_usr_pre_move_installed_app(const char *pkgid,
852 GList *dir_list, app2ext_move_type move_type, uid_t uid)
857 char *image_path = NULL;
858 char mmc_path[FILENAME_MAX] = { 0, };
860 /* validate function arguments */
861 if (pkgid == NULL || dir_list == NULL
862 || move_type < APP2EXT_MOVE_TO_EXT
863 || move_type > APP2EXT_MOVE_TO_PHONE) {
864 _E("invalid function arguments");
865 return APP2EXT_ERROR_INVALID_ARGUMENTS;
868 /* check whether MMC is present or not */
869 ret = _app2sd_check_mmc_status(&sdpath);
871 _E("MMC not present OR Not ready(%d)", ret);
872 return APP2EXT_ERROR_MMC_STATUS;
874 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
878 ret = __app2sd_create_app2sd_directories(uid, mmc_path);
880 _E("failed to create app2sd dirs");
884 ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid, mmc_path, &image_path);
886 _D("unable to move application");
890 /* if move is completed, then update installed storage to pkgmgr_parser db */
891 if (move_type == APP2EXT_MOVE_TO_EXT) {
893 _E("image_path is NULL");
894 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
896 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
897 INSTALL_EXTERNAL, image_path, uid);
898 if (pkgmgr_ret < 0) {
899 _E("failed to update installed location to db " \
900 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
901 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
902 return APP2EXT_ERROR_PKGMGR_ERROR;
905 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
906 INSTALL_INTERNAL, image_path, uid);
907 if (pkgmgr_ret < 0) {
908 _E("failed to update installed location to db " \
909 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
910 pkgid, INSTALL_INTERNAL, uid, pkgmgr_ret);
911 return APP2EXT_ERROR_PKGMGR_ERROR;
919 return APP2EXT_SUCCESS;
922 int app2sd_usr_post_move_installed_app(const char *pkgid,
923 app2ext_move_type move_type, uid_t uid)
926 char mmc_path[FILENAME_MAX] = { 0, };
927 char application_path[FILENAME_MAX] = { 0, };
928 char loopback_device[FILENAME_MAX] = { 0, };
930 char *encoded_id = NULL;
932 /* validate function arguments */
933 if (pkgid == NULL || move_type < APP2EXT_MOVE_TO_EXT
934 || move_type > APP2EXT_MOVE_TO_PHONE) {
935 _E("invalid function arguments");
936 return APP2EXT_ERROR_INVALID_ARGUMENTS;
939 if (move_type == APP2EXT_MOVE_TO_PHONE)
940 return APP2EXT_SUCCESS;
942 /* check whether MMC is present or not */
943 ret = _app2sd_check_mmc_status(&sdpath);
945 _E("MMC not present OR Not ready(%d)", ret);
946 return APP2EXT_ERROR_MMC_STATUS;
948 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
952 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
953 if (encoded_id == NULL)
954 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
956 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
957 mmc_path, EXTIMG_DIR, encoded_id);
959 if (_is_global(uid)) {
960 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
961 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
963 tzplatform_set_user(uid);
964 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
965 tzplatform_getenv(TZ_USER_APP), pkgid);
966 tzplatform_reset_user();
969 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
970 application_path, uid);
972 _E("failed to finalize device setup");
977 return APP2EXT_SUCCESS;
980 int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
983 int ret = APP2EXT_SUCCESS;
984 char app2sd_path[FILENAME_MAX] = { 0, };
985 char loopback_device[FILENAME_MAX] = { 0, };
986 char application_path[FILENAME_MAX] = { 0, };
987 char temp_uid[32] = { 0, };
989 char *temp_pkgid = NULL;
990 char *temp_loopback_device = NULL;
991 char *temp_application_path = NULL;
992 char *device_node = NULL;
993 char *encoded_id = NULL;
994 char *temp_encoded_id = NULL;
996 unsigned long long curr_size = 0;
998 int reqd_disk_size = size + ceil(size * 0.2);
1000 /* validate function arguments*/
1001 if (pkgid == NULL || dir_list == NULL || size <= 0) {
1002 _E("invalid function arguments");
1003 return APP2EXT_ERROR_INVALID_ARGUMENTS;
1006 /* check whether MMC is present or not */
1007 ret = _app2sd_check_mmc_status(&sdpath);
1009 _E("MMC not present OR Not ready (%d)", ret);
1010 return APP2EXT_ERROR_MMC_STATUS;
1012 snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
1013 sdpath, EXTIMG_DIR);
1017 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1018 if (encoded_id == NULL)
1019 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1021 if (_is_global(uid)) {
1022 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1023 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1024 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1025 app2sd_path, encoded_id);
1027 tzplatform_set_user(uid);
1028 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1029 tzplatform_getenv(TZ_USER_APP), pkgid);
1030 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1031 app2sd_path, encoded_id);
1032 tzplatform_reset_user();
1036 /* check app entry is there in sd card or not. */
1037 fp = fopen(loopback_device, "r+");
1039 _E("app entry is not present in SD Card");
1040 return APP2EXT_ERROR_INVALID_PACKAGE;
1044 /* get installed app size*/
1045 curr_size = _app2sd_calculate_file_size(loopback_device);
1046 curr_size = (curr_size) / (1024 * 1024);
1047 if (curr_size == 0) {
1048 _E("app entry is not present in SD Card");
1049 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
1051 if ((int)curr_size < reqd_disk_size) {
1052 len = strlen(pkgid) + strlen(".new");
1053 temp_pkgid = calloc(len + 1, sizeof(char));
1054 if (temp_pkgid == NULL) {
1055 _E("memory alloc failed");
1056 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1058 snprintf(temp_pkgid, len + 1, "%s.new", pkgid);
1060 if (_is_global(uid)) {
1061 len = strlen(tzplatform_getenv(TZ_SYS_RW_APP)) + strlen(temp_pkgid) + 1;
1062 temp_application_path = calloc(len + 1, sizeof(char));
1063 if (temp_application_path == NULL) {
1064 _E("memory alloc failed");
1066 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1068 snprintf(temp_application_path, len + 1, "%s/%s",
1069 tzplatform_getenv(TZ_SYS_RW_APP), temp_pkgid);
1071 temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
1072 if (temp_encoded_id == NULL) {
1074 free(temp_application_path);
1075 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1077 len = strlen(app2sd_path) + strlen(temp_encoded_id) + 1;
1078 temp_loopback_device = calloc(len + 1, sizeof(char));
1079 if (temp_loopback_device == NULL) {
1080 _E("memory alloc failed");
1082 free(temp_application_path);
1083 free(temp_encoded_id);
1084 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1086 snprintf(temp_loopback_device, len + 1, "%s/%s",
1087 app2sd_path, temp_encoded_id);
1088 free(temp_encoded_id);
1090 tzplatform_set_user(uid);
1091 len = strlen(tzplatform_getenv(TZ_USER_APP)) + strlen(temp_pkgid) + 1;
1092 temp_application_path = calloc(len + 1, sizeof(char));
1093 if (temp_application_path == NULL) {
1094 _E("memory alloc failed");
1096 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1098 snprintf(temp_application_path, len + 1, "%s/%s",
1099 tzplatform_getenv(TZ_USER_APP), temp_pkgid);
1101 temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
1102 if (temp_encoded_id == NULL) {
1104 free(temp_application_path);
1105 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1107 snprintf(temp_uid, 32, "%d", uid);
1108 len = strlen(app2sd_path) + strlen(temp_uid) + strlen(temp_encoded_id) + 2;
1109 temp_loopback_device = calloc(len + 1, sizeof(char));
1110 if (temp_loopback_device == NULL) {
1111 _E("memory alloc failed");
1113 free(temp_application_path);
1114 free(temp_encoded_id);
1115 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1117 snprintf(temp_loopback_device, len + 1, "%s/%s",
1118 app2sd_path, temp_encoded_id);
1119 free(temp_encoded_id);
1120 tzplatform_reset_user();
1122 ret = _app2sd_update_loopback_device_size(pkgid,
1123 loopback_device, application_path, temp_pkgid,
1124 temp_loopback_device, temp_application_path,
1125 reqd_disk_size, dir_list, uid);
1127 free(temp_application_path);
1128 free(temp_loopback_device);
1129 if (APP2EXT_SUCCESS != ret) {
1130 _E("failed to update loopback device size");
1135 /* get the associated device node for SD card applicationer */
1136 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
1138 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
1140 device_node = _app2sd_find_associated_device_node(loopback_device);
1142 if (NULL == device_node) {
1143 /* do loopback setup */
1144 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
1145 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
1146 false, uid, &device_node);
1148 _E("dmcrypt open device error");
1149 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
1152 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
1153 loopback_device, uid);
1154 if (device_node == NULL) {
1155 _E("loopback encryption setup failed");
1156 return APP2EXT_ERROR_DO_LOSETUP;
1161 ret = _app2sd_mount_app_content(application_path, pkgid,
1162 device_node, MOUNT_TYPE_RW, dir_list,
1163 APP2SD_PRE_UPGRADE, uid);
1170 return APP2EXT_ERROR_MOUNT_PATH;
1173 /* do re-mounting */
1174 ret = _app2sd_mount_app_content(application_path, pkgid,
1175 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
1176 APP2SD_PRE_UPGRADE, uid);
1178 _E("remount failed");
1183 return APP2EXT_ERROR_MOUNT_PATH;
1196 int app2sd_usr_post_app_upgrade(const char *pkgid,
1197 app2ext_status install_status, uid_t uid)
1199 char mmc_path[FILENAME_MAX] = { 0, };
1200 char loopback_device[FILENAME_MAX] = { 0, };
1201 char application_path[FILENAME_MAX] = { 0, };
1202 char *sdpath = NULL;
1203 char *encoded_id = NULL;
1204 int ret = APP2EXT_SUCCESS;
1207 /* validate the function parameter recieved */
1208 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
1209 || install_status > APP2EXT_STATUS_SUCCESS) {
1210 _E("invalid func parameters");
1211 return APP2EXT_ERROR_INVALID_ARGUMENTS;
1214 /* check whether MMC is present or not */
1215 ret = _app2sd_check_mmc_status(&sdpath);
1217 _E("MMC not present OR Not ready (%d)", ret);
1218 return APP2EXT_ERROR_MMC_STATUS;
1220 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
1224 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1225 if (encoded_id == NULL)
1226 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1228 if (_is_global(uid)) {
1229 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1230 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1231 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
1232 mmc_path, EXTIMG_DIR, encoded_id);
1234 tzplatform_set_user(uid);
1235 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1236 tzplatform_getenv(TZ_USER_APP), pkgid);
1237 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
1238 mmc_path, EXTIMG_DIR, encoded_id);
1239 tzplatform_reset_user();
1243 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
1244 application_path, uid);
1246 _E("failed to finalize device setup");
1250 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
1251 INSTALL_EXTERNAL, loopback_device, uid);
1252 if (pkgmgr_ret < 0) {
1253 _E("fail to update installed location " \
1254 "to db[%s, %d] of uid(%d), pkgmgr ret(%d)",
1255 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
1256 return APP2EXT_ERROR_PKGMGR_ERROR;
1263 int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
1265 char mmc_path[FILENAME_MAX] = { 0, };
1266 char loopback_device[FILENAME_MAX] = { 0, };
1267 char application_path[FILENAME_MAX] = { 0, };
1268 char *sdpath = NULL;
1269 char *encoded_id = NULL;
1270 int ret = APP2EXT_SUCCESS;
1272 /* validate the function parameter recieved */
1273 if (pkgid == NULL) {
1274 _E("invalid func parameters");
1275 return APP2EXT_ERROR_INVALID_ARGUMENTS;
1278 /* check whether MMC is present or not */
1279 ret = _app2sd_check_mmc_status(&sdpath);
1281 _E("MMC not present OR Not ready (%d)", ret);
1282 return APP2EXT_ERROR_MMC_STATUS;
1284 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
1288 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1289 if (encoded_id == NULL)
1290 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1292 if (_is_global(uid)) {
1293 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1294 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1295 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
1296 mmc_path, EXTIMG_DIR, encoded_id);
1298 tzplatform_set_user(uid);
1299 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1300 tzplatform_getenv(TZ_USER_APP), pkgid);
1301 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
1302 mmc_path, EXTIMG_DIR, encoded_id);
1303 tzplatform_reset_user();
1307 ret = _app2sd_force_clean(pkgid, application_path, loopback_device, uid);
1313 int app2sd_enable_full_pkg(void)
1315 int ret = APP2EXT_SUCCESS;
1316 char buf[FILENAME_MAX] = { 0, };
1317 char app2sd_path[FILENAME_MAX] = { 0, };
1318 char loopback_device[FILENAME_MAX] = { 0, };
1319 char *sdpath = NULL;
1322 struct dirent *entry = NULL;
1325 /* check whether MMC is present or not */
1326 ret = _app2sd_check_mmc_status(&sdpath);
1328 _E("MMC not present OR Not ready (%d)", ret);
1329 return APP2EXT_ERROR_MMC_STATUS;
1331 snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
1332 sdpath, EXTIMG_DIR);
1335 dir = opendir(app2sd_path);
1337 strerror_r(errno, buf, sizeof(buf));
1338 _E("failed to opendir (%s)", buf);
1339 return APP2EXT_ERROR_OPEN_DIR;
1342 ret = _app2sd_initialize_db();
1344 _E("app2sd db initialize failed");
1346 return APP2EXT_ERROR_SQLITE_REGISTRY;
1349 while ((entry = readdir(dir)) != NULL) {
1350 if (strcmp(entry->d_name, ".") == 0 ||
1351 strcmp(entry->d_name, "..") == 0)
1353 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1354 app2sd_path, entry->d_name);
1355 ret = _app2sd_get_info_from_db(loopback_device,
1358 _W("failed to get info from db, continue");
1362 _D("pkgid(%s), uid(%d)", pkgid, uid);
1363 ret = app2sd_usr_on_demand_setup_init(pkgid, uid);
1365 _E("error(%d)", ret);
1383 static int _app2sd_info_cb_func(const char *pkgid, uid_t uid)
1385 int ret = APP2EXT_SUCCESS;
1388 _D("pkgid(%s), uid(%d)", pkgid, uid);
1389 ret = app2sd_usr_on_demand_setup_exit(pkgid, uid);
1391 _E("error(%d)", ret);
1397 int app2sd_disable_full_pkg(void)
1399 int ret = APP2EXT_SUCCESS;
1401 ret = _app2sd_initialize_db();
1403 _E("app2sd db initialize failed");
1404 return APP2EXT_ERROR_SQLITE_REGISTRY;
1407 ret = _app2sd_get_foreach_info_from_db((app2sd_info_cb)_app2sd_info_cb_func);
1409 _E("disable full pkg error(%d)", ret);
1415 static int _app2sd_migrate_legacy_image(const char *pkgid, const char *passwd,
1416 const char *mmc_path, uid_t uid)
1419 char *device_node = NULL;
1420 char *encoded_id = NULL;
1421 char new_image[FILENAME_MAX] = { 0, };
1422 char legacy_image[FILENAME_MAX] = { 0, };
1423 char application_path[FILENAME_MAX] = { 0, };
1424 uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
1426 if (_is_global(uid)) {
1427 snprintf(application_path, sizeof(application_path), "%s/%s",
1428 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1430 tzplatform_set_user(uid);
1431 snprintf(application_path, sizeof(application_path), "%s/%s",
1432 tzplatform_getenv(TZ_USER_APP), pkgid);
1433 tzplatform_reset_user();
1436 /* make the information and insert to DB */
1437 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1438 if (encoded_id == NULL)
1439 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1440 snprintf(new_image, sizeof(new_image), "%s/%s",
1441 mmc_path, encoded_id);
1444 snprintf(legacy_image, sizeof(legacy_image), "%s/%s",
1447 ret = _app2sd_rename_dir(legacy_image, new_image);
1449 _E("unable to rename (%s)", legacy_image);
1450 return APP2EXT_ERROR_ACCESS_FILE;
1453 ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
1454 INSTALL_EXTERNAL, new_image, uid);
1456 _E("fail to update installed location " \
1457 "to db[%s, %d] of uid(%d), ret(%d)",
1458 pkgid, INSTALL_EXTERNAL, uid, ret);
1459 return APP2EXT_ERROR_PKGMGR_ERROR;
1462 /* update app2sd db */
1463 if (_is_global(uid)) {
1464 ret = _app2sd_remove_info_from_db(pkgid, default_uid);
1466 _E("cannot remove info from db");
1467 return APP2EXT_ERROR_SQLITE_REGISTRY;
1470 ret = _app2sd_set_info_in_db(pkgid, passwd, new_image, uid);
1472 _E("unable to save info");
1473 return APP2EXT_ERROR_SQLITE_REGISTRY;
1476 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
1478 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
1480 _E("device_node(%s_%d) already associated", pkgid, uid);
1482 return APP2EXT_ERROR_ALREADY_MOUNTED;
1485 ret = _app2sd_dmcrypt_open_device(pkgid, new_image, false,
1488 _E("dmcrypt open device error(%d)", ret);
1489 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
1492 result = (char *)_app2sd_find_associated_device(new_image);
1493 /* process the string */
1494 if ((result != NULL) && strstr(result, "/dev") != NULL) {
1495 _E("already associated");
1498 return APP2EXT_ERROR_ALREADY_MOUNTED;
1501 /* do loopback setup */
1502 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
1504 if (device_node == NULL) {
1505 _E("loopback encryption setup failed");
1506 return APP2EXT_ERROR_DO_LOSETUP;
1511 ret = _app2sd_mount_app_content(application_path, pkgid,
1512 device_node, MOUNT_TYPE_RW, NULL,
1513 APP2SD_MIGRATE_LEGACY, uid);
1520 return APP2EXT_ERROR_MOUNT_PATH;
1529 return APP2EXT_SUCCESS;
1532 int app2sd_pre_migrate_legacy(const char *pkgid, uid_t uid)
1534 int ret = APP2EXT_SUCCESS;
1535 char *passwd = NULL;
1536 char *sdpath = NULL;
1537 char mmc_path[FILENAME_MAX] = { 0, };
1538 char file_path[FILENAME_MAX] = { 0, };
1539 uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
1542 /* check whether MMC is present or not */
1543 ret = _app2sd_check_mmc_status(&sdpath);
1545 _E("MMC not present OR Not ready (%d)", ret);
1546 return APP2EXT_ERROR_MMC_STATUS;
1548 snprintf(mmc_path, sizeof(mmc_path), "%s/%s", sdpath, EXTIMG_DIR);
1550 snprintf(file_path, sizeof(file_path), "%s/%s", mmc_path, pkgid);
1552 ret = _app2sd_initialize_db();
1554 _E("app2sd db initialize failed");
1555 return APP2EXT_ERROR_SQLITE_REGISTRY;
1558 /* In the upgrade script, the information
1559 * of legacy image are filled with default user-id,
1560 * pkgid(from old db), passwd(from old db) and
1561 * empty filename in app2sd db. */
1562 filename = _app2sd_get_filename_from_db(pkgid, default_uid);
1563 if (filename == NULL) {
1564 passwd = _app2sd_get_password_from_db(pkgid, default_uid);
1566 ret = _app2sd_migrate_legacy_image(pkgid, passwd,
1571 _E("invalid package info");
1572 return APP2EXT_ERROR_INVALID_PACKAGE;
1576 _W("same pkg exists, remove legacy file (%s)", file_path);
1577 ret = remove(file_path);
1579 _E("failed to remove, errno(%d)", errno);
1580 return APP2EXT_ERROR_PKG_EXISTS;
1586 int app2sd_post_migrate_legacy(const char *pkgid, uid_t uid)
1588 int ret = APP2EXT_SUCCESS;
1589 char *sdpath = NULL;
1590 char *encoded_id = NULL;
1591 char mmc_path[FILENAME_MAX] = { 0, };
1592 char application_path[FILENAME_MAX] = { 0, };
1593 char loopback_device[FILENAME_MAX] = { 0, };
1595 /* check whether MMC is present or not */
1596 ret = _app2sd_check_mmc_status(&sdpath);
1598 _E("MMC not present OR Not ready (%d)", ret);
1599 return APP2EXT_ERROR_MMC_STATUS;
1601 snprintf(mmc_path, sizeof(mmc_path), "%s/%s",
1602 sdpath, EXTIMG_DIR);
1605 if (_is_global(uid)) {
1606 snprintf(application_path, sizeof(application_path), "%s/%s",
1607 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1609 tzplatform_set_user(uid);
1610 snprintf(application_path, sizeof(application_path), "%s/%s",
1611 tzplatform_getenv(TZ_USER_APP), pkgid);
1612 tzplatform_reset_user();
1615 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1616 if (encoded_id == NULL) {
1617 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1619 snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
1620 mmc_path, encoded_id);
1623 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
1624 application_path, uid);
1626 _E("failed to finalize device setup");
1630 return APP2EXT_SUCCESS;
1633 /* this function is called when sdcard is inserted */
1634 int app2sd_migrate_legacy_all(void)
1636 int ret = APP2EXT_SUCCESS;
1638 char buf[FILENAME_MAX] = { 0, };
1639 char app2sd_path[FILENAME_MAX] = { 0, };
1640 char loopback_device[FILENAME_MAX] = { 0, };
1641 char *sdpath = NULL;
1643 struct dirent entry;
1644 struct dirent *result = NULL;
1645 uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
1648 /* check whether MMC is present or not */
1649 ret = _app2sd_check_mmc_status(&sdpath);
1651 _E("MMC not present OR Not ready (%d)", ret);
1652 return APP2EXT_ERROR_MMC_STATUS;
1654 snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s",
1655 sdpath, EXTIMG_DIR);
1658 dir = opendir(app2sd_path);
1660 strerror_r(errno, buf, sizeof(buf));
1661 _E("failed to opendir (%s)", buf);
1662 return APP2EXT_ERROR_OPEN_DIR;
1665 pc = pkgmgr_client_new(PC_REQUEST);
1667 _E("failed to create pkgmgr client");
1669 return APP2EXT_ERROR_PKGMGR_ERROR;
1672 for (rc = readdir_r(dir, &entry, &result);
1673 rc == 0 && result != NULL;
1674 rc = readdir_r(dir, &entry, &result)) {
1675 if (strcmp(entry.d_name, ".") == 0 ||
1676 strcmp(entry.d_name, "..") == 0)
1678 snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
1679 app2sd_path, entry.d_name);
1680 /* check losetup image */
1681 if (_app2sd_check_is_luks_device(loopback_device) == 0) {
1682 /* call installer backend
1683 * to change access-rule and broadcast this update */
1684 ret = pkgmgr_client_usr_migrate_external_image(pc,
1685 entry.d_name, default_uid);
1687 _E("failed to request migration, ret(%d)", ret);
1691 pkgmgr_client_free(pc);