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

#include "mm-filelock.h"

gboolean lock_file_acquire(const gchar *file_path)
{
    GFileOutputStream *stream = NULL;
    GError *error = NULL;

    GFile *file = g_file_new_for_path(file_path);

    stream = g_file_create(file, G_FILE_CREATE_NONE, NULL, &error);

    if (error != NULL) {
        g_info(" failed to create lock file.");
        g_error_free(error);
        g_object_unref(stream);
        g_object_unref(file);
        return FALSE;
    }

    g_info(" suceess to create lock file.");

    g_object_unref(stream);
    g_object_unref(file);

    return TRUE;
}

gboolean lock_file_release(const gchar *file_path)
{
    GError *error = NULL;
    GFile *file = g_file_new_for_path(file_path);

    if (!g_file_delete(file, NULL, &error)) {
        g_info("failed to delete lock file.");
        return FALSE;
    }

    g_info("success to delete lock file.");
    if (error != NULL)
        g_error_free(error);
    g_object_unref(file);

    return TRUE;
}
