#ifndef __ZVFS_HOOK_FD_H__
#define __ZVFS_HOOK_FD_H__

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/**
 * open / creat：
 *   zvfs 路径 + O_CREAT → blob_create + xattr_write + inode_alloc + openfile_alloc
 *   zvfs 路径，无 O_CREAT  → xattr_read_blob_id + blob_open + inode_alloc(若未缓存) + openfile_alloc
 *   非 zvfs 路径          → 透传
 *
 * close：
 *   zvfs fd → blob_sync_md + blob_close
 *             inode ref_count--（归零：若 inode->deleted 则 blob_delete，再 inode_free）
 *             real_close
 *   非 zvfs fd → 透传
 *
 * dup / dup2 / dup3：
 *   zvfs fd → real_dup* + daemon ADD_REF + 本地 openfile/inode 引用维护
 *   非 zvfs fd → 透传
 *
 * fork：
 *   子进程会对已继承的 zvfs handle 执行 ADD_REF_BATCH（失败时退化为逐个 ADD_REF）
 */

/* open 族 */
int open(const char *path, int flags, ...);
int open64(const char *path, int flags, ...);
int openat(int dirfd, const char *path, int flags, ...);
int openat64(int dirfd, const char *path, int flags, ...);
int creat(const char *path, mode_t mode);
int creat64(const char *path, mode_t mode);
FILE *fopen(const char *path, const char *mode);
FILE *fopen64(const char *path, const char *mode);

/* close 族 */
int close(int fd);
int close_range(unsigned int first, unsigned int last, int flags);

/* dup 族 */
int dup(int oldfd);
int dup2(int oldfd, int newfd);
int dup3(int oldfd, int newfd, int flags);
pid_t fork(void);

/* 给 fcntl(F_DUPFD*) 复用的内部辅助接口 */
int zvfs_dup_attach_newfd(int oldfd, int newfd, int new_fd_flags);

/* glibc 内部别名（与 open/close 实现体共享逻辑，转发即可） */
int __open(const char *path, int flags, ...);
int __open64(const char *path, int flags, ...);
int __openat(int dirfd, const char *path, int flags, ...);
int __openat64(int dirfd, const char *path, int flags, ...);
int __libc_open(const char *path, int flags, ...);
int __libc_open64(const char *path, int flags, ...);
int __libc_openat(int dirfd, const char *path, int flags, ...);
int __libc_openat64(int dirfd, const char *path, int flags, ...);
int __open_2(const char *path, int flags);
int __open64_2(const char *path, int flags);
int __openat_2(int dirfd, const char *path, int flags);
int __openat64_2(int dirfd, const char *path, int flags);
int __open_nocancel(const char *path, int flags, ...);
int __open64_nocancel(const char *path, int flags, ...);
int __openat_nocancel(int dirfd, const char *path, int flags, ...);
int __openat64_nocancel(int dirfd, const char *path, int flags, ...);
int __close(int fd);
int __libc_close(int fd);

#endif // __ZVFS_HOOK_FD_H__
