File Coverage

blib/lib/Brackup/GPGProcess.pm
Criterion Covered Total %
statement 32 41 78.0
branch 5 16 31.2
condition 1 2 50.0
subroutine 11 11 100.0
pod 0 6 0.0
total 49 76 64.4


line stmt bran cond sub pod time code
1             package Brackup::GPGProcess;
2 13     13   73 use strict;
  13         82  
  13         523  
3 13     13   69 use warnings;
  13         24  
  13         350  
4 13     13   67 use Brackup::Util qw(tempfile_obj);
  13         24  
  13         664  
5 13     13   16378 use POSIX qw(_exit);
  13         127590  
  13         101  
6 13     13   21945 use IO::File;
  13         39  
  13         9902  
7              
8             sub new {
9 39     39 0 104 my ($class, $pchunk) = @_;
10              
11 39         1007 my $destfh = tempfile_obj();
12 39         78315 my $destfn = $destfh->filename;
13              
14 39   50     881 my $no_fork = $ENV{BRACKUP_NOFORK} || 0; # if true (perhaps on Windows?), then don't fork... do all inline.
15              
16 39 50       146983 my $pid = $no_fork ? 0 : fork;
17 39 50       13379 if (!defined $pid) {
18 0         0 die "Failed to fork: $!";
19             }
20              
21             # caller (parent)
22 39 50       1708 if ($pid) {
23 39         12207 return bless {
24             destfh => $destfh,
25             pid => $pid,
26             running => 1,
27             }, $class;
28             }
29              
30             # child: encrypt and exit(0)...
31 0         0 $pchunk->root->encrypt($pchunk->raw_chunkref, $destfn);
32              
33 0 0       0 unless (-e $destfn) {
34             # if the file's gone, that likely means the parent process
35             # already terminated and unlinked our temp file, in
36             # which case we should just exit (with error code), rather
37             # than spewing error messages to stderr.
38 0         0 POSIX::_exit(1);
39             }
40 0 0       0 unless (-s $destfn) {
41 0         0 die "No data in encrypted output file";
42             }
43              
44 0 0       0 if ($no_fork) {
45 0         0 return bless {
46             destfh => $destfh,
47             pid => 0,
48             }, $class;
49             }
50              
51             # Note: we have to do this, to avoid some END block, somewhere,
52             # from cleaning up something or doing something. probably tempfiles
53             # being destroyed in File::Temp.
54 0         0 POSIX::_exit(0);
55             }
56              
57 78     78 0 2990 sub pid { $_[0]{pid} }
58              
59 46     46 0 363 sub running { $_[0]{running} }
60 39     39 0 140 sub note_stopped { $_[0]{running} = 0; }
61              
62             sub chunkref {
63 33     33 0 74 my ($self) = @_;
64 33 50       108 die "Still running!" if $self->{running};
65 33 50       128 die "No data in file" unless $self->size_on_disk;
66              
67 33         4586 return $self->{destfh};
68             }
69              
70             sub size_on_disk {
71 138     138 0 390 my $self = shift;
72 138         822 return -s $self->{destfh}->filename;
73             }
74              
75             1;
76