1 package Fedora
::Rebuild
::Package
::StateLock
;
4 use version
0.77; our $VERSION = version
->declare("v0.12.1");
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->logfile . "' logfile: $!";
111 # Remove log file (if exists) without any other actions.
112 # Return value is not specified.
115 if (-e
$self->logfile) { unlink $self->logfile; }
118 # Create lock file signalling the state has been finished. is_done() return
119 # true then. Return true if succeeded.
123 my $file = IO
::Handle
->new();
124 open ($file, '>', $self->lockfile) or
125 croak
"Could not open `" . $self->lockfile .
126 "' lockfile for writing: $!";
127 $file->sync && close($file) or
128 croak
"Could not sync and close `" . $self->lockfile .
133 # Close log file. Remove lock file if exist. is_done() return false then.
142 # Convert ${^CHILD_ERROR_NATIVE} to string description.
143 # XXX: This is not a method.
144 sub child_error_as_string
{
145 my $reason = ${^CHILD_ERROR_NATIVE
};
146 if (WIFEXITED
($reason)) {
147 $reason = "exit code " . WEXITSTATUS
($reason);
148 } elsif (WIFSIGNALED
($reason)) {
149 $reason = "signal " . WTERMSIG
($reason);
154 # Format array of command with argument as quoted string
155 # XXX: This not a method
157 $Data::Dumper
::Indent
=0;
158 $Data::Dumper
::Terse
=1;
159 return '(' . join(' ', map {Dumper
($_)} @_) . ')';
162 # Run command while appending output to log. Blocks. If workdir is nonempty
163 # string, switch into it befere execution (and opening the log).
164 # Return true if command succeeds.
166 my ($self, $workdir, @command) = @_;
169 open(STDOUT
, '>&', $self->logfd->fileno) and
170 open(STDERR
, '>&STDOUT');
171 $self->log("Executing: " . format_command
(@command) . "\n");
172 if (defined $workdir && $workdir ne '' && !chdir $workdir) {
173 $self->log("Could not change directory to $workdir: $!\n");
178 my $pid = Proc
::SyncExec
::sync_exec
($redirect, @command);
180 $self->log("Could not execute " . format_command
(@command) . ": $!\n");
183 if ($pid != waitpid($pid, 0) || $?
) {
184 $self->log("Command " . format_command
(@command) . " failed: " .
185 child_error_as_string
. "\n");
188 $self->log("Command " . format_command
(@command) .
189 " returned successfully.\n");
193 # Run command while appending stderr and stdout to log and stdout to refered
194 # output argument. In case of empty command output fill empty string;
195 # Blocks. If workdir is nonempty string, switch into it befere execution
196 # (and opening the log).
197 # Return true if command succeeds.
199 my ($self, $workdir, $output, @command) = @_;
201 my ($parent, $child);
202 if (!pipe $child, $parent) {
203 $self->log("Could not get connected pipes for command " .
204 format_command
(@command) . ": $!\n");
210 open(STDOUT
, '>&', fileno $parent) and
213 open(STDERR
, '>&', $self->logfd->fileno) and
214 $self->log("Executing: " . format_command
(@command) . "\n");
215 if (defined $workdir && $workdir ne '' && !chdir $workdir) {
216 $self->log("Could not change directory to $workdir: $!\n");
221 my $pid = Proc
::SyncExec
::sync_exec
($redirect, @command);
228 $self->log("Could not execute " . format_command
(@command) . ": $!\n");
232 for ($$output = ''; local $_ = <$child>;) {
237 if ($pid != waitpid($pid, 0) || $?
) {
238 $self->log("Command " . format_command
(@command) . " failed: " .
239 child_error_as_string
. "\n");
243 $self->log("Command " . format_command
(@command) .
244 " returned successfully.\n");
248 # Serialize referenced variable into file identified by name.
249 # The file is recreated.
250 # Return true if command succeeds, otherwise false;
251 sub nstorereference
{
252 my ($self, $reference, $filename) = @_;
255 my $file = IO
::Handle
->new();
256 if (! open($file, '>', $filename)) {
257 $self->log("Could not open `" . $filename . "' file for writing: $!\n");
260 if (! eval { nstore_fd
($reference, $file); }) {
261 $self->log("Could not store variable into `" . $filename .
266 if (!$file->sync or !close($file)) {
267 $self->log("Could not sync and close `" . $filename . "' file: $!\n");
274 # Load serialized variable from file identified by name.
275 # Return reference to the variable or undef in case of error.
276 sub retrievereference
{
277 my ($self, $filename) = @_;
280 if (! eval { $reference = retrieve
($filename); } || !$reference) {
281 $self->log("Could not load variable from `" . $filename .