# deb-format -- lintian check script

# Copyright (C) 2004 Denis Barbier
# Based on a check provided by Marc 'HE' Brockschmidt.
# Code cleared up a bit, and fix one thinko by Jeroen van Wolffelaar
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

package Lintian::deb_format;
use strict;
use Tags;

sub run {

my $pkg = shift;
my $type = shift;

if (open (LIST, "ar t deb |")) {
	while (<LIST>) {
		chomp;
		tag "deb-data-member-wrongly-compressed", ""
		    if (($_ eq "data.tar.bz2") or ($_ eq "data.tar"));
	}
	close LIST;
}

open INPUT, "dpkg-deb --fsys-tarfile deb |";

my $chunk;
# Previous was a LongLink?
my $is_long = 0;
while (read(INPUT, $chunk, 512)) {
	last if length($chunk) != 512;
	my ($name, $size, $htype) = unpack "A100x24A12x20A1", $chunk;
	$size = oct($size);

	if (!$is_long && length($name) == 100) {
		# This should have been a LongLink, as now the filename isn't
		# NUL-terminated, as dpkg -i expects.

		# Remove the leading dot
		$name =~ s/^\.//;

		tag "deb-created-with-broken-tar", "file: $name";
	}

	$is_long = $htype eq 'L';

	# eat data
	if ($size) {
		read(INPUT, $chunk, get_block_size($size)) or last;
	}
}

close INPUT;

} # </run>

sub get_block_size {
	my $size = shift;
	my $nblocks = int($size / 512);
	$nblocks ++ if (($size % 512) > 0);
	return 512*$nblocks;
}

1;

# vim: ts=4 sw=4
