| 1 | # Copyright (c) 2014, cPanel, Inc. | ||||||||||||
| 2 | # All rights reserved. | ||||||||||||
| 3 | # http://cpanel.net/ | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # This is free software; you can redistribute it and/or modify it under the same | ||||||||||||
| 6 | # terms as Perl itself. See the LICENSE file for further details. | ||||||||||||
| 7 | |||||||||||||
| 8 | package Filesys::POSIX::VFS; | ||||||||||||
| 9 | |||||||||||||
| 10 | 25 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 25 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 25 | ▶ | use Filesys::POSIX::Bits; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 25 | ▶ | use Filesys::POSIX::Path (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 25 | ▶ | use Filesys::POSIX::VFS::Inode (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 25 | ▶ | use Filesys::POSIX::Error qw(throw); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | 39 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 20 | 39 | return bless { | |||||||||||
| 21 | 'mounts' => [], | ||||||||||||
| 22 | 'devices' => {}, | ||||||||||||
| 23 | 'vnodes' => {} | ||||||||||||
| 24 | }, | ||||||||||||
| 25 | shift; | ||||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 17 | ▶ | sub statfs { | ||||||||||
|
Subroutine
statfs: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||
| 29 | 17 | my ( $self, $start, %opts ) = @_; | |||||||||||
| 30 | 17 | my $inode = $start; | |||||||||||
| 31 | 17 | my $ret; | |||||||||||
| 32 | |||||||||||||
| 33 | 17 | ▶ | while ( $inode->{'vnode'} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 0 | $inode = $inode->{'vnode'}; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 17 | ▶ | if ( $opts{'exact'} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 11 | $ret = $self->{'vnodes'}->{$inode}; | |||||||||||
| 39 | } | ||||||||||||
| 40 | else { | ||||||||||||
| 41 | 6 | $ret = $self->{'devices'}->{ $inode->{'dev'} }; | |||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | 17 | ▶ | unless ($ret) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 4 | ▶ | throw &Errno::ENXIO unless $opts{'silent'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 16 | return $ret; | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | 1 | ▶ | sub mountlist { | ||||||||||
|
Subroutine
mountlist: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 52 | 1 | my ($self) = @_; | |||||||||||
| 53 | 1 | return @{ $self->{'mounts'} }; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | # | ||||||||||||
| 57 | # It should be noted that any usage of pathnames in this module are entirely | ||||||||||||
| 58 | # symbolic and are not used for canonical purposes. The higher-level | ||||||||||||
| 59 | # filesystem layer should take on the responsibility of providing both the | ||||||||||||
| 60 | # canonically-correct absolute pathnames for mount points, and helping locate | ||||||||||||
| 61 | # the appropriate VFS mount point for querying purposes. | ||||||||||||
| 62 | # | ||||||||||||
| 63 | 51 | ▶ | sub mount { | ||||||||||
|
Subroutine
mount: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 64 | 51 | my ( $self, $dev, $path, $mountpoint, %data ) = @_; | |||||||||||
| 65 | |||||||||||||
| 66 | 51 | ▶ | if ( grep { $_->{'dev'} eq $dev } @{ $self->{'mounts'} } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 2 | throw &Errno::EBUSY; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 49 | ▶ | $data{'special'} ||= scalar $dev; | ||||||||||
|
Condition50%1/2
$data{"special"} ||= scalar $dev
MC/DC0%0/1
$data{"special"} ||= scalar $dev
$data{"special"}
Truth table50%1/2
A
A= $data{"special"}
| |||||||||||||
| 71 | |||||||||||||
| 72 | # | ||||||||||||
| 73 | # Generate a generic BSD-style filesystem type string. | ||||||||||||
| 74 | # | ||||||||||||
| 75 | 49 | my $type = lc ref $dev; | |||||||||||
| 76 | 49 | $type =~ s/^([a-z_][a-z0-9_]*::)*//; | |||||||||||
| 77 | |||||||||||||
| 78 | # | ||||||||||||
| 79 | # Create a vnode record munged from the mountpoint and new | ||||||||||||
| 80 | # filesystem root. | ||||||||||||
| 81 | # | ||||||||||||
| 82 | 49 | my $vnode = Filesys::POSIX::VFS::Inode->new( $mountpoint, $dev->{'root'} ); | |||||||||||
| 83 | |||||||||||||
| 84 | # | ||||||||||||
| 85 | # Associate the mountpoint and filesystem roots with this vnode. | ||||||||||||
| 86 | # | ||||||||||||
| 87 | 49 | $mountpoint->{'vnode'} = $vnode; | |||||||||||
| 88 | 49 | $dev->{'root'}->{'vnode'} = $vnode; | |||||||||||
| 89 | |||||||||||||
| 90 | # | ||||||||||||
| 91 | # Generate the mount record. | ||||||||||||
| 92 | # | ||||||||||||
| 93 | my $mount = { | ||||||||||||
| 94 | 'mountpoint' => $mountpoint, | ||||||||||||
| 95 | 'root' => $dev->{'root'}, | ||||||||||||
| 96 | 'special' => $data{'special'}, | ||||||||||||
| 97 | 'dev' => $dev, | ||||||||||||
| 98 | 'type' => $type, | ||||||||||||
| 99 | 'path' => $path, | ||||||||||||
| 100 | 'vnode' => $vnode, | ||||||||||||
| 101 | |||||||||||||
| 102 | 49 | 'flags' => { map { $_ => $data{$_} } grep { $_ ne 'special' } keys %data } | |||||||||||
| 103 | }; | ||||||||||||
| 104 | |||||||||||||
| 105 | # | ||||||||||||
| 106 | # Store the mount record in the ordered mount list. | ||||||||||||
| 107 | # | ||||||||||||
| 108 | 49 | push @{ $self->{'mounts'} }, $mount; | |||||||||||
| 109 | |||||||||||||
| 110 | # | ||||||||||||
| 111 | # Associate the vnode with the mount rcord. | ||||||||||||
| 112 | # | ||||||||||||
| 113 | 49 | $self->{'vnodes'}->{$vnode} = $mount; | |||||||||||
| 114 | |||||||||||||
| 115 | # | ||||||||||||
| 116 | # Finally, associate the filesystem with the mount record. | ||||||||||||
| 117 | # | ||||||||||||
| 118 | 49 | $self->{'devices'}->{$dev} = $mount; | |||||||||||
| 119 | |||||||||||||
| 120 | 49 | return $self; | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 1830 | ▶ | sub vnode { | ||||||||||
|
Subroutine
vnode: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||
| 124 | 1830 | my ( $self, $start ) = @_; | |||||||||||
| 125 | 1830 | my $inode = $start; | |||||||||||
| 126 | |||||||||||||
| 127 | 1830 | ▶ | return undef unless $inode; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | |||||||||||||
| 129 | 1736 | ▶ | while ( $inode->{'vnode'} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | 217 | $inode = $inode->{'vnode'}; | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | 1736 | my $mount = $self->{'devices'}->{ $inode->{'dev'} }; | |||||||||||
| 134 | |||||||||||||
| 135 | 1736 | ▶ | if ( $mount->{'flags'}->{'noexec'} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | 9 | $inode->{'mode'} &= ~$S_IX; | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 1736 | ▶ | if ( $mount->{'flags'}->{'nosuid'} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 140 | 9 | $inode->{'mode'} &= ~$S_ISUID; | |||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | 1736 | foreach (qw(uid gid)) { | |||||||||||
| 144 | 3472 | ▶ | if ( defined $mount->{'flags'}->{$_} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | 18 | $inode->{$_} = $mount->{'flags'}->{$_}; | |||||||||||
| 146 | } | ||||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | 1736 | return $inode; | |||||||||||
| 150 | } | ||||||||||||
| 151 | |||||||||||||
| 152 | 4 | ▶ | sub unmount { | ||||||||||
|
Subroutine
unmount: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 153 | 4 | my ( $self, $mount ) = @_; | |||||||||||
| 154 | |||||||||||||
| 155 | # | ||||||||||||
| 156 | # First, check to see that the filesystem mount record found is a | ||||||||||||
| 157 | # dependency for another mounted filesystem. | ||||||||||||
| 158 | # | ||||||||||||
| 159 | 4 | foreach ( @{ $self->{'mounts'} } ) { | |||||||||||
| 160 | 12 | ▶ | next if $_ == $mount; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 161 | |||||||||||||
| 162 | 8 | ▶ | throw &Errno::EBUSY if $_->{'mountpoint'}->{'dev'} == $mount->{'dev'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | # | ||||||||||||
| 166 | # Pluck the filesystem from the mount list. | ||||||||||||
| 167 | # | ||||||||||||
| 168 | 3 | ▶ | for ( my $i = 0; $self->{'mounts'}->[$i]; $i++ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 169 | 9 | ▶ | next unless $self->{'mounts'}->[$i] eq $mount; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 170 | 3 | splice @{ $self->{'mounts'} }, $i; | |||||||||||
| 171 | 3 | last; | |||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | # | ||||||||||||
| 175 | # Untie the vnode reference from its original mount point and root. | ||||||||||||
| 176 | # | ||||||||||||
| 177 | 3 | delete $mount->{'mountpoint'}->{'vnode'}; | |||||||||||
| 178 | 3 | delete $mount->{'root'}->{'vnode'}; | |||||||||||
| 179 | |||||||||||||
| 180 | # | ||||||||||||
| 181 | # Break references to the mount record from the per-vnode hash. | ||||||||||||
| 182 | # | ||||||||||||
| 183 | 3 | delete $self->{'vnodes'}->{ $mount->{'vnode'} }; | |||||||||||
| 184 | |||||||||||||
| 185 | # | ||||||||||||
| 186 | # Kill references to the mount record from the per-device hash. | ||||||||||||
| 187 | # | ||||||||||||
| 188 | 3 | delete $self->{'devices'}->{ $mount->{'dev'} }; | |||||||||||
| 189 | |||||||||||||
| 190 | 3 | return $self; | |||||||||||
| 191 | } | ||||||||||||
| 192 | |||||||||||||
| 193 | 25 | 1; | |||||||||||