/*
 * SPDX-FileCopyrightText: 2003-2026 Sébastien Helleu <flashcode@flashtux.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

/* Save/restore data for upgrading WeeChat */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "weechat.h"
#include "core-upgrade-file.h"
#include "core-infolist.h"
#include "core-string.h"
#include "core-utf8.h"
#include "../gui/gui-chat.h"
#include "../gui/gui-main.h"
#include "../plugins/plugin.h"


struct t_upgrade_file *upgrade_files = NULL;
struct t_upgrade_file *last_upgrade_file = NULL;


/*
 * Display an error with upgrade.
 */

void
upgrade_file_error (struct t_upgrade_file *upgrade_file, char *message1,
                    char *message2, char *file, int line)
{
    gui_chat_printf (NULL,
                     _("%sError upgrading WeeChat with file \"%s\":"),
                     gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
                     upgrade_file->filename);
    gui_chat_printf (NULL,
                     _("%s    error: %s%s%s%s"),
                     gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
                     message1,
                     (message2 && message2[0]) ? " (" : "",
                     (message2 && message2[0]) ? message2 : "",
                     (message2 && message2[0]) ? ")" : "");
    if ((upgrade_file->last_read_pos > 0)
        || (upgrade_file->last_read_length > 0))
    {
        gui_chat_printf (NULL,
                         _("%s    last read: position: %ld, length: %d"),
                         gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
                         upgrade_file->last_read_pos,
                         upgrade_file->last_read_length);
    }
    gui_chat_printf (NULL,
                     _("%s    source: %s, line: %d"),
                     gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
                     file, line);
    gui_chat_printf (NULL,
                     _("%s    *** Please report above info to developers ***"),
                     gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]);
}

/*
 * Write an integer value in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_write_integer (struct t_upgrade_file *upgrade_file, int value)
{
    if (fwrite ((void *)(&value), sizeof (value), 1, upgrade_file->file) <= 0)
        return 0;

    return 1;
}

/*
 * Write a time value in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_write_time (struct t_upgrade_file *upgrade_file, time_t date)
{
    if (fwrite ((void *)(&date), sizeof (date), 1, upgrade_file->file) <= 0)
        return 0;

    return 1;
}

/*
 * Write a long integer value in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_write_long (struct t_upgrade_file *upgrade_file, long value)
{
    if (fwrite ((void *)(&value), sizeof (value), 1, upgrade_file->file) <= 0)
        return 0;

    return 1;
}

/*
 * Write a long long integer value in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_write_longlong (struct t_upgrade_file *upgrade_file, long long value)
{
    if (fwrite ((void *)(&value), sizeof (value), 1, upgrade_file->file) <= 0)
        return 0;

    return 1;
}

/*
 * Write a string in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_write_string (struct t_upgrade_file *upgrade_file,
                           const char *string)
{
    int length;

    if (string && string[0])
    {
        length = strlen (string);
        if (!upgrade_file_write_integer (upgrade_file, length))
            return 0;
        if (fwrite ((void *)string, length, 1, upgrade_file->file) <= 0)
            return 0;
    }
    else
    {
        if (!upgrade_file_write_integer (upgrade_file, 0))
            return 0;
    }

    return 1;
}

/*
 * Write a buffer in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_write_buffer (struct t_upgrade_file *upgrade_file, void *pointer,
                           int size)
{
    if (pointer)
    {
        if (!upgrade_file_write_integer (upgrade_file, size))
            return 0;
        if (fwrite (pointer, size, 1, upgrade_file->file) <= 0)
            return 0;
    }
    else
    {
        if (!upgrade_file_write_integer (upgrade_file, 0))
            return 0;
    }

    return 1;
}

/*
 * Create an upgrade file.
 *
 * If write == 1, then opens in write mode, otherwise in read mode.
 *
 * Return pointer to new upgrade file, NULL if error.
 */

