From 5424276603219adf560c4748713ccf5377775f34 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Fri, 8 Nov 2024 15:50:48 +0000 Subject: [PATCH] Add a helper function to get a file from its descriptor (FD), w/ E --- src/userprog/syscall.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index f89e2f9..ee91f43 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -281,6 +281,22 @@ fd_less (const struct hash_elem *a_, const struct hash_elem *b_, return a->fd < b->fd; } +/* Gets a file from its descriptor (FD number). If there is no file with the fd + FD it returns NULL. */ +static struct file * +fd_get_file (int fd) +{ + /* We have to set up a fake open_file in order to be able to search the hash + table. See hash.h. */ + struct open_file *fake_file_info; + fake_file_info->fd = fd; + + struct file *file + = hash_find (thread_current ()->open_files, fake_file_info->elem); + + return file; +} + /* Validates if a block of memory starting at PTR and of size SIZE bytes is fully contained within user virtual memory. Kills the thread (by calling thread_exit) if the memory is invalid. Otherwise, returns the PTR given.