/*
 * 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/>.
 */

#ifndef FS_INODE_H
#define FS_INODE_H

#include <state.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>

struct fs;
struct fs_mp;
struct fs_ret;
struct inode_prv;

enum
{
    INODE_DIR = 1,
    INODE_REGFILE = 1 << 1,
    INODE_BLOCKDEV = 1 << 2,
    INODE_MOUNTPOINT = 1 << 3,
};

struct inode
{
    char *name;
    int flags;
    ino_t ino;
    mode_t mode;
    uid_t uid;
    gid_t gid;
    off_t size;
    struct timespec atim, mtim, ctim;
    struct inode *parent, *child, *left, *right;
    struct inode_prv *prv;
    void (*dealloc)(void *);
};

union inode_result
{
    struct inode cachei, *memi;
};

typedef int (*inode_search_done)(enum state state, const char *relpath,
    const struct fs_mp *mp, const union inode_result *inode, void *args);

struct inode_search
{
    const char *path;
    inode_search_done done;
    void *args;
};

struct inode_ops
{
    int (*search)(const char *path, const struct fs_mp *mp,
        union inode_result *inode, struct fs_ret *r);
    int (*reserve)(const struct inode *i, void *args, struct fs_ret *r);
    int (*flags)(const union inode_result *i);
};

int inode_search(const struct inode_search *s, struct fs_ret *r);
void inode_search_free(struct inode_search *s);
void inode_free(struct inode *i);

#endif