struct t_upgrade_file *
upgrade_file_new (const char *filename,
                  int (*callback_read)(const void *pointer,
                                       void *data,
                                       struct t_upgrade_file *upgrade_file,
                                       int object_id,
                                       struct t_infolist *infolist),
                  const void *callback_read_pointer,
                  void *callback_read_data)
{
    struct t_upgrade_file *new_upgrade_file;

    if (!filename)
        return NULL;

    new_upgrade_file = malloc (sizeof (*new_upgrade_file));
    if (new_upgrade_file)
    {
        /* Build name of file. */
        if (string_asprintf (&new_upgrade_file->filename,
                             "%s/%s.upgrade",
                             weechat_data_dir, filename) < 0)
        {
            free (new_upgrade_file);
            return NULL;
        }
        new_upgrade_file->callback_read = callback_read;
        new_upgrade_file->callback_read_pointer = callback_read_pointer;
        new_upgrade_file->callback_read_data = callback_read_data;

        /* Open file in read or write mode. */
        if (callback_read)
            new_upgrade_file->file = fopen (new_upgrade_file->filename, "rb");
        else
            new_upgrade_file->file = fopen (new_upgrade_file->filename, "wb");

        if (!new_upgrade_file->file)
        {
            free (new_upgrade_file->filename);
            free (new_upgrade_file);
            return NULL;
        }

        /*
         * Use a bigger stdio buffer than the default: upgrade files are
         * read/written as a huge number of small (per-field) fread/fwrite
         * calls, so a small buffer means many more underlying syscalls
         * than necessary; failure to set it is not fatal, default
         * buffering is still correct, just slower.
         */
        setvbuf (new_upgrade_file->file, NULL, _IOFBF, UPGRADE_FILE_BUFFER_SIZE);

        /* Change permissions if write mode. */
        if (!callback_read)
        {
            chmod (new_upgrade_file->filename, 0600);

            /* Write signature. */
            upgrade_file_write_string (new_upgrade_file, UPGRADE_SIGNATURE);
        }

        /* Init positions. */
        new_upgrade_file->read_offset = 0;
        new_upgrade_file->last_read_pos = 0;
        new_upgrade_file->last_read_length = 0;

        /* Add upgrade file to list of upgrade files. */
        new_upgrade_file->prev_upgrade = last_upgrade_file;
        new_upgrade_file->next_upgrade = NULL;
        if (last_upgrade_file)
            last_upgrade_file->next_upgrade = new_upgrade_file;
        else
            upgrade_files = new_upgrade_file;
        last_upgrade_file = new_upgrade_file;
    }

    return new_upgrade_file;
}

