Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
syscall filesize
Browse files Browse the repository at this point in the history
  • Loading branch information
CCXXXI committed Jan 10, 2021
1 parent a4f9da3 commit b032a2f
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/userprog/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "userprog/process.h"
#include "devices/shutdown.h"
#include "filesys/filesys.h"
#include "filesys/file.h"

#define USER_ASSERT(CONDITION) \
if (CONDITION) \
Expand All @@ -33,6 +34,7 @@ static void syscall_handler(struct intr_frame *);
static bool is_valid_ptr(const void *ptr);
static bool is_user_mem(const void *start, size_t size);
static bool is_valid_str(const char *str);
static struct open_file *get_file_by_fd(const int fd);

static void halt(void) NO_RETURN;
static void exit(int status) NO_RETURN;
Expand Down Expand Up @@ -177,6 +179,20 @@ static bool is_valid_str(const char *str)
return true;
}

static struct open_file *get_file_by_fd(const int fd)
{
struct list *l = &thread_current()->process->files;

for (struct list_elem *e = list_begin(l); e != list_end(l); e = list_next(e))
{
struct open_file *f = list_entry(e, struct open_file, elem);
if (f->fd == fd)
return f;
}

return NULL;
}

/* Terminates the current user program, returning
STATUS to the kernel. If the process’s parent
waits for it, this is the status that will be
Expand Down Expand Up @@ -380,8 +396,15 @@ static int open(const char *file)
/* Returns the size, in bytes, of the file open as FD. */
static int filesize(int fd)
{
// todo
return -1;
struct open_file *f = get_file_by_fd(fd);

USER_ASSERT(f != NULL);

lock_acquire(&file_lock);
int ret = file_length(f->file);
lock_release(&file_lock);

return ret;
}

/* Reads SIZE bytes from the file open as FD into buffer. Returns
Expand Down

0 comments on commit b032a2f

Please sign in to comment.