/* Copyright (c) KylinSoft  Co., Ltd. [2023].All rights reserved.
    [ostree-maintain-daemon] licensed under the [Your LICENSE].
    Author: [ZhangTingting]
    Create: [2024]
*/

#include "mm-upgrade-create-ovldir.h"
#include "mm-public.h"


static gboolean create_pending_deploy_dir(const gchar *hash_value)
{
    gchar *ovl_etc_lower = g_strconcat(OVL_DIR_PRE, hash_value, ETC_LAYER_LOWER, NULL);
    if (g_mkdir_with_parents(ovl_etc_lower, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }

    gchar *ovl_etc_upper = g_strconcat(OVL_DIR_PRE, hash_value, ETC_LAYER_UPPER, NULL);
    if (g_mkdir_with_parents(ovl_etc_upper, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }

    gchar *ovl_etc_tmp_upper = g_strconcat(OVL_DIR_PRE, hash_value, ETC_LAYER_TMP_UPPER, NULL);
    if (g_mkdir_with_parents(ovl_etc_tmp_upper, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }
    gchar *ovl_etc_work = g_strconcat(OVL_DIR_PRE, hash_value, ETC_LAYER_WORK, NULL);
    if (g_mkdir_with_parents(ovl_etc_work, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }

    gchar *ovl_usr_lower = g_strconcat(OVL_DIR_PRE, hash_value, USR_LAYER_LOWER, NULL);
    if (g_mkdir_with_parents(ovl_usr_lower, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }

    gchar *ovl_usr_upper = g_strconcat(OVL_DIR_PRE, hash_value, USR_LAYER_UPPER, NULL);
    if (g_mkdir_with_parents(ovl_usr_upper, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }

    gchar *ovl_usr_tmp_upper = g_strconcat(OVL_DIR_PRE, hash_value, USR_LAYER_TMP_UPPER, NULL);
    if (g_mkdir_with_parents(ovl_usr_tmp_upper, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }
    gchar *ovl_usr_work = g_strconcat(OVL_DIR_PRE, hash_value, USR_LAYER_WORK, NULL);
    if (g_mkdir_with_parents(ovl_usr_work, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }

    gchar *ovl_var_lower = g_strconcat(OVL_DIR_PRE, hash_value, VAR_LAYER_LOWER, NULL);
    if (g_mkdir_with_parents(ovl_var_lower, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }
    gchar *ovl_var_upper = g_strconcat(OVL_DIR_PRE, hash_value, VAR_LAYER_UPPER, NULL);
    if (g_mkdir_with_parents(ovl_var_upper, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }
    gchar *ovl_var_tmp_upper = g_strconcat(OVL_DIR_PRE, hash_value, VAR_LAYER_TMP_UPPER, NULL);
    if (g_mkdir_with_parents(ovl_var_tmp_upper, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }
    gchar *ovl_var_work = g_strconcat(OVL_DIR_PRE, hash_value, VAR_LAYER_WORK, NULL);
    if (g_mkdir_with_parents(ovl_var_work, 0755) == -1) {
        g_print("make ovl dir failed.\n");
        return FALSE;
    }

    return TRUE;
}

gboolean mm_upgrade_create_ovldir(void)
{
    gboolean ret;
    gchar *pending_hash = get_pending_deploy_hash();
    if (pending_hash != NULL) {
        g_print("pending hash value: %s\n", pending_hash);
        // 创建相应的ovl目录
        ret = create_pending_deploy_dir(pending_hash);
        g_free(pending_hash);
        return ret;
    }
    g_free(pending_hash);
    return FALSE;
}
