4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Garima Shrivastava<garima.s@samsung.com>
7 * Jyotsna Dhumale <jyotsna.a@samsung.com>
8 * Venkatesha Sarpangala <sarpangala.v@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 #include <app2sd_internals.h>
25 #include <app2sd_interface.h>
27 #include <sys/types.h>
30 int app2sd_pre_app_install(const char *pkgid, GList* dir_list,
35 char *device_node = NULL;
39 /*Validate the function parameter recieved */
40 if (pkgid == NULL || dir_list == NULL || size <= 0) {
41 app2ext_print("App2Sd Error : Invalid function arguments\n");
42 return APP2EXT_ERROR_INVALID_ARGUMENTS;
44 /*Check whether MMC is present or not */
45 ret = _app2sd_check_mmc_status();
47 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
49 return APP2EXT_ERROR_MMC_STATUS;
51 /*Find available free memory in the MMC card */
52 ret = _app2sd_get_available_free_memory(MMC_PATH,
55 app2ext_print("App2Sd Error : Unable to get available free memory in MMC %d\n", ret);
56 return APP2EXT_ERROR_MMC_STATUS;
58 /*If avaialalbe free memory in MMC is less than required size + 5MB , return error */
59 if ((size + PKG_BUF_SIZE + MEM_BUF_SIZE) > free_mmc_mem) {
60 app2ext_print("Insufficient memory in MMC for application installation %d\n", ret);
61 return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
63 /*Create a loopback device */
64 ret = _app2sd_create_loopback_device(pkgid, (size+PKG_BUF_SIZE));
66 app2ext_print("App2Sd Error : Package already present\n");
69 /*Perform Loopback encryption setup */
70 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
72 app2ext_print("App2Sd Error : Loopback encryption setup failed\n");
73 _app2sd_delete_loopback_device(pkgid);
74 return APP2EXT_ERROR_DO_LOSETUP;
76 /*Check whether loopback device is associated with device node or not */
77 devi = _app2sd_find_associated_device_node(pkgid);
79 app2ext_print("App2Sd Error : finding associated device node failed\n");
80 ret = APP2EXT_ERROR_DO_LOSETUP;
84 /*Format the loopback file system */
85 ret = _app2sd_create_file_system(device_node);
87 app2ext_print("App2Sd Error : creating FS failed failed\n");
88 ret = APP2EXT_ERROR_CREATE_FS;
92 /*Mount the loopback encrypted pseudo device on application installation path as with Read Write permission */
93 ret =_app2sd_mount_app_content(pkgid, device_node, MOUNT_TYPE_RW,
94 dir_list, APP2SD_PRE_INSTALL);
96 app2ext_print("App2Sd Error : mounting dev path to app install path failed\n");
97 ret = APP2EXT_ERROR_MOUNT_PATH;
102 ret = APP2EXT_SUCCESS;
108 result = _app2sd_detach_loop_device(device_node);
113 _app2sd_delete_loopback_device(pkgid);
127 int app2sd_post_app_install(const char *pkgid,
128 app2ext_status install_status)
130 char *device_name = NULL;
131 char buf_dir[FILENAME_MAX] = { 0, };
132 int ret = APP2EXT_SUCCESS;
133 /*Validate the function parameter recieved */
134 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
135 || install_status > APP2EXT_STATUS_SUCCESS) {
136 app2ext_print("Invalid func parameters\n");
137 return APP2EXT_ERROR_INVALID_ARGUMENTS;
140 /*Check whether MMC is present or not */
141 ret = _app2sd_check_mmc_status();
143 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
145 return APP2EXT_ERROR_MMC_STATUS;
148 /*Get the associated device node for SD card applicationer */
149 device_name = _app2sd_find_associated_device_node(pkgid);
150 if (NULL == device_name) {
151 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
153 ret = _app2sd_unmount_app_content(pkgid);
159 app2ext_print("Unable to unmount the app content %d\n", ret);
160 return APP2EXT_ERROR_UNMOUNT;
162 ret = _app2sd_remove_loopback_encryption_setup(pkgid);
169 ("Unable to Detach the loopback encryption setup for the application");
170 return APP2EXT_ERROR_UNMOUNT;
177 /*Take appropriate action based on installation
178 status of application package */
179 if (install_status == APP2EXT_STATUS_FAILED) {
180 /*Delete the loopback device from the SD card */
181 ret = _app2sd_delete_loopback_device(pkgid);
184 ("App2Sd Error : Unable to delete the loopback device from the SD Card\n");
185 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
187 ret = _app2sd_remove_password_from_db(pkgid);
191 ("App2Sd Error : Unable to delete the password\n");
194 snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
196 ret = _app2sd_delete_directory(buf_dir);
200 ("App2Sd Error : Unable to delete the directory %s\n",
208 int app2sd_on_demand_setup_init(const char *pkgid)
210 int ret = APP2EXT_SUCCESS;
211 char app_path[FILENAME_MAX] = { 0, };
212 char *device_node = NULL;
216 /*Validate the function parameter recieved */
219 ("App2Sd Error : Invalid function arguments to app launch setup\n");
220 return APP2EXT_ERROR_INVALID_ARGUMENTS;
223 /*Check whether MMC is present or not */
224 ret = _app2sd_check_mmc_status();
226 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
228 return APP2EXT_ERROR_MMC_STATUS;
231 /*check app entry is there in sd card or not. */
232 snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
234 fp = fopen(app_path, "r+");
237 ("App2SD Error: App Entry is not present in SD Card\n");
238 return APP2EXT_ERROR_INVALID_PACKAGE;
241 result = (char *)_app2sd_find_associated_device(app_path);
242 /*process the string */
243 if ((result!=NULL) && strstr(result, "/dev") != NULL) {
244 app2ext_print("App2SD Error! Already associated\n");
247 return APP2EXT_ERROR_ALREADY_MOUNTED;
250 /*Do loopback setup */
251 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
252 if (device_node == NULL) {
254 ("App2Sd Error : loopback encryption setup failed\n");
255 return APP2EXT_ERROR_DO_LOSETUP;
260 _app2sd_mount_app_content(pkgid, device_node, MOUNT_TYPE_RD,
261 NULL, APP2SD_APP_LAUNCH);
263 app2ext_print("App2Sd Error : Re-mount failed\n");
268 return APP2EXT_ERROR_MOUNT_PATH;
277 int app2sd_on_demand_setup_exit(const char *pkgid)
279 int ret = APP2EXT_SUCCESS;
280 char app_path[FILENAME_MAX] = { 0, };
283 /*Validate the function parameter recieved */
286 ("App2Sd Error : Invalid function arguments to app launch setup\n");
287 return APP2EXT_ERROR_INVALID_ARGUMENTS;
290 /*Check whether MMC is present or not */
291 ret = _app2sd_check_mmc_status();
293 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
295 return APP2EXT_ERROR_MMC_STATUS;
297 /*check app entry is there in sd card or not. */
298 snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
300 fp = fopen(app_path, "r+");
303 ("App2SD Error: App Entry is not present in SD Card\n");
304 return APP2EXT_ERROR_INVALID_PACKAGE;
307 ret = _app2sd_unmount_app_content(pkgid);
310 ("App2SD Error: Unable to unmount the SD application\n");
311 return APP2EXT_ERROR_UNMOUNT;
313 ret = _app2sd_remove_loopback_encryption_setup(pkgid);
315 app2ext_print("App2SD Error: Unable to remove loopback setup\n");
316 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
321 int app2sd_pre_app_uninstall(const char *pkgid)
323 int ret = APP2EXT_SUCCESS;
324 char app_path[FILENAME_MAX] = { 0, };
325 char *device_node = NULL;
328 /*Validate the function parameter recieved */
331 ("App2Sd Error : Invalid function arguments to app launch setup\n");
332 return APP2EXT_ERROR_INVALID_ARGUMENTS;
334 /*Check whether MMC is present or not */
335 ret = _app2sd_check_mmc_status();
337 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
339 return APP2EXT_ERROR_MMC_STATUS;
341 /*check app entry is there in sd card or not. */
342 snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
343 fp = fopen(app_path, "r+");
346 ("App2SD Error: App Entry is not present in SD Card\n");
347 return APP2EXT_ERROR_INVALID_PACKAGE;
351 /*Get the associated device node for SD card applicationer */
352 device_node = _app2sd_find_associated_device_node(pkgid);
353 if (NULL == device_node) {
354 /*Do loopback setup */
355 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
357 if (device_node == NULL) {
359 ("App2Sd Error : loopback encryption setup failed\n");
360 return APP2EXT_ERROR_DO_LOSETUP;
364 _app2sd_mount_app_content(pkgid, device_node,
366 APP2SD_PRE_UNINSTALL);
369 app2ext_print("App2Sd Error : RW-mount failed\n");
374 return APP2EXT_ERROR_MOUNT_PATH;
379 _app2sd_mount_app_content(pkgid, device_node,
380 MOUNT_TYPE_RW_REMOUNT, NULL,
381 APP2SD_PRE_UNINSTALL);
384 app2ext_print("App2Sd Error : Re-mount failed\n");
389 return APP2EXT_ERROR_MOUNT_PATH;
400 * app2sd_post_app_uninstall_setup
401 * Uninstall Application and free all the allocated resources
402 * Called after dpkg remove, It deallocates dev node and loopback
404 int app2sd_post_app_uninstall(const char *pkgid)
406 char buf_dir[FILENAME_MAX] = { 0, };
407 int ret = APP2EXT_SUCCESS;
408 int ret1 = APP2EXT_SUCCESS;
409 /*Validate the function parameter recieved */
412 ("App2Sd Error : Invalid function arguments to Post Uninstall\n");
413 return APP2EXT_ERROR_INVALID_ARGUMENTS;
415 /*Check whether MMC is present or not */
416 ret = _app2sd_check_mmc_status();
418 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
420 return APP2EXT_ERROR_MMC_STATUS;
422 /*Unmount the loopback encrypted pseudo device from the application installation path */
423 ret = _app2sd_unmount_app_content(pkgid);
425 app2ext_print("Unable to unmount the app content %d\n", ret);
426 return APP2EXT_ERROR_UNMOUNT;
428 /*Detach the loopback encryption setup for the application */
429 ret = _app2sd_remove_loopback_encryption_setup(pkgid);
432 ("Unable to Detach the loopback encryption setup for the application");
433 return APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
435 /*Delete the loopback device from the SD card */
436 ret = _app2sd_delete_loopback_device(pkgid);
439 ("App2Sd Error : Unable to delete the loopback device from the SD Card\n");
440 return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
442 memset((void *)&buf_dir, '\0', FILENAME_MAX);
443 snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
444 ret1 = _app2sd_delete_directory(buf_dir);
447 ("App2Sd Error : Unable to delete the directory %s\n",
450 /*remove encryption password from DB */
451 ret = _app2sd_initialize_db();
453 app2ext_print("\n app2sd db initialize failed");
454 return APP2EXT_ERROR_SQLITE_REGISTRY;
456 ret = _app2sd_remove_password_from_db(pkgid);
458 app2ext_print("cannot remove password from db \n");
459 return APP2EXT_ERROR_SQLITE_REGISTRY;
464 int app2sd_move_installed_app(const char *pkgid, GList* dir_list,
465 app2ext_move_type move_type)
469 /*Validate function arguments*/
470 if (pkgid == NULL || dir_list == NULL
471 || move_type < APP2EXT_MOVE_TO_EXT
472 || move_type > APP2EXT_MOVE_TO_PHONE) {
473 app2ext_print("App2Sd Error : Invalid function arguments\n");
474 return APP2EXT_ERROR_INVALID_ARGUMENTS;
477 ret = _app2sd_move_app(pkgid, move_type, dir_list);
479 app2ext_print("App2Sd Error : Unable to move application\n");
485 int app2sd_pre_app_upgrade(const char *pkgid, GList* dir_list,
488 int ret = APP2EXT_SUCCESS;
489 char app_path[FILENAME_MAX] = { 0, };
490 char *device_node = NULL;
491 unsigned long long curr_size = 0;
494 /*Validate function arguments*/
495 if (pkgid == NULL || dir_list == NULL || size<=0) {
497 ("App2Sd Error : Invalid function arguments \n");
498 return APP2EXT_ERROR_INVALID_ARGUMENTS;
500 /*Check whether MMC is present or not */
501 ret = _app2sd_check_mmc_status();
503 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
505 return APP2EXT_ERROR_MMC_STATUS;
507 /*check app entry is there in sd card or not. */
508 snprintf(app_path, FILENAME_MAX, "%s%s", APP2SD_PATH,
510 app2ext_print("App2Sd Log : Checking path %s\n", app_path);
511 fp = fopen(app_path, "r+");
514 ("App2SD Error: App Entry is not present in SD Card\n");
515 return APP2EXT_ERROR_INVALID_PACKAGE;
518 /*Get installed app size*/
519 curr_size = _app2sd_calculate_file_size(app_path);
520 curr_size = (curr_size/1024)/1024;
524 ("App2SD Error: App Entry is not present in SD Card\n");
525 return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
527 if (curr_size<size) {
528 ret = _app2sd_update_loopback_device_size(pkgid, size, dir_list);
529 if(APP2EXT_SUCCESS !=ret) {
531 ("App2SD Error: _app2sd_update_loopback_device_size() failed\n");
536 /*Get the associated device node for SD card applicationer */
537 device_node = _app2sd_find_associated_device_node(pkgid);
538 if (NULL == device_node) {
539 /*Do loopback setup */
540 device_node = _app2sd_do_loopback_encryption_setup(pkgid);
541 if (device_node == NULL) {
543 ("App2Sd Error : loopback encryption setup failed\n");
544 return APP2EXT_ERROR_DO_LOSETUP;
548 _app2sd_mount_app_content(pkgid, device_node,
552 app2ext_print("App2Sd Error : Re-mount failed\n");
557 return APP2EXT_ERROR_MOUNT_PATH;
562 _app2sd_mount_app_content(pkgid, device_node,
563 MOUNT_TYPE_RW_REMOUNT, NULL,
566 app2ext_print("App2Sd Error : Re-mount failed\n");
571 return APP2EXT_ERROR_MOUNT_PATH;
583 int app2sd_post_app_upgrade(const char *pkgid,
584 app2ext_status install_status)
586 char *device_name = NULL;
587 int ret = APP2EXT_SUCCESS;
588 /*Validate the function parameter recieved */
589 if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
590 || install_status > APP2EXT_STATUS_SUCCESS) {
591 app2ext_print("Invalid func parameters\n");
592 return APP2EXT_ERROR_INVALID_ARGUMENTS;
594 /*Check whether MMC is present or not */
595 ret = _app2sd_check_mmc_status();
597 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
599 return APP2EXT_ERROR_MMC_STATUS;
602 /*Get the associated device node for SD card applicationer */
603 device_name = _app2sd_find_associated_device_node(pkgid);
604 if (NULL == device_name) {
605 return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
607 ret = _app2sd_unmount_app_content(pkgid);
613 app2ext_print("Unable to unmount the app content %d\n", ret);
614 return APP2EXT_ERROR_UNMOUNT;
616 ret = _app2sd_remove_loopback_encryption_setup(pkgid);
623 ("Unable to Detach the loopback encryption setup for the application");
624 return APP2EXT_ERROR_UNMOUNT;
635 * Reserved API for forced cleanup
638 int app2sd_force_cleanup(const char *pkgid){
639 char *device_name = NULL;
640 char buf_dir[FILENAME_MAX] = { 0, };
641 int ret = APP2EXT_SUCCESS;
644 /*Validate the function parameter recieved */
646 app2ext_print("invalid func parameters\n");
647 return APP2EXT_ERROR_INVALID_ARGUMENTS;
649 memset((void *)&buf_dir, '\0', FILENAME_MAX);
650 snprintf(buf_dir, FILENAME_MAX, "%s%s", APP2SD_PATH, pkgid);
651 fp = fopen(buf_dir, "r+");
653 app2ext_print("\"%s\" not installed on SD Card\n", pkgid);
654 return APP2EXT_ERROR_INVALID_PACKAGE;
657 /*Check whether MMC is present or not */
658 ret = _app2sd_check_mmc_status();
660 app2ext_print("App2Sd Error : MMC not preset OR Not ready %d\n",
662 return APP2EXT_ERROR_MMC_STATUS;
665 /*Get the associated device node for SD card applicationer */
666 device_name = _app2sd_find_associated_device_node(pkgid);
667 if (NULL != device_name) {
670 ret = _app2sd_unmount_app_content(pkgid);
672 app2ext_print("Unable to unmount the app content %d\n", ret);
673 return APP2EXT_ERROR_UNMOUNT;
675 ret = _app2sd_remove_loopback_encryption_setup(pkgid);
678 ("Unable to Detach the loopback encryption setup for the application");
679 return APP2EXT_ERROR_UNMOUNT;
683 memset((void *)&buf_dir, '\0', FILENAME_MAX);
684 snprintf(buf_dir, FILENAME_MAX, "%s%s", APP_INSTALLATION_PATH, pkgid);
685 ret = _app2sd_delete_directory(buf_dir);
688 ("App2Sd Error : Unable to delete the directory %s\n",
692 /*remove passwrd from DB*/
693 ret = _app2sd_initialize_db();
695 app2ext_print("\n app2sd db initialize failed");
696 return APP2EXT_ERROR_SQLITE_REGISTRY;
698 ret = _app2sd_remove_password_from_db(pkgid);
700 app2ext_print("cannot remove password from db \n");
701 return APP2EXT_ERROR_SQLITE_REGISTRY;
707 /* This is the plug-in load function. The plugin has to bind its functions to function pointers of handle
708 @param[in/out] st_interface Specifies the storage interface.
711 app2ext_on_load(app2ext_interface *st_interface)
714 st_interface->pre_install= app2sd_pre_app_install;
715 st_interface->post_install= app2sd_post_app_install;
716 st_interface->pre_uninstall= app2sd_pre_app_uninstall;
717 st_interface->post_uninstall= app2sd_post_app_uninstall;
718 st_interface->pre_upgrade= app2sd_pre_app_upgrade;
719 st_interface->post_upgrade= app2sd_post_app_upgrade;
720 st_interface->move= app2sd_move_installed_app;
721 st_interface->enable= app2sd_on_demand_setup_init;
722 st_interface->disable= app2sd_on_demand_setup_exit;