From 5e9fd9f60d6a4f62bf88b67465e88990413355c8 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 14 Feb 2023 00:49:52 -0500 Subject: [PATCH] libroot_build & fs_shell: Provide _kern_ functions for readv/writev... ..instead of readv_pos/writev_pos. This way, we can be sure that we are remapping them properly even under Haiku, as well as remove some potential confusions. bfs_shell seems to still work fine on a regular build. --- headers/build/BeOSBuildCompatibility.h | 4 --- headers/build/private/kernel/syscalls.h | 6 ++++ src/build/libroot/fs.cpp | 41 ++++++++----------------- src/tools/fs_shell/uio.cpp | 22 ++++++++----- src/tools/fs_shell/unistd.cpp | 10 ++++-- 5 files changed, 40 insertions(+), 43 deletions(-) diff --git a/headers/build/BeOSBuildCompatibility.h b/headers/build/BeOSBuildCompatibility.h index 45af40681b..3706e39ee8 100644 --- a/headers/build/BeOSBuildCompatibility.h +++ b/headers/build/BeOSBuildCompatibility.h @@ -48,10 +48,6 @@ extern size_t strnlen(const char* string, size_t length); #if !defined(HAIKU_HOST_PLATFORM_HAIKU) extern ssize_t read_pos(int fd, off_t pos, void* buffer, size_t count); extern ssize_t write_pos(int fd, off_t pos, const void* buffer, size_t count); -extern ssize_t readv_pos(int fd, off_t pos, const struct iovec* vec, - int count); -extern ssize_t writev_pos(int fd, off_t pos, const struct iovec* vec, - int count); #endif diff --git a/headers/build/private/kernel/syscalls.h b/headers/build/private/kernel/syscalls.h index 74164156ed..c8d20ddb49 100644 --- a/headers/build/private/kernel/syscalls.h +++ b/headers/build/private/kernel/syscalls.h @@ -35,7 +35,9 @@ extern "C" { #define _kern_remove_attr _kernbuild_remove_attr #define _kern_rename_attr _kernbuild_rename_attr #define _kern_read _kernbuild_read +#define _kern_readv _kernbuild_readv #define _kern_write _kernbuild_write +#define _kern_writev _kernbuild_writev #define _kern_read_dir _kernbuild_read_dir #define _kern_rewind_dir _kernbuild_rewind_dir #define _kern_read_stat _kernbuild_read_stat @@ -80,8 +82,12 @@ extern status_t _kern_rename_attr(int fromFile, const char *fromName, // file descriptor functions extern ssize_t _kern_read(int fd, off_t pos, void *buffer, size_t bufferSize); +extern ssize_t _kern_readv(int fd, off_t pos, const struct iovec *vecs, + size_t count); extern ssize_t _kern_write(int fd, off_t pos, const void *buffer, size_t bufferSize); +extern ssize_t _kern_writev(int fd, off_t pos, const struct iovec *vecs, + size_t count); extern ssize_t _kern_read_dir(int fd, struct dirent *buffer, size_t bufferSize, uint32 maxCount); extern status_t _kern_rewind_dir(int fd); diff --git a/src/build/libroot/fs.cpp b/src/build/libroot/fs.cpp index 0e1cc40aa4..41e33d50e8 100644 --- a/src/build/libroot/fs.cpp +++ b/src/build/libroot/fs.cpp @@ -1121,18 +1121,12 @@ _kern_unlock_node(int fd) // #pragma mark - + #if !defined(HAIKU_HOST_PLATFORM_HAIKU) -// read_pos ssize_t read_pos(int fd, off_t pos, void *buffer, size_t bufferSize) { - // seek - off_t result = lseek(fd, pos, SEEK_SET); - if (result < 0) - return errno; - - // read - ssize_t bytesRead = haiku_host_platform_read(fd, buffer, bufferSize); + ssize_t bytesRead = _kern_read(fd, pos, buffer, bufferSize); if (bytesRead < 0) { errno = bytesRead; return -1; @@ -1141,7 +1135,6 @@ read_pos(int fd, off_t pos, void *buffer, size_t bufferSize) return bytesRead; } -// write_pos ssize_t write_pos(int fd, off_t pos, const void *buffer, size_t bufferSize) { @@ -1158,24 +1151,19 @@ write_pos(int fd, off_t pos, const void *buffer, size_t bufferSize) return bufferSize; } - // seek - off_t result = lseek(fd, pos, SEEK_SET); - if (result < 0) - return errno; - - // write - ssize_t bytesWritten = haiku_host_platform_write(fd, buffer, bufferSize); - if (bytesWritten < 0) { - errno = bytesWritten; + ssize_t bytesRead = _kern_write(fd, pos, buffer, bufferSize); + if (bytesRead < 0) { + errno = bytesRead; return -1; } - return bytesWritten; + return bytesRead; } +#endif + -// readv_pos ssize_t -readv_pos(int fd, off_t pos, const struct iovec *vec, int count) +_kern_readv(int fd, off_t pos, const struct iovec *vec, size_t count) { // seek off_t result = lseek(fd, pos, SEEK_SET); @@ -1192,9 +1180,9 @@ readv_pos(int fd, off_t pos, const struct iovec *vec, int count) return bytesRead; } -// writev_pos + ssize_t -writev_pos(int fd, off_t pos, const struct iovec *vec, int count) +_kern_writev(int fd, off_t pos, const struct iovec *vec, size_t count) { // seek off_t result = lseek(fd, pos, SEEK_SET); @@ -1203,14 +1191,11 @@ writev_pos(int fd, off_t pos, const struct iovec *vec, int count) // read ssize_t bytesWritten = haiku_host_platform_writev(fd, vec, count); - if (bytesWritten < 0) { - errno = bytesWritten; - return -1; - } + if (bytesWritten < 0) + return bytesWritten; return bytesWritten; } -#endif // #pragma mark - diff --git a/src/tools/fs_shell/uio.cpp b/src/tools/fs_shell/uio.cpp index 261f2d23d1..9a323d8b2d 100644 --- a/src/tools/fs_shell/uio.cpp +++ b/src/tools/fs_shell/uio.cpp @@ -18,6 +18,14 @@ #include "partition_support.h" +#if (!defined(__BEOS__) && !defined(__HAIKU__)) + // Defined in libroot_build.so. +# define _kern_readv _kernbuild_readv +# define _kern_writev _kernbuild_writev + extern "C" ssize_t _kern_readv(int fd, off_t pos, const struct iovec *vecs, size_t count); + extern "C" ssize_t _kern_writev(int fd, off_t pos, const struct iovec *vecs, size_t count); +#endif + static const int kMaxIOVecs = 1024; @@ -55,14 +63,13 @@ fssh_readv(int fd, const struct fssh_iovec *vector, int count) #if !defined(HAIKU_HOST_PLATFORM_FREEBSD) return readv(fd, systemVecs, count); #else - return readv_pos(fd, lseek(fd, 0, SEEK_CUR), systemVecs, count); + return _kern_readv(fd, lseek(fd, 0, SEEK_CUR), systemVecs, count); #endif } fssh_ssize_t -fssh_readv_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec, - int count) +fssh_readv_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec, int count) { struct iovec systemVecs[kMaxIOVecs]; if (!prepare_iovecs(vec, count, systemVecs)) @@ -72,7 +79,7 @@ fssh_readv_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec, if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0) return -1; - return readv_pos(fd, pos, systemVecs, count); + return _kern_readv(fd, pos, systemVecs, count); } @@ -91,14 +98,13 @@ fssh_writev(int fd, const struct fssh_iovec *vector, int count) #if !defined(HAIKU_HOST_PLATFORM_FREEBSD) return writev(fd, systemVecs, count); #else - return writev_pos(fd, lseek(fd, 0, SEEK_CUR), systemVecs, count); + return _kern_writev(fd, lseek(fd, 0, SEEK_CUR), systemVecs, count); #endif } fssh_ssize_t -fssh_writev_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec, - int count) +fssh_writev_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec, int count) { struct iovec systemVecs[kMaxIOVecs]; if (!prepare_iovecs(vec, count, systemVecs)) @@ -108,5 +114,5 @@ fssh_writev_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec, if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0) return -1; - return writev_pos(fd, pos, systemVecs, count); + return _kern_writev(fd, pos, systemVecs, count); } diff --git a/src/tools/fs_shell/unistd.cpp b/src/tools/fs_shell/unistd.cpp index d24b02c6cc..8502a1ea96 100644 --- a/src/tools/fs_shell/unistd.cpp +++ b/src/tools/fs_shell/unistd.cpp @@ -41,8 +41,12 @@ #if (!defined(__BEOS__) && !defined(__HAIKU__)) // Defined in libroot_build.so. +# define _kern_read _kernbuild_read +# define _kern_write _kernbuild_write # define _kern_dup _kernbuild_dup # define _kern_close _kernbuild_close + extern "C" ssize_t _kern_read(int fd, off_t pos, void *buffer, size_t bufferSize); + extern "C" ssize_t _kern_write(int fd, off_t pos, const void *buffer, size_t bufferSize); extern "C" int _kern_dup(int fd); extern "C" status_t _kern_close(int fd); #endif @@ -372,7 +376,7 @@ fssh_read(int fd, void *buffer, fssh_size_t count) return -1; return read(fd, buffer, count); #else - fssh_ssize_t bytesRead = read_pos(fd, fssh_lseek(fd, 0, FSSH_SEEK_CUR), + fssh_ssize_t bytesRead = _kern_read(fd, fssh_lseek(fd, 0, FSSH_SEEK_CUR), buffer, count); if (bytesRead > 0) fssh_lseek(fd, bytesRead, FSSH_SEEK_CUR); @@ -386,7 +390,7 @@ fssh_read_pos(int fd, fssh_off_t pos, void *buffer, fssh_size_t count) { if (FSShell::restricted_file_restrict_io(fd, pos, count) < 0) return -1; - return read_pos(fd, pos, buffer, count); + return _kern_read(fd, pos, buffer, count); } @@ -413,7 +417,7 @@ fssh_write_pos(int fd, fssh_off_t pos, const void *buffer, fssh_size_t count) { if (FSShell::restricted_file_restrict_io(fd, pos, count) < 0) return -1; - return write_pos(fd, pos, buffer, count); + return _kern_write(fd, pos, buffer, count); }