Skip to content

Commit

Permalink
propagate errors from flush and close within ios_close
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Apr 1, 2020
1 parent 351d7e9 commit ce73730
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ Standard library changes
order from `order` is used to compare the values of `by(element)`. In the latter case,
any order different from `Forward` or `Reverse` will raise an error about the
ambiguity.
* `close` on a file (`IOStream`) can now throw an exception if an error occurs when trying
to flush buffered data to disk ([#35303]).

#### LinearAlgebra
* The BLAS submodule now supports the level-2 BLAS subroutine `hpmv!` ([#34211]).
Expand Down
10 changes: 9 additions & 1 deletion base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,23 @@ to synchronous `File`'s and `IOStream`'s not to any of the asynchronous streams.
fd(s::IOStream) = Int(ccall(:jl_ios_fd, Clong, (Ptr{Cvoid},), s.ios))

stat(s::IOStream) = stat(fd(s))
close(s::IOStream) = @lock_nofail s.lock ccall(:ios_close, Cvoid, (Ptr{Cvoid},), s.ios)

isopen(s::IOStream) = ccall(:ios_isopen, Cint, (Ptr{Cvoid},), s.ios) != 0

function close(s::IOStream)
bad = @lock_nofail s.lock ccall(:ios_close, Cint, (Ptr{Cvoid},), s.ios) != 0
systemerror("close", bad)
end

function flush(s::IOStream)
sigatomic_begin()
bad = @lock_nofail s.lock ccall(:ios_flush, Cint, (Ptr{Cvoid},), s.ios) != 0
sigatomic_end()
systemerror("flush", bad)
end

iswritable(s::IOStream) = ccall(:ios_get_writable, Cint, (Ptr{Cvoid},), s.ios)!=0

isreadable(s::IOStream) = ccall(:ios_get_readable, Cint, (Ptr{Cvoid},), s.ios)!=0

"""
Expand Down
12 changes: 8 additions & 4 deletions src/support/ios.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,17 +648,21 @@ int ios_flush(ios_t *s)
return 0;
}

void ios_close(ios_t *s)
int ios_close(ios_t *s)
{
ios_flush(s);
if (s->fd != -1 && s->ownfd)
close(s->fd);
int err = ios_flush(s);
if (s->fd != -1 && s->ownfd) {
int err2 = close(s->fd);
if (err2 != 0)
err = err2;
}
s->fd = -1;
if (s->buf!=NULL && s->ownbuf && s->buf!=&s->local[0]) {
LLT_FREE(s->buf);
}
s->buf = NULL;
s->size = s->maxsize = s->bpos = 0;
return err;
}

int ios_isopen(ios_t *s)
Expand Down
2 changes: 1 addition & 1 deletion src/support/ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ JL_DLLEXPORT int ios_trunc(ios_t *s, size_t size) JL_NOTSAFEPOINT;
JL_DLLEXPORT int ios_eof(ios_t *s);
JL_DLLEXPORT int ios_eof_blocking(ios_t *s);
JL_DLLEXPORT int ios_flush(ios_t *s);
JL_DLLEXPORT void ios_close(ios_t *s);
JL_DLLEXPORT int ios_close(ios_t *s);
JL_DLLEXPORT int ios_isopen(ios_t *s);
JL_DLLEXPORT char *ios_take_buffer(ios_t *s, size_t *psize); // release buffer to caller
// set buffer space to use
Expand Down

0 comments on commit ce73730

Please sign in to comment.