Skip to content

Commit

Permalink
fix(stdio): allow usage of file writer on shared non-blocking file de…
Browse files Browse the repository at this point in the history
…scriptors

Signed-off-by: Luís Ferreira <[email protected]>
  • Loading branch information
ljmf00 committed Mar 20, 2023
1 parent 91308f2 commit b2a4047
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -1267,12 +1267,7 @@ Throws: `ErrnoException` if the file is not opened or if the call to `fwrite` fa
}
}

auto result = trustedFwrite(_p.handle, buffer);
if (result == result.max) result = 0;
errnoEnforce(result == buffer.length,
text("Wrote ", result, " instead of ", buffer.length,
" objects of type ", T.stringof, " to file `",
_name, "'"));
_rawWrite(_p.handle, buffer, _name);
}

///
Expand Down Expand Up @@ -3168,10 +3163,7 @@ is empty, throws an `Exception`. In case of an I/O error throws
{
if (orientation_ <= 0)
{
//file.write(writeme); causes infinite recursion!!!
//file.rawWrite(writeme);
auto result = trustedFwrite(file_._p.handle, writeme);
if (result != writeme.length) errnoEnforce(0);
_rawWrite(file_._p.handle, writeme, file_._name);
return;
}
}
Expand Down Expand Up @@ -3406,12 +3398,7 @@ is empty, throws an `Exception`. In case of an I/O error throws
import std.conv : text;
import std.exception : errnoEnforce;

auto result = trustedFwrite(file_._p.handle, buffer);
if (result == result.max) result = 0;
errnoEnforce(result == buffer.length,
text("Wrote ", result, " instead of ", buffer.length,
" objects of type ", T.stringof, " to file `",
name, "'"));
_rawWrite(file_._p.handle, buffer, file_._name);
}

version (Windows)
Expand Down Expand Up @@ -4680,6 +4667,31 @@ if ((isSomeFiniteCharInputRange!R1 || isSomeString!R1) &&
return _fopenImpl(namez, modez);
}

@trusted
private void _rawWrite(T)(FILE* handle, in T[] buffer, string name = null)
{
import std.conv : text;
import std.exception : errnoEnforce;

size_t result;
do
{
.clearerr(handle);
result += trustedFwrite(handle, buffer[result .. $]);

import core.stdc.errno : errno, EAGAIN;
if (.ferror(handle) && errno != EAGAIN)
break;
}
while(result < buffer.length);

if (result == result.max) result = 0;
errnoEnforce(result == buffer.length,
text("Wrote ", result, " instead of ", buffer.length,
" objects of type ", T.stringof, " to file `",
name, "'"));
}

version (Posix)
{
/***********************************
Expand Down

0 comments on commit b2a4047

Please sign in to comment.