diff --git a/src/fs/fd.c b/src/fs/fd.c index 7a1afe52..4d0ea590 100644 --- a/src/fs/fd.c +++ b/src/fs/fd.c @@ -110,6 +110,40 @@ int fd_free(fdtable_t* fdtable, int ifd){ } +int fd_tableclone(fdtable_t* source, fdtable_t* dest){ + + spinlock_acquire(&source->lock); + + if(source->fdcount != dest->fdcount){ + void* tmp = alloc(source->fdcount*sizeof(fd_t*)); + if(!tmp){ + spinlock_release(&source->lock); + return ENOMEM; + } + dest->fdcount = source->fdcount; + } + + + for(uintmax_t i = 0; i < dest->fdcount; ++i){ + if(!source->fd[i]) + continue; + + dest->fd[i] = source->fd[i]; + + spinlock_acquire(&dest->fd[i]->lock); + + ++dest->fd[i]->refcount; + + spinlock_release(&dest->fd[i]->lock); + + } + + + spinlock_release(&source->lock); + + +} + int fd_tableinit(fdtable_t* fdtable){ fdtable->fd = alloc(sizeof(fd_t*)*3); diff --git a/src/include/kernel/fd.h b/src/include/kernel/fd.h index 7880d766..8c374c42 100644 --- a/src/include/kernel/fd.h +++ b/src/include/kernel/fd.h @@ -53,5 +53,6 @@ int fd_free(fdtable_t* fdtable, int ifd); int fd_access(fdtable_t* fdtable, fd_t** fd, int ifd); int fd_release(fd_t* fd); int fd_tableinit(fdtable_t* fdtable); +int fd_tableclone(fdtable_t* source, fdtable_t* dest); #endif