File Coverage

blib/lib/DataCube/FileUtils.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1              
2              
3              
4             package DataCube::FileUtils;
5              
6 1     1   7 use strict;
  1         2  
  1         38  
7 1     1   5 use warnings;
  1         2  
  1         26  
8              
9 1     1   5 use Fcntl;
  1         2  
  1         394  
10 1     1   82 use DataCube;
  0            
  0            
11             use DataCube::Schema;
12             use DataCube::MeasureUpdater;
13              
14             sub new {
15             my($class,%opts) = @_;
16             bless {%opts}, ref($class) || $class;
17             }
18              
19             sub dir {
20             my($self,$path) = @_;
21             opendir(my $D, $path) or die "DataCube::FileUtils(dir):\ncant open directory:$path\n$!\n";
22             grep {/[^\.]/} readdir($D);
23             }
24              
25             sub unlink_recursive {
26             my($self,$d) = @_;
27             if(-d($d)){
28             my @d = $self->dir($d);
29             $self->unlink_recursive("$d/$_") for @d;
30             rmdir($d) or die "DataCube::FileUtils(unlink_recursive | rmdir):\ncant rmdir $d\n$!\n";
31             } elsif( -f($d) ) {
32             unlink($d)
33             or die "DataCube::FileUtils(unlink_recursive | unlink):\ncant unlink $d\n$!\n";
34             }
35             return $self
36             }
37              
38              
39             sub ensure_directory {
40             my($self,$dir) = @_;
41             return if -d($dir);
42             my @dir = ();
43             my $tmp = $dir;
44             parent_tree:
45             while($tmp =~ /[\\\/]/){
46             $tmp =~ /^(.*?)[\\\/]([^\\\/]+)$/;
47             $tmp = $1;
48             unshift @dir, $2;
49             last parent_tree if -d($tmp);
50             }
51             mkdir($tmp);
52             for(@dir){
53             $tmp .= '/' . $_;
54             mkdir($tmp)
55             or die "DataCube::FileUtils(ensure_directory):\n".
56             "could not ensure directory:\n$dir:\nfailed at:\n$tmp\n$!\n";
57             }
58             return $self;
59             }
60              
61             sub contents {
62             my($self,$path) = @_;
63             sysopen(my $F, $path, O_RDONLY)
64             or die "DataCube::FileUtils(contents | sysopen):\ncant sysopen:\n$path\n$!\n";
65             my $size = -s($path);
66             my $bytes = sysread($F, my $contents, $size);
67             die "DataCube::FileUtils(contents | sysread | bytes):\nwanted:\n".
68             "$size bytes\ngot:\n$bytes bytes\nfrom:\n$path\n$!\n"
69             unless $size == $bytes;
70             return wantarray ? split/\n/,$contents,-1 : $contents;
71             }
72              
73              
74             sub write {
75             my($self,%opts) = @_;
76             sysopen(my $F, $opts{target}, O_WRONLY | O_CREAT)
77             or die "DataCube::FileUtils(contents | sysopen):\ncant sysopen:\n$opts{target}\n$!\n";
78             use bytes;
79             my $size = bytes::length($opts{content});
80             my $bytes = syswrite($F, $opts{content}, $size);
81             die "DataCube::FileUtils(contents | syswrite | bytes):\nwanted to write:\n".
82             "$size bytes\nbut wrote:\n$bytes bytes\ninto:\n$opts{target}\n$!\n"
83             unless $size == $bytes;
84             return $self;
85             }
86              
87              
88             1;
89              
90              
91              
92              
93             __DATA__