1 package Fedora
::Rebuild
::Package
::StateLock
;
4 use version
0.77; our $VERSION = version
->declare("v0.2.1");
11 use Storable
qw(nstore_fd retrieve);
17 isa
=> 'Fedora::Rebuild::Package',
50 if (! defined $self->state || $self->state eq '') {
51 croak
"Invalid `state' attributed passed to StateLock constructor";
57 return $self->package->packagedir . '/.' . $self->state;
62 return $self->lockfile. '.log';
67 my $file = IO
::Handle
->new();
68 open ($file, '>', $self->logfile) or
69 croak
"Could not create `" . $self->logfile . "' logfile: $!";
73 # Print array into log
75 shift->logfd->print(@_);
78 # Return true if state is finshed, otherwise open log file.
82 if (-e
$self->lockfile) {
90 # Remove lock file (if exists) without any other actions.
91 # Use mark_failed() to close log file too.
92 # Return value is not specified.
95 if (-e
$self->lockfile) { unlink $self->lockfile; }
98 # Fsync and close log file. Croaks on error.
101 $self->logfd->sync && $self->logfd->close or
102 croak
"Could not sync and close `" . $self->logile . "' logfile: $!";
105 # Create lock file signalling the state has been finished. is_done() return
106 # true then. Return true if succeeded.
110 my $file = IO
::Handle
->new();
111 open ($file, '>', $self->lockfile) or
112 croak
"Could not open `" . $self->lockfile .
113 "' lockfile for writing: $!";
114 $file->sync && close($file) or
115 croak
"Could not sync and close `" . $self->lockfile .
120 # Close log file. Remove lock file if exist. is_done() return false then.
129 # Convert ${^CHILD_ERROR_NATIVE} to string description.
130 # XXX: This is not a method.
131 sub child_error_as_string
{
132 my $reason = ${^CHILD_ERROR_NATIVE
};
133 if (WIFEXITED
($reason)) {
134 $reason = "exit code " . WEXITSTATUS
($reason);
135 } elsif (WIFSIGNALED
($reason)) {
136 $reason = "signal " . WTERMSIG
($reason);
141 # Format array of command with argument as quoted string
142 # XXX: This not a method
144 $Data::Dumper
::Indent
=0;
145 $Data::Dumper
::Terse
=1;
146 return '(' . join(' ', map {Dumper
($_)} @_) . ')';
149 # Run command while appending output to log. Blocks. If workdir is nonempty
150 # string, switch into it befere execution (and opening the log).
151 # Return true if command succeeds.
153 my ($self, $workdir, @command) = @_;
156 open(STDOUT
, '>&', $self->logfd->fileno) and
157 open(STDERR
, '>&STDOUT');
158 print STDERR
"Executing: " . format_command
(@command) . "\n";
159 if (defined $workdir && $workdir ne '' && !chdir $workdir) {
160 print STDERR
"Could not change directory to $workdir: $!\n";
165 my $pid = Proc
::SyncExec
::sync_exec
($redirect, @command);
167 $self->log("Could not execute " . format_command
(@command) . ": $!\n");
170 if ($pid != waitpid($pid, 0) || $?
) {
171 $self->log("Command " . format_command
(@command) . " failed: " .
172 child_error_as_string
. "\n");
175 $self->log("Command " . format_command
(@command) .
176 " returned successfully.\n");
180 # Run command while appending stderr and stdout to log and stdout to refered
181 # output argument. In case of empty command output fill empty string;
182 # Blocks. If workdir is nonempty string, switch into it befere execution
183 # (and opening the log).
184 # Return true if command succeeds.
186 my ($self, $workdir, $output, @command) = @_;
188 my ($parent, $child);
189 if (!pipe $child, $parent) {
190 $self->log("Could not get connected pipes for command " .
191 format_command
(@command) . ": $!\n");
197 open(STDOUT
, '>&', fileno $parent) and
200 open(STDERR
, '>&', $self->logfd->fileno) and
201 print STDERR
"Executing: " . format_command
(@command) . "\n";
202 if (defined $workdir && $workdir ne '' && !chdir $workdir) {
203 print STDERR
"Could not change directory to $workdir: $!\n";
208 my $pid = Proc
::SyncExec
::sync_exec
($redirect, @command);
215 $self->log("Could not execute " . format_command
(@command) . ": $!\n");
219 for ($$output = ''; local $_ = <$child>;) {
224 if ($pid != waitpid($pid, 0) || $?
) {
225 $self->log("Command " . format_command
(@command) . " failed: " .
226 child_error_as_string
. "\n");
230 $self->log("Command " . format_command
(@command) .
231 " returned successfully.\n");
235 # Serialize referenced variable into file identified by name.
236 # The file is recreated.
237 # Return true if command succeeds, otherwise false;
238 sub nstorereference
{
239 my ($self, $reference, $filename) = @_;
242 my $file = IO
::Handle
->new();
243 if (! open($file, '>', $filename)) {
244 $self->log("Could not open `" . $filename . "' file for writing: $!\n");
247 if (! eval { nstore_fd
($reference, $file); }) {
248 $self->log("Could not store variable into `" . $filename .
253 if (!$file->sync or !close($file)) {
254 $self->log("Could not sync and close `" . $filename . "' file: $!\n");
261 # Load serialized variable from file identified by name.
262 # Return reference to the variable or undef in case of error.
263 sub retrievereference
{
264 my ($self, $filename) = @_;
267 if (! eval { $reference = retrieve
($filename); } || !$reference) {
268 $self->log("Could not load variable from `" . $filename .