remove unused vconf key.
[platform/core/appfw/app2sd.git] / plugin / app2sd / src / app2sd_internals.c
index 9aba3fb..22fd099 100755 (executable)
 /*
 ########### Internal APIs ##################
  */
+enum path_type {
+       PATH_PRIVATE,
+       PATH_GROUP_RW,
+       PATH_PUBLIC_RO,
+       PATH_SETTINGS_RW,
+       PATH_NPRUNTIME,
+       PATH_ANY_LABEL
+};
+
+static int _app2sd_apply_app_smack(const char *pkgid, GList* dir_list, const char *groupid)
+{
+       int ret = APP2EXT_SUCCESS;
+       GList *list = NULL;
+       app2ext_dir_details* dir_detail = NULL;
+       char path[FILENAME_MAX] = { 0, };
+
+       list = g_list_first(dir_list);
+       while (list) {
+               dir_detail = (app2ext_dir_details *)list->data;
+               if (dir_detail && dir_detail->name
+                       && dir_detail->type == APP2EXT_DIR_RO) {
+                       snprintf(path, FILENAME_MAX, "%s%s/%s",APP_INSTALLATION_PATH, pkgid, dir_detail->name);
+                       ret = _app2sd_setup_path(pkgid, path, PATH_ANY_LABEL, groupid);
+                       if (ret) {
+                               app2ext_print ("App2Sd Error : unable to smack %s\n", path);
+                               return APP2EXT_ERROR_MOVE;
+                       }
+               }
+               list = g_list_next(list);
+       }
+
+       return APP2EXT_SUCCESS;
+}
+
+static int _app2sd_apply_mmc_smack(const char *pkgid, GList* dir_list, const char *groupid)
+{
+       int ret = APP2EXT_SUCCESS;
+       GList *list = NULL;
+       app2ext_dir_details* dir_detail = NULL;
+       char path[FILENAME_MAX] = { 0, };
+
+       list = g_list_first(dir_list);
+       while (list) {
+               dir_detail = (app2ext_dir_details *)list->data;
+               if (dir_detail && dir_detail->name
+                       && dir_detail->type == APP2EXT_DIR_RO) {
+                       snprintf(path, FILENAME_MAX, "%s%s/.mmc/%s",APP_INSTALLATION_PATH, pkgid, dir_detail->name);
+
+                       ret = _app2sd_setup_path(pkgid, path, PATH_ANY_LABEL, groupid);
+                       if (ret) {
+                               app2ext_print ("App2Sd Error : unable to smack %s\n", path);
+                               return APP2EXT_ERROR_MOVE;
+                       }
+               }
+               list = g_list_next(list);
+       }
+
+       return APP2EXT_SUCCESS;
+}
 
 char *_app2sd_find_associated_device_node(const char *pkgid)
 {
@@ -431,6 +490,12 @@ static int _app2sd_create_dir_with_link(const char *pkgid,
                }
        }
 
+       ret = _app2sd_setup_path(pkgid, app_dir_path, PATH_ANY_LABEL, pkgid);
+       if (ret) {
+               app2ext_print ("App2Sd Error : unable to smack %s\n", app_dir_mmc_path);
+               return APP2EXT_ERROR_MOVE;
+       }
+
        return ret;
 }
 
@@ -499,6 +564,9 @@ int _app2sd_mount_app_content(const char *pkgid, const char *dev,
                }
        }
 
