1# ex:ts=8 sw=4: 2# $OpenBSD: Mtree.pm,v 1.14 2023/06/13 09:07:17 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 v5.36; 19 20package OpenBSD::Mtree; 21use File::Spec; 22 23# read an mtree file, and produce the corresponding directory hierarchy 24 25sub parse_fh($mtree, $basedir, $fh, $h = undef) 26{ 27 while(<$fh>) { 28 chomp; 29 s/^\s*//o; 30 next if /^\#/o || /^\//o; 31 s/\s.*$//o; 32 next if /^$/o; 33 if ($_ eq '..') { 34 $basedir =~ s|/[^/]*$||o; 35 next; 36 } elsif (m/^\//) { 37 $basedir = $_; 38 } else { 39 $basedir.="/$_"; 40 } 41 $_ = $basedir; 42 while (s|/\./|/|o) {} 43 if (defined $h) { 44 $mtree->{File::Spec->canonpath($_)} //= {}; 45 } else { 46 $mtree->{File::Spec->canonpath($_)} = 1; 47 } 48 } 49} 50 51sub parse($mtree, $basedir, $filename, $h = undef) 52{ 53 open my $file, '<', $filename or die "can't open $filename: $!"; 54 parse_fh($mtree, $basedir, $file, $h); 55 close $file; 56} 57 581; 59