Redid file checking to use the provided chunked checksums, added (broken) support for partial package downloads (resumed package fails verification), misc. supporting changes.

This commit is contained in:
2021-11-17 22:34:15 -06:00
parent 2ccc09d0b4
commit 662e5c2b0c
8 changed files with 81 additions and 37 deletions

View File

@@ -174,19 +174,21 @@ class W3DHub
if package_details
@packages_to_download = []
update_application_taskbar("Downloading #{@application.name}...", "Verifying local packages...", 0.0)
package_details.each do |pkg|
unless verify_package(pkg, pkg.category, pkg.subcategory, pkg.name, pkg.version)
unless verify_package(pkg)
@packages_to_download << pkg
end
end
@total_bytes_to_download = @packages_to_download.sum { |pkg| pkg.size }
@total_bytes_to_download = @packages_to_download.sum { |pkg| pkg.size - pkg.custom_partially_valid_at_bytes }
@bytes_downloaded = 0
@packages_to_download.each do |pkg|
package_bytes_downloaded = 0
package_fetch(pkg.category, pkg.subcategory, pkg.name, pkg.version) do |chunk, remaining_bytes, total_bytes|
package_fetch(pkg) do |chunk, remaining_bytes, total_bytes|
@bytes_downloaded += chunk.to_s.length
package_bytes_downloaded += chunk.to_s.length
@@ -235,21 +237,21 @@ class W3DHub
# Check for and integrity of local manifest
if File.exist?(Cache.package_path(category, subcategory, name, version))
package = Api.package_details([{ category: category, subcategory: subcategory, name: name, version: version }])
verified = verify_package(package, category, subcategory, name, version)
verified = verify_package(package)
# download manifest if not valid
package_fetch(category, subcategory, name, version) unless verified
package_fetch(package) unless verified
true if verified
else
# download manifest if not cached
package_fetch(category, subcategory, name, version)
package_fetch(package)
end
end
def package_fetch(category, subcategory, name, version, &block)
puts "Downloading: #{category}:#{subcategory}:#{name}-#{version}"
def package_fetch(package, &block)
puts "Downloading: #{package.category}:#{package.subcategory}:#{package.name}-#{package.version}"
Api.package(category, subcategory, name, version) do |chunk, remaining_bytes, total_bytes|
Api.package(package) do |chunk, remaining_bytes, total_bytes|
# Store progress somewhere
# Kernel.puts "#{name}-#{version}: #{(remaining_bytes.to_f / total_bytes).round}%"
@@ -257,22 +259,41 @@ class W3DHub
end
end
def verify_package(package, category, subcategory, name, version, &block)
puts "Verifying: #{category}:#{subcategory}:#{name}-#{version}"
def verify_package(package, &block)
puts "Verifying: #{package.category}:#{package.subcategory}:#{package.name}-#{package.version}"
digest = Digest::SHA256.new
path = Cache.package_path(category, subcategory, name, version)
path = Cache.package_path(package.category, package.subcategory, package.name, package.version)
return false unless File.exists?(path)
file_size = File.size(path)
puts " File size: #{file_size}"
chunk_size = 128_000_000 if file_size >= 32_000_000
chunk_size ||= 32_000_000
chunk_size = package.checksum_chunk_size
File.open(path) do |f|
while (chunk = f.read(32_000_000))
package.checksum_chunks.each do |chunk_start, checksum|
chunk_start = Integer(chunk_start.to_s)
read_length = chunk_size
read_length = file_size - chunk_start if chunk_start + chunk_size > file_size
break if file_size - chunk_start < 0
f.seek(chunk_start)
chunk = f.read(read_length)
digest.update(chunk)
if Digest::SHA256.new.hexdigest(chunk).upcase == checksum.upcase
valid_at = chunk_start + read_length
puts " Passed chunk: #{chunk_start}"
# package.partially_valid_at_bytes = valid_at
package.partially_valid_at_bytes = chunk_start
else
puts " FAILED chunk: #{chunk_start}"
break
end
end
end

View File

@@ -6,7 +6,7 @@ class W3DHub
manifests = fetch_manifests
return false if failed?
# update_application_taskbar("Downloading #{@application.name}...", "Building package list...", 0.0)
update_application_taskbar("Downloading #{@application.name}...", "Building package list...", 0.0)
packages = build_package_list(manifests)
return false if failed?