Implement syscall for file opening and refactor open_files initialisation in thread.c w/ S.

This commit is contained in:
EDiasAlberto
2024-11-08 15:33:47 +00:00
parent 5bd94894e0
commit 92e93b8060
3 changed files with 27 additions and 6 deletions

View File

@@ -4,6 +4,7 @@
#include "filesys/filesys.h"
#include "threads/vaddr.h"
#include "threads/interrupt.h"
#include "threads/malloc.h"
#include "threads/thread.h"
#include "threads/synch.h"
#include "userprog/process.h"
@@ -12,11 +13,12 @@
#include <syscall-nr.h>
static struct lock filesys_lock;
static unsigned fd_counter = MIN_USER_FD;
struct open_file
{
int fd; /* File Descriptor / Identifier */
struct file *file; /* Pointer to the associated file */
int fd; /* File Descriptor / Identifier */
struct file *file; /* Pointer to the associated file */
struct hash_elem elem; /* elem for a hash table */
};
@@ -165,10 +167,27 @@ syscall_remove (const char *file)
}
static int
syscall_open (const char *file UNUSED)
syscall_open (const char *file)
{
//TODO
return 0;
validate_user_pointer (file, 1);
lock_acquire (&filesys_lock);
struct file *ptr = filesys_open (file);
lock_release (&filesys_lock);
if (ptr == NULL)
return -1;
struct open_file *file_info
= (struct open_file*) malloc (sizeof (struct open_file));
if (file_info == NULL)
return -1;
file_info->fd = fd_counter++;
file_info->file = ptr;
hash_insert (&thread_current ()->open_files, &file_info->elem);
return file_info->fd;
}
static int