diff --git a/sys/fs/vfs/file.c b/sys/fs/vfs/file.c index 00f076b..6ea6cd4 100644 --- a/sys/fs/vfs/file.c +++ b/sys/fs/vfs/file.c @@ -541,23 +541,28 @@ for (tmpFd = fdTable; tmpFd != 0x0; tmpFd = tmpFd->next) { if (tmpFd == fd) { - if (fd->res != 0x0) - fl_fclose(fd->res); + if (fd->dup > 0) + fd->dup--; + else { - if (tmpFd->prev) - tmpFd->prev->next = tmpFd->next; - if (tmpFd->next) - tmpFd->next->prev = tmpFd->prev; + if (fd->res != 0x0) + fl_fclose(fd->res); - if (tmpFd == fdTable) - fdTable = tmpFd->next; + if (tmpFd->prev) + tmpFd->prev->next = tmpFd->next; + if (tmpFd->next) + tmpFd->next->prev = tmpFd->prev; - systemVitals->openFiles--; - spinUnlock(&fdTable_lock); - if (tmpFd->buffer != NULL) - kfree(tmpFd->buffer); - kfree(tmpFd); - return (0x0); + if (tmpFd == fdTable) + fdTable = tmpFd->next; + + systemVitals->openFiles--; + spinUnlock(&fdTable_lock); + if (tmpFd->buffer != NULL) + kfree(tmpFd->buffer); + kfree(tmpFd); + return (0x0); + } } } diff --git a/sys/include/vfs/file.h b/sys/include/vfs/file.h index 29e8dea..b69789d 100644 --- a/sys/include/vfs/file.h +++ b/sys/include/vfs/file.h @@ -80,6 +80,7 @@ struct inode inode; int fd_type; void *res; + int dup; } fileDescriptor_t; typedef struct userFileDescriptorStruct { diff --git a/sys/kernel/descrip.c b/sys/kernel/descrip.c index 6833f7e..5ae3253 100644 --- a/sys/kernel/descrip.c +++ b/sys/kernel/descrip.c @@ -56,6 +56,8 @@ if (td->o_files[i] == 0x0) { td->o_files[i] = (void*) fp; td->td_retval[0] = i; + ((fileDescriptor_t*) td->o_files[uap->fd])->dup++; + fclose(td->o_files[uap->fd]); td->o_files[uap->fd] = 0; break; } @@ -325,6 +327,7 @@ } td->o_files[to] = td->o_files[from]; + ((fileDescriptor_t*) td->o_files[from])->dup++; kprintf("DUP2: %i:%i [0x%X:0x%X]", from, to, td->o_files[from], td->o_files[to]); return (0x0);