File Coverage

blib/lib/IO/AIO/Util.pm
Criterion Covered Total %
statement 42 42 100.0
branch 8 8 100.0
condition 10 13 76.9
subroutine 8 8 100.0
pod 1 1 100.0
total 69 72 95.8


line stmt bran cond sub pod time code
1             package IO::AIO::Util;
2              
3 2     2   60454 use strict;
  2         6  
  2         92  
4 2     2   11 use warnings;
  2         5  
  2         72  
5 2     2   10 use base qw(Exporter);
  2         15  
  2         233  
6              
7 2     2   1305 use IO::AIO 2;
  2         8323  
  2         597  
8 2         211 use File::Spec::Functions qw(
9             canonpath catpath catdir splitdir splitpath updir
10 2     2   2188 );
  2         2046  
11 2     2   2160 use POSIX ();
  2         18061  
  2         970  
12              
13             our $VERSION = '0.09';
14             $VERSION = eval $VERSION;
15              
16             our @EXPORT_OK = qw(aio_mkpath aio_mktree);
17              
18             sub aio_mkpath {
19 4     4 1 6477 my ($path, $mode, $cb) = @_;
20              
21 4         22 my $pri = aioreq_pri;
22 4         39 my $grp = aio_group $cb;
23              
24             # Default is success.
25 4         27 local $!;
26 4         19 $grp->result(0);
27              
28             # Clean up the path.
29 4         15 $path = canonpath($path);
30              
31 4         19 my ($vol, $dir, undef) = splitpath($path, 1);
32 4         46 my @dir = splitdir($dir);
33 4         35 my $idx = 0;
34              
35 4         4 my $sub; $sub = sub {
36 17     17   43 for (; $idx < @dir; $idx++) {
37             # Root and parent directories are assumed to always exist.
38 19 100 66     94 last if '' ne $dir[$idx] and updir ne $dir[$idx];
39             }
40 17 100       48 return $grp if @dir <= $idx;
41              
42 15         105 my $path = catpath($vol, catdir(@dir[0 .. $idx]), '');
43              
44 15         134 aioreq_pri $pri;
45             add $grp aio_mkdir $path, $mode, sub {
46 15 100 50     1444 $idx++ and return $sub->() unless $_[0];
47              
48             # Ignore "file exists" errors unless it is the last component,
49             # then stat it to ensure it is a directory. This matches
50             # the behaviour of `mkdir -p` from GNU coreutils.
51 13 100 50     264 $idx++ and return $sub->() if &POSIX::EEXIST == $!
      100        
      100        
52             and not ($idx == $#dir and not -d $path);
53              
54 2         13 $grp->cancel_subs;
55 2         11 $grp->errno($!);
56 2         17 $grp->result($_[0]);
57 2         13 return $grp;
58 15         750 };
59 4         21 };
60              
61 4         10 return $sub->();
62             }
63              
64             *aio_mktree = \&aio_mkpath;
65              
66              
67             1;
68              
69             __END__