1# ex:ts=8 sw=4: 2# $OpenBSD: Mtree.pm,v 1.13 2014/03/18 18:53:29 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 while(<$fh>) { 30 chomp; 31 s/^\s*//o; 32 next if /^\#/o || /^\//o; 33 s/\s.*$//o; 34 next if /^$/o; 35 if ($_ eq '..') { 36 $basedir =~ s|/[^/]*$||o; 37 next; 38 } elsif (m/^\//) { 39 $basedir = $_; 40 } else { 41 $basedir.="/$_"; 42 } 43 $_ = $basedir; 44 while (s|/\./|/|o) {} 45 if (defined $h) { 46 $mtree->{File::Spec->canonpath($_)} //= {}; 47 } else { 48 $mtree->{File::Spec->canonpath($_)} = 1; 49 } 50 } 51} 52 53sub parse 54{ 55 my ($mtree, $basedir, $filename, $h) = @_; 56 open my $file, '<', $filename or die "can't open $filename: $!"; 57 parse_fh($mtree, $basedir, $file, $h); 58 close $file; 59} 60 611; 62