Skip to content

Commit

Permalink
Implement fstat
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnycase committed Nov 2, 2018
1 parent 55b44be commit ec9db39
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
32 changes: 0 additions & 32 deletions lib/bsp/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,38 +235,6 @@ static ssize_t sys_read(int file, void *ptr, size_t len)
return res;
}

static int sys_fstat(int file, struct stat *st)
{
int res = -EBADF;

/*
* Status of an open file. The sys/stat.h header file required
* is
* distributed in the include subdirectory for this C library.
*
* int fstat(int file, struct stat* st)
*
* IN : regs[10] = file, regs[11] = st
* OUT: regs[10] = Upon successful completion, 0 shall be
* returned.
* Otherwise, -1 shall be returned and errno set to indicate
* the error.
*/

UNUSED(file);

if (st != NULL)
memset(st, 0, sizeof(struct stat));
/* Return the result */
res = -ENOSYS;
/*
* Note: This value will return to syscall wrapper, syscall
* wrapper will set errno to ENOSYS and return -1
*/

return res;
}

static int sys_close(int file)
{
/*
Expand Down
32 changes: 26 additions & 6 deletions lib/bsp/syscalls/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ using namespace sys;

int sys_open(const char *name, int flags, int mode)
{
handle_t handle = NULL_HANDLE;
if (strstr(name, "/dev/") != NULL)
{
return io_open(name);
handle = io_open(name);
}
else if (strstr(name, "/fs/") != NULL)
{
Expand All @@ -43,12 +44,12 @@ int sys_open(const char *name, int flags, int mode)
file_mode |= FILE_MODE_APPEND;
if (flags & O_TRUNC)
file_mode |= FILE_MODE_TRUNCATE;
return filesystem_file_open(name, file_access, file_mode);
}
else
{
return -EINVAL;
handle = filesystem_file_open(name, file_access, file_mode);
}

if (handle)
return handle;
return -1;
}

off_t sys_lseek(int fildes, off_t offset, int whence)
Expand Down Expand Up @@ -83,3 +84,22 @@ off_t sys_lseek(int fildes, off_t offset, int whence)
return -1;
}
}

int sys_fstat(int fd, struct stat *buf)
{
try
{
auto &obj = system_handle_to_object(fd);
if (auto f = obj.as<filesystem_file>())
{
buf->st_size = f->get_size();
return 0;
}

return -1;
}
catch (...)
{
return -1;
}
}
1 change: 1 addition & 0 deletions lib/bsp/syscalls/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C"

int sys_open(const char *name, int flags, int mode);
off_t sys_lseek(int fildes, off_t offset, int whence);
int sys_fstat(int fd, struct stat *buf);

#ifdef __cplusplus
}
Expand Down
4 changes: 3 additions & 1 deletion lib/freertos/kernel/devices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,9 @@ handle_t sys::system_alloc_handle(object_accessor<object_access> object)

object_accessor<object_access> &sys::system_handle_to_object(handle_t file)
{
configASSERT(file);
if (file < HANDLE_OFFSET)
throw std::invalid_argument("Invalid handle.");

_file *rfile = (_file *)handles_[file - HANDLE_OFFSET];
configASSERT(rfile);
return rfile->object;
Expand Down

0 comments on commit ec9db39

Please sign in to comment.