File indexing completed on 2024-04-21 16:00:53

0001 # frozen_string_literal: true
0002 #
0003 # Copyright (C) 2016 Harald Sitter <sitter@kde.org>
0004 #
0005 # This library is free software; you can redistribute it and/or
0006 # modify it under the terms of the GNU Lesser General Public
0007 # License as published by the Free Software Foundation; either
0008 # version 2.1 of the License, or (at your option) version 3, or any
0009 # later version accepted by the membership of KDE e.V. (or its
0010 # successor approved by the membership of KDE e.V.), which shall
0011 # act as a proxy defined in Section 6 of version 3 of the license.
0012 #
0013 # This library is distributed in the hope that it will be useful,
0014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016 # Lesser General Public License for more details.
0017 #
0018 # You should have received a copy of the GNU Lesser General Public
0019 # License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0020 
0021 require 'insensitive_hash/minimal'
0022 
0023 require_relative 'deb822'
0024 
0025 module Debian
0026   # Debian Release (repo) parser
0027   class Release < Deb822
0028     # FIXME: lazy read automatically when accessing fields
0029     attr_reader :fields
0030 
0031     Checksum = Struct.new(:sum, :size, :file_name) do
0032       def to_s
0033         "#{sum} #{size} #{file_name}"
0034       end
0035     end
0036 
0037     # FIXME: pretty sure that should be in the base
0038     def initialize(file)
0039       @file = file
0040       @fields = InsensitiveHash.new
0041       @spec = { mandatory: %w(),
0042                 relationship: %w(),
0043                 multiline: %w(md5sum sha1 sha256 sha512) }
0044       @spec[:foldable] = %w() + @spec[:relationship]
0045     end
0046 
0047     def parse!
0048       lines = ::File.new(@file).readlines
0049       @fields = parse_paragraph(lines, @spec)
0050       post_process
0051 
0052       # FIXME: signing verification not implemented
0053     end
0054 
0055     def dump
0056       output = ''
0057       output += dump_paragraph(@fields, @spec)
0058       output + "\n"
0059     end
0060 
0061     private
0062 
0063     def post_process
0064       return unless @fields
0065 
0066       # NB: need case sensitive here, or we overwrite the correct case with
0067       #     a bogus one.
0068       %w(MD5Sum SHA1 SHA256 SHA512).each do |key|
0069         @fields[key] = parse_types(fields[key], Checksum)
0070       end
0071     end
0072 
0073     def parse_types(lines, klass)
0074       lines.split($/).collect do |line|
0075         klass.new(*line.split(' '))
0076       end.unshift(klass.new)
0077       # Push an empty isntance in to make sure output is somewhat sane
0078     end
0079   end
0080 end