diff options
author | Andreas Kling <kling@serenityos.org> | 2021-01-12 19:21:59 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-12 23:34:01 +0100 |
commit | 1a08ac72ad4610689f23ae76f41bd8adcb23fe47 (patch) | |
tree | 844395bad7a402d864512296360c9c2ef0083712 | |
parent | d551263b111bc83e65fa845ef245f43982dfd7ad (diff) | |
download | serenity-1a08ac72ad4610689f23ae76f41bd8adcb23fe47.zip |
LibC+Everywhere: Remove open_with_path_length() in favor of open()
This API was a mostly gratuitous deviation from POSIX that gave up some
portability in exchange for avoiding the occasional strlen().
I don't think that was actually achieving anything valuable, so let's
just chill out and have the same open() API as everyone else. :^)
-rw-r--r-- | AK/MappedFile.cpp | 4 | ||||
-rw-r--r-- | AK/MappedFile.h | 2 | ||||
-rw-r--r-- | AK/Platform.h | 16 | ||||
-rw-r--r-- | AK/PrintfImplementation.h | 10 | ||||
-rw-r--r-- | AK/StackInfo.cpp | 1 | ||||
-rw-r--r-- | AK/Tests/TestBase64.cpp | 1 | ||||
-rw-r--r-- | AK/Tests/TestSpan.cpp | 1 | ||||
-rw-r--r-- | Userland/Applications/HexEditor/HexEditor.cpp | 4 | ||||
-rw-r--r-- | Userland/Applications/HexEditor/HexEditor.h | 2 | ||||
-rw-r--r-- | Userland/DevTools/UserspaceEmulator/Emulator.cpp | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibC/fcntl.cpp | 37 | ||||
-rw-r--r-- | Userland/Libraries/LibC/fcntl.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/LocalServer.cpp | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/LocalSocket.cpp | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibPCIDB/Database.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibPCIDB/Database.h | 3 | ||||
-rw-r--r-- | Userland/Services/SystemServer/Service.cpp | 2 | ||||
-rw-r--r-- | Userland/Shell/AST.cpp | 1 | ||||
-rw-r--r-- | Userland/Utilities/js.cpp | 5 |
21 files changed, 48 insertions, 66 deletions
diff --git a/AK/MappedFile.cpp b/AK/MappedFile.cpp index b855a42cc7..55f0b62d5c 100644 --- a/AK/MappedFile.cpp +++ b/AK/MappedFile.cpp @@ -35,9 +35,9 @@ namespace AK { -Result<NonnullRefPtr<MappedFile>, OSError> MappedFile::map(const StringView& path) +Result<NonnullRefPtr<MappedFile>, OSError> MappedFile::map(const String& path) { - int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_RDONLY | O_CLOEXEC, 0); + int fd = open(path.characters(), O_RDONLY | O_CLOEXEC, 0); if (fd < 0) return OSError(errno); diff --git a/AK/MappedFile.h b/AK/MappedFile.h index b3bd099cdd..0294752f0a 100644 --- a/AK/MappedFile.h +++ b/AK/MappedFile.h @@ -39,7 +39,7 @@ class MappedFile : public RefCounted<MappedFile> { AK_MAKE_NONMOVABLE(MappedFile); public: - static Result<NonnullRefPtr<MappedFile>, OSError> map(const StringView& path); + static Result<NonnullRefPtr<MappedFile>, OSError> map(const String& path); ~MappedFile(); void* data() { return m_data; } diff --git a/AK/Platform.h b/AK/Platform.h index 440490c0b3..d8d610e295 100644 --- a/AK/Platform.h +++ b/AK/Platform.h @@ -67,22 +67,6 @@ #ifndef __serenity__ # define PAGE_SIZE sysconf(_SC_PAGESIZE) - -# include <errno.h> -# include <fcntl.h> -# include <stdlib.h> -# include <string.h> -inline int open_with_path_length(const char* path, size_t path_length, int options, mode_t mode) -{ - auto* tmp = (char*)malloc(path_length + 1); - memcpy(tmp, path, path_length); - tmp[path_length] = '\0'; - int fd = open(tmp, options, mode); - int saved_errno = errno; - free(tmp); - errno = saved_errno; - return fd; -} #endif ALWAYS_INLINE int count_trailing_zeroes_32(unsigned int val) diff --git a/AK/PrintfImplementation.h b/AK/PrintfImplementation.h index 0fd229bd1f..df22925735 100644 --- a/AK/PrintfImplementation.h +++ b/AK/PrintfImplementation.h @@ -33,17 +33,17 @@ #include <AK/Types.h> #include <stdarg.h> -namespace PrintfImplementation { - -static constexpr const char* printf_hex_digits_lower = "0123456789abcdef"; -static constexpr const char* printf_hex_digits_upper = "0123456789ABCDEF"; - #ifdef __serenity__ extern "C" size_t strlen(const char*); #else # include <string.h> #endif +namespace PrintfImplementation { + +static constexpr const char* printf_hex_digits_lower = "0123456789abcdef"; +static constexpr const char* printf_hex_digits_upper = "0123456789ABCDEF"; + template<typename PutChFunc, typename T> ALWAYS_INLINE int print_hex(PutChFunc putch, char*& bufptr, T number, bool upper_case, bool alternate_form, bool left_pad, bool zero_pad, u8 field_width) { diff --git a/AK/StackInfo.cpp b/AK/StackInfo.cpp index abf786a810..2e7f46153a 100644 --- a/AK/StackInfo.cpp +++ b/AK/StackInfo.cpp @@ -27,6 +27,7 @@ #include <AK/Assertions.h> #include <AK/StackInfo.h> #include <stdio.h> +#include <string.h> #ifdef __serenity__ # include <serenity.h> diff --git a/AK/Tests/TestBase64.cpp b/AK/Tests/TestBase64.cpp index ef008d01f3..ded70eca91 100644 --- a/AK/Tests/TestBase64.cpp +++ b/AK/Tests/TestBase64.cpp @@ -29,6 +29,7 @@ #include <AK/Base64.h> #include <AK/ByteBuffer.h> #include <AK/String.h> +#include <string.h> TEST_CASE(test_decode) { diff --git a/AK/Tests/TestSpan.cpp b/AK/Tests/TestSpan.cpp index 5aded06a73..798418d0f9 100644 --- a/AK/Tests/TestSpan.cpp +++ b/AK/Tests/TestSpan.cpp @@ -29,6 +29,7 @@ #include <AK/Checked.h> #include <AK/Span.h> #include <AK/StdLibExtras.h> +#include <string.h> TEST_CASE(constexpr_default_constructor_is_empty) { diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index 6ccba8579a..edd98b8c56 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -98,12 +98,12 @@ void HexEditor::set_position(int position) update_status(); } -bool HexEditor::write_to_file(const StringView& path) +bool HexEditor::write_to_file(const String& path) { if (m_buffer.is_empty()) return true; - int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_WRONLY | O_CREAT | O_TRUNC, 0666); + int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666); if (fd < 0) { perror("open"); return false; diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h index e47eedeec3..15d7632495 100644 --- a/Userland/Applications/HexEditor/HexEditor.h +++ b/Userland/Applications/HexEditor/HexEditor.h @@ -51,7 +51,7 @@ public: void set_buffer(const ByteBuffer&); void fill_selection(u8 fill_byte); - bool write_to_file(const StringView& path); + bool write_to_file(const String& path); bool has_selection() const { return !(m_selection_start == -1 || m_selection_end == -1 || (m_selection_end - m_selection_start) < 0 || m_buffer.is_empty()); } bool copy_selected_text_to_clipboard(); diff --git a/Userland/DevTools/UserspaceEmulator/Emulator.cpp b/Userland/DevTools/UserspaceEmulator/Emulator.cpp index c463d24618..107dbf5cbb 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator.cpp +++ b/Userland/DevTools/UserspaceEmulator/Emulator.cpp @@ -988,10 +988,14 @@ u32 Emulator::virt$open(u32 params_addr) auto path = mmu().copy_buffer_from_vm((FlatPtr)params.path.characters, params.path.length); - int fd = openat_with_path_length(params.dirfd, (const char*)path.data(), path.size(), params.options, params.mode); - if (fd < 0) - return -errno; - return fd; + Syscall::SC_open_params host_params {}; + host_params.dirfd = params.dirfd; + host_params.mode = params.mode; + host_params.options = params.options; + host_params.path.characters = (const char*)path.data(); + host_params.path.length = path.size(); + + return syscall(SC_open, &host_params); } int Emulator::virt$pipe(FlatPtr vm_pipefd, int flags) diff --git a/Userland/Libraries/LibC/fcntl.cpp b/Userland/Libraries/LibC/fcntl.cpp index dd71fd1559..7790bec113 100644 --- a/Userland/Libraries/LibC/fcntl.cpp +++ b/Userland/Libraries/LibC/fcntl.cpp @@ -53,42 +53,24 @@ int creat(const char* path, mode_t mode) return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode); } -int creat_with_path_length(const char* path, size_t path_length, mode_t mode) -{ - return open_with_path_length(path, path_length, O_CREAT | O_WRONLY | O_TRUNC, mode); -} - -int open_with_path_length(const char* path, size_t path_length, int options, mode_t mode) -{ - return openat_with_path_length(AT_FDCWD, path, path_length, options, mode); -} - -int openat_with_path_length(int dirfd, const char* path, size_t path_length, int options, mode_t mode) +int open(const char* path, int options, ...) { if (!path) { errno = EFAULT; return -1; } + auto path_length = strlen(path); if (path_length > INT32_MAX) { errno = EINVAL; return -1; } - Syscall::SC_open_params params { dirfd, { path, path_length }, options, mode }; - int rc = syscall(SC_open, ¶ms); - __RETURN_WITH_ERRNO(rc, rc, -1); -} - -int open(const char* path, int options, ...) -{ - if (!path) { - errno = EFAULT; - return -1; - } va_list ap; va_start(ap, options); auto mode = (mode_t)va_arg(ap, unsigned); va_end(ap); - return open_with_path_length(path, strlen(path), options, mode); + Syscall::SC_open_params params { AT_FDCWD, { path, path_length }, options, mode }; + int rc = syscall(SC_open, ¶ms); + __RETURN_WITH_ERRNO(rc, rc, -1); } int openat(int dirfd, const char* path, int options, ...) @@ -97,10 +79,17 @@ int openat(int dirfd, const char* path, int options, ...) errno = EFAULT; return -1; } + auto path_length = strlen(path); + if (path_length > INT32_MAX) { + errno = EINVAL; + return -1; + } va_list ap; va_start(ap, options); auto mode = (mode_t)va_arg(ap, unsigned); va_end(ap); - return openat_with_path_length(dirfd, path, strlen(path), options, mode); + Syscall::SC_open_params params { dirfd, { path, path_length }, options, mode }; + int rc = syscall(SC_open, ¶ms); + __RETURN_WITH_ERRNO(rc, rc, -1); } } diff --git a/Userland/Libraries/LibC/fcntl.h b/Userland/Libraries/LibC/fcntl.h index 1aa6e6cd09..e184808762 100644 --- a/Userland/Libraries/LibC/fcntl.h +++ b/Userland/Libraries/LibC/fcntl.h @@ -85,11 +85,8 @@ __BEGIN_DECLS int creat(const char* path, mode_t); int open(const char* path, int options, ...); -int creat_with_path_length(const char* path, size_t path_length, mode_t); -int open_with_path_length(const char* path, size_t path_length, int options, mode_t); #define AT_FDCWD -100 int openat(int dirfd, const char* path, int options, ...); -int openat_with_path_length(int dirfd, const char* path, size_t path_length, int options, mode_t); int fcntl(int fd, int cmd, ...); int watch_file(const char* path, size_t path_length); diff --git a/Userland/Libraries/LibCore/LocalServer.cpp b/Userland/Libraries/LibCore/LocalServer.cpp index 092cf82388..ec99d51317 100644 --- a/Userland/Libraries/LibCore/LocalServer.cpp +++ b/Userland/Libraries/LibCore/LocalServer.cpp @@ -27,6 +27,7 @@ #include <LibCore/LocalServer.h> #include <LibCore/LocalSocket.h> #include <LibCore/Notifier.h> +#include <fcntl.h> #include <stdio.h> #include <sys/socket.h> #include <sys/stat.h> diff --git a/Userland/Libraries/LibCore/LocalSocket.cpp b/Userland/Libraries/LibCore/LocalSocket.cpp index d49aa1d166..81b30d4c0b 100644 --- a/Userland/Libraries/LibCore/LocalSocket.cpp +++ b/Userland/Libraries/LibCore/LocalSocket.cpp @@ -26,6 +26,7 @@ #include <LibCore/LocalSocket.h> #include <errno.h> +#include <fcntl.h> #include <stdio.h> #include <sys/socket.h> #include <sys/stat.h> diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index bd2e220d19..4b7c5d41d9 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -1050,9 +1050,9 @@ void TextEditor::timer_event(Core::TimerEvent&) update_cursor(); } -bool TextEditor::write_to_file(const StringView& path) +bool TextEditor::write_to_file(const String& path) { - int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_WRONLY | O_CREAT | O_TRUNC, 0666); + int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666); if (fd < 0) { perror("open"); return false; diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index 1ffeb36555..05ddf20383 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -121,7 +121,7 @@ public: TextRange normalized_selection() const { return m_selection.normalized(); } void insert_at_cursor_or_replace_selection(const StringView&); - bool write_to_file(const StringView& path); + bool write_to_file(const String& path); bool has_selection() const { return m_selection.is_valid(); } String selected_text() const; void set_selection(const TextRange&); diff --git a/Userland/Libraries/LibPCIDB/Database.cpp b/Userland/Libraries/LibPCIDB/Database.cpp index 597868ae1a..bb73c308ea 100644 --- a/Userland/Libraries/LibPCIDB/Database.cpp +++ b/Userland/Libraries/LibPCIDB/Database.cpp @@ -32,7 +32,7 @@ namespace PCIDB { -RefPtr<Database> Database::open(const StringView& file_name) +RefPtr<Database> Database::open(const String& file_name) { auto file_or_error = MappedFile::map(file_name); if (file_or_error.is_error()) diff --git a/Userland/Libraries/LibPCIDB/Database.h b/Userland/Libraries/LibPCIDB/Database.h index dbbe8c9482..9bdeafbe93 100644 --- a/Userland/Libraries/LibPCIDB/Database.h +++ b/Userland/Libraries/LibPCIDB/Database.h @@ -31,6 +31,7 @@ #include <AK/NonnullOwnPtr.h> #include <AK/RefCounted.h> #include <AK/RefPtr.h> +#include <AK/String.h> #include <AK/StringView.h> namespace PCIDB { @@ -72,7 +73,7 @@ struct Class { class Database : public RefCounted<Database> { public: - static RefPtr<Database> open(const StringView& file_name); + static RefPtr<Database> open(const String& file_name); static RefPtr<Database> open() { return open("/res/pci.ids"); }; const StringView get_vendor(u16 vendor_id) const; diff --git a/Userland/Services/SystemServer/Service.cpp b/Userland/Services/SystemServer/Service.cpp index 99b01b5ab6..dc798ba8cb 100644 --- a/Userland/Services/SystemServer/Service.cpp +++ b/Userland/Services/SystemServer/Service.cpp @@ -174,7 +174,7 @@ void Service::spawn(int socket_fd) if (!m_stdio_file_path.is_null()) { close(STDIN_FILENO); - int fd = open_with_path_length(m_stdio_file_path.characters(), m_stdio_file_path.length(), O_RDWR, 0); + int fd = open(m_stdio_file_path.characters(), O_RDWR, 0); ASSERT(fd <= 0); if (fd < 0) { perror("open"); diff --git a/Userland/Shell/AST.cpp b/Userland/Shell/AST.cpp index 992452f503..91478a77a0 100644 --- a/Userland/Shell/AST.cpp +++ b/Userland/Shell/AST.cpp @@ -33,6 +33,7 @@ #include <AK/URL.h> #include <LibCore/EventLoop.h> #include <LibCore/File.h> +#include <fcntl.h> #include <signal.h> //#define EXECUTE_DEBUG diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 036c097d7a..a7538d5107 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -55,6 +55,7 @@ #include <LibJS/Runtime/TypedArray.h> #include <LibJS/Runtime/Value.h> #include <LibLine/Editor.h> +#include <fcntl.h> #include <signal.h> #include <stdio.h> @@ -422,9 +423,9 @@ static StringView strip_shebang(AK::ByteBuffer file_contents) return StringView((const char*)file_contents.data() + i, file_contents.size() - i); } -static bool write_to_file(const StringView& path) +static bool write_to_file(const String& path) { - int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_WRONLY | O_CREAT | O_TRUNC, 0666); + int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666); for (size_t i = 0; i < repl_statements.size(); i++) { auto line = repl_statements[i]; if (line.length() && i != repl_statements.size() - 1) { |