/*
 * Write an object in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_write_object (struct t_upgrade_file *upgrade_file, int object_id,
                           struct t_infolist *infolist)
{
    int i, argc, length;
    char **argv;
    const char *fields;
    void *buf;

    /* Write all infolist variables. */
    infolist_reset_item_cursor (infolist);
    while (infolist_next (infolist))
    {
        /* Write object start with id. */
        if (!upgrade_file_write_integer (upgrade_file, UPGRADE_TYPE_OBJECT_START))
        {
            UPGRADE_ERROR(_("write - object type"), "object start");
            return 0;
        }
        if (!upgrade_file_write_integer (upgrade_file, object_id))
        {
            UPGRADE_ERROR(_("write - object id"), "");
            return 0;
        }

        fields = infolist_fields (infolist);
        if (fields)
        {
            argv = string_split (fields, ",", NULL,
                                 WEECHAT_STRING_SPLIT_STRIP_LEFT
                                 | WEECHAT_STRING_SPLIT_STRIP_RIGHT
                                 | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
                                 0, &argc);
            if (argv && (argc > 0))
            {
                for (i = 0; i < argc; i++)
                {
                    switch (argv[i][0])
                    {
                        case 'i': /* Integer */
                            if (!upgrade_file_write_integer (upgrade_file, UPGRADE_TYPE_OBJECT_VAR))
                            {
                                UPGRADE_ERROR(_("write - object type"), "object var");
                                return 0;
                            }
                            if (!upgrade_file_write_string (upgrade_file, argv[i] + 2))
                            {
                                UPGRADE_ERROR(_("write - variable name"), "");
                                return 0;
                            }
                            if (!upgrade_file_write_integer (upgrade_file, INFOLIST_INTEGER))
                            {
                                UPGRADE_ERROR(_("write - infolist type"), "integer");
                                return 0;
                            }
                            if (!upgrade_file_write_integer (upgrade_file,
                                                             infolist_integer (infolist, argv[i] + 2)))
                            {
                                UPGRADE_ERROR(_("write - variable"), "integer");
                                return 0;
                            }
                            break;
                        case 's': /* String */
                            if (!upgrade_file_write_integer (upgrade_file, UPGRADE_TYPE_OBJECT_VAR))
                            {
                                UPGRADE_ERROR(_("write - object type"), "object var");
                                return 0;
                            }
                            if (!upgrade_file_write_string (upgrade_file, argv[i] + 2))
                            {
                                UPGRADE_ERROR(_("write - variable name"), "");
                                return 0;
                            }
                            if (!upgrade_file_write_integer (upgrade_file, INFOLIST_STRING))
                            {
                                UPGRADE_ERROR(_("write - infolist type"), "string");
                                return 0;
                            }
                            if (!upgrade_file_write_string (upgrade_file,
                                                            infolist_string (infolist, argv[i] + 2)))
                            {
                                UPGRADE_ERROR(_("write - variable"), "string");
                                return 0;
                            }
                            break;
                        case 'p': /* Pointer */
                            /* Pointer in not used in upgrade files, only buffer is. */
                            break;
                        case 'b': /* Buffer */
                            buf = infolist_buffer (infolist, argv[i] + 2, &length);
                            if (buf && (length > 0))
                            {
                                if (!upgrade_file_write_integer (upgrade_file, UPGRADE_TYPE_OBJECT_VAR))
                                {
                                    UPGRADE_ERROR(_("write - object type"), "object var");
                                    return 0;
                                }
                                if (!upgrade_file_write_string (upgrade_file, argv[i] + 2))
                                {
                                    UPGRADE_ERROR(_("write - variable name"), "");
                                    return 0;
                                }
                                if (!upgrade_file_write_integer (upgrade_file, INFOLIST_BUFFER))
                                {
                                    UPGRADE_ERROR(_("write - infolist type"), "buffer");
                                    return 0;
                                }
                                if (!upgrade_file_write_buffer (upgrade_file, buf, length))
                                {
                                    UPGRADE_ERROR(_("write - variable"), "buffer");
                                    return 0;
                                }
                            }
                            break;
                        case 't': /* Time */
                            if (!upgrade_file_write_integer (upgrade_file, UPGRADE_TYPE_OBJECT_VAR))
                            {
                                UPGRADE_ERROR(_("write - object type"), "object var");
                                return 0;
                            }
                            if (!upgrade_file_write_string (upgrade_file, argv[i] + 2))
                            {
                                UPGRADE_ERROR(_("write - variable name"), "");
                                return 0;
                            }
                            if (!upgrade_file_write_integer (upgrade_file, INFOLIST_TIME))
                            {
                                UPGRADE_ERROR(_("write - infolist type"), "time");
                                return 0;
                            }
                            if (!upgrade_file_write_time (upgrade_file,
                                                          infolist_time (infolist, argv[i] + 2)))
                            {
                                UPGRADE_ERROR(_("write - variable"), "time");
                                return 0;
                            }
                            break;
                        case 'l': /* Long */
                            if (!upgrade_file_write_integer (upgrade_file, UPGRADE_TYPE_OBJECT_VAR))
                            {
                                UPGRADE_ERROR(_("write - object type"), "object var");
                                return 0;
                            }
                            if (!upgrade_file_write_string (upgrade_file, argv[i] + 2))
                            {
                                UPGRADE_ERROR(_("write - variable name"), "");
                                return 0;
                            }
                            if (!upgrade_file_write_integer (upgrade_file, INFOLIST_LONG))
                            {
                                UPGRADE_ERROR(_("write - infolist type"), "long");
                                return 0;
                            }
                            if (!upgrade_file_write_long (upgrade_file,
                                                          infolist_long (infolist, argv[i] + 2)))
                            {
                                UPGRADE_ERROR(_("write - variable"), "long");
                                return 0;
                            }
                            break;
                        case 'L': /* Long long */
                            if (!upgrade_file_write_integer (upgrade_file, UPGRADE_TYPE_OBJECT_VAR))
                            {
                                UPGRADE_ERROR(_("write - object type"), "object var");
                                return 0;
                            }
                            if (!upgrade_file_write_string (upgrade_file, argv[i] + 2))
                            {
                                UPGRADE_ERROR(_("write - variable name"), "");
                                return 0;
                            }
                            if (!upgrade_file_write_integer (upgrade_file, INFOLIST_LONGLONG))
                            {
                                UPGRADE_ERROR(_("write - infolist type"), "longlong");
                                return 0;
                            }
                            if (!upgrade_file_write_longlong (upgrade_file,
                                                              infolist_longlong (infolist, argv[i] + 2)))
                            {
                                UPGRADE_ERROR(_("write - variable"), "longlong");
                                return 0;
                            }
                            break;
                    }
                }
            }
            string_free_split (argv);
        }

        /* Write object end. */
        if (!upgrade_file_write_integer (upgrade_file, UPGRADE_TYPE_OBJECT_END))
            return 0;
    }

    return 1;
}

