Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nb_available(open(fn)) != nb_available(Filesystem.open(fn)) #14624

Open
samoconnor opened this issue Jan 10, 2016 · 7 comments
Open

nb_available(open(fn)) != nb_available(Filesystem.open(fn)) #14624

samoconnor opened this issue Jan 10, 2016 · 7 comments
Labels
docs This change adds or pertains to documentation filesystem Underlying file system and functions that use it io Involving the I/O subsystem: libuv, read, write, etc.

Comments

@samoconnor
Copy link
Contributor

Is this expected behaviour?

Version 0.5.0-dev+1968 (2016-01-02 23:28 UTC)

julia> f = open("/tmp/foo.csv")
IOStream(<file /tmp/foo.csv>)

julia> nb_available(f)
0

julia> f = Base.Filesystem.open("/tmp/foo.csv", Base.Filesystem.JL_O_RDONLY)
Base.Filesystem.File(true,RawFD(18))

julia> Base.Filesystem.nb_available(f)
6

@vtjnash
Copy link
Member

vtjnash commented Jan 10, 2016

nb_available is the number of bytes that can be read from the fd without a syscall (or other operation that might block). The correct answer is 0. It looks like the libuv File wrapper is not consistent here however.

@vtjnash vtjnash changed the title nb_available() broken for ordinary files? nb_available() broken for the libuv File Jan 10, 2016
@tkelman tkelman added the io Involving the I/O subsystem: libuv, read, write, etc. label Jan 10, 2016
@samoconnor
Copy link
Contributor Author

The manual entry says:

nb_available(stream)
Returns the number of bytes available for reading before
a read from this stream or buffer will block.

Given that /tmp/foo.csz is a 6-byte local file, it seems fair to assume that if you do a read it will return the 6 bytes without having to block.
The definition in Filesystem.jl is nb_available(f::File) = filesize(f) - position(f). This makes it pretty clear that @stevengj intended this behaviour.

@samoconnor samoconnor changed the title nb_available() broken for the libuv File nb_available() behaviour not documented for ordinary files Jan 10, 2016
@samoconnor
Copy link
Contributor Author

@stevengj, do you have any comment on this?
I'd like to add a test that checks consistency of nb_available across different stream types to this test.

@samoconnor
Copy link
Contributor Author

@stevengj bump...

@samoconnor samoconnor changed the title nb_available() behaviour not documented for ordinary files nb_available(open(fn)) != nb_available(Filesystem.open(fn)) Feb 15, 2016
@samoconnor
Copy link
Contributor Author

Note change to nb_available(File) : #15090

@stevengj
Copy link
Member

stevengj commented Mar 3, 2016

In general, it doesn't seem possible for nb_available to return the number of bytes that can be read from an arbitrary stream (e.g. in the case of a pipe this is not known in advance). So, there are two options here:

  • For the specific case of a file stream, return the same thing as nb_available(::File). This has the disadvantage of requiring us to work around libuv.
  • Leave the behavior as-is.

In either case, better documentation is required.

@ihnorton ihnorton added the docs This change adds or pertains to documentation label May 12, 2016
@brenhinkeller brenhinkeller added the filesystem Underlying file system and functions that use it label Nov 21, 2022
@vtjnash
Copy link
Member

vtjnash commented Feb 10, 2024

Filesystem.File still appears to be wrong here:

julia> f = Base.Filesystem.open("VERSION", Base.Filesystem.JL_O_RDONLY)
Base.Filesystem.File(true, RawFD(19))

julia> bytesavailable(f)
11

julia> f = open("VERSION", "r")
IOStream(<file VERSION>)

julia> bytesavailable(f)
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation filesystem Underlying file system and functions that use it io Involving the I/O subsystem: libuv, read, write, etc.
Projects
None yet
Development

No branches or pull requests

6 participants