Perl/getdents.pm

filter: Statementstmt 91.4% 43 / 47 Branchbran 60.0% 6 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 88.0% 59 / 67 CC 6 SCAR 12.2
CC
6
Cov
86%
getdents
6.6
BEGIN
1.0
BEGIN
1.0
CRAP 3.4
« Perl/SigSet.pm ↑ Summary Perl/ParseFlags.pm »
1 package Linux::Perl::getdents;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Linux:Perl::getdents - read full directory information
8  
9 =head1 SYNOPSIS
10  
11 #Platform-specific invocation uses e.g.:
12 # Linux::Perl::getdents::arm->getdents(...)
13  
14 my @entities = Linux::Perl::getdents->getdents( $filehandle, $buffer_size );
15  
16 =head1 DESCRIPTION
17  
18 This module provides support for the kernel-level logic to read directories.
19  
20 Directories store more than just the node names that Perl’s C<readdir()>
21 returns; for example, they store the file type and node number. By calling
22 the kernel’s C<getdents> logic directly, you can get this
23 information without making additional system calls.
24  
25 =cut
26  
27 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 ▶use parent qw( Linux::Perl::Base::BitsTest );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 ▶use Linux::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use Linux::Perl::Endian;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use Linux::Perl::EasyPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 use constant {
37 3 DT_UNKNOWN => 0,
38 DT_FIFO => 1,
39 DT_CHR => 2,
40 DT_DIR => 4,
41 DT_BLK => 6,
42 DT_REG => 8,
43 DT_LNK => 10,
44 DT_SOCK => 12,
45 DT_WHT => 14,
46 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 3 my ($lde64_keys, $lde64_pack, $lde64_start_size);
49 BEGIN {
50 3 ▶ ($lde64_keys, $lde64_pack) = Linux::Perl::EasyPack::split_pack_list(
Subroutine BEGIN: called CC=1 SCAR=0.0
51 ino => __PACKAGE__->_PACK_u64(), #ino64_t
52 off => __PACKAGE__->_PACK_u64(), #off64_t
53 reclen => 'S!',
54 type => 'C',
55 #name => 'a*',
56 );
57  
58 3 $lde64_start_size = length pack $lde64_pack;
59 }
60  
61 =head1 METHODS
62  
63 In addition to the following, this module exposes the constants
64 C<DT_UNKNOWN()> et al. (cf. C<man 2 getdents>)
65  
66 =head2 @ENTRIES = I<CLASS>->getdents( $FILEHANDLE_OR_FD, $READ_SIZE )
67  
68 Reads from the given $FILEHANDLE_OR_FD using a buffer of $READ_SIZE bytes.
69 There’s no good way to know how many @ENTRIES you can receive given the
70 $READ_SIZE, unfortunately.
71  
72 The return is a list of hash references; each hash contains the keys
73 C<ino>, C<off>, C<type>, and C<name>. These correspond with the relevant
74 parts of struct C<linux_dirent64> (cf. C<man 2 getdents>).
75  
76 (In scalar context, this returns the number of hash references that would
77 be returned in list context.)
78  
79 For now, this is implemented via the C<getdents64> system call.
80  
81 B<NOTE:> Perl 5.20 and earlier doesn’t understand C<fileno()> on a directory
82 handle, so to use this function you’ll need to pass the file descriptor rather
83 than the handle. (To get the file descriptor, you can parse F</proc/$$/fd>
84 for the symlink that refers to the directory’s path. See this module’s tests
85 for an implementation of this.)
86  
87 =cut
88  
89 sub getdents {
90 2 ▶ my ($class, $fh_or_fileno, $bufsize) = @_;
Subroutine getdents: called CC=6 SCAR=18.8
Pod documented
91  
92 2 ▶ if (!$class->can('NR_getdents64')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("NR_getdents64")) 1 1 2
93 1 require Linux::Perl::ArchLoader;
94 1 $class = Linux::Perl::ArchLoader::get_arch_module($class);
95 }
96  
97 2 my $buf = "\0" x $bufsize;
98  
99 2 my $fileno;
100  
101 2 ▶ if ( ref $fh_or_fileno ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh_or_fileno) { } 2 0 2
102 2 $fileno = fileno($fh_or_fileno);
103  
104 2 ▶ if (!defined $fileno) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fileno) 0 2 2
105 0 die "Filehandle ($fh_or_fileno) has no underlying file descriptor!";
106 }
107 }
108 else {
109 0 $fileno = $fh_or_fileno;
110  
111 0 ▶ if (!defined $fileno) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fileno) 0 0 0
112 0 die "Neither a filehandle nor a file descriptor was given!";
113 }
114 }
115  
116 2 my $bytes = Linux::Perl::call(
117 0 + $class->NR_getdents64(),
118 0 + $fileno,
119 $buf,
120 0 + $bufsize,
121 );
122  
123 2 my @structs;
124 2 ▶ while ($bytes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes > 0) 10 2 12
125 10 my %struct;
126 10 @struct{ @$lde64_keys } = unpack $lde64_pack, substr( $buf, 0, $lde64_start_size, q<> );
127  
128 10 ( $struct{'name'} = substr( $buf, 0, $struct{'reclen'} - $lde64_start_size, q<> ) ) =~ tr<\0><>d;
129  
130 10 push @structs, \%struct;
131  
132 10 $bytes -= delete $struct{'reclen'};
133 }
134  
135 2 return @structs;
136 }
137  
138 3 1;
« Perl/SigSet.pm ↑ Summary Perl/ParseFlags.pm »