Skip to content

Commit

Permalink
Shim IO#pread when not supported
Browse files Browse the repository at this point in the history
Typically on Windows.
  • Loading branch information
byroot committed Aug 4, 2023
1 parent 6fbb687 commit 4c4c49b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,35 @@ on:

jobs:
build:
runs-on: ubuntu-latest
name: "Ruby ${{ matrix.ruby }} / Failure allowed: ${{ matrix.experimental }}"
runs-on: ${{ matrix.os }}-latest
name: "Ruby ${{ matrix.ruby }} / ${{ matrix.os }} / Failure allowed: ${{ matrix.experimental }}"
continue-on-error: ${{ matrix.experimental }}
timeout-minutes: 5

strategy:
fail-fast: false
matrix:
os: ["ubuntu"]
ruby: ["2.6", "2.7", "3.0", "3.1", "3.2"]
experimental: [false]
include:
- ruby: "3.2"
os: "windows"
experimental: false
- ruby: "ruby-head"
os: "ubuntu"
experimental: true
- ruby: "truffleruby-head"
os: "ubuntu"
experimental: true
- ruby: "jruby-head"
os: "ubuntu"
experimental: true
- ruby: "jruby-9.3.9.0"
os: "ubuntu"
experimental: true
- ruby: "jruby-9.4.0.0"
os: "ubuntu"
experimental: true
steps:
- uses: actions/checkout@v3
Expand Down
30 changes: 28 additions & 2 deletions lib/mini_mime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,40 @@ def fetch(key, &blk)
end
end

if ::File.method_defined?(:pread)
PReadFile = ::File
else
# For Windows support
class PReadFile
def initialize(filename)
@mutex = Mutex.new
# We must open the file in binary mode
# otherwise Ruby's automatic line terminator
# translation will skew the row size
@file = ::File.open(filename, 'rb')
end

def readline(*args)
@file.readline(*args)
end

def pread(size, offset)
@mutex.synchronize do
@file.seek(offset, IO::SEEK_SET)
@file.read(size)
end
end
end
end

class RandomAccessDb
MAX_CACHED = 100

def initialize(path, sort_order)
@path = path
@file = File.open(@path)
@file = PReadFile.new(@path)

@row_length = @file.readline.length
@row_length = @file.readline("\n").length
@file_length = File.size(@path)
@rows = @file_length / @row_length

Expand Down
2 changes: 2 additions & 0 deletions test/mini_mime_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def should_prioritize_extensions_correctly

if defined? MIME::Types
def test_full_parity_with_mime_types
skip("Windows MIME::Types isn't reliable") if RUBY_PLATFORM.match?(/windows/i)

exts = Set.new
MIME::Types.each do |type|
type.extensions.each { |ext| exts << ext }
Expand Down

0 comments on commit 4c4c49b

Please sign in to comment.