Skip to content

Commit

Permalink
Update for Crystal v0.35
Browse files Browse the repository at this point in the history
  • Loading branch information
m-o-e authored Jun 12, 2020
2 parents ea551d1 + 0890e2f commit 6c6b1fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jobs:
build:
working_directory: ~/app
docker:
- image: crystallang/crystal:0.33.0
- image: crystallang/crystal:0.35.0
steps:
- checkout
- run:
Expand Down
29 changes: 17 additions & 12 deletions src/rucksack.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ require "openssl"

class Rucksack
class NotFound < Exception; end

class FileNotFound < Exception; end

class FileCorrupted < Exception; end

class RucksackHeaderNotFound < Exception; end

class RucksackNotFound < Exception; end

class RucksackEmpty < Exception; end

class RucksackCorrupted < Exception; end

KNAUTSCHZONE = ("\000" * 8192).to_slice
EOF_DELIM = "RS".to_slice
DATA_FILE = Process.executable_path.not_nil!
MODE = ENV.fetch("RUCKSACK_MODE", "0").to_i
EOF_DELIM = "RS".to_slice
DATA_FILE = Process.executable_path.not_nil!
MODE = ENV.fetch("RUCKSACK_MODE", "0").to_i

@@offset : UInt64 = 0
@@index = {} of String => File
Expand Down Expand Up @@ -128,13 +134,13 @@ class Rucksack
raise NotImplementedError.new("")
end

def write(slice : Bytes) : Nil
def write(slice : Bytes) : Int64
@md.update(slice)
nil
slice.size.to_i64
end

def digest
@md.digest
def final
@md.final
end
end

Expand Down Expand Up @@ -162,7 +168,7 @@ class Rucksack
::File.open(@path) do |fd|
IO.copy(fd, c)
end
c.digest
c.final
end

def size : UInt64
Expand All @@ -182,7 +188,7 @@ class Rucksack
def verify!
c = Checksummer.new
read(c, true)
raise FileCorrupted.new(@path) unless @checksum == c.digest
raise FileCorrupted.new(@path) unless @checksum == c.final
@verified = true
end

Expand All @@ -203,8 +209,7 @@ end
macro rucksack(path)
{% if path.is_a? StringLiteral && !path.empty? %}
{{
system( \
<<-EOC
system(<<-EOC
cat >#{__DIR__}/.rucksack_packer.cr <<EOF
require "openssl"
Expand Down Expand Up @@ -247,7 +252,7 @@ File.open(".rucksack.toc", "a") do |fd|
end
EOF
EOC
)
)
}}
{{ run("./.rucksack_packer", path) }}
{{ system("rm -f #{__DIR__}/.rucksack_packer.cr") }}
Expand Down
6 changes: 3 additions & 3 deletions test/test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ checksums = {} of String => Slice(UInt8)
File.open({{path}}) do |fd|
IO.copy(fd, c)
end
checksums[{{path}}] = c.digest
checksums[{{path}}] = c.final

rucksack({{path}})
{% end %}

checksums.each do |file, checksum|
c = Rucksack::Checksummer.new
rucksack(file).read(c)
if checksum != c.digest
if checksum != c.final
puts
puts "ERROR: Checksum mismatch #{file}"
exit 1
Expand All @@ -30,7 +30,7 @@ end

c = Rucksack::Checksummer.new
rucksack("./fixtures/cat3.txt").read(c)
if checksums["./fixtures/cat3.txt"] != c.digest
if checksums["./fixtures/cat3.txt"] != c.final
puts
puts "ERROR: Meow. :("
exit 1
Expand Down

0 comments on commit 6c6b1fd

Please sign in to comment.