Skip to content

Commit ccc306a

Browse files
committed
Merge pull request #102365 from bruvzg/pipe_buffer_chk
Implement `get_length()` for pipes.
2 parents 61808e9 + e6e108d commit ccc306a

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

core/io/file_access.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ String FileAccess::get_line() const {
451451
uint8_t c = get_8();
452452

453453
while (!eof_reached()) {
454-
if (c == '\n' || c == '\0') {
454+
if (c == '\n' || c == '\0' || get_error() != OK) {
455455
line.push_back(0);
456456
return String::utf8(line.get_data());
457457
} else if (c != '\r') {

doc/classes/FileAccess.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
<method name="get_length" qualifiers="const">
205205
<return type="int" />
206206
<description>
207-
Returns the size of the file in bytes.
207+
Returns the size of the file in bytes. For a pipe, returns the number of bytes available for reading from the pipe.
208208
</description>
209209
</method>
210210
<method name="get_line" qualifiers="const">

drivers/unix/file_access_unix_pipe.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include <errno.h>
3939
#include <fcntl.h>
40+
#include <sys/ioctl.h>
4041
#include <sys/stat.h>
4142
#include <sys/types.h>
4243
#include <unistd.h>
@@ -132,6 +133,14 @@ String FileAccessUnixPipe::get_path_absolute() const {
132133
return path_src;
133134
}
134135

136+
uint64_t FileAccessUnixPipe::get_length() const {
137+
ERR_FAIL_COND_V_MSG(fd[0] < 0, 0, "Pipe must be opened before use.");
138+
139+
int buf_rem = 0;
140+
ERR_FAIL_COND_V(ioctl(fd[0], FIONREAD, &buf_rem) != 0, 0);
141+
return buf_rem;
142+
}
143+
135144
uint64_t FileAccessUnixPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
136145
ERR_FAIL_COND_V_MSG(fd[0] < 0, -1, "Pipe must be opened before use.");
137146
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);

drivers/unix/file_access_unix_pipe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class FileAccessUnixPipe : public FileAccess {
6161
virtual void seek(uint64_t p_position) override {}
6262
virtual void seek_end(int64_t p_position = 0) override {}
6363
virtual uint64_t get_position() const override { return 0; }
64-
virtual uint64_t get_length() const override { return 0; }
64+
virtual uint64_t get_length() const override;
6565

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

drivers/windows/file_access_windows_pipe.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ String FileAccessWindowsPipe::get_path_absolute() const {
102102
return path_src;
103103
}
104104

105+
uint64_t FileAccessWindowsPipe::get_length() const {
106+
ERR_FAIL_COND_V_MSG(fd[0] == nullptr, -1, "Pipe must be opened before use.");
107+
108+
DWORD buf_rem = 0;
109+
ERR_FAIL_COND_V(!PeekNamedPipe(fd[0], nullptr, 0, nullptr, &buf_rem, nullptr), 0);
110+
return buf_rem;
111+
}
112+
105113
uint64_t FileAccessWindowsPipe::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
106114
ERR_FAIL_COND_V_MSG(fd[0] == nullptr, -1, "Pipe must be opened before use.");
107115
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);

drivers/windows/file_access_windows_pipe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class FileAccessWindowsPipe : public FileAccess {
6060
virtual void seek(uint64_t p_position) override {}
6161
virtual void seek_end(int64_t p_position = 0) override {}
6262
virtual uint64_t get_position() const override { return 0; }
63-
virtual uint64_t get_length() const override { return 0; }
63+
virtual uint64_t get_length() const override;
6464

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

0 commit comments

Comments
 (0)