+       usleep(200 * 1000);     /* 200ms sleep*/
+       app2ext_print ("App2Sd info : give a delay for mount\n");
+
        switch (mount_type) {
        case MOUNT_TYPE_RD:
                {
@@ -642,10 +710,10 @@ int _app2sd_move_app_to_external(const char *pkgid, GList* dir_list)
        fp = fopen(mmc_path, "r+");
        if (fp != NULL) {
                app2ext_print
-                   ("Already %s entry is present in the SD Card\n",
+                   ("Already %s entry is present in the SD Card, delete entry and go on without return\n",
                     pkgid);
                fclose(fp);
-               return APP2EXT_ERROR_ALREADY_FILE_PRESENT;
+//             return APP2EXT_ERROR_ALREADY_FILE_PRESENT;
        }
 
        snprintf(app_mmc_path, FILENAME_MAX,
@@ -659,7 +727,7 @@ int _app2sd_move_app_to_external(const char *pkgid, GList* dir_list)
                        app2ext_print
                            ("App2sd Error: Unable to create directory for archiving, error no is %d\n",
                             errno);
-                       return APP2EXT_ERROR_CREATE_DIRECTORY;
+//                     return APP2EXT_ERROR_CREATE_DIRECTORY;
                }
        }
 
@@ -706,7 +774,7 @@ int _app2sd_move_app_to_external(const char *pkgid, GList* dir_list)
        if (ret) {
                app2ext_print
                    ("App2Sd Error : loopback node creation failed\n");
-               return APP2EXT_ERROR_CREATE_DEVICE;
+//             return APP2EXT_ERROR_CREATE_DEVICE;
        }
        /*Perform Loopback encryption setup */
        device_node =
@@ -758,8 +826,7 @@ int _app2sd_move_app_to_external(const char *pkgid, GList* dir_list)
                                            ("App2Sd Error : unable to copy from %s to %s \n",
                                             path,
                                             app_archive_path);
-                                       return
-                                           APP2EXT_ERROR_MOVE;
+//                                     return APP2EXT_ERROR_MOVE;
                                }
                        }
                }
@@ -803,8 +870,7 @@ int _app2sd_move_app_to_external(const char *pkgid, GList* dir_list)
                                             app_mmc_path,
                                             strerror
                                             (errno));
-                                       return
-                                           APP2EXT_ERROR_MOVE;
+//                                     return APP2EXT_ERROR_MOVE;
                                }
                        }
                        ret =
@@ -832,8 +898,15 @@ int _app2sd_move_app_to_external(const char *pkgid, GList* dir_list)
                app2ext_print
                    ("App2Sd Error : unable to delete %s \n",
                     app_archive_path);
-               return APP2EXT_ERROR_DELETE_DIRECTORY;
+//             return APP2EXT_ERROR_DELETE_DIRECTORY;
        }
+
+       ret = _app2sd_apply_mmc_smack(pkgid, dir_list, pkgid);
+       if (ret) {
+               app2ext_print("App2Sd Error : unable to apply app smack\n");
+               return APP2EXT_ERROR_MOVE;
+       }
+
        /*Restore archive ends */
        /*Re-mount the loopback encrypted pseudo device on application installation path as with Read Only permission */
        ret = _app2sd_unmount_app_content(pkgid);
@@ -939,7 +1012,7 @@ int _app2sd_move_app_to_internal(const char *pkgid, GList* dir_list)
                app2ext_print
                    ("App2Sd Error : unable to create directory%s\n",
                     app_archive_path);
-               return APP2EXT_ERROR_CREATE_DIRECTORY;
+//             return APP2EXT_ERROR_CREATE_DIRECTORY;
        }
 
 
@@ -971,8 +1044,7 @@ int _app2sd_move_app_to_internal(const char *pkgid, GList* dir_list)
                                                     app_archive_path,
                                                     strerror
                                                     (errno));
-                                               return
-                                                   APP2EXT_ERROR_MOVE;
+//                                             return APP2EXT_ERROR_MOVE;
                                        }
                                }
 
@@ -991,10 +1063,9 @@ int _app2sd_move_app_to_internal(const char *pkgid, GList* dir_list)
                                                     path);
                                        } else {
                                                app2ext_print
-                                                   ("App2Sd Error : unable to remove the symbolic link file %s\n",
+                                                   ("App2Sd Error : unable to remove the symbolic link file %s, it is already unlinked!!!\n",
                                                     path);
-                                               return
-                                                   APP2EXT_ERROR_DELETE_LINK_FILE;
+//                                             return APP2EXT_ERROR_DELETE_LINK_FILE;
                                        }
                                }
 
@@ -1020,8 +1091,7 @@ int _app2sd_move_app_to_internal(const char *pkgid, GList* dir_list)
                                                     app_path,
                                                     strerror
                                                     (errno));
-                                               return
-                                                   APP2EXT_ERROR_MOVE;
+//                                             return APP2EXT_ERROR_MOVE;
                                        }
                                }
                }
