POSIX/IO.pm

filter: Statementstmt 100% 70 / 70 Branchbran 100% 22 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod 100% 9 / 9 totaltotal 100% 117 / 117 CC 12 SCAR 12.2
CC
12
Cov
100%
open
8.0
read
2.0
write
2.0
CRAP 3.4
« POSIX/Snapshot/Inode.pm ↑ Summary POSIX/Userland.pm »
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::IO;
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::FdTable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 25 ▶use Filesys::POSIX::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 25 ▶use Filesys::POSIX::Error qw(throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 25 my @MODULES = qw(open read write print printf tell seek close fdopen);
20  
21 25 Filesys::POSIX::Module->export_methods( __PACKAGE__, @MODULES );
22  
23 =head1 NAME
24  
25 Filesys::POSIX::IO - Provides file I/O calls for L<Filesys::POSIX>
26  
27 =head1 DESCRIPTION
28  
29 C<Filesys::POSIX::IO> is a mixin imported into the Filesys::POSIX namespace by
30 the L<Filesys::POSIX> module itself. This module provides the standard file I/O
31 routines.
32  
33 =over
34  
35 =item C<$fs-E<gt>open($path, $flags)>
36  
37 =item C<$fs-E<gt>open($path, $flags, $mode)>
38  
39 Open a file descriptor for an inode specified by $path. This operation can be
40 modified by usage of the following flags which can be specified together using
41 logical OR (|). The flags as follows are exported by L<Filesys::POSIX::Bits>:
42  
43 =over
44  
45 =item C<$O_CREAT>
46  
47 If an inode at the specified path does not exist, attempt to create one.
48  
49 When a mode is specified, the value is split into the format (C<$S_IFMT>),
50 permission (C<$S_IPERM>), and protection (C<$S_IPROT>) bitfields. If no
51 value was specified for the format, then the default value of C<$S_IFREG>
52 (regular file) is substituted.
53  
54 When no mode is specified whatsoever, the default values of an C<$S_IFREG>
55 format, and a mode of 0666 are used, modified by the current umask value.
56  
57 In either case, the permissions to be used are modified with an exclusive OR
58 operation by the current umask value.
59  
60 =item C<$O_EXCL>
61  
62 When specified in the presence of C<$O_CREAT>, the call will only succeed when
63 the path lists a nonexisting inode. A "File exists" exception will be thrown if
64 this is not the case.
65  
66 =item C<$O_TRUNC>
67  
68 When specified, any existing file data will be truncated, and the file handle
69 position will start at offset 0 (zero).
70  
71 =item C<$O_APPEND>
72  
73 When specified, the file handle position will start at the offset value equal
74 to the size of the file.
75  
76 =item C<$O_RDONLY>
77  
78 The default flag field value. When neither C<$O_WRONLY> nor C<$O_RDWR> are
79 specified, any write operations will be prohibited on the newly issued file
80 descriptor.
81  
82 =item C<$O_WRONLY>
83  
84 When specified, any read operations will be prohibited on the newly issued file
85 descriptor.
86  
87 =item C<$O_RDWR>
88  
89 When specified, both read and write operations will be allowed on the newly
90 issued file descriptor.
91  
92 =back
93  
94 The following exceptions may be thrown.
95  
96 =over
97  
98 =item * EINVAL (Invalid argument)
99  
100 No flags were specified in I<$flags>.
101  
102 =item * EEXIST (File exists)
103  
104 When the C<$O_CREAT> flag is passed, this error may occur if a file located at
105 I<$path> already exists.
106  
107 =back
108  
109 =cut
110  
111 78 ▶sub open {
Subroutine open: called CC=8 SCAR=20.8
Pod documented
112 78 my ( $self, $path, $flags, $mode ) = @_;
113 78 my $hier = Filesys::POSIX::Path->new($path);
114 78 my $name = $hier->basename;
115 78 my $inode;
116  
117 78 ▶ throw &Errno::EINVAL unless defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 1 77 78
118  
119 77 ▶ if ( $flags & $O_CREAT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & $Filesys::POSIX::IO::O_CREAT) { } 72 5 77
120 72 my $parent = $self->stat( $hier->dirname );
121 72 my $directory = $parent->directory;
122  
123 71 ▶ if ( $inode = $directory->get($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inode = $directory->get($name)) { } 7 64 71
124 7 ▶ throw &Errno::EEXIST if $flags & $O_EXCL;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & $Filesys::POSIX::IO::O_EXCL 1 6 7
125 }
126 else {
127 64 ▶ my $format =
Branch: 2 branches
BranchTrueFalseTotal
$mode ? : 13 51 64
$mode & $Filesys::POSIX::IO::S_IFMT ? : 2 11 13
128 $mode
129 ? ( $mode & $S_IFMT ? $mode & $S_IFMT : $S_IFREG )
130 : $S_IFREG;
131 64 ▶ my $perms = $mode ? $mode & ( $S_IPERM | $S_IPROT ) : $S_IRW;
Branch: 1 branch
BranchTrueFalseTotal
$mode ? : 13 51 64
132  
133 64 $perms &= ~$self->{'umask'};
134  
135 64 $inode = $parent->child( $name, $format | $perms );
136 }
137 }
138 else {
139 5 $inode = $self->stat($path);
140 }
141  
142 75 return $self->{'fds'}->open( $inode, $flags );
143 }
144  
145 =item C<$fs-E<gt>read($fd, $buf, $len)>
146  
147 Perform a read on the file descriptor passed, storing at maximum the number of
148 bytes specified in C<$len>, into C<$buf>. Returns the number of bytes actually
149 read; fewer bytes may be read than requested if the expected amount of data from
150 the current file handle position, plus the requested length, does not
151 match the requested length, such as when the length exceeds the end of the file
152 stream. Returns zero if no more data is available to be read.
153  
154 Exceptions are thrown for the following:
155  
156 =over
157  
158 =item * EINVAL (Invalid argument)
159  
160 A read was attempted on a write-only file descriptor.
161  
162 =back
163  
164 =cut
165  
166 5 ▶sub read {
Subroutine read: called CC=2 SCAR=6.9
Pod documented
167 5 my $self = shift;
168 5 my $fd = shift;
169 5 my $entry = $self->{'fds'}->lookup($fd);
170  
171 4 ▶ throw &Errno::EBADF if $entry->{'flags'} & $O_WRONLY;
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"flags"} & $Filesys::POSIX::IO::O_WRONLY 1 3 4
172  
173 3 return $entry->{'handle'}->read(@_);
174 }
175  
176 =item C<$fs-E<gt>write($fd, $buf, $len)>
177  
178 Perform a write on the file descriptor passed, writing at maximum the number of
179 bytes specified in C<$len> from C<$buf> to the open file. Returns the number of
180 bytes actually written; fewer bytes may be written than requested if the buffer
181 does not contain enough, or if the underlying file handle implementation was
182 not able to write the full amount in the case of a L<Filesys::POSIX::IO::Handle>
183 object issued for an open L<Filesys::POSIX::Real::Inode> object.
184  
185 The following exceptions may be thrown:
186  
187 =over
188  
189 =item * EINVAL (Invalid argument)
190  
191 A write was attempted on a read-only file descriptor.
192  
193 =back
194  
195 =cut
196  
197 524 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
Pod documented
198 524 my ( $self, $fd, $buf, $len ) = @_;
199 524 my $entry = $self->{'fds'}->lookup($fd);
200  
201 524 ▶ throw &Errno::EINVAL unless $entry->{'flags'} & ( $O_WRONLY | $O_RDWR );
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"flags"} & ($Filesys::POSIX::IO::O_WRONLY | $Filesys::POSIX::IO::O_RDWR) 1 523 524
202  
203 523 return $entry->{'handle'}->write( $buf, $len );
204 }
205  
206 =item C<$fs-E<gt>print($fd, @args)>
207  
208 Works similarly to C<$fs-E<gt>write>. Each argument is concatenated using the
209 current value of C<$/> (see L<perlvar>), and passed with the amalgamated value's
210 length to the underlying file handle's C<$handle-E<gt>write> call.
211  
212 Exceptions may be thrown for the following:
213  
214 =over
215  
216 =item * EINVAL (Invalid argument)
217  
218 Issued when called on a read-only file descriptor.
219  
220 =back
221  
222 =cut
223  
224 9 ▶sub print {
Subroutine print: called CC=2 SCAR=6.9
Pod documented
225 9 my ( $self, $fd, @args ) = @_;
226 9 my $entry = $self->{'fds'}->lookup($fd);
227  
228 9 ▶ throw &Errno::EINVAL unless $entry->{'flags'} & ( $O_WRONLY | $O_RDWR );
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"flags"} & ($Filesys::POSIX::IO::O_WRONLY | $Filesys::POSIX::IO::O_RDWR) 1 8 9
229  
230 8 my $buf = join( $/, @args );
231  
232 8 return $entry->{'handle'}->write( $buf, length $buf );
233 }
234  
235 =item C<$fs-E<gt>printf($fd, $format, @args)>
236  
237 Similar to C<$fs-E<gt>print>, this call allows writes formatted by
238 L<sprintf()|perlfunc/sprintf> to be made to the given file descriptor.
239  
240 Exceptions are thrown for:
241  
242 =over
243  
244 =item * EINVAL (Invalid argument)
245  
246 Issued when called on a read-only file descriptor.
247  
248 =back
249  
250 =cut
251  
252 2 ▶sub printf {
Subroutine printf: called CC=2 SCAR=6.9
Pod documented
253 2 my ( $self, $fd, $format, @args ) = @_;
254 2 my $entry = $self->{'fds'}->lookup($fd);
255  
256 2 ▶ throw &Errno::EINVAL unless $entry->{'flags'} & ( $O_WRONLY | $O_RDWR );
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"flags"} & ($Filesys::POSIX::IO::O_WRONLY | $Filesys::POSIX::IO::O_RDWR) 1 1 2
257  
258 1 my $buf = sprintf( $format, @args );
259  
260 1 return $entry->{'handle'}->write( $buf, length $buf );
261 }
262  
263 =item C<$fs-E<gt>tell($fd)>
264  
265 Returns the byte offset of the file descriptor's file handle.
266  
267 =cut
268  
269 4 ▶sub tell {
Subroutine tell: called CC=1 SCAR=0.0
Pod documented
270 4 my ( $self, $fd ) = @_;
271 4 my $entry = $self->{'fds'}->lookup($fd);
272  
273 4 return $entry->{'handle'}->tell;
274 }
275  
276 =item C<$fs-E<gt>seek($fd, $pos, $whence)>
277  
278 Sets the byte offset of the file descriptor's file handle, relative to the
279 current offset as modified by the value specified in $whence. $whence can be
280 used to specify how the new position will be set relative to the current offset
281 with the following values (in L<Filesys::POSIX::Bits>):
282  
283 =over
284  
285 =item C<$SEEK_SET>
286  
287 The new offset of the file handle will be set to C<0 + $pos> bytes, or, relative
288 to the beginning of the file. This sets the file handle to an absolute offset.
289  
290 =item C<$SEEK_CUR>
291  
292 The new offset of the file handle will be set to C<$cur + $pos> bytes, or,
293 relative to the current file handle offset.
294  
295 =item C<$SEEK_END>
296  
297 The new offset of the file will be set to C<$size + $pos> bytes, or, relative to
298 the end of the file.
299  
300 =back
301  
302 =cut
303  
304 4 ▶sub seek {
Subroutine seek: called CC=1 SCAR=0.0
Pod documented
305 4 my ( $self, $fd, $pos, $whence ) = @_;
306 4 my $entry = $self->{'fds'}->lookup($fd);
307  
308 4 return $entry->{'handle'}->seek( $pos, $whence );
309 }
310  
311 =item C<$fs-E<gt>close($fd)>
312  
313 Close the file handle issued for the given file descriptor, and deallocate said
314 file descriptor. The file descriptor will then be freed for subsequent use and
315 issue by C<$fs-E<gt>open>.
316  
317 =cut
318  
319 66 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
320 66 my ( $self, $fd ) = @_;
321 66 $self->{'fds'}->close($fd);
322 }
323  
324 =item C<$fs-E<gt>fdopen($fd)>
325  
326 Returns the underlying file handle opened for the file descriptor passed.
327  
328 =cut
329  
330 1 ▶sub fdopen {
Subroutine fdopen: called CC=1 SCAR=0.0
Pod documented
331 1 my ( $self, $fd ) = @_;
332 1 my $entry = $self->{'fds'}->lookup($fd);
333  
334 1 return $entry->{'handle'};
335 }
336  
337 =back
338  
339 =cut
340  
341 25 1;
342  
343 __END__
« POSIX/Snapshot/Inode.pm ↑ Summary POSIX/Userland.pm »