/*
 * Read an integer in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_read_integer (struct t_upgrade_file *upgrade_file, int *value)
{
    upgrade_file->last_read_pos = upgrade_file->read_offset;
    upgrade_file->last_read_length = sizeof (*value);

    if (value)
    {
        if (fread ((void *)value, sizeof (*value), 1, upgrade_file->file) <= 0)
            return 0;
    }
    else
    {
        if (fseek (upgrade_file->file, sizeof (*value), SEEK_CUR) < 0)
            return 0;
    }
    upgrade_file->read_offset += sizeof (*value);
    return 1;
}

/*
 * Read a string in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_read_string (struct t_upgrade_file *upgrade_file, char **string)
{
    int length;

    if (string && *string)
    {
        free (*string);
        *string = NULL;
    }

    if (!upgrade_file_read_integer (upgrade_file, &length))
        return 0;

    upgrade_file->last_read_pos = upgrade_file->read_offset;
    upgrade_file->last_read_length = length;

    if (string)
    {
        if (length == 0)
            return 1;

        (*string) = malloc (length + 1);
        if (!(*string))
            return 0;

        if (fread ((void *)(*string), length, 1, upgrade_file->file) <= 0)
        {
            free (*string);
            *string = NULL;
            return 0;
        }
        (*string)[length] = '\0';
    }
    else
    {
        if (fseek (upgrade_file->file, length, SEEK_CUR) < 0)
            return 0;
    }
    upgrade_file->read_offset += length;
    return 1;
}

/*
 * Read a buffer in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_read_buffer (struct t_upgrade_file *upgrade_file,
                          void **buffer, int *size)
{
    if (!buffer)
        return 0;

    if (*buffer)
    {
        free (*buffer);
        *buffer = NULL;
    }

    if (!upgrade_file_read_integer (upgrade_file, size))
        return 0;

    if (*size > 0)
    {
        upgrade_file->last_read_pos = upgrade_file->read_offset;
        upgrade_file->last_read_length = *size;

        *buffer = malloc (*size);

        if (*buffer)
        {
            if (fread (*buffer, *size, 1, upgrade_file->file) <= 0)
                return 0;
        }
        else
        {
            if (fseek (upgrade_file->file, *size, SEEK_CUR) < 0)
                return 0;
        }
        upgrade_file->read_offset += *size;
    }

    return 1;
}

/*
 * Read time in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_read_time (struct t_upgrade_file *upgrade_file, time_t *time)
{
    upgrade_file->last_read_pos = upgrade_file->read_offset;
    upgrade_file->last_read_length = sizeof (*time);

    if (time)
    {
        if (fread ((void *)time, sizeof (*time), 1, upgrade_file->file) <= 0)
            return 0;
    }
    else
    {
        if (fseek (upgrade_file->file, sizeof (*time), SEEK_CUR) < 0)
            return 0;
    }

    upgrade_file->read_offset += sizeof (*time);
    return 1;
}

/*
 * Read a long integer in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_read_long (struct t_upgrade_file *upgrade_file, long *value)
{
    upgrade_file->last_read_pos = ftell (upgrade_file->file);
    upgrade_file->last_read_length = sizeof (*value);

    if (value)
    {
        if (fread ((void *)value, sizeof (*value), 1, upgrade_file->file) <= 0)
            return 0;
    }
    else
    {
        if (fseek (upgrade_file->file, sizeof (*value), SEEK_CUR) < 0)
            return 0;
    }
    return 1;
}

/*
 * Read a long long integer in upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_read_longlong (struct t_upgrade_file *upgrade_file, long long *value)
{
    upgrade_file->last_read_pos = ftell (upgrade_file->file);
    upgrade_file->last_read_length = sizeof (*value);

    if (value)
    {
        if (fread ((void *)value, sizeof (*value), 1, upgrade_file->file) <= 0)
            return 0;
    }
    else
    {
        if (fseek (upgrade_file->file, sizeof (*value), SEEK_CUR) < 0)
            return 0;
    }
    return 1;
}

/*
 * Read an object in upgrade file and calls read callback.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_read_object (struct t_upgrade_file *upgrade_file)
{
    struct t_infolist *infolist;
    struct t_infolist_item *item;
    int rc, object_id, type, type_var, value_int, size;
    long value_long;
    long long value_longlong;
    char *name, *value_str;
    void *buffer;
    time_t time;

    rc = 0;

    infolist = NULL;
    name = NULL;
    value_str = NULL;
    buffer = NULL;

    if (!upgrade_file_read_integer (upgrade_file, &type))
    {
        if (feof (upgrade_file->file))
            rc = 1;
        else
            UPGRADE_ERROR(_("read - object type"), "");
        goto end;
    }

    if (type != UPGRADE_TYPE_OBJECT_START)
    {
        UPGRADE_ERROR(_("read - bad object type (\"object start\" expected)"), "");
        goto end;
    }

    if (!upgrade_file_read_integer (upgrade_file, &object_id))
    {
        UPGRADE_ERROR(_("read - object id"), "");
        goto end;
    }

    infolist = infolist_new (NULL);
    if (!infolist)
    {
        UPGRADE_ERROR(_("read - infolist creation"), "");
        goto end;
    }
    item = infolist_new_item (infolist);
    if (!item)
    {
        UPGRADE_ERROR(_("read - infolist item creation"), "");
        goto end;
    }

    while (1)
    {
        if (!upgrade_file_read_integer (upgrade_file, &type))
        {
            UPGRADE_ERROR(_("read - object type"), "");
            goto end;
        }

        if (type == UPGRADE_TYPE_OBJECT_END)
            break;

        if (type == UPGRADE_TYPE_OBJECT_VAR)
        {
            if (!upgrade_file_read_string (upgrade_file, &name))
            {
                UPGRADE_ERROR(_("read - variable name"), "");
                goto end;
            }
            if (!name)
            {
                UPGRADE_ERROR(_("read - variable name"), "");
                goto end;
            }
            if (!upgrade_file_read_integer (upgrade_file, &type_var))
            {
                UPGRADE_ERROR(_("read - variable type"), "");
                goto end;
            }

            /*
             * For every variable type below, "name" (and, for string/buffer,
             * the value too) is transferred to the infolist via a
             * "take ownership" constructor instead of being copied again:
             * "name" was just freshly allocated by upgrade_file_read_string()
             * above and is not used afterwards, so re-strdup'ing it inside
             * the infolist would be a wasted allocation+copy on every single
             * field of every restored object; the constructor takes over the
             * pointer(s) in all cases, freeing them itself on error, so we
             * just reset the local pointer(s) afterwards to prevent the
             * cleanup below and the next loop iteration from freeing memory
             * that is no longer ours.
             */
            switch (type_var)
            {
                case INFOLIST_INTEGER:
                    if (!upgrade_file_read_integer (upgrade_file, &value_int))
                    {
                        UPGRADE_ERROR(_("read - variable"), "integer");
                        goto end;
                    }
                    infolist_new_var_integer_take_name_ownership (
                        item, name, value_int);
                    name = NULL;
                    break;
                case INFOLIST_STRING:
                    if (!upgrade_file_read_string (upgrade_file, &value_str))
                    {
                        UPGRADE_ERROR(_("read - variable"), "string");
                        goto end;
                    }
                    infolist_new_var_string_take_ownership (item, name,
                                                            value_str);
                    name = NULL;
                    value_str = NULL;
                    break;
                case INFOLIST_POINTER:
                    break;
                case INFOLIST_BUFFER:
                    if (!upgrade_file_read_buffer (upgrade_file, &buffer, &size))
                    {
                        UPGRADE_ERROR(_("read - variable"), "buffer");
                        goto end;
                    }
                    infolist_new_var_buffer_take_ownership (item, name,
                                                            buffer, size);
                    name = NULL;
                    buffer = NULL;
                    break;
                case INFOLIST_TIME:
                    if (!upgrade_file_read_time (upgrade_file, &time))
                    {
                        UPGRADE_ERROR(_("read - variable"), "time");
                        goto end;
                    }
                    infolist_new_var_time_take_name_ownership (
                        item, name, time);
                    name = NULL;
                    break;
                case INFOLIST_LONG:
                    if (!upgrade_file_read_long (upgrade_file, &value_long))
                    {
                        UPGRADE_ERROR(_("read - variable"), "long");
                        goto end;
                    }
                    infolist_new_var_long_take_name_ownership (
                        item, name, value_long);
                    name = NULL;
                    break;
                case INFOLIST_LONGLONG:
                    if (!upgrade_file_read_longlong (upgrade_file, &value_longlong))
                    {
                        UPGRADE_ERROR(_("read - variable"), "longlong");
                        goto end;
                    }
                    infolist_new_var_longlong_take_name_ownership (
                        item, name, value_longlong);
                    name = NULL;
                    break;
                case INFOLIST_NUM_TYPES:
                    break;
            }
        }
    }

    rc = 1;

    if (upgrade_file->callback_read)
    {
        if ((int)(upgrade_file->callback_read) (
                upgrade_file->callback_read_pointer,
                upgrade_file->callback_read_data,
                upgrade_file,
                object_id,
                infolist) == WEECHAT_RC_ERROR)
        {
            rc = 0;
        }
    }

