Skip to content

Commit 959ccf0

Browse files
committed
add seek syscall
1 parent 17db43a commit 959ccf0

File tree

11 files changed

+55
-6
lines changed

11 files changed

+55
-6
lines changed

src/filesys/sysfile.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,20 @@ syscall_fstat(void)
210210
return SYS_FAIL_RC;
211211
return 0;
212212
}
213+
214+
/** int32_t seek(int32_t fd, uint32_t offset); */
215+
int32_t
216+
syscall_seek(void)
217+
{
218+
int32_t fd;
219+
uint32_t offset;
220+
221+
if (!sysarg_get_int(0, &fd))
222+
return SYS_FAIL_RC;
223+
if (!sysarg_get_uint(1, &offset))
224+
return SYS_FAIL_RC;
225+
226+
if (!filesys_seek(fd, offset))
227+
return SYS_FAIL_RC;
228+
return 0;
229+
}

src/filesys/sysfile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ int32_t syscall_chdir();
3838
int32_t syscall_getcwd();
3939
int32_t syscall_exec();
4040
int32_t syscall_fstat();
41+
int32_t syscall_seek();
4142

4243

4344
#endif

src/filesys/vsfs.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,30 @@ filesys_fstat(int8_t fd, file_stat_t *stat)
679679
}
680680

681681

682+
/** Seek to absolute file offset. */
683+
bool
684+
filesys_seek(int8_t fd, size_t offset)
685+
{
686+
file_t *file = _find_process_file(fd);
687+
if (file == NULL) {
688+
warn("seek: cannot find file for fd %d", fd);
689+
return false;
690+
}
691+
692+
inode_lock(file->inode);
693+
size_t filesize = file->inode->d_inode.size;
694+
inode_unlock(file->inode);
695+
696+
if (offset > filesize) {
697+
warn("seek: offset %lu beyond filesize %lu", offset, filesize);
698+
return false;
699+
}
700+
701+
file->offset = offset;
702+
return true;
703+
}
704+
705+
682706
/** Flush the in-memory modified bitmap block to disk. */
683707
bool
684708
inode_bitmap_update(uint32_t slot_no)

src/filesys/vsfs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ bool filesys_exec(char *path, char **argv);
130130

131131
bool filesys_fstat(int8_t fd, file_stat_t *stat);
132132

133+
bool filesys_seek(int8_t fd, size_t offset);
134+
133135
bool inode_bitmap_update(uint32_t slot_no);
134136
bool data_bitmap_update(uint32_t slot_no);
135137

src/interrupt/syscall.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ static syscall_t syscall_handlers[] = {
4747
[SYSCALL_CHDIR] syscall_chdir,
4848
[SYSCALL_GETCWD] syscall_getcwd,
4949
[SYSCALL_EXEC] syscall_exec,
50-
[SYSCALL_FSTAT] syscall_fstat
50+
[SYSCALL_FSTAT] syscall_fstat,
51+
[SYSCALL_SEEK] syscall_seek
5152
};
5253

5354
#define NUM_SYSCALLS ((int32_t) (sizeof(syscall_handlers) / sizeof(syscall_t)))

src/interrupt/syscall.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define SYSCALL_GETCWD 18
3939
#define SYSCALL_EXEC 19
4040
#define SYSCALL_FSTAT 20
41+
#define SYSCALL_SEEK 21
4142

4243

4344
/**

user/lib/syscall.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ extern int32_t chdir(char *path);
7474
extern int32_t getcwd(char *buf, uint32_t limit);
7575
extern int32_t exec(char *path, char **argv);
7676
extern int32_t fstat(int32_t fd, file_stat_t *stat);
77+
extern int32_t seek(int32_t fd, uint32_t offset);
7778

7879

7980
#endif

user/lib/syscall.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ SYSCALL_LIBGEN chdir, SYSCALL_CHDIR
4040
SYSCALL_LIBGEN getcwd, SYSCALL_GETCWD
4141
SYSCALL_LIBGEN exec, SYSCALL_EXEC
4242
SYSCALL_LIBGEN fstat, SYSCALL_FSTAT
43+
SYSCALL_LIBGEN seek, SYSCALL_SEEK

user/lib/syslist.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ SYSCALL_CHDIR = 17
3131
SYSCALL_GETCWD = 18
3232
SYSCALL_EXEC = 19
3333
SYSCALL_FSTAT = 20
34+
SYSCALL_SEEK = 21

user/put.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ _open_writable_file(char *path, bool overwrite)
4444

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

7473
/** If putting newline after string. */
75-
if (newline)
74+
if (newline) {
7675
bytes_written = write(fd, "\n", 1);
77-
if (bytes_written != 1)
78-
warn("put: newline written %lu != 1", bytes_written);
76+
if (bytes_written != 1)
77+
warn("put: newline written %lu != 1", bytes_written);
78+
}
7979

8080
close(fd);
8181
}

0 commit comments

Comments
 (0)