xref: /openbsd/usr.sbin/pkg_add/OpenBSD/Mtree.pm (revision 8932bfb7)
1# ex:ts=8 sw=4:
2# $OpenBSD: Mtree.pm,v 1.12 2010/12/24 09:04:14 espie Exp $
3#
4# Copyright (c) 2004-2005 Marc Espie <espie@openbsd.org>
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18use strict;
19use warnings;
20
21package OpenBSD::Mtree;
22use File::Spec;
23
24# read an mtree file, and produce the corresponding directory hierarchy
25
26sub parse_fh
27{
28	my ($mtree, $basedir, $fh, $h) = @_;
29	my $_;
30	while(<$fh>) {
31		chomp;
32		s/^\s*//o;
33		next if /^\#/o || /^\//o;
34		s/\s.*$//o;
35		next if /^$/o;
36		if ($_ eq '..') {
37			$basedir =~ s|/[^/]*$||o;
38			next;
39		} elsif (m/^\//) {
40			$basedir = $_;
41		} else {
42			$basedir.="/$_";
43		}
44		$_ = $basedir;
45		while (s|/\./|/|o)	{}
46		if (defined $h) {
47			$mtree->{File::Spec->canonpath($_)} //= {};
48		} else {
49			$mtree->{File::Spec->canonpath($_)} = 1;
50		}
51	}
52}
53
54sub parse
55{
56	my ($mtree, $basedir, $filename, $h) = @_;
57	open my $file, '<', $filename or die "can't open $filename: $!";
58	parse_fh($mtree, $basedir, $file, $h);
59	close $file;
60}
61
621;
63