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 c037b2b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 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
25 changes: 24 additions & 1 deletion lib/mini_mime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,35 @@ def fetch(key, &blk)
end
end

if ::File.method_defined?(:pread)
PReadFile = ::File
else
# For Windows support
class PReadFile
def initialize(filename, mode)
@mutex = Mutex.new
@file = ::File.new(filename, mode)
end

def readline
@file.readline
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.open(@path)

@row_length = @file.readline.length
@file_length = File.size(@path)
Expand Down

0 comments on commit c037b2b

Please sign in to comment.