| 1 | #!/usr/bin/perl -Tw | ||||||||||
| 2 | #Copyright 1998-2001, Randall Maas. All rights reserved. This program is free | ||||||||||
| 3 | #software; you can redistribute it and/or modify it under the same terms as | ||||||||||
| 4 | #PERL itself. | ||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | C<CfgTie::TieMTab> -- an associative array of mount entries | ||||||||||
| 9 | |||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||
| 11 | |||||||||||
| 12 | makes the mount table available as a regular hash: | ||||||||||
| 13 | |||||||||||
| 14 | tie %MTab, 'CfgTie::TieMTab'; | ||||||||||
| 15 | tie %MTab, 'CfgTie::TieMTab_dev'; | ||||||||||
| 16 | |||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||
| 18 | |||||||||||
| 19 | The keys are path and devices. | ||||||||||
| 20 | |||||||||||
| 21 | The values are always list references. The lists are one of two forms. For | ||||||||||
| 22 | local path, the list is of the form: | ||||||||||
| 23 | |||||||||||
| 24 | [$device, $path, $type, $options] | ||||||||||
| 25 | |||||||||||
| 26 | Other paths not in the mount table are of the form | ||||||||||
| 27 | |||||||||||
| 28 | [$device] | ||||||||||
| 29 | |||||||||||
| 30 | The form of I<device> varies from system to system. It is usually the device | ||||||||||
| 31 | specified in the mount table. NFS and other network mounts are of the form | ||||||||||
| 32 | I<host:path>. C<amd> devices are different, and (at the time of this writing) | ||||||||||
| 33 | their form isn't known. | ||||||||||
| 34 | |||||||||||
| 35 | =head1 Caveats | ||||||||||
| 36 | |||||||||||
| 37 | This requires C<Quota> to work. You can get it from CPAN. | ||||||||||
| 38 | |||||||||||
| 39 | =head1 Files | ||||||||||
| 40 | |||||||||||
| 41 | =over 1 | ||||||||||
| 42 | |||||||||||
| 43 | =item F</etc/mtab> | ||||||||||
| 44 | |||||||||||
| 45 | (on many machines) | ||||||||||
| 46 | |||||||||||
| 47 | =item F</proc/mounts> | ||||||||||
| 48 | |||||||||||
| 49 | (on Linux machines) | ||||||||||
| 50 | |||||||||||
| 51 | =back | ||||||||||
| 52 | |||||||||||
| 53 | =head1 See Also | ||||||||||
| 54 | |||||||||||
| 55 | L<Quota::> | ||||||||||
| 56 | |||||||||||
| 57 | =head1 Author | ||||||||||
| 58 | |||||||||||
| 59 | Randall Maas (L<mailto:randym@acm.org>, L<http://www.hamline.edu/~rcmaas/>) | ||||||||||
| 60 | |||||||||||
| 61 | =cut | ||||||||||
| 62 | |||||||||||
| 63 | package CfgTie::TieMTab; | ||||||||||
| 64 | |||||||||||
| 65 | 1 | my $Ok=0; | |||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | if (eval("use Quota;")) {$Ok=1;} | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.7
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | 1 | my (%by_dev,%by_path); | |||||||||
| 70 | 1 | 1; | |||||||||
| 71 | |||||||||||
| 72 | sub TIEHASH | ||||||||||
| 73 | 0 | ▶ | { | ||||||||
|
Subroutine
TIEHASH: not called CC=2 SCAR=17.9
| |||||||||||
| 74 | # Fail the tie has if the Quota package wasn't tied in | ||||||||||
| 75 | 0 | ▶ | if (!$Ok) {return undef;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | my $self=shift; | |||||||||
| 77 | |||||||||||
| 78 | #Scan the mount table now | ||||||||||
| 79 | 0 | MTab_scan(); | |||||||||
| 80 | |||||||||||
| 81 | 0 | return bless {},$self; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | sub EXISTS | ||||||||||
| 85 | 0 | ▶ | { | ||||||||
|
Subroutine
EXISTS: not called CC=4 SCAR=30.0
| |||||||||||
| 86 | 0 | my ($self, $path)=@_; | |||||||||
| 87 | 0 | ▶ | if (exists $by_path{$path}) {return 1;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | |||||||||||
| 89 | #Try to look up remotely things... | ||||||||||
| 90 | 0 | my $dev = Quota::getdev($path); | |||||||||
| 91 | |||||||||||
| 92 | #If it failed, it does not exist | ||||||||||
| 93 | 0 | ▶ | if (!defined $dev) {return 0;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | if (!exists $by_dev{$dev}) {$by_dev{$dev}=[$dev];} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 0 | return 1; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | sub FETCH | ||||||||||
| 101 | 0 | ▶ | { | ||||||||
|
Subroutine
FETCH: not called CC=4 SCAR=30.0
| |||||||||||
| 102 | 0 | my ($self,$path)=@_; | |||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | if (exists $by_path{$path}) {return $by_path{$path};} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | #Try to look up remotely things... | ||||||||||
| 107 | 0 | my $dev = Quota::getdev($path); | |||||||||
| 108 | |||||||||||
| 109 | #If it failed, it does not exist | ||||||||||
| 110 | 0 | ▶ | if (!defined $dev) {return undef;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | if (!exists $by_dev{$dev}) {$by_dev{$dev}=[$dev];} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | |||||||||||
| 114 | 0 | $by_dev{$dev}; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | sub MTab_scan() | ||||||||||
| 118 | 0 | ▶ | { | ||||||||
|
Subroutine
MTab_scan: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 119 | 0 | Quota::setmntent(); | |||||||||
| 120 | 0 | ▶ | while (my @A=Quota::getmntent()) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | { | ||||||||||
| 122 | 0 | $by_dev {$A[0]}=\@A; | |||||||||
| 123 | 0 | $by_path{$A[1]}=\@A; | |||||||||
| 124 | } | ||||||||||
| 125 | 0 | Quota::endmntent(); | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | package CfgTie::TieMTab_dev; | ||||||||||
| 129 | |||||||||||
| 130 | sub TIEHASH | ||||||||||
| 131 | 0 | ▶ | { | ||||||||
|
Subroutine
TIEHASH: not called CC=3 SCAR=24.8
| |||||||||||
| 132 | # Fail the tie has if the Quota package wasn't tied in | ||||||||||
| 133 | 0 | ▶ | if (!$Ok) {return undef;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | my $self=shift; | |||||||||
| 135 | |||||||||||
| 136 | #Scan the mount table now | ||||||||||
| 137 | 0 | ▶ | if (!scalar keys %CfgTie::TieMTab::by_dev) {MTab_scan();} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | |||||||||||
| 139 | 0 | return bless {},$self; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | sub EXISTS {exists $CfgTie::TieMTab::by_dev{$_[1]};} | ||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||
| 143 | 0 | ▶ | sub FETCH {$CfgTie::TieMTab::by_dev{$_[1]};} | ||||||||
|
Subroutine
FETCH: not called CC=1 SCAR=6.9
| |||||||||||