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 288460f
Show file tree
Hide file tree
Showing 2 changed files with 36 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
27 changes: 25 additions & 2 deletions lib/mini_mime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,37 @@ 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
@file = ::File.new(filename)
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

0 comments on commit 288460f

Please sign in to comment.