File Coverage

perllib/Arch/TempFiles.pm
Criterion Covered Total %
statement 58 64 90.6
branch 8 14 57.1
condition 8 20 40.0
subroutine 16 20 80.0
pod 12 13 92.3
total 102 131 77.8


line stmt bran cond sub pod time code
1             # Arch Perl library, Copyright (C) 2004 Mikhael Goikhman
2             #
3             # This program is free software; you can redistribute it and/or modify
4             # it under the terms of the GNU General Public License as published by
5             # the Free Software Foundation; either version 2 of the License, or
6             # (at your option) any later version.
7             #
8             # This program is distributed in the hope that it will be useful,
9             # but WITHOUT ANY WARRANTY; without even the implied warranty of
10             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11             # GNU General Public License for more details.
12             #
13             # You should have received a copy of the GNU General Public License
14             # along with this program; if not, write to the Free Software
15             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16              
17 6     6   157 use 5.005;
  6         21  
  6         236  
18 6     6   77 use strict;
  6         14  
  6         242  
19              
20             package Arch::TempFiles;
21              
22 6     6   28 use Exporter;
  6         12  
  6         360  
23 6     6   45 use vars qw($global_tmp @ISA @EXPORT_OK);
  6         11  
  6         533  
24             @ISA = qw(Exporter);
25             @EXPORT_OK = qw(
26             temp_root temp_name temp_file_name temp_dir_name temp_file temp_dir
27             );
28              
29 6     6   485 use Arch::Util qw(remove_dir);
  6         11  
  6         5231  
30              
31             sub new ($) {
32 5     5 0 96 my $class = shift;
33 5   100     132 my $self = {
34             root => $ENV{TMP_DIR} || "/tmp",
35             files => [],
36             dirs => [],
37             };
38 5         39 return bless $self, $class;
39             }
40              
41             sub DESTROY ($) {
42 4     4   4959 my $self = shift;
43 4         12 my @temp_files = @{$self->{files}};
  4         31  
44 4         15 my @temp_dirs = @{$self->{dirs}};
  4         21  
45              
46 4         28 foreach my $file (@temp_files) {
47 2 50 0     207 unlink($file) || warn "Can't unlink $file: $!\n" if -f $file;
48             }
49 4 100       67 remove_dir(@temp_dirs) if @temp_dirs;
50             }
51              
52             sub root ($;$) {
53 6     6 1 11 my $self = shift;
54 6 100       18 $self->{root} = shift if @_;
55 6         28 return $self->{root};
56             }
57              
58             sub name ($;$) {
59 9     9 1 18 my $self = shift;
60 9   100     54 my $label = shift || "arch";
61 9 50 33     416 die "Can't make temporary $label name, no valid temp root defined\n"
62             unless $self->{root} && -d $self->{root};
63 9         36 my $prefix = "$self->{root}/,,$label-";
64 9         14 my $file_name;
65 9         14 my $tries = 10000;
66 9   33     13 do {
67 9         1746 $file_name = $prefix . sprintf("%06d", rand(1000000));
68             } while -e $file_name && --$tries;
69 9 50       29 die "Failed to acquire unused temp name $prefix*\n" unless $tries;
70 9         32 return $file_name;
71             }
72              
73             sub file_name ($;$) {
74 2     2 1 5 my $self = shift;
75 2         11 my $file_name = $self->name($_[0]);
76 2         6 push @{$self->{files}}, $file_name;
  2         7  
77 2         8 return $file_name;
78             }
79              
80             sub dir_name ($;$) {
81 5     5 1 13 my $self = shift;
82 5         21 my $dir_name = $self->name($_[0]);
83 5         14 push @{$self->{dirs}}, $dir_name;
  5         17  
84 5         14 return $dir_name;
85             }
86              
87             sub file ($;$) {
88 1     1 1 3 my $self = shift;
89 1         6 my $file_name = $self->file_name($_[0]);
90             # don't create file currently
91 1         8 return $file_name;
92             }
93              
94             sub dir ($;$) {
95 3     3 1 8 my $self = shift;
96 3         16 my $dir_name = $self->dir_name($_[0]);
97 3 50       612 mkdir($dir_name, 0777) and return $dir_name;
98 0 0 0     0 die "Can't mkdir $dir_name: $!" if ($_[1] || 10) <= 1;
99 0   0     0 $self->dir($_[0], ($_[1] || 10) - 1);
100             }
101              
102             sub _self () {
103 4   66 4   21 return $global_tmp ||= Arch::TempFiles->new;
104             }
105              
106             sub temp_root (;$) {
107 2     2 1 5 _self()->root(@_);
108             }
109              
110             sub temp_name (;$) {
111 2     2 1 5 _self()->name(@_);
112             }
113              
114             sub temp_file_name (;$) {
115 0     0 1   _self()->file_name(@_);
116             }
117              
118             sub temp_dir_name (;$) {
119 0     0 1   _self()->dir_name(@_);
120             }
121              
122             sub temp_file (;$) {
123 0     0 1   _self()->file(shift);
124             }
125              
126             sub temp_dir (;$) {
127 0     0 1   _self()->dir(shift);
128             }
129              
130             1;
131              
132             __END__