2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-06-07 09:41:11 +00:00
|
|
|
#include <AK/PrintfImplementation.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/IODevice.h>
|
|
|
|
#include <LibCore/SyscallUtils.h>
|
2019-06-22 19:21:57 +00:00
|
|
|
#include <errno.h>
|
2019-03-18 13:09:58 +00:00
|
|
|
#include <stdio.h>
|
2019-05-28 11:48:06 +00:00
|
|
|
#include <sys/select.h>
|
2019-07-27 12:24:19 +00:00
|
|
|
#include <sys/stat.h>
|
2019-05-28 11:48:06 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2019-03-17 14:54:43 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
IODevice::IODevice(Object* parent)
|
|
|
|
: Object(parent)
|
2019-03-17 14:54:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
IODevice::~IODevice()
|
2019-03-17 14:54:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
const char* IODevice::error_string() const
|
2019-03-17 14:54:43 +00:00
|
|
|
{
|
|
|
|
return strerror(m_error);
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
int IODevice::read(u8* buffer, int length)
|
2019-07-27 14:37:48 +00:00
|
|
|
{
|
|
|
|
auto read_buffer = read(length);
|
2019-09-11 19:13:14 +00:00
|
|
|
if (read_buffer.is_null())
|
|
|
|
return 0;
|
2019-07-27 14:37:48 +00:00
|
|
|
memcpy(buffer, read_buffer.data(), length);
|
|
|
|
return read_buffer.size();
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
ByteBuffer IODevice::read(size_t max_size)
|
2019-03-17 14:54:43 +00:00
|
|
|
{
|
|
|
|
if (m_fd < 0)
|
2019-06-07 09:46:37 +00:00
|
|
|
return {};
|
2019-03-17 14:54:43 +00:00
|
|
|
if (!max_size)
|
2019-06-07 09:46:37 +00:00
|
|
|
return {};
|
2019-03-17 14:54:43 +00:00
|
|
|
auto buffer = ByteBuffer::create_uninitialized(max_size);
|
2019-09-30 06:57:01 +00:00
|
|
|
auto* buffer_ptr = (char*)buffer.data();
|
2020-02-25 13:49:47 +00:00
|
|
|
size_t remaining_buffer_space = buffer.size();
|
|
|
|
size_t taken_from_buffered = 0;
|
2019-03-17 14:54:43 +00:00
|
|
|
if (!m_buffered_data.is_empty()) {
|
2019-06-22 21:07:42 +00:00
|
|
|
taken_from_buffered = min(remaining_buffer_space, m_buffered_data.size());
|
2019-03-17 14:54:43 +00:00
|
|
|
memcpy(buffer_ptr, m_buffered_data.data(), taken_from_buffered);
|
2019-07-03 19:17:35 +00:00
|
|
|
Vector<u8> new_buffered_data;
|
2019-03-17 14:54:43 +00:00
|
|
|
new_buffered_data.append(m_buffered_data.data() + taken_from_buffered, m_buffered_data.size() - taken_from_buffered);
|
|
|
|
m_buffered_data = move(new_buffered_data);
|
|
|
|
remaining_buffer_space -= taken_from_buffered;
|
|
|
|
buffer_ptr += taken_from_buffered;
|
|
|
|
}
|
|
|
|
if (!remaining_buffer_space)
|
|
|
|
return buffer;
|
|
|
|
int nread = ::read(m_fd, buffer_ptr, remaining_buffer_space);
|
|
|
|
if (nread < 0) {
|
2019-06-22 21:07:42 +00:00
|
|
|
if (taken_from_buffered) {
|
|
|
|
buffer.trim(taken_from_buffered);
|
|
|
|
return buffer;
|
|
|
|
}
|
2019-03-17 14:54:43 +00:00
|
|
|
set_error(errno);
|
2019-06-07 09:46:37 +00:00
|
|
|
return {};
|
2019-03-17 14:54:43 +00:00
|
|
|
}
|
2019-04-07 12:36:10 +00:00
|
|
|
if (nread == 0) {
|
|
|
|
set_eof(true);
|
2019-06-22 21:07:42 +00:00
|
|
|
if (taken_from_buffered) {
|
|
|
|
buffer.trim(taken_from_buffered);
|
|
|
|
return buffer;
|
|
|
|
}
|
2019-06-07 09:46:37 +00:00
|
|
|
return {};
|
2019-04-07 12:36:10 +00:00
|
|
|
}
|
2019-06-22 21:07:42 +00:00
|
|
|
buffer.trim(taken_from_buffered + nread);
|
2019-03-17 14:54:43 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool IODevice::can_read_from_fd() const
|
2019-03-18 13:09:58 +00:00
|
|
|
{
|
2019-04-10 18:22:23 +00:00
|
|
|
// FIXME: Can we somehow remove this once CSocket is implemented using non-blocking sockets?
|
2019-03-18 13:09:58 +00:00
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(m_fd, &rfds);
|
2019-06-07 09:46:37 +00:00
|
|
|
struct timeval timeout {
|
|
|
|
0, 0
|
|
|
|
};
|
2020-02-06 14:04:57 +00:00
|
|
|
int rc = Core::safe_syscall(select, m_fd + 1, &rfds, nullptr, nullptr, &timeout);
|
2019-03-18 13:09:58 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
// NOTE: We don't set m_error here.
|
2020-02-02 11:34:39 +00:00
|
|
|
perror("IODevice::can_read: select");
|
2019-03-18 13:09:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return FD_ISSET(m_fd, &rfds);
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool IODevice::can_read_line()
|
2019-03-18 13:09:58 +00:00
|
|
|
{
|
|
|
|
if (m_eof && !m_buffered_data.is_empty())
|
|
|
|
return true;
|
|
|
|
if (m_buffered_data.contains_slow('\n'))
|
|
|
|
return true;
|
2019-03-18 13:38:30 +00:00
|
|
|
if (!can_read_from_fd())
|
2019-03-18 13:09:58 +00:00
|
|
|
return false;
|
|
|
|
populate_read_buffer();
|
|
|
|
return m_buffered_data.contains_slow('\n');
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool IODevice::can_read() const
|
2019-03-18 13:38:30 +00:00
|
|
|
{
|
|
|
|
return !m_buffered_data.is_empty() || can_read_from_fd();
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
ByteBuffer IODevice::read_all()
|
2019-03-18 13:38:30 +00:00
|
|
|
{
|
2019-07-27 12:24:19 +00:00
|
|
|
off_t file_size = 0;
|
|
|
|
struct stat st;
|
|
|
|
int rc = fstat(fd(), &st);
|
|
|
|
if (rc == 0)
|
|
|
|
file_size = st.st_size;
|
|
|
|
|
|
|
|
Vector<u8> data;
|
|
|
|
data.ensure_capacity(file_size);
|
|
|
|
|
2019-03-18 13:38:30 +00:00
|
|
|
if (!m_buffered_data.is_empty()) {
|
2019-07-27 12:24:19 +00:00
|
|
|
data.append(m_buffered_data.data(), m_buffered_data.size());
|
2019-03-18 13:38:30 +00:00
|
|
|
m_buffered_data.clear();
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:51:23 +00:00
|
|
|
while (true) {
|
2019-03-18 13:38:30 +00:00
|
|
|
char read_buffer[4096];
|
|
|
|
int nread = ::read(m_fd, read_buffer, sizeof(read_buffer));
|
|
|
|
if (nread < 0) {
|
2019-08-17 09:07:15 +00:00
|
|
|
set_error(errno);
|
2019-09-11 17:33:51 +00:00
|
|
|
break;
|
2019-03-18 13:38:30 +00:00
|
|
|
}
|
|
|
|
if (nread == 0) {
|
|
|
|
set_eof(true);
|
|
|
|
break;
|
|
|
|
}
|
2019-07-27 12:24:19 +00:00
|
|
|
data.append((const u8*)read_buffer, nread);
|
2019-03-18 13:38:30 +00:00
|
|
|
}
|
2019-09-11 17:33:51 +00:00
|
|
|
if (data.is_empty())
|
|
|
|
return {};
|
2019-07-27 12:24:19 +00:00
|
|
|
return ByteBuffer::copy(data.data(), data.size());
|
2019-03-18 13:38:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
ByteBuffer IODevice::read_line(size_t max_size)
|
2019-03-17 14:54:43 +00:00
|
|
|
{
|
2019-04-07 18:18:58 +00:00
|
|
|
if (m_fd < 0)
|
2019-06-07 09:46:37 +00:00
|
|
|
return {};
|
2019-04-07 18:18:58 +00:00
|
|
|
if (!max_size)
|
2019-06-07 09:46:37 +00:00
|
|
|
return {};
|
2019-04-07 18:18:58 +00:00
|
|
|
if (!can_read_line())
|
2019-06-07 09:46:37 +00:00
|
|
|
return {};
|
2019-03-18 13:09:58 +00:00
|
|
|
if (m_eof) {
|
|
|
|
if (m_buffered_data.size() > max_size) {
|
2020-02-02 11:34:39 +00:00
|
|
|
dbgprintf("IODevice::read_line: At EOF but there's more than max_size(%d) buffered\n", max_size);
|
2019-06-07 09:46:37 +00:00
|
|
|
return {};
|
2019-03-17 14:54:43 +00:00
|
|
|
}
|
2019-03-18 13:09:58 +00:00
|
|
|
auto buffer = ByteBuffer::copy(m_buffered_data.data(), m_buffered_data.size());
|
|
|
|
m_buffered_data.clear();
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
auto line = ByteBuffer::create_uninitialized(max_size + 1);
|
2020-02-25 13:49:47 +00:00
|
|
|
size_t line_index = 0;
|
2019-03-18 13:09:58 +00:00
|
|
|
while (line_index < max_size) {
|
2019-07-03 19:17:35 +00:00
|
|
|
u8 ch = m_buffered_data[line_index];
|
2019-03-17 14:54:43 +00:00
|
|
|
line[line_index++] = ch;
|
|
|
|
if (ch == '\n') {
|
2019-07-03 19:17:35 +00:00
|
|
|
Vector<u8> new_buffered_data;
|
2019-03-17 14:54:43 +00:00
|
|
|
new_buffered_data.append(m_buffered_data.data() + line_index, m_buffered_data.size() - line_index);
|
|
|
|
m_buffered_data = move(new_buffered_data);
|
2019-03-18 13:09:58 +00:00
|
|
|
line[line_index] = '\0';
|
|
|
|
line.trim(line_index + 1);
|
2019-03-17 14:54:43 +00:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 09:46:37 +00:00
|
|
|
return {};
|
2019-03-17 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool IODevice::populate_read_buffer()
|
2019-03-17 14:54:43 +00:00
|
|
|
{
|
|
|
|
if (m_fd < 0)
|
|
|
|
return false;
|
2019-07-03 19:17:35 +00:00
|
|
|
u8 buffer[1024];
|
2019-05-14 14:36:29 +00:00
|
|
|
int nread = ::read(m_fd, buffer, sizeof(buffer));
|
2019-03-17 14:54:43 +00:00
|
|
|
if (nread < 0) {
|
|
|
|
set_error(errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nread == 0) {
|
|
|
|
set_eof(true);
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-14 14:36:29 +00:00
|
|
|
m_buffered_data.append(buffer, nread);
|
2019-03-17 14:54:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-03-18 13:09:58 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool IODevice::close()
|
2019-03-18 13:09:58 +00:00
|
|
|
{
|
|
|
|
if (fd() < 0 || mode() == NotOpen)
|
|
|
|
return false;
|
|
|
|
int rc = ::close(fd());
|
|
|
|
if (rc < 0) {
|
2019-08-17 09:07:15 +00:00
|
|
|
set_error(errno);
|
2019-03-18 13:09:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
set_fd(-1);
|
2020-02-02 11:34:39 +00:00
|
|
|
set_mode(IODevice::NotOpen);
|
2019-03-18 13:09:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-15 22:51:05 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool IODevice::seek(i64 offset, SeekMode mode, off_t* pos)
|
2019-04-15 22:51:05 +00:00
|
|
|
{
|
2019-05-16 13:02:17 +00:00
|
|
|
int m = SEEK_SET;
|
|
|
|
switch (mode) {
|
|
|
|
case SeekMode::SetPosition:
|
|
|
|
m = SEEK_SET;
|
|
|
|
break;
|
|
|
|
case SeekMode::FromCurrentPosition:
|
|
|
|
m = SEEK_CUR;
|
|
|
|
break;
|
|
|
|
case SeekMode::FromEndPosition:
|
|
|
|
m = SEEK_END;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off_t rc = lseek(m_fd, offset, m);
|
2019-04-15 22:51:05 +00:00
|
|
|
if (rc < 0) {
|
2019-05-16 13:02:17 +00:00
|
|
|
set_error(errno);
|
|
|
|
if (pos)
|
|
|
|
*pos = -1;
|
2019-04-15 22:51:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_buffered_data.clear();
|
2019-04-18 00:09:15 +00:00
|
|
|
m_eof = false;
|
2019-05-16 13:02:17 +00:00
|
|
|
if (pos)
|
|
|
|
*pos = rc;
|
2019-04-15 22:51:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-08 02:38:41 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool IODevice::write(const u8* data, int size)
|
2019-05-08 02:38:41 +00:00
|
|
|
{
|
|
|
|
int rc = ::write(m_fd, data, size);
|
|
|
|
if (rc < 0) {
|
2020-02-02 11:34:39 +00:00
|
|
|
perror("IODevice::write: write");
|
2019-06-02 13:18:27 +00:00
|
|
|
set_error(errno);
|
2019-05-08 02:38:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return rc == size;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
int IODevice::printf(const char* format, ...)
|
2019-05-08 02:38:41 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
// FIXME: We're not propagating write() failures to client here!
|
2019-06-07 09:46:37 +00:00
|
|
|
int ret = printf_internal([this](char*&, char ch) {
|
2019-08-01 08:41:04 +00:00
|
|
|
write((const u8*)&ch, 1);
|
2019-06-07 09:46:37 +00:00
|
|
|
},
|
|
|
|
nullptr, format, ap);
|
2019-05-08 02:38:41 +00:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-07-27 08:47:46 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void IODevice::set_fd(int fd)
|
2019-07-27 08:47:46 +00:00
|
|
|
{
|
|
|
|
if (m_fd == fd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_fd = fd;
|
|
|
|
did_update_fd(fd);
|
|
|
|
}
|
2020-02-14 21:29:06 +00:00
|
|
|
|
|
|
|
bool IODevice::write(const StringView& v)
|
|
|
|
{
|
|
|
|
return write((const u8*)v.characters_without_null_termination(), v.length());
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
}
|