/*
 * wnix, a Unix-like operating system for WebAssembly applications.
 * Copyright (C) 2025  Xavier Del Campo Romero
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <aio.h>
#include <aio/types.h>
#include <fs/fs.h>
#include <fs/inode.h>
#include <sys/stat.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

struct mkdir
{
    struct fs_mkdir mkdir;
    struct aio_done done;
    char *path, *parent;
    struct aio *aio;
};

static void free_mkdir(struct mkdir *const mk)
{
    if (mk)
    {
        free(mk->path);
        free(mk->parent);
    }

    free(mk);
}

static int done(const enum state state, void *const args)
{
    int ret = 0;
    struct mkdir *const mk = args;
    const struct aio_done *const d = &mk->done;

    if (d->f)
        ret = d->f(state, d->args);

    free_mkdir(mk);
    return ret;
}

static int search_done(const enum state state,
    const char *const relpath, const struct fs_mp *const mp,
    const union inode_result *const inode, void *const args)
{
    struct mkdir *const mk = args;
    struct aio *const aio = mk->aio;
    const struct fs *const fs = mp->fs;
    const char *path = mk->path;
    struct fs_ret r;

    path += strlen(mp->tgt) + strlen(relpath);
    mk->mkdir.path = path;

    if (fs->mkdir(&mk->mkdir, mp, inode, &r))
        goto failure;

    aio->r = r;
    return 0;

failure:
    aio->error = errno;
    aio->done = mk->done;
    free_mkdir(mk);
    return -1;
}

struct aio *aio_mkdir(const struct fs_mkdir *const m,
    const struct aio_done *const d)
{
    struct aio *aio = NULL;
    struct mkdir *mk = NULL;
    char *const pathdup = strdup(m->path), *parent = NULL;

    if (!pathdup
        || !(parent = fs_parent(m->path))
        || !(mk = malloc(sizeof *mk))
        || !(aio = malloc(sizeof *aio)))
        goto failure;

    *mk = (const struct mkdir)
    {
        .path = pathdup,
        .parent = parent,
        .aio = aio,
        .mkdir = *m
    };

    *aio = (const struct aio)
    {
        .done =
        {
            .args = mk,
            .f = done
        }
    };

    const struct inode_search s =
    {
        .path = parent,
        .done = search_done,
        .args = mk
    };

    if (inode_search(&s, &aio->r))
        goto failure;
    else if (d)
        mk->done = *d;

    return aio;

failure:
    free(mk);
    free(aio);
    free(parent);
    free(pathdup);
    return NULL;
}
