Perl/getdents.pm

filter: Statementstmt 91.1% 41 / 45 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 87.3% 55 / 63 CC 5 SCAR 10.2
CC
5
Cov
85%
getdents
5.6
BEGIN
1.0
BEGIN
1.0
CRAP 2.8
« Perl/EasyPack.pm ↑ Summary Perl/Base/BitsTest.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(
Subroutine BEGIN: called CC=1 SCAR=0.0
31 Linux::Perl::Base
32 Linux::Perl::Base::BitsTest
33 3 );
34  
35 3 ▶use Linux::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use Linux::Perl::Endian;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use Linux::Perl::EasyPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 DT_UNKNOWN => 0,
41 DT_FIFO => 1,
42 DT_CHR => 2,
43 DT_DIR => 4,
44 DT_BLK => 6,
45 DT_REG => 8,
46 DT_LNK => 10,
47 DT_SOCK => 12,
48 DT_WHT => 14,
49 3 };
50  
51 3 my ($lde64_keys, $lde64_pack, $lde64_start_size);
52 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 ($lde64_keys, $lde64_pack) = Linux::Perl::EasyPack::split_pack_list(
54 ino => __PACKAGE__->_PACK_u64(), #ino64_t
55 off => __PACKAGE__->_PACK_u64(), #off64_t
56 reclen => 'S!',
57 type => 'C',
58 #name => 'a*',
59 );
60  
61 3 $lde64_start_size = length pack $lde64_pack;
62 }
63  
64 =head1 METHODS
65  
66 In addition to the following, this module exposes the constants
67 C<DT_UNKNOWN()> et al. (cf. C<man 2 getdents>)
68  
69 =head2 @ENTRIES = I<CLASS>->getdents( $FILEHANDLE_OR_FD, $READ_SIZE )
70  
71 Reads from the given $FILEHANDLE_OR_FD using a buffer of $READ_SIZE bytes.
72 There’s no good way to know how many @ENTRIES you can receive given the
73 $READ_SIZE, unfortunately.
74  
75 The return is a list of hash references; each hash contains the keys
76 C<ino>, C<off>, C<type>, and C<name>. These correspond with the relevant
77 parts of struct C<linux_dirent64> (cf. C<man 2 getdents>).
78  
79 (In scalar context, this returns the number of hash references that would
80 be returned in list context.)
81  
82 For now, this is implemented via the C<getdents64> system call.
83  
84 B<NOTE:> Perl 5.20 and earlier doesn’t understand C<fileno()> on a directory
85 handle, so to use this function you’ll need to pass the file descriptor rather
86 than the handle. (To get the file descriptor, you can parse F</proc/$$/fd>
87 for the symlink that refers to the directory’s path. See this module’s tests
88 for an implementation of this.)
89  
90 =cut
91  
92 2 ▶sub getdents {
Subroutine getdents: called CC=5 SCAR=17.2
Pod documented
93 2 my ($class, $fh_or_fileno, $bufsize) = @_;
94  
95 2 $class = $class->_get_arch_module();
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/EasyPack.pm ↑ Summary Perl/Base/BitsTest.pm »