| 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::Userland; | ||||||||||||||||||||||||||||
| 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::Module (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 25 | ▶ | use Filesys::POSIX::Path (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 25 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 25 | my @METHODS = qw( | |||||||||||||||||||||||||||
| 20 | _find_inode_path mkpath getcwd realpath opendir readdir closedir touch | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 25 | Filesys::POSIX::Module->export_methods( __PACKAGE__, @METHODS ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Filesys::POSIX::Userland - Provide implementations for higher-level, "userland" | ||||||||||||||||||||||||||||
| 28 | functionality in L<Filesys::POSIX> | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This module is a mixin imported by L<Filesys::POSIX> into its own namespace, and | ||||||||||||||||||||||||||||
| 33 | provides a variety of higher-level calls to supplement the normal suite of | ||||||||||||||||||||||||||||
| 34 | system calls provided in L<Filesys::POSIX> itself. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =over | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 23 | ▶ | sub _find_inode_path { | ||||||||||||||||||||||||||
|
Subroutine
_find_inode_path: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 43 | 23 | my ( $self, $start ) = @_; | |||||||||||||||||||||||||||
| 44 | 23 | my $inode = $self->{'vfs'}->vnode($start); | |||||||||||||||||||||||||||
| 45 | 23 | my @ret; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 23 | ▶ | while ( my $dir = $self->{'vfs'}->vnode($inode)->{'parent'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 41 | my $directory = $dir->directory; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 41 | foreach my $item ( $directory->list ) { | |||||||||||||||||||||||||||
| 51 | 187 | ▶ | next if $item eq '.' || $item eq '..'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$item eq "." or $item eq ".."
MC/DC100%2/2
$item eq "." or $item eq ".."
$item eq "."
$item eq ".."
Truth table100%3/3
A or B
A= $item eq "."B= $item eq ".."
| |||||||||||||||||||||||||||||
| 52 | next | ||||||||||||||||||||||||||||
| 53 | 105 | ▶ | unless $self->{'vfs'}->vnode( $directory->get($item) ) == $self->{'vfs'}->vnode($inode); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 41 | push @ret, $item; | |||||||||||||||||||||||||||
| 56 | 41 | $inode = $dir; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 23 | return '/' . join( '/', reverse @ret ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item C<$fs-E<gt>mkpath($path)> | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item C<$fs-E<gt>mkpath($path, $mode)> | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Similar to the C<-p> flag that can be passed to L<mkdir(1)>, this | ||||||||||||||||||||||||||||
| 68 | method attempts to create a hierarchy of directories specified in C<$path>. | ||||||||||||||||||||||||||||
| 69 | Each path component created will be made with the mode specified by C<$mode>, if | ||||||||||||||||||||||||||||
| 70 | any, if a directory in that location does not already exist. Exceptions will be | ||||||||||||||||||||||||||||
| 71 | thrown if one of the items along the path hierarchy exists but is not a | ||||||||||||||||||||||||||||
| 72 | directory. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | A default mode of 0777 is assumed; only the permissions field of C<$mode> is | ||||||||||||||||||||||||||||
| 75 | used when it is specified. In both cases, the mode specified is modified with | ||||||||||||||||||||||||||||
| 76 | exclusive OR by the current umask value. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 20 | ▶ | sub mkpath { | ||||||||||||||||||||||||||
|
Subroutine
mkpath: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 20 | my ( $self, $path, $mode ) = @_; | |||||||||||||||||||||||||||
| 82 | 20 | ▶ | my $perm = $mode ? $mode & ( $S_IPERM | $S_IPROT ) : $S_IPERM ^ $self->{'umask'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 20 | my $hier = Filesys::POSIX::Path->new($path); | |||||||||||||||||||||||||||
| 84 | 20 | my $dir = $self->{'cwd'}; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 20 | ▶ | while ( $hier->count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 95 | my $item = $hier->shift; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 95 | ▶ | unless ($item) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 11 | $dir = $self->{'root'}; | |||||||||||||||||||||||||||
| 91 | 11 | next; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 84 | my $directory = $dir->directory; | |||||||||||||||||||||||||||
| 95 | 84 | my $inode = $self->{'vfs'}->vnode( $directory->get($item) ); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 84 | ▶ | if ($inode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 11 | $dir = $inode; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||||||||||
| 101 | 73 | $dir = $dir->child( $item, $perm | $S_IFDIR ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 20 | return $dir; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item C<$fs-E<gt>getcwd> | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Returns a string representation of the current working directory. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 7 | ▶ | sub getcwd { | ||||||||||||||||||||||||||
|
Subroutine
getcwd: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 7 | return $self->_find_inode_path( $self->{'cwd'} ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =item C<$fs-E<gt>realpath($path)> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Returns a string representation of the full, true and original path of the | ||||||||||||||||||||||||||||
| 123 | inode specified by C<$path>. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Using C<$fs-E<gt>stat>, the inode of C<$path> is resolved, then starting at that | ||||||||||||||||||||||||||||
| 126 | inode, each subsequent inode's name is found from its parent and appended to a | ||||||||||||||||||||||||||||
| 127 | list of path components. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | ▶ | sub realpath { | ||||||||||||||||||||||||||
|
Subroutine
realpath: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 2 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||
| 133 | 2 | my $inode = $self->stat($path); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | return $self->_find_inode_path($inode); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item C<$fs-E<gt>opendir($path)> | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Returns a newly opened directory handle for the item pointed to by C<$path>. | ||||||||||||||||||||||||||||
| 141 | Using other methods in this module, the directory can be read and closed. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 4 | ▶ | sub opendir { | ||||||||||||||||||||||||||
|
Subroutine
opendir: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 4 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 4 | my $directory = $self->stat($path)->directory; | |||||||||||||||||||||||||||
| 149 | 4 | $directory->open; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 4 | return $directory; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =item C<$fs-E<gt>readdir($directory)> | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Read the next member of the directory passed. Returns undef if there are no | ||||||||||||||||||||||||||||
| 157 | more entries to be read. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 14 | ▶ | sub readdir { | ||||||||||||||||||||||||||
|
Subroutine
readdir: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 14 | my ( $self, $directory ) = @_; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 14 | ▶ | return $directory->read unless wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | my @ret; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 2 | ▶ | while ( defined( my $item = $directory->read ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 10 | push @ret, $item; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 2 | return @ret; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =item C<$fs-E<gt>closedir($directory)> | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Closes the directory handle for reading. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 4 | ▶ | sub closedir { | ||||||||||||||||||||||||||
|
Subroutine
closedir: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | 4 | my ( $self, $directory ) = @_; | |||||||||||||||||||||||||||
| 183 | 4 | return $directory->close; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =item C<$fs-E<gt>touch($path)> | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Acts like the userland utility L<touch()|perlfunc/touch>. Uses C<$fs-E<gt>open> | ||||||||||||||||||||||||||||
| 189 | with the C<$O_CREAT> flag to open the file specified by C<$path>, and | ||||||||||||||||||||||||||||
| 190 | immediately closes the file descriptor returned. This causes an update of the | ||||||||||||||||||||||||||||
| 191 | inode modification time for existing files, and the creation of new, empty files | ||||||||||||||||||||||||||||
| 192 | otherwise. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 27 | ▶ | sub touch { | ||||||||||||||||||||||||||
|
Subroutine
touch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 197 | 27 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||
| 198 | 27 | my $fd = $self->open( $path, $O_CREAT ); | |||||||||||||||||||||||||||
| 199 | 27 | my $inode = $self->fstat($fd); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 27 | $self->close($fd); | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 27 | return $inode; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =back | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 25 | 1; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||