@@ -1055,15 +1125,22 @@ int _app2sd_move_app_to_internal(const char *pkgid, GList* dir_list)
                app2ext_print
                    ("App2Sd Error : unable to delete %s \n",
                     app_mmc_path);
-               return APP2EXT_ERROR_DELETE_DIRECTORY;
+//             return APP2EXT_ERROR_DELETE_DIRECTORY;
        }
        ret = _app2sd_delete_directory(app_archive_path);
        if (ret) {
                app2ext_print
                    ("App2Sd Error : unable to delete %s \n",
                     app_archive_path);
-               return APP2EXT_ERROR_DELETE_DIRECTORY;
+//             return APP2EXT_ERROR_DELETE_DIRECTORY;
        }
+
+       ret = _app2sd_apply_app_smack(pkgid, dir_list, pkgid);
+       if (ret) {
+               app2ext_print("App2Sd Error : unable to apply app smack\n");
+               return APP2EXT_ERROR_MOVE;
+       }
+
        return APP2EXT_SUCCESS;
 }
 
@@ -1278,7 +1355,7 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
                if (ret) {
                        app2ext_print("App2Sd Error : Re-mount failed\n");
                        err_res = APP2EXT_ERROR_MOUNT_PATH;
-                       goto FINISH_OFF;
+//                     goto FINISH_OFF;
                }
        } else {
                /*Do  re-mounting */
@@ -1289,7 +1366,7 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
                if (ret) {
                        app2ext_print("App2Sd Error : Re-mount failed\n");
                        err_res = APP2EXT_ERROR_MOUNT_PATH;
-                       goto FINISH_OFF;
+//                     goto FINISH_OFF;
                }
        }
 
@@ -1302,7 +1379,7 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
        if (ret) {
                app2ext_print("App2Sd Error : copy ro content  failed\n");
                err_res = ret;
-               goto FINISH_OFF;
+//             goto FINISH_OFF;
        }
 
        ret = _app2sd_unmount_app_content(pkgid);
@@ -1310,13 +1387,13 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
                app2ext_print
                    ("App2SD Error: Unable to unmount the SD application\n");
                err_res = APP2EXT_ERROR_UNMOUNT;
-               goto FINISH_OFF;
+//             goto FINISH_OFF;
        }
        ret = _app2sd_remove_loopback_encryption_setup(pkgid);
        if (ret) {
                app2ext_print("App2SD Error: Unable to remove loopback setup\n");
                err_res = APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
-               goto FINISH_OFF;
+//             goto FINISH_OFF;
        }
        ret = _app2sd_unmount_app_content(temp_pkgid);
        if (ret) {
@@ -1374,3 +1451,46 @@ FINISH_OFF:
        }
        return err_res;
 }
+
+void _app2sd_make_result_info_file(char *pkgid, int size)
+{
+       int ret = 0;
+       FILE* file = NULL;
+       int fd = 0;
+       char buf[FILENAME_MAX] = {0};
+       const char* app_info_label = "*";
+       char info_file[FILENAME_MAX] = {'\0', };
+
+       if(pkgid == NULL)
+               return;
+
+       snprintf(info_file, FILENAME_MAX, "/tmp/%s", pkgid);
+       app2ext_print("App2SD info : File path = %s\n", info_file);
+
+       file = fopen(info_file, "w");
+       if (file == NULL) {
+               app2ext_print("App2SD Error: Couldn't open the file %s \n", info_file);
+               return;
+       }
+
+       snprintf(buf, 128, "%d\n", size);
+       fwrite(buf, 1, strlen(buf), file);
+
+       fflush(file);
+       fd = fileno(file);
+       fsync(fd);
+       fclose(file);
+
+       if(lsetxattr(info_file, "security.SMACK64", app_info_label, strlen(app_info_label), 0)) {
+               app2ext_print("App2SD Error: error(%d) in setting smack label",errno);
+       }
+       ret = chmod(info_file, 0777);
+       if (ret == -1) {
+               return;
+       }
+
+       ret = chown(info_file, 5000, 5000);
+       if (ret == -1) {
+               return;
+       }
+}