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

#include "mm-upgrade-move-user-data.h"
#include "mm-public.h"

gboolean mm_upgrade_move_user_data(void)
{
    gboolean ret;

    gchar *current_hash = get_current_deploy_hash();
    if (current_hash != NULL) 
    {
        g_print("current hash value: %s\n", current_hash);
    }
    
    gchar *pending_hash = get_pending_deploy_hash();
    if (pending_hash != NULL) 
    {
        g_print("pending hash value: %s\n", pending_hash);
    }

    // move etc-upper
    gchar copy_command[2048];

    gchar *ovl_etc_upper_current = g_strconcat(OVL_DIR_PRE, current_hash, ETC_LAYER_UPPER, NULL);
    gchar *ovl_etc_upper_pending = g_strconcat(OVL_DIR_PRE, pending_hash, ETC_LAYER_UPPER, NULL);
    snprintf(copy_command, sizeof(copy_command), "rsync -aAXc  --inplace --force -H  --numeric-ids --ignore-times --atimes --links --log-file=/var/log/ostree.log --log-file-format=\"ostree-maintain: %%o %%f \"  %s %s   ", ovl_etc_upper_current, ovl_etc_upper_pending);
    ret = system(copy_command);

    gchar *ovl_etc_lower_current = g_strconcat(OVL_DIR_PRE, current_hash, ETC_LAYER_LOWER, NULL);
    gchar *ovl_etc_lower_pending = g_strconcat(OVL_DIR_PRE, pending_hash, ETC_LAYER_LOWER, NULL);
    snprintf(copy_command, sizeof(copy_command), "rsync -aAXc  --inplace --force -H  --numeric-ids --ignore-times --atimes --links --log-file=/var/log/ostree.log --log-file-format=\"ostree-maintain: %%o %%f \"  %s %s ", ovl_etc_lower_current, ovl_etc_lower_pending);
    ret = system(copy_command);

    // move usr-lower
    gchar *ovl_usr_lower_current = g_strconcat(OVL_DIR_PRE, current_hash, USR_LAYER_LOWER, NULL);
    gchar *ovl_usr_lower_pending = g_strconcat(OVL_DIR_PRE, pending_hash, USR_LAYER_LOWER, NULL);
    snprintf(copy_command, sizeof(copy_command), "rsync -aAXc  --inplace --force -H  --numeric-ids --ignore-times --atimes --links  --log-file=/var/log/ostree.log --log-file-format=\"ostree-maintain: %%o %%f \"  %s %s  ", ovl_usr_lower_current, ovl_usr_lower_pending);
    ret = system(copy_command);

    gchar *ovl_usr_upper_current = g_strconcat(OVL_DIR_PRE, current_hash, USR_LAYER_UPPER, NULL);
    gchar *ovl_usr_upper_pending = g_strconcat(OVL_DIR_PRE, pending_hash, USR_LAYER_UPPER, NULL);
    snprintf(copy_command, sizeof(copy_command), "rsync -aAXc  --inplace --force -H  --numeric-ids --ignore-times --atimes --links  --log-file=/var/log/ostree.log --log-file-format=\"ostree-maintain: %%o %%f \"  %s %s  ", ovl_usr_upper_current, ovl_usr_upper_pending);
    ret = system(copy_command);

    // move var-upper
    gchar *ovl_var_upper_current = g_strconcat(OVL_DIR_PRE, current_hash, VAR_LAYER_UPPER, NULL);
    gchar *ovl_var_upper_pending = g_strconcat(OVL_DIR_PRE, pending_hash, VAR_LAYER_UPPER, NULL);
    snprintf(copy_command, sizeof(copy_command), "rsync -aAXc  --inplace --force -H  --numeric-ids --ignore-times --atimes --links  --log-file=/var/log/ostree.log --log-file-format=\"ostree-maintain: %%o %%f \"  %s %s   ", ovl_var_upper_current, ovl_var_upper_pending);
    ret = system(copy_command);

    gchar *ovl_var_lower_current = g_strconcat(OVL_DIR_PRE, current_hash, VAR_LAYER_LOWER, NULL);
    gchar *ovl_var_lower_pending = g_strconcat(OVL_DIR_PRE, pending_hash, VAR_LAYER_LOWER, NULL);
    snprintf(copy_command, sizeof(copy_command), "rsync -aAXc  --inplace --force -H  --numeric-ids --ignore-times --atimes --links  --log-file=/var/log/ostree.log --log-file-format=\"ostree-maintain: %%o %%f \"  %s %s  ", ovl_etc_lower_current, ovl_etc_lower_pending);
    ret = system(copy_command);

    g_free(current_hash);
    g_free(pending_hash);
    return TRUE;
}