end:
    infolist_free (infolist);
    free (name);
    free (value_str);
    free (buffer);
    return rc;
}

/*
 * Read an upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
upgrade_file_read (struct t_upgrade_file *upgrade_file)
{
    char *signature;

    if (!upgrade_file || !upgrade_file->callback_read)
        return 0;

    signature = NULL;
    if (!upgrade_file_read_string (upgrade_file, &signature))
    {
        UPGRADE_ERROR(_("read - signature not found"), "");
        return 0;
    }

    if (!signature || (strcmp (signature, UPGRADE_SIGNATURE) != 0))
    {
        UPGRADE_ERROR(_("read - bad signature (upgrade file format may have "
                        "changed since last version)"), "");
        free (signature);
        return 0;
    }

    free (signature);

    while (!feof (upgrade_file->file))
    {
        if (!upgrade_file_read_object (upgrade_file))
            return 0;
    }

    return 1;
}

/*
 * Close and frees an upgrade file.
 */

void
upgrade_file_close (struct t_upgrade_file *upgrade_file)
{
    if (!upgrade_file)
        return;

    free (upgrade_file->filename);
    if (upgrade_file->file)
        fclose (upgrade_file->file);
    free (upgrade_file->callback_read_data);

    /* Remove upgrade file list. */
    if (upgrade_file->prev_upgrade)
        (upgrade_file->prev_upgrade)->next_upgrade = upgrade_file->next_upgrade;
    if (upgrade_file->next_upgrade)
        (upgrade_file->next_upgrade)->prev_upgrade = upgrade_file->prev_upgrade;
    if (upgrade_files == upgrade_file)
        upgrade_files = upgrade_file->next_upgrade;
    if (last_upgrade_file == upgrade_file)
        last_upgrade_file = upgrade_file->prev_upgrade;

    free (upgrade_file);
}
