/*
 * 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/types.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

struct mount
{
    char *src, *tgt;
    struct aio *aio;
    const struct fs *fs;
    struct fs_mount mount;
    struct aio_done done;
};

static void free_mount(struct mount *const m)
{
    if (m)
    {
        free(m->src);
        free(m->tgt);
    }

    free(m);
}

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

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

    free_mount(m);
    return ret;
}

static int search_done(const enum state s, const char *const relpath,
    const struct fs_mp *const mp, const union inode_result *const inode,
    void *const args)
{
    struct mount *const m = args;
    struct aio *const aio = m->aio;
    const struct fs *const fs = m->fs;

    if (strcmp(m->tgt, "/"))
    {
        if (!inode)
        {
            aio->error = ENOENT;
            goto failure;
        }
        else if (mp->fs->iops.flags(inode) & INODE_MOUNTPOINT)
        {
            aio->error = EBUSY;
            goto failure;
        }
    }

    if (fs->mount(&m->mount, &aio->r))
    {
        aio->error = errno;
        goto failure;
    }

    return 0;

failure:
    aio->done = m->done;
    free_mount(m);
    return -1;
}

struct aio *aio_mount(const struct aio_mount *const am,
    const struct aio_done *const d)
{
    struct aio *aio = NULL;
    const struct fs_mount *const m = &am->mount;
    const struct fs *const fs = fs_from_type(am->type);
    char *srcdup = NULL, *tgtdup = NULL;
    struct mount *pm = NULL;
    struct fs_mp mp;

    if (!fs)
    {
        errno = ENODEV;
        goto failure;
    }
    else if (!m->src && fs->flags & FS_DEV_REQUIRED)
    {
        errno = ENOTBLK;
        goto failure;
    }
    else if (!fs_mp_from_path(m->tgt, &mp)
        && m->src && !strcmp(mp.src, m->src))
    {
        errno = EBUSY;
        goto failure;
    }
    else if ((m->src && !(srcdup = strdup(m->src)))
        || !(tgtdup = strdup(m->tgt))
        || !(pm = malloc(sizeof *pm))
        || !(aio = malloc(sizeof *aio)))
        goto failure;

    const struct inode_search s =
    {
        .path = m->tgt,
        .done = search_done,
        .args = pm
    };

    *pm = (const struct mount)
    {
        .aio = aio,
        .src = srcdup,
        .tgt = tgtdup,
        .fs = fs,
        .mount =
        {
            .src = srcdup,
            .tgt = tgtdup,
            .mode = m->mode,
            .gid = m->gid,
            .uid = m->uid
        }
    };

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

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

    return aio;

failure:
    free(tgtdup);
    free(srcdup);
    free(pm);
    free(aio);
    return NULL;
}
