2021-03-24 05:09:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Packages
|
|
|
|
module Debian
|
|
|
|
class ExtractChangesMetadataService
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
ExtractionError = Class.new(StandardError)
|
|
|
|
|
|
|
|
def initialize(package_file)
|
|
|
|
@package_file = package_file
|
|
|
|
@entries = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
{
|
|
|
|
file_type: file_type,
|
|
|
|
architecture: metadata[:architecture],
|
|
|
|
fields: fields,
|
|
|
|
files: files
|
|
|
|
}
|
|
|
|
rescue ActiveModel::ValidationError => e
|
2021-05-04 11:10:36 -04:00
|
|
|
raise ExtractionError, e.message
|
2021-03-24 05:09:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def metadata
|
|
|
|
strong_memoize(:metadata) do
|
|
|
|
::Packages::Debian::ExtractMetadataService.new(@package_file).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_type
|
|
|
|
metadata[:file_type]
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
metadata[:fields]
|
|
|
|
end
|
|
|
|
|
|
|
|
def files
|
|
|
|
strong_memoize(:files) do
|
2021-05-04 11:10:36 -04:00
|
|
|
raise ExtractionError, "is not a changes file" unless file_type == :changes
|
|
|
|
raise ExtractionError, "Files field is missing" if fields['Files'].blank?
|
|
|
|
raise ExtractionError, "Checksums-Sha1 field is missing" if fields['Checksums-Sha1'].blank?
|
|
|
|
raise ExtractionError, "Checksums-Sha256 field is missing" if fields['Checksums-Sha256'].blank?
|
2021-03-24 05:09:10 -04:00
|
|
|
|
|
|
|
init_entries_from_files
|
|
|
|
entries_from_checksums_sha1
|
|
|
|
entries_from_checksums_sha256
|
|
|
|
entries_from_package_files
|
|
|
|
|
|
|
|
@entries
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def init_entries_from_files
|
2021-03-30 05:10:51 -04:00
|
|
|
each_lines_for('Files') do |line|
|
2021-03-24 05:09:10 -04:00
|
|
|
md5sum, size, section, priority, filename = line.split
|
|
|
|
entry = FileEntry.new(
|
|
|
|
filename: filename,
|
|
|
|
size: size.to_i,
|
|
|
|
md5sum: md5sum,
|
|
|
|
section: section,
|
|
|
|
priority: priority)
|
|
|
|
|
|
|
|
@entries[filename] = entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def entries_from_checksums_sha1
|
2021-03-30 05:10:51 -04:00
|
|
|
each_lines_for('Checksums-Sha1') do |line|
|
2021-03-24 05:09:10 -04:00
|
|
|
sha1sum, size, filename = line.split
|
|
|
|
entry = @entries[filename]
|
2021-05-04 11:10:36 -04:00
|
|
|
raise ExtractionError, "#{filename} is listed in Checksums-Sha1 but not in Files" unless entry
|
|
|
|
raise ExtractionError, "Size for #{filename} in Files and Checksums-Sha1 differ" unless entry.size == size.to_i
|
2021-03-24 05:09:10 -04:00
|
|
|
|
|
|
|
entry.sha1sum = sha1sum
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def entries_from_checksums_sha256
|
2021-03-30 05:10:51 -04:00
|
|
|
each_lines_for('Checksums-Sha256') do |line|
|
2021-03-24 05:09:10 -04:00
|
|
|
sha256sum, size, filename = line.split
|
|
|
|
entry = @entries[filename]
|
2021-05-04 11:10:36 -04:00
|
|
|
raise ExtractionError, "#{filename} is listed in Checksums-Sha256 but not in Files" unless entry
|
|
|
|
raise ExtractionError, "Size for #{filename} in Files and Checksums-Sha256 differ" unless entry.size == size.to_i
|
2021-03-24 05:09:10 -04:00
|
|
|
|
|
|
|
entry.sha256sum = sha256sum
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_lines_for(field)
|
|
|
|
fields[field].split("\n").each do |line|
|
|
|
|
next if line.blank?
|
|
|
|
|
|
|
|
yield(line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def entries_from_package_files
|
|
|
|
@entries.each do |filename, entry|
|
|
|
|
entry.package_file = ::Packages::PackageFileFinder.new(@package_file.package, filename).execute!
|
|
|
|
entry.validate!
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
2021-05-04 11:10:36 -04:00
|
|
|
raise ExtractionError, "#{filename} is listed in Files but was not uploaded"
|
2021-03-24 05:09:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|