Skip to content

Commit

Permalink
Implement get_length() for pipes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruvzg committed Feb 3, 2025
1 parent 1586c56 commit 3f733aa
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/io/file_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ String FileAccess::get_line() const {
uint8_t c = get_8();

while (!eof_reached()) {
if (c == '\n' || c == '\0') {
if (c == '\n' || c == '\0' || get_error() != OK) {
line.push_back(0);
return String::utf8(line.get_data());
} else if (c != '\r') {
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/FileAccess.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
<method name="get_length" qualifiers="const">
<return type="int" />
<description>
Returns the size of the file in bytes.
Returns the size of the file in bytes. If [FileAccess] is pipe, returns number of bytes available to be read from the pipe.
</description>
</method>
<method name="get_line" qualifiers="const">
Expand Down
9 changes: 9 additions & 0 deletions drivers/unix/file_access_unix_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Expand Down Expand Up @@ -132,6 +133,14 @@ String FileAccessUnixPipe::get_path_absolute() const {
return path_src;
}

uint64_t FileAccessUnixPipe::get_length() const {
ERR_FAIL_COND_V_MSG(fd[0] < 0, 0, "Pipe must be opened before use.");

int buf_rem = 0;
ERR_FAIL_COND_V(ioctl(fd[0], FIONREAD, &buf_rem) != 0, 0);
return buf_rem;
}

uint64_t FileAccessUnixPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V_MSG(fd[0] < 0, -1, "Pipe must be opened before use.");
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
Expand Down
2 changes: 1 addition & 1 deletion drivers/unix/file_access_unix_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class FileAccessUnixPipe : public FileAccess {
virtual void seek(uint64_t p_position) override {}
virtual void seek_end(int64_t p_position = 0) override {}
virtual uint64_t get_position() const override { return 0; }
virtual uint64_t get_length() const override { return 0; }
virtual uint64_t get_length() const override;

virtual bool eof_reached() const override { return false; }

Expand Down
8 changes: 8 additions & 0 deletions drivers/windows/file_access_windows_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ String FileAccessWindowsPipe::get_path_absolute() const {
return path_src;
}

uint64_t FileAccessWindowsPipe::get_length() const {
ERR_FAIL_COND_V_MSG(fd[0] == nullptr, -1, "Pipe must be opened before use.");

DWORD buf_rem = 0;
ERR_FAIL_COND_V(!PeekNamedPipe(fd[0], nullptr, 0, nullptr, &buf_rem, nullptr), 0);
return buf_rem;
}

uint64_t FileAccessWindowsPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
ERR_FAIL_COND_V_MSG(fd[0] == nullptr, -1, "Pipe must be opened before use.");
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
Expand Down
2 changes: 1 addition & 1 deletion drivers/windows/file_access_windows_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class FileAccessWindowsPipe : public FileAccess {
virtual void seek(uint64_t p_position) override {}
virtual void seek_end(int64_t p_position = 0) override {}
virtual uint64_t get_position() const override { return 0; }
virtual uint64_t get_length() const override { return 0; }
virtual uint64_t get_length() const override;

virtual bool eof_reached() const override { return false; }

Expand Down

0 comments on commit 3f733aa

Please sign in to comment.