Skip to content

Commit

Permalink
Prevent verifying schema version of a file to mmap it (#615)
Browse files Browse the repository at this point in the history
Before the whole file was mmap:ed for just reading the first four bytes,
causing high memory usage.
  • Loading branch information
carlhoerberg authored Dec 14, 2023
1 parent bb22364 commit e1909f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/lavinmq/mfile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,12 @@ class MFile < IO
@size = new_size.to_i64
@pos = new_size.to_i64 if @pos > new_size
end

# Read from a specific position in the file
# but without mapping the whole file, it uses `pread`
def read_at(pos, bytes)
cnt = LibC.pread(@fd, bytes, bytes.bytesize, pos)
raise IO::Error.from_errno("pread") if cnt == -1
cnt
end
end
12 changes: 11 additions & 1 deletion src/lavinmq/schema.cr
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,24 @@ module LavinMQ
index: 4,
}

def self.verify(file, type) : Int32
def self.verify(file : File, type) : Int32
version = file.read_bytes Int32
if version != VERSIONS[type]
raise OutdatedSchemaVersion.new version, file.path
end
version
end

def self.verify(file : MFile, type) : Int32
buf = uninitialized UInt8[4]
file.read_at(0, buf.to_slice)
version = IO::ByteFormat::SystemEndian.decode(Int32, buf.to_slice)
if version != VERSIONS[type]
raise OutdatedSchemaVersion.new version, file.path
end
version
end

def self.prefix(file, type) : Int32
version = VERSIONS[type]
file.write_bytes version
Expand Down

0 comments on commit e1909f4

Please sign in to comment.