/* fd.c
*
* Copyright (C) 2009 Guillaume Duranceau
* This file is part of CROCOS.
*
* This file is distributed under the terms of the GNU General Public License.
* This license is available in the COPYING file, in the main repository.
*/
#include "config/config.h"
#include "fs/fd.h"
#include "fs/inode.h"
#include "fs/vfs.h"
#include "intr/errno.h"
#include "klibc/list.h"
#include "klibc/stdbool.h"
#include "klibc/stddef.h"
#include "process/pcb.h"
void fd_init_process (struct pcb *process) {
int fd; for (fd = 0; fd < CONFIG_FS_MAX_FD; fd++)
process->fd_tbl[fd].used = false;
}
void fd_kill_process (struct pcb *process) {
/* close opened file descriptors */
int fd; for (fd = 0; fd < CONFIG_FS_MAX_FD; fd++)
if (process->fd_tbl[fd].used) fs_close (process, fd);
}
void fd_copy_fd_tbl (struct pcb *process_src, struct pcb *process_dest) {
int fd; for (fd = 0; fd < CONFIG_FS_MAX_FD; fd++) {
struct fd *src_fd = process_src->fd_tbl + fd;
struct fd *dest_fd = process_dest->fd_tbl + fd;
dest_fd->used = src_fd->used;
dest_fd->inode = src_fd->inode;
dest_fd->flags = src_fd->flags;
dest_fd->offset = src_fd->offset;
}
}
int fd_allocate (struct pcb *process, struct inode *inode, int flags) {
struct fd *freefd;
int fd; for (fd = 0; fd < CONFIG_FS_MAX_FD; fd++)
if (!process->fd_tbl[fd].used) break;
if (fd == CONFIG_FS_MAX_FD) return -EMFILE;
freefd = process->fd_tbl + fd;
freefd->used = true;
freefd->inode = inode;
freefd->flags = flags;
freefd->offset = 0;
return fd;
}
void fd_free (struct pcb *process, int fd) {
process->fd_tbl[fd].used = false;
}
struct fd *fd_get (struct pcb *process, int fd) {
if ((fd < 0) || (fd >= CONFIG_FS_MAX_FD) || (!(process->fd_tbl[fd].used)))
return NULL;
return process->fd_tbl + fd;
}