Skip to content

Commit

Permalink
add seek syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
josehu07 committed Jan 15, 2022
1 parent 17db43a commit 959ccf0
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/filesys/sysfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,20 @@ syscall_fstat(void)
return SYS_FAIL_RC;
return 0;
}

/** int32_t seek(int32_t fd, uint32_t offset); */
int32_t
syscall_seek(void)
{
int32_t fd;
uint32_t offset;

if (!sysarg_get_int(0, &fd))
return SYS_FAIL_RC;
if (!sysarg_get_uint(1, &offset))
return SYS_FAIL_RC;

if (!filesys_seek(fd, offset))
return SYS_FAIL_RC;
return 0;
}
1 change: 1 addition & 0 deletions src/filesys/sysfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ int32_t syscall_chdir();
int32_t syscall_getcwd();
int32_t syscall_exec();
int32_t syscall_fstat();
int32_t syscall_seek();


#endif
24 changes: 24 additions & 0 deletions src/filesys/vsfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,30 @@ filesys_fstat(int8_t fd, file_stat_t *stat)
}


/** Seek to absolute file offset. */
bool
filesys_seek(int8_t fd, size_t offset)
{
file_t *file = _find_process_file(fd);
if (file == NULL) {
warn("seek: cannot find file for fd %d", fd);
return false;
}

inode_lock(file->inode);
size_t filesize = file->inode->d_inode.size;
inode_unlock(file->inode);

if (offset > filesize) {
warn("seek: offset %lu beyond filesize %lu", offset, filesize);
return false;
}

file->offset = offset;
return true;
}


/** Flush the in-memory modified bitmap block to disk. */
bool
inode_bitmap_update(uint32_t slot_no)
Expand Down
2 changes: 2 additions & 0 deletions src/filesys/vsfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ bool filesys_exec(char *path, char **argv);

bool filesys_fstat(int8_t fd, file_stat_t *stat);

bool filesys_seek(int8_t fd, size_t offset);

bool inode_bitmap_update(uint32_t slot_no);
bool data_bitmap_update(uint32_t slot_no);

Expand Down
3 changes: 2 additions & 1 deletion src/interrupt/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ static syscall_t syscall_handlers[] = {
[SYSCALL_CHDIR] syscall_chdir,
[SYSCALL_GETCWD] syscall_getcwd,
[SYSCALL_EXEC] syscall_exec,
[SYSCALL_FSTAT] syscall_fstat
[SYSCALL_FSTAT] syscall_fstat,
[SYSCALL_SEEK] syscall_seek
};

#define NUM_SYSCALLS ((int32_t) (sizeof(syscall_handlers) / sizeof(syscall_t)))
Expand Down
1 change: 1 addition & 0 deletions src/interrupt/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#define SYSCALL_GETCWD 18
#define SYSCALL_EXEC 19
#define SYSCALL_FSTAT 20
#define SYSCALL_SEEK 21


/**
Expand Down
1 change: 1 addition & 0 deletions user/lib/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ extern int32_t chdir(char *path);
extern int32_t getcwd(char *buf, uint32_t limit);
extern int32_t exec(char *path, char **argv);
extern int32_t fstat(int32_t fd, file_stat_t *stat);
extern int32_t seek(int32_t fd, uint32_t offset);


#endif
1 change: 1 addition & 0 deletions user/lib/syscall.s
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ SYSCALL_LIBGEN chdir, SYSCALL_CHDIR
SYSCALL_LIBGEN getcwd, SYSCALL_GETCWD
SYSCALL_LIBGEN exec, SYSCALL_EXEC
SYSCALL_LIBGEN fstat, SYSCALL_FSTAT
SYSCALL_LIBGEN seek, SYSCALL_SEEK
1 change: 1 addition & 0 deletions user/lib/syslist.s
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ SYSCALL_CHDIR = 17
SYSCALL_GETCWD = 18
SYSCALL_EXEC = 19
SYSCALL_FSTAT = 20
SYSCALL_SEEK = 21
8 changes: 4 additions & 4 deletions user/put.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ _open_writable_file(char *path, bool overwrite)

/** If not overwriting, seek to current file end. */
if (!overwrite) {
assert(stat.size >= 0);
int ret = seek(fd, stat.size);
if (ret != 0) {
warn("put: cannot seek to offset %lu", stat.size);
Expand Down Expand Up @@ -72,10 +71,11 @@ _file_put_str(char *path, char *str, size_t len, bool overwrite, bool newline)
}

/** If putting newline after string. */
if (newline)
if (newline) {
bytes_written = write(fd, "\n", 1);
if (bytes_written != 1)
warn("put: newline written %lu != 1", bytes_written);
if (bytes_written != 1)
warn("put: newline written %lu != 1", bytes_written);
}

close(fd);
}
Expand Down
2 changes: 1 addition & 1 deletion wiki
Submodule wiki updated from 716b0c to 668fd5

0 comments on commit 959ccf0

Please sign in to comment.