Bus/file.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 301 CC 32 SCAR 35.5
CC
32
Cov
0%
load
90.0
lock
56.0
open
42.0
CRAP 34.9
1 #!perl -w
2 #
3 # CGI::Bus::file - File object
4 #
5 # admiral
6 #
7 #
8
9 package CGI::Bus::file;
10 require 5.000;
11 use strict;
12 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
13 use CGI::Bus::Base;
14 use IO::File;
15 use Fcntl qw(:DEFAULT :flock);
16 use vars qw(@ISA $AUTOLOAD);
17 @ISA =qw(CGI::Bus::Base);
18
19
20 1;
21
22
23 sub new {
24 my $c=shift;
25 my $s ={};
26 bless $s,$c;
27 $s =$s->CGI::Bus::Base::initialize(@_);
28 $s->parent->set('-reset')->{-file}=1 if $s->parent;
29 $s->iofile();
30 $s
31 }
32
33
34 sub AUTOLOAD {
35 my $s =shift;
36 my $m =substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
37 if (substr($m,2) eq 'O_') {eval "Fcntl::$m()"}
38 elsif (substr($m,5) eq 'LOCK_') {eval "Fcntl::$m()"}
39 else {$s->iofile->$m(@_)}
40 }
41
42
43
44 sub DESTROY {
45 my $s =shift;
46 eval {$s->close()};
47 eval {$s->CGI::Bus::Base::DESTROY};
48 }
49
50
51 #######################
52
53
54 sub iofile { # IO::File object
55 my $s =shift;
56 if (!$s->{-iofile}) {
57 $s->{-iofile} =IO::File->new();
58 if (scalar(@_)) {$s->open(@_)}
59 elsif ($s->{-name}) {$s->open($s->{-name},$s->{-mode},$s->{-perm})}
60 }
61 elsif (scalar(@_)) {
62 $s->{-iofile} =IO::File->new();
63 $s->open(@_)
64 }
65 $s->{-iofile}
66 }
67
68
69
70 sub open { # Open file 'r', 'rw', 'rwc', 'w', 'a' modes
71 my $s =shift;
72 $s->{-name} =$_[0];
73 $s->{-mode} =!$_[1] ?'r' :lc($_[1]) eq 'rw' ?'r+' :lc($_[1]) eq 'rwc' ?(O_CREAT|O_RDWR) :$_[1];
74 $s->{-perm} =($_[2]||0666);
75 $s->{-iofile} =IO::File->new() if !$s->{-iofile};
76 $s->{-iofile}->open($s->{-name}, $s->{-mode}, $s->{-perm})
77 || die("open '" .($s->{-name}||'') ."': $!\n");
78 $s->seek(0,0) if ($s->{-mode} ne 'a') && !($s->{-mode} & O_APPEND);
79 #$s->lock(($s->{-mode} eq 'r') || ($s->{-mode} & O_RDONLY) ?LOCK_SH :LOCK_EX);
80 $s
81 }
82
83
84
85 sub close { # Close file
86 my $s =shift;
87 $s->iofile->close() if $s->iofile->opened;
88 $s->{-name} =undef;
89 $s->{-mode} =undef;
90 $s->{-perm} =undef;
91 $s->{-lock} =undef;
92 $s->{-iofile} =undef;
93 $s
94 }
95
96
97
98 sub lock { # Lock file
99 my $s =shift;
100 return ($s->{-lock}||0) if !scalar(@_);
101 my $l =shift;
102 if ($l =~/\w{2}/) {$l =eval('Fcntl::LOCK_' .uc($l) .'()')}
103 elsif ($l ==0) {$l =LOCK_UN}
104 if (($s->{-lock} ||0) != (($l != LOCK_UN) ?$l :0)) {
105 flock($s->iofile, LOCK_UN);
106 if ($l !=LOCK_UN) {
107 flock($s->iofile, $l) ||die("lock '" .($s->{-name}||'') ."', '$l': $!\n");
108 $s->{-lock} =$l
109 }
110 else {
111 $s->{-lock} =0
112 }
113 }
114 $s
115 }
116
117
118 sub seek { # Position file
119 my $s =shift;
120 CORE::seek ($s->iofile, $_[0], $_[1]||0) ||die("seek '" .($s->{-name}||'') ."', '" .join(',',@_) ."': $!\n");
121 CORE::sysseek($s->iofile, $_[0], $_[1]||0) ||die("sysseek '" .($s->{-name}||'') ."', '" .join(',',@_) ."': $!\n");
122 $s;
123 }
124
125
126
127 sub load { # Load file contents
128 my $s =shift;
129 my $opt =($_[0] =~/^\-/i ? shift : ''); # 'a'rray, 's'calar, 'b'inary
130 $opt =$opt .'a' if $opt !~/[asb]/i && wantarray;
131 my $sub =shift;
132 my ($row, @rez);
133 $s->lock(LOCK_SH) if !$s->{-lock} ||$s->{-lock} ne LOCK_SH ||$s->{-lock} ne LOCK_EX;
134 $s->seek(0,0);
135 if ($sub) {
136 $row =1;
137 local $_;
138 while (!eof($s->iofile)) {
139 defined($_ =$s->iofile->getline) || die("load '" .$s->{-name} ."': $!\n");
140 chomp;
141 $opt=~/a/i ? &$sub() && push(@rez,$_)
142 : &$sub();
143 }
144 }
145 elsif ($opt=~/a/i) {
146 while (!eof($s->iofile)) {
147 defined($row =$s->iofile->getline) || die("load '" .$s->{-name} ."': $!\n");
148 chomp($row);
149 push (@rez, $row);
150 }
151 }
152 else {
153 binmode($s->iofile) if $opt =~/b/i;
154 defined(read($s->iofile, $row, -s $s->{-name})) || die("load '" .$s->{-name} ."': $!\n");
155 }
156 $opt=~/a/i ? @rez : $row
157 }
158
159
160
161 sub store { # Store file contents
162 my $s =shift;
163 my $opt =($_[0] =~/^\-/i ? shift : ''); # 'b'inary
164 $s->lock(LOCK_EX);
165 if ($opt=~/b/i) {
166 binmode($s->iofile);
167 $s->iofile->print(@_) ||die("store '" .$s->{-name} ."': $!\n");
168 }
169 else {
170 foreach my $row (@_) {
171 next if !defined($row);
172 $s->iofile->print($row, "\n") ||die("store '" .$s->{-name} ."': $!\n");
173 }
174 }
175 eval{$s->iofile->flush};
176 $s
177 }
178
179
180
181 sub dump { # Load or Store data dump
182 my ($s,$d) =@_;
183 if (scalar(@_) >1) {$s->truncate(0); $s->seek(0)->store('-',$s->parent->dumpout($d))}
184 else {$s->parent->dumpin($s->load('-s'))}
185 }
186
187
188
189 sub dumpload { # Load data dump from file
190 my ($s) =@_;
191 $s->parent->dumpin($s->load('-s'))
192 }
193
194
195
196 sub dumpstore { # Store data dump to file
197 my ($s,$d) =@_;
198 $s->truncate(0);
199 $s->seek(0)->store('-',$s->parent->dumpout($d))
200 }
201
202