File: Path.pm

package info (click to toggle)
systemconfigurator 2.0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 724 kB
  • ctags: 313
  • sloc: perl: 7,423; makefile: 48
file content (86 lines) | stat: -rw-r--r-- 2,640 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package Boot::Path;

#   $Header: /cvsroot/systemconfig/systemconfig/lib/Boot/Path.pm,v 1.1 2003/08/18 00:16:46 dannf Exp $

#   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, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#   dann frazier <dannf@dannf.org>

use strict;
use Carp;
use File::Spec;

## ($device, $mount) = get_mount_info($file, $fstab)
## scans fstab to find the device and mountpoint on which a given file exists.
sub get_mountinfo {
    my ($this, $file, $fstab) = @_;
    
    my @path = File::Spec->splitdir($file);

    open(FSTAB, "<$fstab") or croak("Couldn't open $fstab for reading");
    ### cycle through fstab, each time chopping off another component from
    ### the end of our path name - we assume the longest parent path is the
    ### mount point.
    while($file && $file ne File::Spec->rootdir()) {
	seek FSTAB,0,0;

	pop @path;
	$file = File::Spec->catdir(@path);
	while(<FSTAB>) {
	    s/#.*//;   ## strip comments
	    if (m,^\s*(/dev/\S+\d+)\s+$file\s,) {
		close FSTAB;
		return ($1, $file);
	    }
	}
    }
    close FSTAB;
    return (undef, undef);
}

## $stripped = strip_parent($parent, $path)
##
## Return $path with the $parent component dropped.
## Could be done with a simple s//, but this should allow for
## more flexibility by allowing multiple strings to reference the
## same pathname.
sub strip_parent {
    my ($this, $parent, $path) = @_;

    $parent = File::Spec->canonpath($parent);
    $path = File::Spec->canonpath($path);

    if ($parent eq File::Spec->rootdir()) {
	return $path;
    }
    
    my @parentdirs = reverse File::Spec->splitdir($parent);
    my @pathdirs = reverse  File::Spec->splitdir($path);

    while (@parentdirs) {
	unless (@pathdirs) {
	    croak("$parent has more components than $path");
	}
	my $parentnext = pop @parentdirs;
	my $pathnext = pop @pathdirs;

	if ($parentnext ne $pathnext) {
	    croak("$path doesn't appear to be a path under $parent");
	}
    }
    return File::Spec->catdir(reverse (@pathdirs, "/"));
}
	
1;