1 package Fedora
::Rebuild
::Package
::StateLock
;
4 use version
0.77; our $VERSION = version
->declare("v0.8.0");
11 use Storable
qw(nstore_fd retrieve);
18 isa
=> 'Fedora::Rebuild::Package',
51 if (! defined $self->state || $self->state eq '') {
52 croak
"Invalid `state' attributed passed to StateLock constructor";
58 return $self->package->packagedir . '/.' . $self->state;
63 return $self->lockfile. '.log';
68 my $file = IO
::Handle
->new();
69 open ($file, '>', $self->logfile) or
70 croak
"Could not create `" . $self->logfile . "' logfile: $!";
74 # Print array into log
76 shift->logfd->print(@_);
79 # Print current time and array into log
81 shift->lograw(DateTime
->now, ' ', @_);
84 # Return true if state is finshed, otherwise open log file.
88 if (-e
$self->lockfile) {
96 # Remove lock file (if exists) without any other actions.
97 # Use mark_failed() to close log file too.
98 # Return value is not specified.
101 if (-e
$self->lockfile) { unlink $self->lockfile; }
104 # Fsync and close log file. Croaks on error.
107 $self->logfd->sync && $self->logfd->close or
108 croak
"Could not sync and close `" . $self->logile . "' logfile: $!";
111 # Create lock file signalling the state has been finished. is_done() return
112 # true then. Return true if succeeded.
116 my $file = IO
::Handle
->new();
117 open ($file, '>', $self->lockfile) or
118 croak
"Could not open `" . $self->lockfile .
119 "' lockfile for writing: $!";
120 $file->sync && close($file) or
121 croak
"Could not sync and close `" . $self->lockfile .
126 # Close log file. Remove lock file if exist. is_done() return false then.
135 # Convert ${^CHILD_ERROR_NATIVE} to string description.
136 # XXX: This is not a method.
137 sub child_error_as_string
{
138 my $reason = ${^CHILD_ERROR_NATIVE
};
139 if (WIFEXITED
($reason)) {
140 $reason = "exit code " . WEXITSTATUS
($reason);
141 } elsif (WIFSIGNALED
($reason)) {
142 $reason = "signal " . WTERMSIG
($reason);
147 # Format array of command with argument as quoted string
148 # XXX: This not a method
150 $Data::Dumper
::Indent
=0;
151 $Data::Dumper
::Terse
=1;
152 return '(' . join(' ', map {Dumper
($_)} @_) . ')';
155 # Run command while appending output to log. Blocks. If workdir is nonempty
156 # string, switch into it befere execution (and opening the log).
157 # Return true if command succeeds.
159 my ($self, $workdir, @command) = @_;
162 open(STDOUT
, '>&', $self->logfd->fileno) and
163 open(STDERR
, '>&STDOUT');
164 $self->log("Executing: " . format_command
(@command) . "\n");
165 if (defined $workdir && $workdir ne '' && !chdir $workdir) {
166 $self->log("Could not change directory to $workdir: $!\n");
171 my $pid = Proc
::SyncExec
::sync_exec
($redirect, @command);
173 $self->log("Could not execute " . format_command
(@command) . ": $!\n");
176 if ($pid != waitpid($pid, 0) || $?
) {
177 $self->log("Command " . format_command
(@command) . " failed: " .
178 child_error_as_string
. "\n");
181 $self->log("Command " . format_command
(@command) .
182 " returned successfully.\n");
186 # Run command while appending stderr and stdout to log and stdout to refered
187 # output argument. In case of empty command output fill empty string;
188 # Blocks. If workdir is nonempty string, switch into it befere execution
189 # (and opening the log).
190 # Return true if command succeeds.
192 my ($self, $workdir, $output, @command) = @_;
194 my ($parent, $child);
195 if (!pipe $child, $parent) {
196 $self->log("Could not get connected pipes for command " .
197 format_command
(@command) . ": $!\n");
203 open(STDOUT
, '>&', fileno $parent) and
206 open(STDERR
, '>&', $self->logfd->fileno) and
207 $self->log("Executing: " . format_command
(@command) . "\n");
208 if (defined $workdir && $workdir ne '' && !chdir $workdir) {
209 $self->log("Could not change directory to $workdir: $!\n");
214 my $pid = Proc
::SyncExec
::sync_exec
($redirect, @command);
221 $self->log("Could not execute " . format_command
(@command) . ": $!\n");
225 for ($$output = ''; local $_ = <$child>;) {
230 if ($pid != waitpid($pid, 0) || $?
) {
231 $self->log("Command " . format_command
(@command) . " failed: " .
232 child_error_as_string
. "\n");
236 $self->log("Command " . format_command
(@command) .
237 " returned successfully.\n");
241 # Serialize referenced variable into file identified by name.
242 # The file is recreated.
243 # Return true if command succeeds, otherwise false;
244 sub nstorereference
{
245 my ($self, $reference, $filename) = @_;
248 my $file = IO
::Handle
->new();
249 if (! open($file, '>', $filename)) {
250 $self->log("Could not open `" . $filename . "' file for writing: $!\n");
253 if (! eval { nstore_fd
($reference, $file); }) {
254 $self->log("Could not store variable into `" . $filename .
259 if (!$file->sync or !close($file)) {
260 $self->log("Could not sync and close `" . $filename . "' file: $!\n");
267 # Load serialized variable from file identified by name.
268 # Return reference to the variable or undef in case of error.
269 sub retrievereference
{
270 my ($self, $filename) = @_;
273 if (! eval { $reference = retrieve
($filename); } || !$reference) {
274 $self->log("Could not load variable from `" . $filename .