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;
527 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
529 _E("failed to get pkginfo");
530 return APP2EXT_ERROR_PKGMGR_ERROR;
533 ret = pkgmgrinfo_appinfo_get_usr_list(handle,
534 PMINFO_ALL_APP, _app2sd_application_handler, &uid, uid);
536 _E("failed to get appinfo");
537 return APP2EXT_ERROR_PKGMGR_ERROR;
540 ret = pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
542 _E("failed to destroy pkginfo");
543 return APP2EXT_ERROR_PKGMGR_ERROR;
546 return APP2EXT_SUCCESS;
549 int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
551 int ret = APP2EXT_SUCCESS;
552 char application_path[FILENAME_MAX] = { 0, };
553 char loopback_device[FILENAME_MAX] = { 0, };
554 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
555 char mmc_path[FILENAME_MAX] = { 0, };
557 char *encoded_id = NULL;
560 /* validate the function parameter recieved */
562 _E("invalid function arguments to app launch setup");
563 return APP2EXT_ERROR_INVALID_ARGUMENTS;
566 _app2sd_kill_running_app(pkgid, uid);
568 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
569 if (_is_global(uid)) {
570 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
571 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
573 tzplatform_set_user(uid);
574 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
575 tzplatform_getenv(TZ_USER_APP), pkgid);
576 tzplatform_reset_user();
579 /* check whether MMC is present or not */
580 ret = _app2sd_check_mmc_status(&sdpath);
582 _W("MMC not present OR Not ready (%d)", ret);
584 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
587 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
588 if (encoded_id == NULL)
589 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
591 if (_is_global(uid)) {
592 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
593 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
594 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
595 mmc_path, EXTIMG_DIR, encoded_id);
597 tzplatform_set_user(uid);
598 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
599 tzplatform_getenv(TZ_USER_APP), pkgid);
600 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
601 mmc_path, EXTIMG_DIR, encoded_id);
602 tzplatform_reset_user();
607 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
608 application_path, uid);
610 _E("failed to finalize device setup");
617 int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
619 int ret = APP2EXT_SUCCESS;
620 char mmc_path[FILENAME_MAX] = { 0, };
621 char application_path[FILENAME_MAX] = { 0, };
622 char loopback_device[FILENAME_MAX] = { 0, };
624 char *encoded_id = NULL;
625 char *device_node = NULL;
628 /* validate the function parameter recieved */
630 _E("invalid function arguments to app launch setup");
631 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
635 /* check whether MMC is present or not */
636 ret = _app2sd_check_mmc_status(&sdpath);
638 _E("MMC not present OR Not ready (%d)", ret);
639 ret = APP2EXT_ERROR_MMC_STATUS;
642 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
646 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
647 if (encoded_id == NULL)
648 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
650 if (_is_global(uid)) {
651 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
652 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
653 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
654 mmc_path, EXTIMG_DIR, encoded_id);
656 tzplatform_set_user(uid);
657 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
658 tzplatform_getenv(TZ_USER_APP), pkgid);
659 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
660 mmc_path, EXTIMG_DIR, encoded_id);
661 tzplatform_reset_user();
665 /* check app entry is there in sd card or not. */
666 fp = fopen(loopback_device, "r+");
668 _E("app entry is not present in SD Card");
669 ret = APP2EXT_ERROR_INVALID_PACKAGE;
674 /* get the associated device node for SD card applicationer */
675 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
677 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
679 device_node = _app2sd_find_associated_device_node(loopback_device);
681 if (NULL == device_node) {
682 /* do loopback setup */
683 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
684 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
685 false, uid, &device_node);
687 _E("dmcrypt open device error(%d)", ret);
688 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
691 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
692 loopback_device, uid);
693 if (device_node == NULL) {
694 _E("loopback encryption setup failed");
695 ret = APP2EXT_ERROR_DO_LOSETUP;
700 ret = _app2sd_mount_app_content(application_path, pkgid,
701 device_node, MOUNT_TYPE_RW, NULL,
702 APP2SD_PRE_UNINSTALL, uid);
709 ret = APP2EXT_ERROR_MOUNT_PATH;
714 ret = _app2sd_mount_app_content(application_path, pkgid,
715 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
716 APP2SD_PRE_UNINSTALL, uid);
718 _E("remount failed");
723 ret = APP2EXT_ERROR_MOUNT_PATH;
737 int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
739 char mmc_path[FILENAME_MAX] = { 0, };
740 char application_path[FILENAME_MAX] = { 0, };
741 char application_mmc_path[FILENAME_MAX] = { 0, };
742 char loopback_device[FILENAME_MAX] = { 0, };
744 char *encoded_id = NULL;
745 int ret = APP2EXT_SUCCESS;
747 /* validate the function parameter recieved */
749 _E("invalid function arguments");
750 ret = APP2EXT_ERROR_INVALID_ARGUMENTS;
754 /* check whether MMC is present or not */
755 ret = _app2sd_check_mmc_status(&sdpath);
757 _E("MMC not present OR Not ready (%d)", ret);
758 ret = APP2EXT_ERROR_MMC_STATUS;
761 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
765 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
766 if (encoded_id == NULL)
767 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
769 if (_is_global(uid)) {
770 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
771 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
772 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
773 mmc_path, EXTIMG_DIR, encoded_id);
775 tzplatform_set_user(uid);
776 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
777 tzplatform_getenv(TZ_USER_APP), pkgid);
778 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
779 mmc_path, EXTIMG_DIR, encoded_id);
780 tzplatform_reset_user();
784 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
785 application_path, uid);
787 _E("failed to finalize device setup");
791 /* delete the loopback device from the SD card */
792 ret = _app2sd_delete_loopback_device(loopback_device);
794 _E("unable to delete the " \
795 "loopback device from the SD Card");
796 ret = APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
800 snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
802 ret = _app2sd_delete_directory(application_mmc_path);
804 _E("unable to delete the directory (%s)",
809 /* remove encryption password from DB */
810 ret = _app2sd_initialize_db();
812 _E("app2sd db initialize failed");
813 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
817 ret = _app2sd_remove_info_from_db(pkgid, uid);
819 _E("cannot remove info from db");
820 ret = APP2EXT_ERROR_SQLITE_REGISTRY;
829 int app2sd_usr_pre_move_installed_app(const char *pkgid,
830 GList *dir_list, app2ext_move_type move_type, uid_t uid)
835 char *image_path = NULL;
836 char mmc_path[FILENAME_MAX] = { 0, };
838 /* validate function arguments */
839 if (pkgid == NULL || dir_list == NULL
840 || move_type < APP2EXT_MOVE_TO_EXT
841 || move_type > APP2EXT_MOVE_TO_PHONE) {
842 _E("invalid function arguments");
843 return APP2EXT_ERROR_INVALID_ARGUMENTS;
846 /* check whether MMC is present or not */
847 ret = _app2sd_check_mmc_status(&sdpath);
849 _E("MMC not present OR Not ready(%d)", ret);
850 return APP2EXT_ERROR_MMC_STATUS;
852 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
856 ret = __app2sd_create_app2sd_directories(uid, mmc_path);
858 _E("failed to create app2sd dirs");
862 ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid, mmc_path, &image_path);
864 _D("unable to move application");
868 /* if move is completed, then update installed storage to pkgmgr_parser db */
869 if (move_type == APP2EXT_MOVE_TO_EXT) {
871 _E("image_path is NULL");
872 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
874 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
875 INSTALL_EXTERNAL, image_path, uid);
876 if (pkgmgr_ret < 0) {
877 _E("failed to update installed location to db " \
878 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
879 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
880 return APP2EXT_ERROR_PKGMGR_ERROR;
883 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
884 INSTALL_INTERNAL, image_path, uid);
885 if (pkgmgr_ret < 0) {
886 _E("failed to update installed location to db " \
887 "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
888 pkgid, INSTALL_INTERNAL, uid, pkgmgr_ret);
889 return APP2EXT_ERROR_PKGMGR_ERROR;
897 return APP2EXT_SUCCESS;
900 int app2sd_usr_post_move_installed_app(const char *pkgid,
901 app2ext_move_type move_type, uid_t uid)
904 char mmc_path[FILENAME_MAX] = { 0, };
905 char application_path[FILENAME_MAX] = { 0, };
906 char loopback_device[FILENAME_MAX] = { 0, };
908 char *encoded_id = NULL;
910 /* validate function arguments */
911 if (pkgid == NULL || move_type < APP2EXT_MOVE_TO_EXT
912 || move_type > APP2EXT_MOVE_TO_PHONE) {
913 _E("invalid function arguments");
914 return APP2EXT_ERROR_INVALID_ARGUMENTS;
917 if (move_type == APP2EXT_MOVE_TO_PHONE)
918 return APP2EXT_SUCCESS;
920 /* check whether MMC is present or not */
921 ret = _app2sd_check_mmc_status(&sdpath);
923 _E("MMC not present OR Not ready(%d)", ret);
924 return APP2EXT_ERROR_MMC_STATUS;
926 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
930 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
931 if (encoded_id == NULL)
932 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
934 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
935 mmc_path, EXTIMG_DIR, encoded_id);
937 if (_is_global(uid)) {
938 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
939 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
941 tzplatform_set_user(uid);
942 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
943 tzplatform_getenv(TZ_USER_APP), pkgid);
944 tzplatform_reset_user();
947 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
948 application_path, uid);
950 _E("failed to finalize device setup");
955 return APP2EXT_SUCCESS;
958 int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
961 int ret = APP2EXT_SUCCESS;
962 char app2sd_path[FILENAME_MAX] = { 0, };
963 char loopback_device[FILENAME_MAX] = { 0, };
964 char application_path[FILENAME_MAX] = { 0, };
965 char temp_uid[32] = { 0, };
967 char *temp_pkgid = NULL;
968 char *temp_loopback_device = NULL;
969 char *temp_application_path = NULL;
970 char *device_node = NULL;
971 char *encoded_id = NULL;
972 char *temp_encoded_id = NULL;
974 unsigned long long curr_size = 0;
976 int reqd_disk_size = size + ceil(size * 0.2);
978 /* validate function arguments*/
979 if (pkgid == NULL || dir_list == NULL || size <= 0) {
980 _E("invalid function arguments");
981 return APP2EXT_ERROR_INVALID_ARGUMENTS;
984 /* check whether MMC is present or not */
985 ret = _app2sd_check_mmc_status(&sdpath);
987 _E("MMC not present OR Not ready (%d)", ret);
988 return APP2EXT_ERROR_MMC_STATUS;
990 snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
995 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
996 if (encoded_id == NULL)
997 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
999 if (_is_global(uid)) {
1000 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1001 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1002 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1003 app2sd_path, encoded_id);
1005 tzplatform_set_user(uid);
1006 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1007 tzplatform_getenv(TZ_USER_APP), pkgid);
1008 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1009 app2sd_path, encoded_id);
1010 tzplatform_reset_user();
1014 /* check app entry is there in sd card or not. */
1015 fp = fopen(loopback_device, "r+");
1017 _E("app entry is not present in SD Card");
1018 return APP2EXT_ERROR_INVALID_PACKAGE;
1022 /* get installed app size*/
1023 curr_size = _app2sd_calculate_file_size(loopback_device);
1024 curr_size = (curr_size) / (1024 * 1024);
1025 if (curr_size == 0) {
1026 _E("app entry is not present in SD Card");
1027 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
1029 if ((int)curr_size < reqd_disk_size) {
1030 len = strlen(pkgid) + strlen(".new");
1031 temp_pkgid = calloc(len + 1, sizeof(char));
1032 if (temp_pkgid == NULL) {
1033 _E("memory alloc failed");
1034 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1036 snprintf(temp_pkgid, len + 1, "%s.new", pkgid);
1038 if (_is_global(uid)) {
1039 len = strlen(tzplatform_getenv(TZ_SYS_RW_APP)) + strlen(temp_pkgid) + 1;
1040 temp_application_path = calloc(len + 1, sizeof(char));
1041 if (temp_application_path == NULL) {
1042 _E("memory alloc failed");
1044 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1046 snprintf(temp_application_path, len + 1, "%s/%s",
1047 tzplatform_getenv(TZ_SYS_RW_APP), temp_pkgid);
1049 temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
1050 if (temp_encoded_id == NULL) {
1052 free(temp_application_path);
1053 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1055 len = strlen(app2sd_path) + strlen(temp_encoded_id) + 1;
1056 temp_loopback_device = calloc(len + 1, sizeof(char));
1057 if (temp_loopback_device == NULL) {
1058 _E("memory alloc failed");
1060 free(temp_application_path);
1061 free(temp_encoded_id);
1062 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1064 snprintf(temp_loopback_device, len + 1, "%s/%s",
1065 app2sd_path, temp_encoded_id);
1066 free(temp_encoded_id);
1068 tzplatform_set_user(uid);
1069 len = strlen(tzplatform_getenv(TZ_USER_APP)) + strlen(temp_pkgid) + 1;
1070 temp_application_path = calloc(len + 1, sizeof(char));
1071 if (temp_application_path == NULL) {
1072 _E("memory alloc failed");
1074 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1076 snprintf(temp_application_path, len + 1, "%s/%s",
1077 tzplatform_getenv(TZ_USER_APP), temp_pkgid);
1079 temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
1080 if (temp_encoded_id == NULL) {
1082 free(temp_application_path);
1083 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1085 snprintf(temp_uid, 32, "%d", uid);
1086 len = strlen(app2sd_path) + strlen(temp_uid) + strlen(temp_encoded_id) + 2;
1087 temp_loopback_device = calloc(len + 1, sizeof(char));
1088 if (temp_loopback_device == NULL) {
1089 _E("memory alloc failed");
1091 free(temp_application_path);
1092 free(temp_encoded_id);
1093 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1095 snprintf(temp_loopback_device, len + 1, "%s/%s",
1096 app2sd_path, temp_encoded_id);
1097 free(temp_encoded_id);
1098 tzplatform_reset_user();
1100 ret = _app2sd_update_loopback_device_size(pkgid,
1101 loopback_device, application_path, temp_pkgid,
1102 temp_loopback_device, temp_application_path,
1103 reqd_disk_size, dir_list, uid);
1105 free(temp_application_path);
1106 free(temp_loopback_device);
1107 if (APP2EXT_SUCCESS != ret) {
1108 _E("failed to update loopback device size");
1113 /* get the associated device node for SD card applicationer */
1114 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
1116 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
1118 device_node = _app2sd_find_associated_device_node(loopback_device);
1120 if (NULL == device_node) {
1121 /* do loopback setup */
1122 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
1123 ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
1124 false, uid, &device_node);
1126 _E("dmcrypt open device error");
1127 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
1130 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
1131 loopback_device, uid);
1132 if (device_node == NULL) {
1133 _E("loopback encryption setup failed");
1134 return APP2EXT_ERROR_DO_LOSETUP;
1139 ret = _app2sd_mount_app_content(application_path, pkgid,
1140 device_node, MOUNT_TYPE_RW, dir_list,
1141 APP2SD_PRE_UPGRADE, uid);
1148 return APP2EXT_ERROR_MOUNT_PATH;
1151 /* do re-mounting */
1152 ret = _app2sd_mount_app_content(application_path, pkgid,
1153 device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
1154 APP2SD_PRE_UPGRADE, uid);
1156 _E("remount failed");
1161 return APP2EXT_ERROR_MOUNT_PATH;
1174 int app2sd_usr_post_app_upgrade(const char *pkgid,
1175 app2ext_status install_status, uid_t uid)
1177 char mmc_path[FILENAME_MAX] = { 0, };
1178 char loopback_device[FILENAME_MAX] = { 0, };
1179 char application_path[FILENAME_MAX] = { 0, };
1180 char *sdpath = NULL;
1181 char *encoded_id = NULL;
1182 int ret = APP2EXT_SUCCESS;
1185 /* validate the function parameter recieved */
1186 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
1187 || install_status > APP2EXT_STATUS_SUCCESS) {
1188 _E("invalid func parameters");
1189 return APP2EXT_ERROR_INVALID_ARGUMENTS;
1192 /* check whether MMC is present or not */
1193 ret = _app2sd_check_mmc_status(&sdpath);
1195 _E("MMC not present OR Not ready (%d)", ret);
1196 return APP2EXT_ERROR_MMC_STATUS;
1198 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
1202 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1203 if (encoded_id == NULL)
1204 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1206 if (_is_global(uid)) {
1207 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1208 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1209 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
1210 mmc_path, EXTIMG_DIR, encoded_id);
1212 tzplatform_set_user(uid);
1213 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1214 tzplatform_getenv(TZ_USER_APP), pkgid);
1215 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
1216 mmc_path, EXTIMG_DIR, encoded_id);
1217 tzplatform_reset_user();
1221 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
1222 application_path, uid);
1224 _E("failed to finalize device setup");
1228 pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
1229 INSTALL_EXTERNAL, loopback_device, uid);
1230 if (pkgmgr_ret < 0) {
1231 _E("fail to update installed location " \
1232 "to db[%s, %d] of uid(%d), pkgmgr ret(%d)",
1233 pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
1234 return APP2EXT_ERROR_PKGMGR_ERROR;
1241 int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
1243 char mmc_path[FILENAME_MAX] = { 0, };
1244 char loopback_device[FILENAME_MAX] = { 0, };
1245 char application_path[FILENAME_MAX] = { 0, };
1246 char *sdpath = NULL;
1247 char *encoded_id = NULL;
1248 int ret = APP2EXT_SUCCESS;
1250 /* validate the function parameter recieved */
1251 if (pkgid == NULL) {
1252 _E("invalid func parameters");
1253 return APP2EXT_ERROR_INVALID_ARGUMENTS;
1256 /* check whether MMC is present or not */
1257 ret = _app2sd_check_mmc_status(&sdpath);
1259 _E("MMC not present OR Not ready (%d)", ret);
1260 return APP2EXT_ERROR_MMC_STATUS;
1262 snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
1266 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1267 if (encoded_id == NULL)
1268 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1270 if (_is_global(uid)) {
1271 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1272 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1273 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
1274 mmc_path, EXTIMG_DIR, encoded_id);
1276 tzplatform_set_user(uid);
1277 snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
1278 tzplatform_getenv(TZ_USER_APP), pkgid);
1279 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
1280 mmc_path, EXTIMG_DIR, encoded_id);
1281 tzplatform_reset_user();
1285 ret = _app2sd_force_clean(pkgid, application_path, loopback_device, uid);
1291 int app2sd_enable_full_pkg(void)
1293 int ret = APP2EXT_SUCCESS;
1294 char buf[FILENAME_MAX] = { 0, };
1295 char app2sd_path[FILENAME_MAX] = { 0, };
1296 char loopback_device[FILENAME_MAX] = { 0, };
1297 char *sdpath = NULL;
1300 struct dirent *entry = NULL;
1303 /* check whether MMC is present or not */
1304 ret = _app2sd_check_mmc_status(&sdpath);
1306 _E("MMC not present OR Not ready (%d)", ret);
1307 return APP2EXT_ERROR_MMC_STATUS;
1309 snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
1310 sdpath, EXTIMG_DIR);
1313 dir = opendir(app2sd_path);
1315 strerror_r(errno, buf, sizeof(buf));
1316 _E("failed to opendir (%s)", buf);
1317 return APP2EXT_ERROR_OPEN_DIR;
1320 ret = _app2sd_initialize_db();
1322 _E("app2sd db initialize failed");
1324 return APP2EXT_ERROR_SQLITE_REGISTRY;
1327 while ((entry = readdir(dir)) != NULL) {
1328 if (strcmp(entry->d_name, ".") == 0 ||
1329 strcmp(entry->d_name, "..") == 0)
1331 snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
1332 app2sd_path, entry->d_name);
1333 ret = _app2sd_get_info_from_db(loopback_device,
1336 _W("failed to get info from db, continue");
1340 _D("pkgid(%s), uid(%d)", pkgid, uid);
1341 ret = app2sd_usr_on_demand_setup_init(pkgid, uid);
1343 _E("error(%d)", ret);
1361 static int _app2sd_info_cb_func(const char *pkgid, uid_t uid)
1363 int ret = APP2EXT_SUCCESS;
1366 _D("pkgid(%s), uid(%d)", pkgid, uid);
1367 ret = app2sd_usr_on_demand_setup_exit(pkgid, uid);
1369 _E("error(%d)", ret);
1375 int app2sd_disable_full_pkg(void)
1377 int ret = APP2EXT_SUCCESS;
1379 ret = _app2sd_initialize_db();
1381 _E("app2sd db initialize failed");
1382 return APP2EXT_ERROR_SQLITE_REGISTRY;
1385 ret = _app2sd_get_foreach_info_from_db((app2sd_info_cb)_app2sd_info_cb_func);
1387 _E("disable full pkg error(%d)", ret);
1393 static int _app2sd_migrate_legacy_image(const char *pkgid, const char *passwd,
1394 const char *mmc_path, uid_t uid)
1397 char *device_node = NULL;
1398 char *encoded_id = NULL;
1399 char new_image[FILENAME_MAX] = { 0, };
1400 char legacy_image[FILENAME_MAX] = { 0, };
1401 char application_path[FILENAME_MAX] = { 0, };
1402 uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
1404 if (_is_global(uid)) {
1405 snprintf(application_path, sizeof(application_path), "%s/%s",
1406 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1408 tzplatform_set_user(uid);
1409 snprintf(application_path, sizeof(application_path), "%s/%s",
1410 tzplatform_getenv(TZ_USER_APP), pkgid);
1411 tzplatform_reset_user();
1414 /* make the information and insert to DB */
1415 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1416 if (encoded_id == NULL)
1417 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1418 snprintf(new_image, sizeof(new_image), "%s/%s",
1419 mmc_path, encoded_id);
1422 snprintf(legacy_image, sizeof(legacy_image), "%s/%s",
1425 ret = _app2sd_rename_dir(legacy_image, new_image);
1427 _E("unable to rename (%s)", legacy_image);
1428 return APP2EXT_ERROR_ACCESS_FILE;
1431 ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
1432 INSTALL_EXTERNAL, new_image, uid);
1434 _E("fail to update installed location " \
1435 "to db[%s, %d] of uid(%d), ret(%d)",
1436 pkgid, INSTALL_EXTERNAL, uid, ret);
1437 return APP2EXT_ERROR_PKGMGR_ERROR;
1440 /* update app2sd db */
1441 if (_is_global(uid)) {
1442 ret = _app2sd_remove_info_from_db(pkgid, default_uid);
1444 _E("cannot remove info from db");
1445 return APP2EXT_ERROR_SQLITE_REGISTRY;
1448 ret = _app2sd_set_info_in_db(pkgid, passwd, new_image, uid);
1450 _E("unable to save info");
1451 return APP2EXT_ERROR_SQLITE_REGISTRY;
1454 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
1456 _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
1458 _E("device_node(%s_%d) already associated", pkgid, uid);
1460 return APP2EXT_ERROR_ALREADY_MOUNTED;
1463 ret = _app2sd_dmcrypt_open_device(pkgid, new_image, false,
1466 _E("dmcrypt open device error(%d)", ret);
1467 return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
1470 result = (char *)_app2sd_find_associated_device(new_image);
1471 /* process the string */
1472 if ((result != NULL) && strstr(result, "/dev") != NULL) {
1473 _E("already associated");
1476 return APP2EXT_ERROR_ALREADY_MOUNTED;
1479 /* do loopback setup */
1480 device_node = _app2sd_do_loopback_encryption_setup(pkgid,
1482 if (device_node == NULL) {
1483 _E("loopback encryption setup failed");
1484 return APP2EXT_ERROR_DO_LOSETUP;
1489 ret = _app2sd_mount_app_content(application_path, pkgid,
1490 device_node, MOUNT_TYPE_RW, NULL,
1491 APP2SD_MIGRATE_LEGACY, uid);
1498 return APP2EXT_ERROR_MOUNT_PATH;
1507 return APP2EXT_SUCCESS;
1510 int app2sd_pre_migrate_legacy(const char *pkgid, uid_t uid)
1512 int ret = APP2EXT_SUCCESS;
1513 char *passwd = NULL;
1514 char *sdpath = NULL;
1515 char mmc_path[FILENAME_MAX] = { 0, };
1516 char file_path[FILENAME_MAX] = { 0, };
1517 uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
1520 /* check whether MMC is present or not */
1521 ret = _app2sd_check_mmc_status(&sdpath);
1523 _E("MMC not present OR Not ready (%d)", ret);
1524 return APP2EXT_ERROR_MMC_STATUS;
1526 snprintf(mmc_path, sizeof(mmc_path), "%s/%s", sdpath, EXTIMG_DIR);
1528 snprintf(file_path, sizeof(file_path), "%s/%s", mmc_path, pkgid);
1530 ret = _app2sd_initialize_db();
1532 _E("app2sd db initialize failed");
1533 return APP2EXT_ERROR_SQLITE_REGISTRY;
1536 /* In the upgrade script, the information
1537 * of legacy image are filled with default user-id,
1538 * pkgid(from old db), passwd(from old db) and
1539 * empty filename in app2sd db. */
1540 filename = _app2sd_get_filename_from_db(pkgid, default_uid);
1541 if (filename == NULL) {
1542 passwd = _app2sd_get_password_from_db(pkgid, default_uid);
1544 ret = _app2sd_migrate_legacy_image(pkgid, passwd,
1549 _E("invalid package info");
1550 return APP2EXT_ERROR_INVALID_PACKAGE;
1554 _W("same pkg exists, remove legacy file (%s)", file_path);
1555 ret = remove(file_path);
1557 _E("failed to remove, errno(%d)", errno);
1558 return APP2EXT_ERROR_PKG_EXISTS;
1564 int app2sd_post_migrate_legacy(const char *pkgid, uid_t uid)
1566 int ret = APP2EXT_SUCCESS;
1567 char *sdpath = NULL;
1568 char *encoded_id = NULL;
1569 char mmc_path[FILENAME_MAX] = { 0, };
1570 char application_path[FILENAME_MAX] = { 0, };
1571 char loopback_device[FILENAME_MAX] = { 0, };
1573 /* check whether MMC is present or not */
1574 ret = _app2sd_check_mmc_status(&sdpath);
1576 _E("MMC not present OR Not ready (%d)", ret);
1577 return APP2EXT_ERROR_MMC_STATUS;
1579 snprintf(mmc_path, sizeof(mmc_path), "%s/%s",
1580 sdpath, EXTIMG_DIR);
1583 if (_is_global(uid)) {
1584 snprintf(application_path, sizeof(application_path), "%s/%s",
1585 tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
1587 tzplatform_set_user(uid);
1588 snprintf(application_path, sizeof(application_path), "%s/%s",
1589 tzplatform_getenv(TZ_USER_APP), pkgid);
1590 tzplatform_reset_user();
1593 encoded_id = _app2sd_get_encoded_name(pkgid, uid);
1594 if (encoded_id == NULL) {
1595 return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
1597 snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
1598 mmc_path, encoded_id);
1601 ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
1602 application_path, uid);
1604 _E("failed to finalize device setup");
1608 return APP2EXT_SUCCESS;
1611 /* this function is called when sdcard is inserted */
1612 int app2sd_migrate_legacy_all(void)
1614 int ret = APP2EXT_SUCCESS;
1616 char buf[FILENAME_MAX] = { 0, };
1617 char app2sd_path[FILENAME_MAX] = { 0, };
1618 char loopback_device[FILENAME_MAX] = { 0, };
1619 char *sdpath = NULL;
1621 struct dirent entry;
1622 struct dirent *result = NULL;
1623 uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
1626 /* check whether MMC is present or not */
1627 ret = _app2sd_check_mmc_status(&sdpath);
1629 _E("MMC not present OR Not ready (%d)", ret);
1630 return APP2EXT_ERROR_MMC_STATUS;
1632 snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s",
1633 sdpath, EXTIMG_DIR);
1636 dir = opendir(app2sd_path);
1638 strerror_r(errno, buf, sizeof(buf));
1639 _E("failed to opendir (%s)", buf);
1640 return APP2EXT_ERROR_OPEN_DIR;
1643 pc = pkgmgr_client_new(PC_REQUEST);
1645 _E("failed to create pkgmgr client");
1647 return APP2EXT_ERROR_PKGMGR_ERROR;
1650 for (rc = readdir_r(dir, &entry, &result);
1651 rc == 0 && result != NULL;
1652 rc = readdir_r(dir, &entry, &result)) {
1653 if (strcmp(entry.d_name, ".") == 0 ||
1654 strcmp(entry.d_name, "..") == 0)
1656 snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
1657 app2sd_path, entry.d_name);
1658 /* check losetup image */
1659 if (_app2sd_check_is_luks_device(loopback_device) == 0) {
1660 /* call installer backend
1661 * to change access-rule and broadcast this update */
1662 ret = pkgmgr_client_usr_migrate_external_image(pc,
1663 entry.d_name, default_uid);
1665 _E("failed to request migration, ret(%d)", ret);
1669 pkgmgr_client_free(pc);