Implement syscall for close() and fix typing bug in fd_get_file w/ S.

This commit is contained in:
EDiasAlberto
2024-11-08 16:02:51 +00:00
parent a80084e907
commit 75bd3fbde0

View File

@@ -1,6 +1,7 @@
#include "userprog/syscall.h" #include "userprog/syscall.h"
#include "devices/shutdown.h" #include "devices/shutdown.h"
#include "devices/input.h" #include "devices/input.h"
#include "filesys/file.h"
#include "filesys/filesys.h" #include "filesys/filesys.h"
#include "threads/vaddr.h" #include "threads/vaddr.h"
#include "threads/interrupt.h" #include "threads/interrupt.h"
@@ -261,9 +262,18 @@ syscall_tell (int fd UNUSED)
} }
static void static void
syscall_close (int fd UNUSED) syscall_close (int fd)
{ {
//TODO struct open_file *file_info = fd_get_file (fd);
if (file_info != NULL)
{
hash_delete (&thread_current ()->open_files, &file_info->elem);
lock_acquire (&filesys_lock);
file_close (file_info->file);
lock_release (&filesys_lock);
free (file_info);
}
} }
unsigned unsigned
@@ -289,11 +299,11 @@ fd_get_file (int fd)
{ {
/* We have to set up a fake open_file in order to be able to search the hash /* We have to set up a fake open_file in order to be able to search the hash
table. See hash.h. */ table. See hash.h. */
struct open_file *fake_file_info; struct open_file fake_file_info;
fake_file_info->fd = fd; fake_file_info.fd = fd;
struct hash_elem *e struct hash_elem *e
= hash_find (&thread_current ()->open_files, &fake_file_info->elem); = hash_find (&thread_current ()->open_files, &fake_file_info.elem);
return hash_entry (e, struct open_file, elem); return hash_entry (e, struct open_file, elem);
} }