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. :^)
This commit is contained in:
Andreas Kling 2021-01-12 19:21:59 +01:00
parent d551263b11
commit 1a08ac72ad
Notes: sideshowbarker 2024-07-18 23:53:09 +09:00
21 changed files with 51 additions and 69 deletions

View File

@ -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);

View File

@ -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; }

View File

@ -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)

View File

@ -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)
{

View File

@ -27,6 +27,7 @@
#include <AK/Assertions.h>
#include <AK/StackInfo.h>
#include <stdio.h>
#include <string.h>
#ifdef __serenity__
# include <serenity.h>

View File

@ -29,6 +29,7 @@
#include <AK/Base64.h>
#include <AK/ByteBuffer.h>
#include <AK/String.h>
#include <string.h>
TEST_CASE(test_decode)
{

View File

@ -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)
{

View File

@ -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;

View File

@ -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();

View File

@ -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)

View File

@ -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)
{
if (!path) {
errno = EFAULT;
return -1;
}
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, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
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;
}
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, &params);
__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, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
}

View File

@ -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);

View File

@ -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>

View File

@ -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>

View File

@ -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;

View File

@ -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&);

View File

@ -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())

View File

@ -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;

View File

@ -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");

View File

@ -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

View File

@ -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) {