File Coverage

blib/lib/CPAN/CacheMgr.pm
Criterion Covered Total %
statement 39 150 26.0
branch 5 88 5.6
condition 2 13 15.3
subroutine 9 16 56.2
pod 2 8 25.0
total 57 275 20.7


line stmt bran cond sub pod time code
1             # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
2             # vim: ts=4 sts=4 sw=4:
3             package CPAN::CacheMgr;
4 22     22   125 use strict;
  22         38  
  22         752  
5 22     22   86 use CPAN::InfoObj;
  22         70  
  22         809  
6             @CPAN::CacheMgr::ISA = qw(CPAN::InfoObj CPAN);
7 22     22   78 use Cwd qw(chdir);
  22         73  
  22         1320  
8 22     22   120 use File::Find;
  22         45  
  22         1376  
9              
10 22         1258 use vars qw(
11             $VERSION
12 22     22   154 );
  22         45  
13             $VERSION = "5.5003";
14              
15             package CPAN::CacheMgr;
16 22     22   95 use strict;
  22         30  
  22         37158  
17              
18             #-> sub CPAN::CacheMgr::as_string ;
19             sub as_string {
20 0     0 1 0 eval { require Data::Dumper };
  0         0  
21 0 0       0 if ($@) {
22 0         0 return shift->SUPER::as_string;
23             } else {
24 0         0 return Data::Dumper::Dumper(shift);
25             }
26             }
27              
28             #-> sub CPAN::CacheMgr::cachesize ;
29             sub cachesize {
30 0     0 0 0 shift->{DU};
31             }
32              
33             #-> sub CPAN::CacheMgr::tidyup ;
34             sub tidyup {
35 0     0 0 0 my($self) = @_;
36 0 0       0 return unless $CPAN::META->{LOCK};
37 0 0       0 return unless -d $self->{ID};
38 0         0 my @toremove = grep { $self->{SIZE}{$_}==0 } @{$self->{FIFO}};
  0         0  
  0         0  
39 0         0 for my $current (0..$#toremove) {
40 0         0 my $toremove = $toremove[$current];
41 0         0 $CPAN::Frontend->myprint(sprintf(
42             "DEL(%d/%d): %s \n",
43             $current+1,
44             scalar @toremove,
45             $toremove,
46             )
47             );
48 0 0       0 return if $CPAN::Signal;
49 0         0 $self->_clean_cache($toremove);
50 0 0       0 return if $CPAN::Signal;
51             }
52 0         0 $self->{FIFO} = [];
53             }
54              
55             #-> sub CPAN::CacheMgr::dir ;
56             sub dir {
57 7     7 1 25 shift->{ID};
58             }
59              
60             #-> sub CPAN::CacheMgr::entries ;
61             sub entries {
62 0     0 0 0 my($self,$dir) = @_;
63 0 0       0 return unless defined $dir;
64 0 0       0 $self->debug("reading dir[$dir]") if $CPAN::DEBUG;
65 0   0     0 $dir ||= $self->{ID};
66 0         0 my($cwd) = CPAN::anycwd();
67 0 0       0 chdir $dir or Carp::croak("Can't chdir to $dir: $!");
68 0 0       0 my $dh = DirHandle->new(File::Spec->curdir)
69             or Carp::croak("Couldn't opendir $dir: $!");
70 0         0 my(@entries);
71 0         0 for ($dh->read) {
72 0 0 0     0 next if $_ eq "." || $_ eq "..";
73 0 0       0 if (-f $_) {
    0          
74 0         0 push @entries, File::Spec->catfile($dir,$_);
75             } elsif (-d _) {
76 0         0 push @entries, File::Spec->catdir($dir,$_);
77             } else {
78 0         0 $CPAN::Frontend->mywarn("Warning: weird direntry in $dir: $_\n");
79             }
80             }
81 0 0       0 chdir $cwd or Carp::croak("Can't chdir to $cwd: $!");
82 0         0 sort { -M $a <=> -M $b} @entries;
  0         0  
83             }
84              
85             #-> sub CPAN::CacheMgr::disk_usage ;
86             sub disk_usage {
87 0     0 0 0 my($self,$dir,$fast) = @_;
88 0 0       0 return if exists $self->{SIZE}{$dir};
89 0 0       0 return if $CPAN::Signal;
90 0         0 my($Du) = 0;
91 0 0       0 if (-e $dir) {
92 0 0       0 if (-d $dir) {
    0          
93 0 0       0 unless (-x $dir) {
94 0 0       0 unless (chmod 0755, $dir) {
95 0         0 $CPAN::Frontend->mywarn("I have neither the -x permission nor the ".
96             "permission to change the permission; cannot ".
97             "estimate disk usage of '$dir'\n");
98 0         0 $CPAN::Frontend->mysleep(5);
99 0         0 return;
100             }
101             }
102             } elsif (-f $dir) {
103             # nothing to say, no matter what the permissions
104             }
105             } else {
106 0         0 $CPAN::Frontend->mywarn("File or directory '$dir' has gone, ignoring\n");
107 0         0 return;
108             }
109 0 0       0 if ($fast) {
110 0         0 $Du = 0; # placeholder
111             } else {
112             find(
113             sub {
114 0 0   0   0 $File::Find::prune++ if $CPAN::Signal;
115 0 0       0 return if -l $_;
116 0 0       0 if (-d _) {
117 0 0       0 unless (-x _) {
118 0 0       0 unless (chmod 0755, $_) {
119 0         0 $CPAN::Frontend->mywarn("I have neither the -x permission nor ".
120             "the permission to change the permission; ".
121             "can only partially estimate disk usage ".
122             "of '$_'\n");
123 0         0 $CPAN::Frontend->mysleep(5);
124 0         0 return;
125             }
126             }
127             } else {
128 0         0 $Du += (-s _);
129             }
130             },
131 0         0 $dir
132             );
133             }
134 0 0       0 return if $CPAN::Signal;
135 0         0 $self->{SIZE}{$dir} = $Du/1024/1024;
136 0         0 unshift @{$self->{FIFO}}, $dir;
  0         0  
137 0 0       0 $self->debug("measured $dir is $Du") if $CPAN::DEBUG;
138 0         0 $self->{DU} += $Du/1024/1024;
139 0         0 $self->{DU};
140             }
141              
142             #-> sub CPAN::CacheMgr::_clean_cache ;
143             sub _clean_cache {
144 0     0   0 my($self,$dir) = @_;
145 0 0       0 return unless -e $dir;
146 0 0       0 unless (File::Spec->canonpath(File::Basename::dirname($dir))
147             eq File::Spec->canonpath($CPAN::Config->{build_dir})) {
148 0         0 $CPAN::Frontend->mywarn("Directory '$dir' not below $CPAN::Config->{build_dir}, ".
149             "will not remove\n");
150 0         0 $CPAN::Frontend->mysleep(5);
151 0         0 return;
152             }
153 0 0       0 $self->debug("have to rmtree $dir, will free $self->{SIZE}{$dir}")
154             if $CPAN::DEBUG;
155 0         0 File::Path::rmtree($dir);
156 0         0 my $id_deleted = 0;
157 0 0 0     0 if ($dir !~ /\.yml$/ && -f "$dir.yml") {
158 0         0 my $yaml_module = CPAN::_yaml_module();
159 0 0       0 if ($CPAN::META->has_inst($yaml_module)) {
160 0         0 my($peek_yaml) = eval { CPAN->_yaml_loadfile("$dir.yml"); };
  0         0  
161 0 0       0 if ($@) {
    0          
162 0         0 $CPAN::Frontend->mywarn("(parse error on '$dir.yml' removing anyway)");
163 0 0       0 unlink "$dir.yml" or
164             $CPAN::Frontend->mywarn("(Could not unlink '$dir.yml': $!)");
165 0         0 return;
166             } elsif (my $id = $peek_yaml->[0]{distribution}{ID}) {
167 0         0 $CPAN::META->delete("CPAN::Distribution", $id);
168              
169             # XXX we should restore the state NOW, otherwise this
170             # distro does not exist until we read an index. BUG ALERT(?)
171              
172             # $CPAN::Frontend->mywarn (" +++\n");
173 0         0 $id_deleted++;
174             }
175             }
176 0         0 unlink "$dir.yml"; # may fail
177 0 0       0 unless ($id_deleted) {
178 0         0 CPAN->debug("no distro found associated with '$dir'");
179             }
180             }
181 0         0 $self->{DU} -= $self->{SIZE}{$dir};
182 0         0 delete $self->{SIZE}{$dir};
183             }
184              
185             #-> sub CPAN::CacheMgr::new ;
186             sub new {
187 7     7 0 32 my($class,$phase) = @_;
188 7   50     77 $phase ||= "atstart";
189 7         17 my $time = time;
190 7         14 my($debug,$t2);
191 7         28 $debug = "";
192             my $self = {
193             ID => $CPAN::Config->{build_dir},
194             MAX => $CPAN::Config->{'build_cache'},
195 7   50     111 SCAN => $CPAN::Config->{'scan_cache'} || 'atstart',
196             DU => 0
197             };
198             $CPAN::Frontend->mydie("Unknown scan_cache argument: $self->{SCAN}")
199 7 50       89 unless $self->{SCAN} =~ /never|atstart|atexit/;
200 7         557 File::Path::mkpath($self->{ID});
201 7         203 my $dh = DirHandle->new($self->{ID});
202 7         648 bless $self, $class;
203 7         47 $self->scan_cache($phase);
204 7         15 $t2 = time;
205 7         29 $debug .= "timing of CacheMgr->new: ".($t2 - $time);
206 7         16 $time = $t2;
207 7 50       21 CPAN->debug($debug) if $CPAN::DEBUG;
208 7         58 $self;
209             }
210              
211             #-> sub CPAN::CacheMgr::scan_cache ;
212             sub scan_cache {
213 7     7 0 19 my ($self, $phase) = @_;
214 7 50       26 $phase = '' unless defined $phase;
215 7 50       101 return unless $phase eq $self->{SCAN};
216 7 50       27 return unless $CPAN::META->{LOCK};
217             $CPAN::Frontend->myprint(
218             sprintf("Scanning cache %s for sizes\n",
219 0           $self->{ID}));
220 0           my $e;
221 0           my @entries = $self->entries($self->{ID});
222 0           my $i = 0;
223 0           my $painted = 0;
224 0           for $e (@entries) {
225 0           my $symbol = ".";
226 0 0         if ($self->{DU} > $self->{MAX}) {
227 0           $symbol = "-";
228 0           $self->disk_usage($e,1);
229             } else {
230 0           $self->disk_usage($e);
231             }
232 0           $i++;
233 0           while (($painted/76) < ($i/@entries)) {
234 0           $CPAN::Frontend->myprint($symbol);
235 0           $painted++;
236             }
237 0 0         return if $CPAN::Signal;
238             }
239 0           $CPAN::Frontend->myprint("DONE\n");
240 0           $self->tidyup;
241             }
242              
243             1;