Sha256: a9bffd007d462d82315ec1030bd10c189a7b113eeadefbdf86105bbde5d02747

Contents?: true

Size: 1.49 KB

Versions: 12

Compression:

Stored size: 1.49 KB

Contents

require "vagrant/util/platform"

module Vagrant
  module Util
    class IO
      # The chunk size for reading from subprocess IO.
      READ_CHUNK_SIZE = 4096

      # Reads data from an IO object while it can, returning the data it reads.
      # When it encounters a case when it can't read anymore, it returns the
      # data.
      #
      # @return [String]
      def self.read_until_block(io)
        data = ""

        while true
          begin
            if Platform.windows?
              # Windows doesn't support non-blocking reads on
              # file descriptors or pipes so we have to get
              # a bit more creative.

              # Check if data is actually ready on this IO device.
              # We have to do this since `readpartial` will actually block
              # until data is available, which can cause blocking forever
              # in some cases.
              results = ::IO.select([io], nil, nil, 1.0)
              break if !results || results[0].empty?

              # Read!
              data << io.readpartial(READ_CHUNK_SIZE).encode(
                "UTF-8", Encoding.default_external,
                invalid: :replace,
                undef: :replace
              )
            else
              # Do a simple non-blocking read on the IO object
              data << io.read_nonblock(READ_CHUNK_SIZE)
            end
          rescue EOFError, Errno::EAGAIN, ::IO::WaitReadable
            break
          end
        end

        data
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
vagrant-unbundled-2.3.6.0 lib/vagrant/util/io.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/lib/vagrant/util/io.rb
vagrant-unbundled-2.3.3.0 lib/vagrant/util/io.rb
vagrant-unbundled-2.3.2.0 lib/vagrant/util/io.rb
vagrant-unbundled-2.2.19.0 lib/vagrant/util/io.rb
vagrant-unbundled-2.2.18.0 lib/vagrant/util/io.rb
vagrant-unbundled-2.2.16.0 lib/vagrant/util/io.rb
vagrant-unbundled-2.2.14.0 lib/vagrant/util/io.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/lib/vagrant/util/io.rb
vagrant-unbundled-2.2.10.0 lib/vagrant/util/io.rb
vagrant-unbundled-2.2.9.0 lib/vagrant/util/io.rb
vagrant-unbundled-2.2.8.0 lib/vagrant/util/io.rb