Skip to content

Commit

Permalink
Kernel+LibC: Merge sys$stat() and sys$lstat()
Browse files Browse the repository at this point in the history
There is now only one sys$stat() instead of two separate syscalls.
  • Loading branch information
awesomekling committed Feb 10, 2020
1 parent b67035c commit 580a94b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
30 changes: 7 additions & 23 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,41 +1783,25 @@ int Process::sys$fstat(int fd, stat* statbuf)
return description->fstat(*statbuf);
}

int Process::sys$lstat(const char* user_path, size_t path_length, stat* user_statbuf)
int Process::sys$stat(const Syscall::SC_stat_params* user_params)
{
REQUIRE_PROMISE(rpath);
if (!validate_write_typed(user_statbuf))
Syscall::SC_stat_params params;
if (!validate_read_and_copy_typed(&params, user_params))
return -EFAULT;
auto path = get_syscall_path_argument(user_path, path_length);
if (path.is_error())
return path.error();
auto metadata_or_error = VFS::the().lookup_metadata(path.value(), current_directory(), O__NOERROR);
if (metadata_or_error.is_error())
return metadata_or_error.error();
stat statbuf;
auto result = metadata_or_error.value().stat(statbuf);
if (result.is_error())
return result;
copy_to_user(user_statbuf, &statbuf);
return 0;
}

int Process::sys$stat(const char* user_path, size_t path_length, stat* user_statbuf)
{
REQUIRE_PROMISE(rpath);
if (!validate_write_typed(user_statbuf))
if (!validate_write_typed(params.statbuf))
return -EFAULT;
auto path = get_syscall_path_argument(user_path, path_length);
auto path = get_syscall_path_argument(params.path);
if (path.is_error())
return path.error();
auto metadata_or_error = VFS::the().lookup_metadata(path.value(), current_directory());
auto metadata_or_error = VFS::the().lookup_metadata(path.value(), current_directory(), params.follow_symlinks ? 0 : O__NOERROR);
if (metadata_or_error.is_error())
return metadata_or_error.error();
stat statbuf;
auto result = metadata_or_error.value().stat(statbuf);
if (result.is_error())
return result;
copy_to_user(user_statbuf, &statbuf);
copy_to_user(params.statbuf, &statbuf);
return 0;
}

Expand Down
3 changes: 1 addition & 2 deletions Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ class Process : public InlineLinkedListNode<Process>
ssize_t sys$write(int fd, const u8*, ssize_t);
ssize_t sys$writev(int fd, const struct iovec* iov, int iov_count);
int sys$fstat(int fd, stat*);
int sys$lstat(const char*, size_t, stat*);
int sys$stat(const char*, size_t, stat*);
int sys$stat(const Syscall::SC_stat_params*);
int sys$lseek(int fd, off_t, int whence);
int sys$kill(pid_t pid, int sig);
[[noreturn]] void sys$exit(int status);
Expand Down
7 changes: 6 additions & 1 deletion Kernel/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ typedef u32 socklen_t;
__ENUMERATE_SYSCALL(mmap) \
__ENUMERATE_SYSCALL(munmap) \
__ENUMERATE_SYSCALL(get_dir_entries) \
__ENUMERATE_SYSCALL(lstat) \
__ENUMERATE_SYSCALL(getcwd) \
__ENUMERATE_SYSCALL(gettimeofday) \
__ENUMERATE_SYSCALL(gethostname) \
Expand Down Expand Up @@ -413,6 +412,12 @@ struct SC_waitid_params {
int options;
};

struct SC_stat_params {
StringArgument path;
struct stat* statbuf;
bool follow_symlinks;
};

void initialize();
int sync();

Expand Down
17 changes: 9 additions & 8 deletions Libraries/LibC/unistd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,25 @@ int close(int fd)
__RETURN_WITH_ERRNO(rc, rc, -1);
}

int lstat(const char* path, struct stat* statbuf)
static int do_stat(const char* path, struct stat* statbuf, bool follow_symlinks)
{
if (!path) {
errno = EFAULT;
return -1;
}
int rc = syscall(SC_lstat, path, strlen(path), statbuf);
Syscall::SC_stat_params params { { path, strlen(path) }, statbuf, follow_symlinks };
int rc = syscall(SC_stat, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

int lstat(const char* path, struct stat* statbuf)
{
return do_stat(path, statbuf, false);
}

int stat(const char* path, struct stat* statbuf)
{
if (!path) {
errno = EFAULT;
return -1;
}
int rc = syscall(SC_stat, path, strlen(path), statbuf);
__RETURN_WITH_ERRNO(rc, rc, -1);
return do_stat(path, statbuf, true);
}

int fstat(int fd, struct stat* statbuf)
Expand Down

0 comments on commit 580a94b

Please sign in to comment.