# /*
#  * Copyright (C) 2020, KylinSoft Co., Ltd.
#  *
#  * This program is free software: you can redistribute it and/or modify
#  * it under the terms of the GNU General Public License as published by
#  * the Free Software Foundation, either version 3 of the License, or
#  * (at your option) any later version.
#  *
#  * This program is distributed in the hope that it will be useful,
#  * but WITHOUT ANY WARRANTY; without even the implied warranty of
#  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  * GNU General Public License for more details.
#  *
#  * You should have received a copy of the GNU General Public License
#  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
#  */

import dbus
import logging
from gettext import gettext as _
from SystemUpdater.Core.errors import *
from SystemUpdater.Core.enums import *
from SystemUpdater.checks.registry import register_check
from SystemUpdater.Core.dbus_helper import get_bus

@register_check("check-backup-space", roles=[ROLE_DEPLOY])
def check_backup_space(trans):
    bus = get_bus()
    try:
        # 获取 D-Bus 对象和接口
        proxy = bus.get_object('com.kylin.systembackup', '/update')
        iface = dbus.Interface(proxy, dbus_interface='com.kylin.systembackup.manager')

        logging.info(f"Starting check backup space via D-Bus...")
        result = iface.checkEnvForSystemUpdate(timeout=10)
    except Exception as e:
        logging.error(f"Failed to check backup space via D-Bus: {e}")
        return False
    
    if int(result) == 0:
        logging.info("Check: Enough backup space For system update...")
        return True
    else:
        logging.error("Not enough backup space For system update, D-Bus return code:%d", result)
        raise UpdateBaseError(ERROR_NOT_DISK_SPACE)
