| 1 | #!/usr/bin/perl -w | ||
| 2 | # -*- perl -*- | ||
| 3 | |||
| 4 | # | ||
| 5 | # Author: Slaven Rezic | ||
| 6 | # | ||
| 7 | # Copyright (C) 2017,2018,2019,2020,2022 Slaven Rezic. All rights reserved. | ||
| 8 | # This program is free software; you can redistribute it and/or | ||
| 9 | # modify it under the same terms as Perl itself. | ||
| 10 | # | ||
| 11 | # Mail: slaven@rezic.de | ||
| 12 | # WWW: http://www.rezic.de/eserte/ | ||
| 13 | # | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | { | ||
| 19 | package Doit; | ||
| 20 | our $VERSION = '0.026'; | ||
| 21 | $VERSION =~ s{_}{}; | ||
| 22 | |||
| 23 | use constant IS_WIN => $^O eq 'MSWin32'; | ||
| 24 | } | ||
| 25 | |||
| 26 | { | ||
| 27 | package Doit::Log; | ||
| 28 | |||
| 29 | sub _use_coloring { | ||
| 30 | no warnings 'redefine'; | ||
| 31 | *colored_error = sub ($) { Term::ANSIColor::colored($_[0], 'red on_black')}; | ||
| 32 | *colored_info = sub ($) { Term::ANSIColor::colored($_[0], 'green on_black')}; | ||
| 33 | } | ||
| 34 | sub _no_coloring { | ||
| 35 | no warnings 'redefine'; | ||
| 36 | *colored_error = *colored_info = sub ($) { $_[0] }; | ||
| 37 | } | ||
| 38 | { | ||
| 39 | my $can_coloring; | ||
| 40 | sub _can_coloring { | ||
| 41 | return $can_coloring if defined $can_coloring; | ||
| 42 | # XXX What needs to be done to get coloring on Windows? | ||
| 43 | # XXX Probably should also check if the terminal is ANSI-capable at all | ||
| 44 | # XXX Probably should not use coloring on non-terminals (but | ||
| 45 | # there could be a --color option like in git to force it) | ||
| 46 | $can_coloring = !Doit::IS_WIN && ($ENV{TERM}||'') !~ m{^(|dumb)$} && eval { require Term::ANSIColor; 1 } ? 1 : 0; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | BEGIN { | ||
| 51 | if (_can_coloring()) { | ||
| 52 | _use_coloring(); | ||
| 53 | } else { | ||
| 54 | _no_coloring(); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | use Exporter 'import'; | ||
| 59 | our @EXPORT; BEGIN { @EXPORT = qw(info warning error) } | ||
| 60 | |||
| 61 | BEGIN { $INC{'Doit/Log.pm'} = __FILE__ } # XXX hack | ||
| 62 | |||
| 63 | my $current_label = ''; | ||
| 64 | |||
| 65 | sub info ($) { print STDERR colored_info("INFO$current_label:"), " ", $_[0], "\n" } | ||
| 66 | sub warning ($) { print STDERR colored_error("WARN$current_label:"), " ", $_[0], "\n" } | ||
| 67 | sub error ($) { require Carp; Carp::croak(colored_error("ERROR$current_label:"), " ", $_[0]) } | ||
| 68 | |||
| 69 | sub set_label ($) { | ||
| 70 | my $label = shift; | ||
| 71 | if (defined $label) { | ||
| 72 | $current_label = " $label"; | ||
| 73 | } else { | ||
| 74 | $current_label = ''; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | { | ||
| 80 | package Doit::Exception; | ||
| 81 | use overload '""' => 'stringify'; | ||
| 82 | use Exporter 'import'; | ||
| 83 | our @EXPORT_OK = qw(throw); | ||
| 84 | $INC{'Doit/Exception.pm'} = __FILE__; # XXX hack | ||
| 85 | |||
| 86 | sub new { | ||
| 87 | my($class, $msg, %opts) = @_; | ||
| 88 | my $level = delete $opts{__level} || 'auto'; | ||
| 89 | if ($level eq 'auto') { | ||
| 90 | my $_level = 0; | ||
| 91 | while() { | ||
| 92 | my @stackinfo = caller($_level); | ||
| 93 | if (!@stackinfo) { | ||
| 94 | $level = $_level - 1; | ||
| 95 | last; | ||
| 96 | } | ||
| 97 | if ($stackinfo[1] !~ m{([/\\]|^)Doit\.pm$}) { | ||
| 98 | $level = $_level; | ||
| 99 | last; | ||
| 100 | } | ||
| 101 | $_level++; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | ($opts{__package}, $opts{__filename}, $opts{__line}) = caller($level); | ||
| 105 | bless { | ||
| 106 | __msg => $msg, | ||
| 107 | %opts, | ||
| 108 | }, $class; | ||
| 109 | } | ||
| 110 | sub stringify { | ||
| 111 | my $self = shift; | ||
| 112 | my $msg = $self->{__msg}; | ||
| 113 | $msg = 'Died' if !defined $msg; | ||
| 114 | if ($msg !~ /\n\z/) { | ||
| 115 | $msg .= ' at ' . $self->{__filename} . ' line ' . $self->{__line} . ".\n"; | ||
| 116 | } | ||
| 117 | $msg; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub throw { die Doit::Exception->new(@_) } | ||
| 121 | } | ||
| 122 | |||
| 123 | { | ||
| 124 | package Doit::ScopeCleanups; | ||
| 125 | $INC{'Doit/ScopeCleanups.pm'} = __FILE__; # XXX hack | ||
| 126 | use Doit::Log; | ||
| 127 | |||
| 128 | sub new { | ||
| 129 | my($class) = @_; | ||
| 130 | bless [], $class; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub add_scope_cleanup { | ||
| 134 | my($self, $code) = @_; | ||
| 135 | push @$self, { code => $code }; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub DESTROY { | ||
| 139 | my $self = shift; | ||
| 140 | for my $scope_cleanup (@$self) { | ||
| 141 | my($code) = $scope_cleanup->{code}; | ||
| 142 | if ($] >= 5.014) { | ||
| 143 | eval { | ||
| 144 | $code->(); | ||
| 145 | }; | ||
| 146 | if ($@) { | ||
| 147 | # error() will give visual feedback about the problem, | ||
| 148 | # die() would be left unnoticed. Note that | ||
| 149 | # an exception in a DESTROY block is not fatal, | ||
| 150 | # and can be only detected by inspecting $@. | ||
| 151 | error "Scope cleanup failed: $@"; | ||
| 152 | } | ||
| 153 | } else { | ||
| 154 | # And eval {} in older perl versions would | ||
| 155 | # clobber an outside $@. See | ||
| 156 | # perldoc perl5140delta, "Exception Handling" | ||
| 157 | $code->(); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | { | ||
| 164 | package Doit::Util; | ||
| 165 | use Exporter 'import'; | ||
| 166 | our @EXPORT; BEGIN { @EXPORT = qw(in_directory new_scope_cleanup copy_stat get_sudo_cmd is_in_path) } | ||
| 167 | $INC{'Doit/Util.pm'} = __FILE__; # XXX hack | ||
| 168 | use Doit::Log; | ||
| 169 | |||
| 170 | sub new_scope_cleanup (&) { | ||
| 171 | my($code) = @_; | ||
| 172 | my $sc = Doit::ScopeCleanups->new; | ||
| 173 | $sc->add_scope_cleanup($code); | ||
| 174 | $sc; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub in_directory (&$) { | ||
| 178 | my($code, $dir) = @_; | ||
| 179 | my $scope_cleanup; | ||
| 180 | if (defined $dir) { | ||
| 181 | require Cwd; | ||
| 182 | my $pwd = Cwd::getcwd(); | ||
| 183 | if (!defined $pwd || $pwd eq '') { # XS variant returns undef, PP variant returns '' --- see https://rt.perl.org/Ticket/Display.html?id=132648 | ||
| 184 | warning "No known current working directory"; | ||
| 185 | } else { | ||
| 186 | $scope_cleanup = new_scope_cleanup | ||
| 187 | (sub { | ||
| 188 | chdir $pwd or error "Can't chdir to $pwd: $!"; | ||
| 189 | }); | ||
| 190 | } | ||
| 191 | chdir $dir | ||
| 192 | or error "Can't chdir to $dir: $!"; | ||
| 193 | } | ||
| 194 | $code->(); | ||
| 195 | } | ||
| 196 | |||
| 197 | # $src may be a source file or an arrayref with stat information | ||
| 198 | sub copy_stat ($$;@) { | ||
| 199 | my($src, $dest, %preserve) = @_; | ||
| 200 | my @stat = ref $src eq 'ARRAY' ? @$src : stat($src); | ||
| 201 | error "Can't stat $src: $!" if !@stat; | ||
| 202 | |||
| 203 | my $preserve_default = !%preserve; | ||
| 204 | my $preserve_ownership = exists $preserve{ownership} ? delete $preserve{ownership} : $preserve_default; | ||
| 205 | my $preserve_mode = exists $preserve{mode} ? delete $preserve{mode} : $preserve_default; | ||
| 206 | my $preserve_time = exists $preserve{time} ? delete $preserve{time} : $preserve_default; | ||
| 207 | |||
| 208 | error "Unhandled preserve values: " . join(" ", %preserve) if %preserve; | ||
| 209 | |||
| 210 | if ($preserve_mode) { | ||
| 211 | chmod $stat[2], $dest | ||
| 212 | or warning "Can't chmod $dest to " . sprintf("0%o", $stat[2]) . ": $!"; | ||
| 213 | } | ||
| 214 | if ($preserve_ownership) { | ||
| 215 | chown $stat[4], $stat[5], $dest | ||
| 216 | or do { | ||
| 217 | my $save_err = $!; # otherwise it's lost in the get... calls | ||
| 218 | warning "Can't chown $dest to " . | ||
| 219 | (getpwuid($stat[4]))[0] . "/" . | ||
| 220 | (getgrgid($stat[5]))[0] . ": $save_err"; | ||
| 221 | }; | ||
| 222 | } | ||
| 223 | if ($preserve_time) { | ||
| 224 | utime $stat[8], $stat[9], $dest | ||
| 225 | or warning "Can't utime $dest to " . | ||
| 226 | scalar(localtime $stat[8]) . "/" . | ||
| 227 | scalar(localtime $stat[9]) . | ||
| 228 | ": $!"; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | sub get_sudo_cmd () { | ||
| 233 | return () if $> == 0; | ||
| 234 | return ('sudo'); | ||
| 235 | } | ||
| 236 | |||
| 237 | sub is_in_path { | ||
| 238 | my($prog) = @_; | ||
| 239 | |||
| 240 | if (!defined &_file_name_is_absolute) { | ||
| 241 | if (eval { require File::Spec; defined &File::Spec::file_name_is_absolute }) { | ||
| 242 | *_file_name_is_absolute = \&File::Spec::file_name_is_absolute; | ||
| 243 | } else { | ||
| 244 | *_file_name_is_absolute = sub { | ||
| 245 | my $file = shift; | ||
| 246 | my $r; | ||
| 247 | if ($^O eq 'MSWin32') { | ||
| 248 | $r = ($file =~ m;^([a-z]:(/|\\)|\\\\|//);i); | ||
| 249 | } else { | ||
| 250 | $r = ($file =~ m|^/|); | ||
| 251 | } | ||
| 252 | $r; | ||
| 253 | }; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | if (_file_name_is_absolute($prog)) { | ||
| 258 | if ($^O eq 'MSWin32') { | ||
| 259 | return $prog if (-f $prog && -x $prog); | ||
| 260 | return "$prog.bat" if (-f "$prog.bat" && -x "$prog.bat"); | ||
| 261 | return "$prog.com" if (-f "$prog.com" && -x "$prog.com"); | ||
| 262 | return "$prog.exe" if (-f "$prog.exe" && -x "$prog.exe"); | ||
| 263 | return "$prog.cmd" if (-f "$prog.cmd" && -x "$prog.cmd"); | ||
| 264 | } else { | ||
| 265 | return $prog if -f $prog and -x $prog; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | require Config; | ||
| 269 | %Config::Config = %Config::Config if 0; # cease -w | ||
| 270 | my $sep = $Config::Config{'path_sep'} || ':'; | ||
| 271 | foreach (split(/$sep/o, $ENV{PATH})) { | ||
| 272 | if ($^O eq 'MSWin32') { | ||
| 273 | # maybe use $ENV{PATHEXT} like maybe_command in ExtUtils/MM_Win32.pm? | ||
| 274 | return "$_\\$prog" if (-f "$_\\$prog" && -x "$_\\$prog"); | ||
| 275 | return "$_\\$prog.bat" if (-f "$_\\$prog.bat" && -x "$_\\$prog.bat"); | ||
| 276 | return "$_\\$prog.com" if (-f "$_\\$prog.com" && -x "$_\\$prog.com"); | ||
| 277 | return "$_\\$prog.exe" if (-f "$_\\$prog.exe" && -x "$_\\$prog.exe"); | ||
| 278 | return "$_\\$prog.cmd" if (-f "$_\\$prog.cmd" && -x "$_\\$prog.cmd"); | ||
| 279 | } else { | ||
| 280 | return "$_/$prog" if (-x "$_/$prog" && !-d "$_/$prog"); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | undef; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | { | ||
| 288 | package Doit::Win32Util; | ||
| 289 | |||
| 290 | # Taken from http://blogs.perl.org/users/graham_knop/2011/12/using-system-or-exec-safely-on-windows.html | ||
| 291 | sub win32_quote_list { | ||
| 292 | my (@args) = @_; | ||
| 293 | |||
| 294 | my $args = join ' ', map { _quote_literal($_) } @args; | ||
| 295 | |||
| 296 | if (_has_shell_metachars($args)) { | ||
| 297 | # cmd.exe treats quotes differently from standard | ||
| 298 | # argument parsing. just escape everything using ^. | ||
| 299 | $args =~ s/([()%!^"<>&|])/^$1/g; | ||
| 300 | } | ||
| 301 | return $args; | ||
| 302 | } | ||
| 303 | |||
| 304 | sub _quote_literal { | ||
| 305 | my ($text) = @_; | ||
| 306 | |||
| 307 | # basic argument quoting. uses backslashes and quotes to escape | ||
| 308 | # everything. | ||
| 309 | # | ||
| 310 | # The original code had a \v here, but this is not supported | ||
| 311 | # in perl5.8. Also, \v probably matches too many characters here | ||
| 312 | # --- restrict to the ones < 0x100 | ||
| 313 | if ($text ne '' && $text !~ /[ \t\n\x0a\x0b\x0c\x0d\x85"]/) { | ||
| 314 | # no quoting needed | ||
| 315 | } else { | ||
| 316 | my @text = split '', $text; | ||
| 317 | $text = q{"}; | ||
| 318 | for (my $i = 0; ; $i++) { | ||
| 319 | my $bs_count = 0; | ||
| 320 | while ( $i < @text && $text[$i] eq "\\" ) { | ||
| 321 | $i++; | ||
| 322 | $bs_count++; | ||
| 323 | } | ||
| 324 | if ($i > $#text) { | ||
| 325 | $text .= "\\" x ($bs_count * 2); | ||
| 326 | last; | ||
| 327 | } elsif ($text[$i] eq q{"}) { | ||
| 328 | $text .= "\\" x ($bs_count * 2 + 1); | ||
| 329 | } else { | ||
| 330 | $text .= "\\" x $bs_count; | ||
| 331 | } | ||
| 332 | $text .= $text[$i]; | ||
| 333 | } | ||
| 334 | $text .= q{"}; | ||
| 335 | } | ||
| 336 | |||
| 337 | return $text; | ||
| 338 | } | ||
| 339 | |||
| 340 | # direct port of code from win32.c | ||
| 341 | sub _has_shell_metachars { | ||
| 342 | my $string = shift; | ||
| 343 | my $inquote = 0; | ||
| 344 | my $quote = ''; | ||
| 345 | |||
| 346 | my @string = split '', $string; | ||
| 347 | for my $char (@string) { | ||
| 348 | if ($char eq q{%}) { | ||
| 349 | return 1; | ||
| 350 | } elsif ($char eq q{'} || $char eq q{"}) { | ||
| 351 | if ($inquote) { | ||
| 352 | if ($char eq $quote) { | ||
| 353 | $inquote = 0; | ||
| 354 | $quote = ''; | ||
| 355 | } | ||
| 356 | } else { | ||
| 357 | $quote = $char; | ||
| 358 | $inquote++; | ||
| 359 | } | ||
| 360 | } elsif ($char eq q{<} || $char eq q{>} || $char eq q{|}) { | ||
| 361 | if ( ! $inquote) { | ||
| 362 | return 1; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | } | ||
| 366 | return; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | { | ||
| 371 | package Doit; | ||
| 372 | |||
| 373 | sub import { | ||
| 374 | warnings->import; | ||
| 375 | strict->import; | ||
| 376 | } | ||
| 377 | |||
| 378 | sub unimport { | ||
| 379 | warnings->unimport; | ||
| 380 | strict->unimport; | ||
| 381 | } | ||
| 382 | |||
| 383 | use Doit::Log; | ||
| 384 | |||
| 385 | my $diff_error_shown; | ||
| 386 | our @diff_cmd; | ||
| 387 | |||
| 388 | sub _new { | ||
| 389 | my $class = shift; | ||
| 390 | my $self = bless { }, $class; | ||
| 391 | $self; | ||
| 392 | } | ||
| 393 | sub runner { | ||
| 394 | my($self) = @_; | ||
| 395 | # XXX hmmm, creating now self-refential data structures ... | ||
| 396 | $self->{runner} ||= Doit::Runner->new($self); | ||
| 397 | } | ||
| 398 | |||
| 399 | sub dryrunner { | ||
| 400 | my($self) = @_; | ||
| 401 | # XXX hmmm, creating now self-refential data structures ... | ||
| 402 | $self->{dryrunner} ||= Doit::Runner->new($self, dryrun => 1); | ||
| 403 | } | ||
| 404 | |||
| 405 | sub init { | ||
| 406 | my($class) = @_; | ||
| 407 | require Getopt::Long; | ||
| 408 | my $getopt = Getopt::Long::Parser->new; | ||
| 409 | $getopt->configure(qw(pass_through noauto_abbrev)); | ||
| 410 | $getopt->getoptions( | ||
| 411 | 'dry-run|n' => \my $dry_run, | ||
| 412 | ); | ||
| 413 | my $doit = $class->_new; | ||
| 414 | if ($dry_run) { | ||
| 415 | $doit->dryrunner; | ||
| 416 | } else { | ||
| 417 | $doit->runner; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | sub install_generic_cmd { | ||
| 422 | my($self, $name, $check, $code, $msg) = @_; | ||
| 423 | if (!$msg) { | ||
| 424 | $msg = sub { my($self, $args) = @_; $name . ($args ? " @$args" : '') }; | ||
| 425 | } | ||
| 426 | my $cmd = sub { | ||
| 427 | my($self, @args) = @_; | ||
| 428 | my @commands; | ||
| 429 | my $addinfo = {}; | ||
| 430 | if ($check->($self, \@args, $addinfo)) { | ||
| 431 | push @commands, { | ||
| 432 | code => sub { $code->($self, \@args, $addinfo) }, | ||
| 433 | msg => $msg->($self, \@args, $addinfo), | ||
| 434 | }; | ||
| 435 | } | ||
| 436 | Doit::Commands->new(@commands); | ||
| 437 | }; | ||
| 438 | no strict 'refs'; | ||
| 439 | *{"cmd_$name"} = $cmd; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub cmd_chmod { | ||
| 443 | my($self, @args) = @_; | ||
| 444 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 445 | my $quiet = delete $options{quiet}; | ||
| 446 | error "Unhandled options: " . join(" ", %options) if %options; | ||
| 447 | my($mode, @files) = @args; | ||
| 448 | my @files_to_change; | ||
| 449 | for my $file (@files) { | ||
| 450 | my @s = stat($file); | ||
| 451 | if (@s) { | ||
| 452 | if (($s[2] & 07777) != $mode) { | ||
| 453 | push @files_to_change, $file; | ||
| 454 | } | ||
| 455 | } else { | ||
| 456 | push @files_to_change, $file; | ||
| 457 | } | ||
| 458 | } | ||
| 459 | if (@files_to_change) { | ||
| 460 | my @commands = { | ||
| 461 | code => sub { | ||
| 462 | my $changed_files = chmod $mode, @files_to_change; | ||
| 463 | if ($changed_files != @files_to_change) { | ||
| 464 | if (@files_to_change == 1) { | ||
| 465 | error "chmod failed: $!"; | ||
| 466 | } elsif ($changed_files == 0) { | ||
| 467 | error "chmod failed on all files: $!"; | ||
| 468 | } else { | ||
| 469 | error "chmod failed on some files (" . (@files_to_change-$changed_files) . "/" . scalar(@files_to_change) . "): $!"; | ||
| 470 | } | ||
| 471 | } | ||
| 472 | }, | ||
| 473 | ($quiet ? () : (msg => sprintf("chmod 0%o %s", $mode, join(" ", @files_to_change)))), # shellquote? | ||
| 474 | rv => scalar @files_to_change, | ||
| 475 | }; | ||
| 476 | Doit::Commands->new(@commands); | ||
| 477 | } else { | ||
| 478 | Doit::Commands->return_zero; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | sub cmd_chown { | ||
| 483 | my($self, @args) = @_; | ||
| 484 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 485 | my $quiet = delete $options{quiet}; | ||
| 486 | error "Unhandled options: " . join(" ", %options) if %options; | ||
| 487 | my($uid, $gid, @files) = @args; | ||
| 488 | |||
| 489 | if (!defined $uid) { | ||
| 490 | $uid = -1; | ||
| 491 | } elsif ($uid !~ /^-?\d+$/) { | ||
| 492 | my $_uid = (getpwnam $uid)[2]; | ||
| 493 | if (!defined $_uid) { | ||
| 494 | # XXX problem: in dry-run mode the user/group could be | ||
| 495 | # created in _this_ pass, so this error would happen | ||
| 496 | # while in wet-run everything would be fine. Good solution? | ||
| 497 | # * do uid/gid resolution _again_ in the command if it failed here? | ||
| 498 | # * maintain a virtual list of created users/groups while this run, and | ||
| 499 | # use this list as a fallback? | ||
| 500 | error "User '$uid' does not exist"; | ||
| 501 | } | ||
| 502 | $uid = $_uid; | ||
| 503 | } | ||
| 504 | if (!defined $gid) { | ||
| 505 | $gid = -1; | ||
| 506 | } elsif ($gid !~ /^-?\d+$/) { | ||
| 507 | my $_gid = (getgrnam $gid)[2]; | ||
| 508 | if (!defined $_gid) { | ||
| 509 | error "Group '$gid' does not exist"; | ||
| 510 | } | ||
| 511 | $gid = $_gid; | ||
| 512 | } | ||
| 513 | |||
| 514 | my @files_to_change; | ||
| 515 | if ($uid != -1 || $gid != -1) { | ||
| 516 | for my $file (@files) { | ||
| 517 | my @s = stat($file); | ||
| 518 | if (@s) { | ||
| 519 | if ($uid != -1 && $s[4] != $uid) { | ||
| 520 | push @files_to_change, $file; | ||
| 521 | } elsif ($gid != -1 && $s[5] != $gid) { | ||
| 522 | push @files_to_change, $file; | ||
| 523 | } | ||
| 524 | } else { | ||
| 525 | push @files_to_change, $file; | ||
| 526 | } | ||
| 527 | } | ||
| 528 | } | ||
| 529 | |||
| 530 | if (@files_to_change) { | ||
| 531 | my @commands = { | ||
| 532 | code => sub { | ||
| 533 | my $changed_files = chown $uid, $gid, @files_to_change; | ||
| 534 | if ($changed_files != @files_to_change) { | ||
| 535 | if (@files_to_change == 1) { | ||
| 536 | error "chown failed: $!"; | ||
| 537 | } elsif ($changed_files == 0) { | ||
| 538 | error "chown failed on all files: $!"; | ||
| 539 | } else { | ||
| 540 | error "chown failed on some files (" . (@files_to_change-$changed_files) . "/" . scalar(@files_to_change) . "): $!"; | ||
| 541 | } | ||
| 542 | } | ||
| 543 | }, | ||
| 544 | ($quiet ? () : (msg => "chown $uid, $gid, @files_to_change")), # shellquote? | ||
| 545 | rv => scalar @files_to_change, | ||
| 546 | }; | ||
| 547 | Doit::Commands->new(@commands); | ||
| 548 | } else { | ||
| 549 | Doit::Commands->return_zero; | ||
| 550 | } | ||
| 551 | } | ||
| 552 | |||
| 553 | sub cmd_cond_run { | ||
| 554 | my($self, %opts) = @_; | ||
| 555 | my $if = delete $opts{if}; | ||
| 556 | my $unless = delete $opts{unless}; | ||
| 557 | my $creates = delete $opts{creates}; | ||
| 558 | my $cmd = delete $opts{cmd}; | ||
| 559 | error "Unhandled options: " . join(" ", %opts) if %opts; | ||
| 560 | |||
| 561 | if (!$cmd) { | ||
| 562 | error "cmd is a mandatory option for cond_run"; | ||
| 563 | } | ||
| 564 | if (ref $cmd ne 'ARRAY') { | ||
| 565 | error "cmd must be an array reference"; | ||
| 566 | } | ||
| 567 | |||
| 568 | my $doit = 1; | ||
| 569 | if ($if && !$if->()) { | ||
| 570 | $doit = 0; | ||
| 571 | } | ||
| 572 | if ($doit && $unless && $unless->()) { | ||
| 573 | $doit = 0; | ||
| 574 | } | ||
| 575 | if ($doit && $creates && -e $creates) { | ||
| 576 | $doit = 0; | ||
| 577 | } | ||
| 578 | |||
| 579 | if ($doit) { | ||
| 580 | my $doit_commands; | ||
| 581 | if (ref $cmd->[0] eq 'ARRAY') { | ||
| 582 | $doit_commands = $self->cmd_run(@$cmd); | ||
| 583 | } else { | ||
| 584 | $doit_commands = $self->cmd_system(@$cmd); | ||
| 585 | } | ||
| 586 | $doit_commands->set_last_rv(1); | ||
| 587 | $doit_commands; | ||
| 588 | } else { | ||
| 589 | Doit::Commands->return_zero; | ||
| 590 | } | ||
| 591 | } | ||
| 592 | |||
| 593 | sub cmd_ln_nsf { | ||
| 594 | my($self, $oldfile, $newfile) = @_; | ||
| 595 | |||
| 596 | my $doit = 1; | ||
| 597 | if (!defined $oldfile) { | ||
| 598 | error "oldfile was not specified for ln_nsf"; | ||
| 599 | } elsif (!defined $newfile) { | ||
| 600 | error "newfile was not specified for ln_nsf"; | ||
| 601 | } elsif (-l $newfile) { | ||
| 602 | my $points_to = readlink $newfile | ||
| 603 | or error "Unexpected: readlink $newfile failed (race condition?)"; | ||
| 604 | if ($points_to eq $oldfile) { | ||
| 605 | $doit = 0; | ||
| 606 | } | ||
| 607 | } elsif (-d $newfile) { | ||
| 608 | # Theoretically "ln -nsf destination directory" works (not always, | ||
| 609 | # e.g. fails with destination=/), but results are not very useful, | ||
| 610 | # so fail here. | ||
| 611 | error qq{"$newfile" already exists as a directory}; | ||
| 612 | } else { | ||
| 613 | # probably a file, keep $doit=1 | ||
| 614 | } | ||
| 615 | |||
| 616 | if ($doit) { | ||
| 617 | my @commands = { | ||
| 618 | code => sub { | ||
| 619 | system 'ln', '-nsf', $oldfile, $newfile; | ||
| 620 | error "ln -nsf $oldfile $newfile failed" if $? != 0; | ||
| 621 | }, | ||
| 622 | msg => "ln -nsf $oldfile $newfile", | ||
| 623 | rv => 1, | ||
| 624 | }; | ||
| 625 | Doit::Commands->new(@commands); | ||
| 626 | } else { | ||
| 627 | Doit::Commands->return_zero; | ||
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | sub cmd_make_path { | ||
| 632 | my($self, @directories) = @_; | ||
| 633 | my $options = {}; if (ref $directories[-1] eq 'HASH') { $options = pop @directories } | ||
| 634 | my @directories_to_create = grep { !-d $_ } @directories; | ||
| 635 | if (@directories_to_create) { | ||
| 636 | my @commands = { | ||
| 637 | code => sub { | ||
| 638 | require File::Path; | ||
| 639 | File::Path::make_path(@directories_to_create, $options) | ||
| 640 | or error $!; | ||
| 641 | }, | ||
| 642 | msg => "make_path @directories", | ||
| 643 | rv => scalar @directories_to_create, | ||
| 644 | }; | ||
| 645 | Doit::Commands->new(@commands); | ||
| 646 | } else { | ||
| 647 | Doit::Commands->return_zero; | ||
| 648 | } | ||
| 649 | } | ||
| 650 | |||
| 651 | sub cmd_mkdir { | ||
| 652 | my($self, $directory, $mode) = @_; | ||
| 653 | if (!-d $directory) { | ||
| 654 | my @commands; | ||
| 655 | if (defined $mode) { | ||
| 656 | push @commands, { | ||
| 657 | code => sub { mkdir $directory, $mode or error "$!" }, | ||
| 658 | msg => "mkdir $directory with mask $mode", | ||
| 659 | rv => 1, | ||
| 660 | }; | ||
| 661 | } else { | ||
| 662 | push @commands, { | ||
| 663 | code => sub { mkdir $directory or error "$!" }, | ||
| 664 | msg => "mkdir $directory", | ||
| 665 | rv => 1, | ||
| 666 | }; | ||
| 667 | } | ||
| 668 | Doit::Commands->new(@commands); | ||
| 669 | } else { | ||
| 670 | Doit::Commands->return_zero; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | sub cmd_remove_tree { | ||
| 675 | my($self, @directories) = @_; | ||
| 676 | my $options = {}; if (ref $directories[-1] eq 'HASH') { $options = pop @directories } | ||
| 677 | my @directories_to_remove = grep { -d $_ } @directories; | ||
| 678 | if (@directories_to_remove) { | ||
| 679 | my @commands = { | ||
| 680 | code => sub { | ||
| 681 | require File::Path; | ||
| 682 | File::Path::remove_tree(@directories_to_remove, $options) | ||
| 683 | or error "$!"; | ||
| 684 | }, | ||
| 685 | msg => "remove_tree @directories_to_remove", | ||
| 686 | rv => scalar @directories_to_remove, | ||
| 687 | }; | ||
| 688 | Doit::Commands->new(@commands); | ||
| 689 | } else { | ||
| 690 | Doit::Commands->return_zero; | ||
| 691 | } | ||
| 692 | } | ||
| 693 | |||
| 694 | sub cmd_rename { | ||
| 695 | my($self, $from, $to) = @_; | ||
| 696 | my @commands; | ||
| 697 | push @commands, { | ||
| 698 | code => sub { rename $from, $to or error "$!" }, | ||
| 699 | msg => "rename $from, $to", | ||
| 700 | rv => 1, | ||
| 701 | }; | ||
| 702 | Doit::Commands->new(@commands); | ||
| 703 | } | ||
| 704 | |||
| 705 | sub cmd_copy { | ||
| 706 | my($self, @args) = @_; | ||
| 707 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 708 | my $quiet = delete $options{quiet}; | ||
| 709 | error "Unhandled options: " . join(" ", %options) if %options; | ||
| 710 | if (@args != 2) { | ||
| 711 | error "Expecting two arguments: from and to filenames"; | ||
| 712 | } | ||
| 713 | my($from, $to) = @args; | ||
| 714 | |||
| 715 | my $real_to; | ||
| 716 | if (-d $to) { | ||
| 717 | require File::Basename; | ||
| 718 | $real_to = "$to/" . File::Basename::basename($from); | ||
| 719 | } else { | ||
| 720 | $real_to = $to; | ||
| 721 | } | ||
| 722 | if (!-e $real_to || do { require File::Compare; File::Compare::compare($from, $real_to) != 0 }) { | ||
| 723 | my @commands = { | ||
| 724 | code => sub { | ||
| 725 | require File::Copy; | ||
| 726 | File::Copy::copy($from, $to) | ||
| 727 | or error "Copy failed: $!"; | ||
| 728 | }, | ||
| 729 | msg => do { | ||
| 730 | if (!-e $real_to) { | ||
| 731 | "copy $from $real_to (destination does not exist)"; | ||
| 732 | } else { | ||
| 733 | if ($quiet) { | ||
| 734 | "copy $from $real_to"; | ||
| 735 | } else { | ||
| 736 | "copy $from $real_to\ndiff:\n" . _diff_files($real_to, $from); | ||
| 737 | } | ||
| 738 | } | ||
| 739 | }, | ||
| 740 | rv => 1, | ||
| 741 | }; | ||
| 742 | Doit::Commands->new(@commands); | ||
| 743 | } else { | ||
| 744 | Doit::Commands->return_zero; | ||
| 745 | } | ||
| 746 | } | ||
| 747 | |||
| 748 | sub cmd_move { | ||
| 749 | my($self, $from, $to) = @_; | ||
| 750 | my @commands = { | ||
| 751 | code => sub { | ||
| 752 | require File::Copy; | ||
| 753 | File::Copy::move($from, $to) | ||
| 754 | or error "Move failed: $!"; | ||
| 755 | }, | ||
| 756 | msg => "move $from $to", | ||
| 757 | rv => 1, | ||
| 758 | }; | ||
| 759 | Doit::Commands->new(@commands); | ||
| 760 | } | ||
| 761 | |||
| 762 | sub _analyze_dollar_questionmark () { | ||
| 763 | if ($? == -1) { | ||
| 764 | ( | ||
| 765 | msg => sprintf("Could not execute command: %s", $!), | ||
| 766 | errno => $!, | ||
| 767 | exitcode => $?, | ||
| 768 | ); | ||
| 769 | } elsif ($? & 127) { | ||
| 770 | my $signalnum = $? & 127; | ||
| 771 | my $coredump = ($? & 128) ? 'with' : 'without'; | ||
| 772 | ( | ||
| 773 | msg => sprintf("Command died with signal %d, %s coredump", $signalnum, $coredump), | ||
| 774 | signalnum => $signalnum, | ||
| 775 | coredump => $coredump, | ||
| 776 | ); | ||
| 777 | } else { | ||
| 778 | my $exitcode = $?>>8; | ||
| 779 | ( | ||
| 780 | msg => "Command exited with exit code " . $exitcode, | ||
| 781 | exitcode => $exitcode, | ||
| 782 | ); | ||
| 783 | } | ||
| 784 | } | ||
| 785 | |||
| 786 | sub _handle_dollar_questionmark (@) { | ||
| 787 | my(%opts) = @_; | ||
| 788 | my $prefix_msg = delete $opts{prefix_msg}; | ||
| 789 | error "Unhandled options: " . join(" ", %opts) if %opts; | ||
| 790 | |||
| 791 | my %res = _analyze_dollar_questionmark; | ||
| 792 | my $msg = delete $res{msg}; | ||
| 793 | if (defined $prefix_msg) { | ||
| 794 | $msg = $prefix_msg.$msg; | ||
| 795 | } | ||
| 796 | Doit::Exception::throw($msg, %res); | ||
| 797 | } | ||
| 798 | |||
| 799 | sub _show_cwd ($) { | ||
| 800 | my $flag = shift; | ||
| 801 | if ($flag) { | ||
| 802 | require Cwd; | ||
| 803 | " (in " . Cwd::getcwd() . ")"; | ||
| 804 | } else { | ||
| 805 | ""; | ||
| 806 | } | ||
| 807 | } | ||
| 808 | |||
| 809 | sub _open2 { | ||
| 810 | my($instr, @args) = @_; | ||
| 811 | @args = Doit::Win32Util::win32_quote_list(@args) if Doit::IS_WIN; | ||
| 812 | |||
| 813 | require IPC::Open2; | ||
| 814 | |||
| 815 | my($chld_out, $chld_in); | ||
| 816 | my $pid = IPC::Open2::open2($chld_out, $chld_in, @args); | ||
| 817 | print $chld_in $instr; | ||
| 818 | close $chld_in; | ||
| 819 | local $/; | ||
| 820 | my $buf = <$chld_out>; | ||
| 821 | close $chld_out; | ||
| 822 | waitpid $pid, 0; | ||
| 823 | |||
| 824 | $buf; | ||
| 825 | } | ||
| 826 | |||
| 827 | sub cmd_open2 { | ||
| 828 | my($self, @args) = @_; | ||
| 829 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 830 | my $quiet = delete $options{quiet}; | ||
| 831 | my $info = delete $options{info}; | ||
| 832 | my $instr = delete $options{instr}; $instr = '' if !defined $instr; | ||
| 833 | error "Unhandled options: " . join(" ", %options) if %options; | ||
| 834 | |||
| 835 | my $code = sub { | ||
| 836 | my $buf = _open2($instr, @args); | ||
| 837 | $? == 0 | ||
| 838 | or _handle_dollar_questionmark($quiet||$info ? (prefix_msg => "open2 command '@args' failed: ") : ()); | ||
| 839 | $buf; | ||
| 840 | }; | ||
| 841 | |||
| 842 | my @commands; | ||
| 843 | push @commands, { | ||
| 844 | ($info ? (rv => $code->(), code => sub {}) : (code => $code)), | ||
| 845 | ($quiet ? () : (msg => "@args")), | ||
| 846 | }; | ||
| 847 | Doit::Commands->new(@commands); | ||
| 848 | } | ||
| 849 | |||
| 850 | sub cmd_info_open2 { | ||
| 851 | my($self, @args) = @_; | ||
| 852 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 853 | $options{info} = 1; | ||
| 854 | $self->cmd_open2(\%options, @args); | ||
| 855 | } | ||
| 856 | |||
| 857 | sub _open3 { | ||
| 858 | my($instr, @args) = @_; | ||
| 859 | @args = Doit::Win32Util::win32_quote_list(@args) if Doit::IS_WIN; | ||
| 860 | |||
| 861 | require IO::Select; | ||
| 862 | require IPC::Open3; | ||
| 863 | require Symbol; | ||
| 864 | |||
| 865 | my($chld_out, $chld_in, $chld_err); | ||
| 866 | $chld_err = Symbol::gensym(); | ||
| 867 | my $pid = IPC::Open3::open3((defined $instr ? $chld_in : undef), $chld_out, $chld_err, @args); | ||
| 868 | if (defined $instr) { | ||
| 869 | print $chld_in $instr; | ||
| 870 | close $chld_in; | ||
| 871 | } | ||
| 872 | |||
| 873 | my $sel = IO::Select->new; | ||
| 874 | $sel->add($chld_out); | ||
| 875 | $sel->add($chld_err); | ||
| 876 | |||
| 877 | my %buf = ($chld_out => '', $chld_err => ''); | ||
| 878 | while(my @ready_fhs = $sel->can_read()) { | ||
| 879 | for my $ready_fh (@ready_fhs) { | ||
| 880 | my $buf = ''; | ||
| 881 | while (sysread $ready_fh, $buf, 1024, length $buf) { } | ||
| 882 | if ($buf eq '') { # eof | ||
| 883 | $sel->remove($ready_fh); | ||
| 884 | $ready_fh->close; | ||
| 885 | last if $sel->count == 0; | ||
| 886 | } else { | ||
| 887 | $buf{$ready_fh} .= $buf; | ||
| 888 | } | ||
| 889 | } | ||
| 890 | } | ||
| 891 | |||
| 892 | waitpid $pid, 0; | ||
| 893 | |||
| 894 | ($buf{$chld_out}, $buf{$chld_err}); | ||
| 895 | } | ||
| 896 | |||
| 897 | sub cmd_open3 { | ||
| 898 | my($self, @args) = @_; | ||
| 899 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 900 | my $quiet = delete $options{quiet}; | ||
| 901 | my $info = delete $options{info}; | ||
| 902 | my $instr = delete $options{instr}; | ||
| 903 | my $errref = delete $options{errref}; | ||
| 904 | my $statusref = delete $options{statusref}; | ||
| 905 | error "Unhandled options: " . join(" ", %options) if %options; | ||
| 906 | |||
| 907 | my $code = sub { | ||
| 908 | my($stdout, $stderr) = _open3($instr, @args); | ||
| 909 | |||
| 910 | if ($errref) { | ||
| 911 | $$errref = $stderr; | ||
| 912 | } | ||
| 913 | |||
| 914 | if ($statusref) { | ||
| 915 | %$statusref = ( _analyze_dollar_questionmark ); | ||
| 916 | } else { | ||
| 917 | if ($? != 0) { | ||
| 918 | _handle_dollar_questionmark($quiet||$info ? (prefix_msg => "open3 command '@args' failed: ") : ()); | ||
| 919 | } | ||
| 920 | } | ||
| 921 | |||
| 922 | $stdout; | ||
| 923 | }; | ||
| 924 | |||
| 925 | my @commands; | ||
| 926 | push @commands, { | ||
| 927 | ($info ? (rv => $code->(), code => sub {}) : (code => $code)), | ||
| 928 | ($quiet ? () : (msg => "@args")), | ||
| 929 | }; | ||
| 930 | Doit::Commands->new(@commands); | ||
| 931 | } | ||
| 932 | |||
| 933 | sub cmd_info_open3 { | ||
| 934 | my($self, @args) = @_; | ||
| 935 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 936 | $options{info} = 1; | ||
| 937 | $self->cmd_open3(\%options, @args); | ||
| 938 | } | ||
| 939 | |||
| 940 | sub _qx { | ||
| 941 | my(@args) = @_; | ||
| 942 | @args = Doit::Win32Util::win32_quote_list(@args) if Doit::IS_WIN; | ||
| 943 | |||
| 944 | open my $fh, '-|', @args | ||
| 945 | or error "Error running '@args': $!"; | ||
| 946 | local $/; | ||
| 947 | my $buf = <$fh>; | ||
| 948 | close $fh; | ||
| 949 | \$buf; | ||
| 950 | } | ||
| 951 | |||
| 952 | sub cmd_qx { | ||
| 953 | my($self, @args) = @_; | ||
| 954 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 955 | my $quiet = delete $options{quiet}; | ||
| 956 | my $info = delete $options{info}; | ||
| 957 | my $statusref = delete $options{statusref}; | ||
| 958 | error "Unhandled options: " . join(" ", %options) if %options; | ||
| 959 | |||
| 960 | my $code = sub { | ||
| 961 | my $bufref = _qx(@args); | ||
| 962 | if ($statusref) { | ||
| 963 | %$statusref = ( _analyze_dollar_questionmark ); | ||
| 964 | } else { | ||
| 965 | if ($? != 0) { | ||
| 966 | _handle_dollar_questionmark($quiet||$info ? (prefix_msg => "qx command '@args' failed: ") : ()); | ||
| 967 | } | ||
| 968 | } | ||
| 969 | $$bufref; | ||
| 970 | }; | ||
| 971 | |||
| 972 | my @commands; | ||
| 973 | push @commands, { | ||
| 974 | ($info ? (rv => $code->(), code => sub {}) : (code => $code)), | ||
| 975 | ($quiet ? () : (msg => "@args")), | ||
| 976 | }; | ||
| 977 | Doit::Commands->new(@commands); | ||
| 978 | } | ||
| 979 | |||
| 980 | sub cmd_info_qx { | ||
| 981 | my($self, @args) = @_; | ||
| 982 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 983 | $options{info} = 1; | ||
| 984 | $self->cmd_qx(\%options, @args); | ||
| 985 | } | ||
| 986 | |||
| 987 | sub cmd_rmdir { | ||
| 988 | my($self, $directory) = @_; | ||
| 989 | if (-d $directory) { | ||
| 990 | my @commands = { | ||
| 991 | code => sub { rmdir $directory or error "$!" }, | ||
| 992 | msg => "rmdir $directory", | ||
| 993 | }; | ||
| 994 | Doit::Commands->new(@commands); | ||
| 995 | } else { | ||
| 996 | Doit::Commands->return_zero; | ||
| 997 | } | ||
| 998 | } | ||
| 999 | |||
| 1000 | sub cmd_run { | ||
| 1001 | my($self, @args) = @_; | ||
| 1002 | my @commands; | ||
| 1003 | push @commands, { | ||
| 1004 | code => sub { | ||
| 1005 | require IPC::Run; | ||
| 1006 | my $success = IPC::Run::run(@args); | ||
| 1007 | if (!$success) { | ||
| 1008 | _handle_dollar_questionmark; | ||
| 1009 | } | ||
| 1010 | }, | ||
| 1011 | msg => do { | ||
| 1012 | my @print_cmd; | ||
| 1013 | for my $arg (@args) { | ||
| 1014 | if (ref $arg eq 'ARRAY') { | ||
| 1015 | push @print_cmd, @$arg; | ||
| 1016 | } else { | ||
| 1017 | push @print_cmd, $arg; | ||
| 1018 | } | ||
| 1019 | } | ||
| 1020 | join " ", @print_cmd; | ||
| 1021 | }, | ||
| 1022 | rv => 1, | ||
| 1023 | }; | ||
| 1024 | Doit::Commands->new(@commands); | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | sub cmd_setenv { | ||
| 1028 | my($self, $key, $val) = @_; | ||
| 1029 | if (!defined $ENV{$key} || $ENV{$key} ne $val) { | ||
| 1030 | my @commands = { | ||
| 1031 | code => sub { $ENV{$key} = $val }, | ||
| 1032 | msg => qq{set \$ENV{$key} to "$val", previous value was } . (defined $ENV{$key} ? qq{"$ENV{$key}"} : qq{unset}), | ||
| 1033 | rv => 1, | ||
| 1034 | }; | ||
| 1035 | Doit::Commands->new(@commands); | ||
| 1036 | } else { | ||
| 1037 | Doit::Commands->return_zero; | ||
| 1038 | } | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | sub cmd_symlink { | ||
| 1042 | my($self, $oldfile, $newfile) = @_; | ||
| 1043 | my $doit; | ||
| 1044 | if (-l $newfile) { | ||
| 1045 | my $points_to = readlink $newfile | ||
| 1046 | or error "Unexpected: readlink $newfile failed (race condition?)"; | ||
| 1047 | if ($points_to ne $oldfile) { | ||
| 1048 | $doit = 1; | ||
| 1049 | } | ||
| 1050 | } elsif (!-e $newfile) { | ||
| 1051 | $doit = 1; | ||
| 1052 | } else { | ||
| 1053 | warning "$newfile exists but is not a symlink, will fail later..."; | ||
| 1054 | } | ||
| 1055 | if ($doit) { | ||
| 1056 | my @commands = { | ||
| 1057 | code => sub { symlink $oldfile, $newfile or error "$!" }, | ||
| 1058 | msg => "symlink $oldfile $newfile", | ||
| 1059 | rv => 1, | ||
| 1060 | }; | ||
| 1061 | Doit::Commands->new(@commands); | ||
| 1062 | } else { | ||
| 1063 | Doit::Commands->return_zero; | ||
| 1064 | } | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | sub cmd_system { | ||
| 1068 | my($self, @args) = @_; | ||
| 1069 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 1070 | my $quiet = delete $options{quiet}; | ||
| 1071 | my $info = delete $options{info}; | ||
| 1072 | my $show_cwd = delete $options{show_cwd}; | ||
| 1073 | error "Unhandled options: " . join(" ", %options) if %options; | ||
| 1074 | |||
| 1075 | @args = Doit::Win32Util::win32_quote_list(@args) if Doit::IS_WIN; | ||
| 1076 | |||
| 1077 | my $code = sub { | ||
| 1078 | system @args; | ||
| 1079 | if ($? != 0) { | ||
| 1080 | _handle_dollar_questionmark; | ||
| 1081 | } | ||
| 1082 | }; | ||
| 1083 | |||
| 1084 | my @commands; | ||
| 1085 | push @commands, { | ||
| 1086 | ($info | ||
| 1087 | ? ( | ||
| 1088 | rv => do { $code->(); 1 }, | ||
| 1089 | code => sub {}, | ||
| 1090 | ) | ||
| 1091 | : ( | ||
| 1092 | rv => 1, | ||
| 1093 | code => $code, | ||
| 1094 | ) | ||
| 1095 | ), | ||
| 1096 | ($quiet ? () : (msg => "@args" . _show_cwd($show_cwd))), | ||
| 1097 | }; | ||
| 1098 | Doit::Commands->new(@commands); | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | sub cmd_info_system { | ||
| 1102 | my($self, @args) = @_; | ||
| 1103 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 1104 | $options{info} = 1; | ||
| 1105 | $self->cmd_system(\%options, @args); | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | sub cmd_touch { | ||
| 1109 | my($self, @files) = @_; | ||
| 1110 | my @commands; | ||
| 1111 | for my $file (@files) { | ||
| 1112 | if (!-e $file) { | ||
| 1113 | push @commands, { | ||
| 1114 | code => sub { open my $fh, '>>', $file or error "$!" }, | ||
| 1115 | msg => "touch non-existent file $file", | ||
| 1116 | } | ||
| 1117 | } else { | ||
| 1118 | push @commands, { | ||
| 1119 | code => sub { utime time, time, $file or error "$!" }, | ||
| 1120 | msg => "touch existent file $file", | ||
| 1121 | }; | ||
| 1122 | } | ||
| 1123 | } | ||
| 1124 | my $doit_commands = Doit::Commands->new(@commands); | ||
| 1125 | $doit_commands->set_last_rv(scalar @files); | ||
| 1126 | $doit_commands; | ||
| 1127 | } | ||
| 1128 | |||
| 1129 | sub cmd_create_file_if_nonexisting { | ||
| 1130 | my($self, @files) = @_; | ||
| 1131 | my @commands; | ||
| 1132 | for my $file (@files) { | ||
| 1133 | if (!-e $file) { | ||
| 1134 | push @commands, { | ||
| 1135 | code => sub { open my $fh, '>>', $file or error "$!" }, | ||
| 1136 | msg => "create empty file $file", | ||
| 1137 | }; | ||
| 1138 | } | ||
| 1139 | } | ||
| 1140 | if (@commands) { | ||
| 1141 | my $doit_commands = Doit::Commands->new(@commands); | ||
| 1142 | $doit_commands->set_last_rv(scalar @commands); | ||
| 1143 | $doit_commands; | ||
| 1144 | } else { | ||
| 1145 | Doit::Commands->return_zero; | ||
| 1146 | } | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | sub cmd_unlink { | ||
| 1150 | my($self, @files) = @_; | ||
| 1151 | my @files_to_remove; | ||
| 1152 | for my $file (@files) { | ||
| 1153 | if (-e $file || -l $file) { | ||
| 1154 | push @files_to_remove, $file; | ||
| 1155 | } | ||
| 1156 | } | ||
| 1157 | if (@files_to_remove) { | ||
| 1158 | my @commands = { | ||
| 1159 | code => sub { unlink @files_to_remove or error "$!" }, | ||
| 1160 | msg => "unlink @files_to_remove", # shellquote? | ||
| 1161 | }; | ||
| 1162 | Doit::Commands->new(@commands); | ||
| 1163 | } else { | ||
| 1164 | Doit::Commands->return_zero; | ||
| 1165 | } | ||
| 1166 | } | ||
| 1167 | |||
| 1168 | sub cmd_unsetenv { | ||
| 1169 | my($self, $key) = @_; | ||
| 1170 | if (defined $ENV{$key}) { | ||
| 1171 | my @commands = { | ||
| 1172 | code => sub { delete $ENV{$key} }, | ||
| 1173 | msg => qq{unset \$ENV{$key}, previous value was "$ENV{$key}"}, | ||
| 1174 | rv => 1, | ||
| 1175 | }; | ||
| 1176 | Doit::Commands->new(@commands); | ||
| 1177 | } else { | ||
| 1178 | Doit::Commands->return_zero; | ||
| 1179 | } | ||
| 1180 | } | ||
| 1181 | |||
| 1182 | sub cmd_utime { | ||
| 1183 | my($self, $atime, $mtime, @files) = @_; | ||
| 1184 | |||
| 1185 | my $now; | ||
| 1186 | if (!defined $atime) { | ||
| 1187 | $atime = ($now ||= time); | ||
| 1188 | } | ||
| 1189 | if (!defined $mtime) { | ||
| 1190 | $mtime = ($now ||= time); | ||
| 1191 | } | ||
| 1192 | |||
| 1193 | my @files_to_change; | ||
| 1194 | for my $file (@files) { | ||
| 1195 | my @s = stat $file; | ||
| 1196 | if (@s) { | ||
| 1197 | if ($s[8] != $atime || $s[9] != $mtime) { | ||
| 1198 | push @files_to_change, $file; | ||
| 1199 | } | ||
| 1200 | } else { | ||
| 1201 | push @files_to_change, $file; # will fail later | ||
| 1202 | } | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | if (@files_to_change) { | ||
| 1206 | my @commands = { | ||
| 1207 | code => sub { | ||
| 1208 | my $changed_files = utime $atime, $mtime, @files; | ||
| 1209 | if ($changed_files != @files_to_change) { | ||
| 1210 | if (@files_to_change == 1) { | ||
| 1211 | error "utime failed: $!"; | ||
| 1212 | } elsif ($changed_files == 0) { | ||
| 1213 | error "utime failed on all files: $!"; | ||
| 1214 | } else { | ||
| 1215 | error "utime failed on some files (" . (@files_to_change-$changed_files) . "/" . scalar(@files_to_change) . "): $!"; | ||
| 1216 | } | ||
| 1217 | } | ||
| 1218 | }, | ||
| 1219 | msg => "utime $atime, $mtime, @files", | ||
| 1220 | rv => scalar @files_to_change, | ||
| 1221 | }; | ||
| 1222 | Doit::Commands->new(@commands); | ||
| 1223 | } else { | ||
| 1224 | Doit::Commands->return_zero; | ||
| 1225 | } | ||
| 1226 | } | ||
| 1227 | |||
| 1228 | sub cmd_which { | ||
| 1229 | my($self, @args) = @_; | ||
| 1230 | if (@args != 1) { | ||
| 1231 | error "Expecting exactly one argument: command"; | ||
| 1232 | } | ||
| 1233 | my $path = Doit::Util::is_in_path($args[0]); | ||
| 1234 | Doit::Commands->new({ rv => $path, code => sub {} }); | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | sub cmd_write_binary { | ||
| 1238 | my($self, @args) = @_; | ||
| 1239 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 1240 | my $quiet = delete $options{quiet} || 0; | ||
| 1241 | my $atomic = exists $options{atomic} ? delete $options{atomic} : 1; | ||
| 1242 | error "Unhandled options: " . join(" ", %options) if %options; | ||
| 1243 | if (@args != 2) { | ||
| 1244 | error "Expecting two arguments: filename and contents"; | ||
| 1245 | } | ||
| 1246 | my($filename, $content) = @args; | ||
| 1247 | |||
| 1248 | my $doit; | ||
| 1249 | my $need_diff; | ||
| 1250 | if (!-e $filename) { | ||
| 1251 | $doit = 1; | ||
| 1252 | } elsif (-s $filename != length($content)) { | ||
| 1253 | $doit = 1; | ||
| 1254 | $need_diff = 1; | ||
| 1255 | } else { | ||
| 1256 | open my $fh, '<', $filename | ||
| 1257 | or error "Can't open $filename: $!"; | ||
| 1258 | binmode $fh; | ||
| 1259 | local $/; | ||
| 1260 | my $file_content = <$fh>; | ||
| 1261 | if ($file_content ne $content) { | ||
| 1262 | $doit = 1; | ||
| 1263 | $need_diff = 1; | ||
| 1264 | } | ||
| 1265 | } | ||
| 1266 | |||
| 1267 | if ($doit) { | ||
| 1268 | my @commands = { | ||
| 1269 | code => sub { | ||
| 1270 | # XXX consider to reuse code for atomic writes: | ||
| 1271 | # either from Doit::File::file_atomic_write (problematic, different component) | ||
| 1272 | # or share code with change_file | ||
| 1273 | my $outfile = $atomic ? "$filename.$$.".time.".tmp" : $filename; | ||
| 1274 | open my $ofh, '>', $outfile | ||
| 1275 | or error "Can't write to $outfile: $!"; | ||
| 1276 | if (-e $filename) { | ||
| 1277 | Doit::Util::copy_stat($filename, $outfile, ownership => 1, mode => 1); | ||
| 1278 | } | ||
| 1279 | binmode $ofh; | ||
| 1280 | print $ofh $content; | ||
| 1281 | close $ofh | ||
| 1282 | or error "While closing $outfile: $!"; | ||
| 1283 | if ($atomic) { | ||
| 1284 | rename $outfile, $filename | ||
| 1285 | or error "Error while renaming $outfile to $filename: $!"; | ||
| 1286 | } | ||
| 1287 | }, | ||
| 1288 | rv => 1, | ||
| 1289 | ($quiet >= 2 | ||
| 1290 | ? () | ||
| 1291 | : (msg => do { | ||
| 1292 | if ($quiet) { | ||
| 1293 | if ($need_diff) { | ||
| 1294 | "Replace existing file $filename"; | ||
| 1295 | } else { | ||
| 1296 | "Create new file $filename"; | ||
| 1297 | } | ||
| 1298 | } else { | ||
| 1299 | if ($need_diff) { | ||
| 1300 | "Replace existing file $filename with diff:\n" . _diff_files($filename, \$content); | ||
| 1301 | } else { | ||
| 1302 | "Create new file $filename with content:\n$content"; | ||
| 1303 | } | ||
| 1304 | } | ||
| 1305 | } | ||
| 1306 | )), | ||
| 1307 | }; | ||
| 1308 | Doit::Commands->new(@commands); | ||
| 1309 | } else { | ||
| 1310 | Doit::Commands->return_zero; | ||
| 1311 | } | ||
| 1312 | } | ||
| 1313 | |||
| 1314 | sub cmd_change_file { | ||
| 1315 | my($self, @args) = @_; | ||
| 1316 | my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } } | ||
| 1317 | my $check = delete $options{check}; | ||
| 1318 | my $debug = delete $options{debug}; | ||
| 1319 | if ($check && ref $check ne 'CODE') { error "check parameter should be a CODE reference" } | ||
| 1320 | error "Unhandled options: " . join(" ", %options) if %options; | ||
| 1321 | |||
| 1322 | if (@args < 1) { | ||
| 1323 | error "Expecting at least a filename and one or more changes"; | ||
| 1324 | } | ||
| 1325 | |||
| 1326 | my($file, @changes) = @args; | ||
| 1327 | if (!-e $file) { | ||
| 1328 | error "$file does not exist"; | ||
| 1329 | } | ||
| 1330 | if (!-f $file) { | ||
| 1331 | error "$file is not a file"; | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | my @commands; | ||
| 1335 | |||
| 1336 | for (@changes) { | ||
| 1337 | if ($_->{add_if_missing}) { | ||
| 1338 | my $line = delete $_->{add_if_missing}; | ||
| 1339 | $_->{unless_match} = $line; | ||
| 1340 | if (defined $_->{add_after} || | ||
| 1341 | defined $_->{add_after_first} || | ||
| 1342 | defined $_->{add_before} || | ||
| 1343 | defined $_->{add_before_last} | ||
| 1344 | ) { | ||
| 1345 | my $defines = | ||
| 1346 | (defined $_->{add_after} || 0) + | ||
| 1347 | (defined $_->{add_after_first} || 0) + | ||
| 1348 | (defined $_->{add_before} || 0) + | ||
| 1349 | (defined $_->{add_before_last} || 0) | ||
| 1350 | ; | ||
| 1351 | if ($defines != 1) { | ||
| 1352 | error "Can specify only one of the following: 'add_after', 'add_after_first', 'add_before', 'add_before_last' (change for $file)\n"; | ||
| 1353 | } | ||
| 1354 | my $add; | ||
| 1355 | my $do_after; | ||
| 1356 | my $reverse; | ||
| 1357 | if (defined $_->{add_after}) { | ||
| 1358 | $add = delete $_->{add_after}; | ||
| 1359 | $reverse = 1; | ||
| 1360 | $do_after = 1; | ||
| 1361 | } elsif (defined $_->{add_after_first}) { | ||
| 1362 | $add = delete $_->{add_after_first}; | ||
| 1363 | $reverse = 0; | ||
| 1364 | $do_after = 1; | ||
| 1365 | } elsif (defined $_->{add_before}) { | ||
| 1366 | $add = delete $_->{add_before}; | ||
| 1367 | $reverse = 0; | ||
| 1368 | $do_after = 0; | ||
| 1369 | } elsif (defined $_->{add_before_last}) { | ||
| 1370 | $add = delete $_->{add_before_last}; | ||
| 1371 | $reverse = 1; | ||
| 1372 | $do_after = 0; | ||
| 1373 | } else { | ||
| 1374 | error "Can never happen"; | ||
| 1375 | } | ||
| 1376 | qr{$add}; # must be a regexp | ||
| 1377 | $_->{action} = sub { | ||
| 1378 | my $arrayref = $_[0]; | ||
| 1379 | my $found = 0; | ||
| 1380 | my $from = $reverse ? $#$arrayref : 0; | ||
| 1381 | my $to = $reverse ? 0 : $#$arrayref; | ||
| 1382 | my $inc = $reverse ? -1 : +1; | ||
| 1383 | for(my $i=$from; ($reverse ? $i>=$to : $i<=$to); $i+=$inc) { | ||
| 1384 | if ($arrayref->[$i] =~ $add) { | ||
| 1385 | if ($do_after) { | ||
| 1386 | splice @$arrayref, $i+1, 0, $line; | ||
| 1387 | } else { | ||
| 1388 | splice @$arrayref, $i, 0, $line; | ||
| 1389 | } | ||
| 1390 | $found = 1; | ||
| 1391 | last; | ||
| 1392 | } | ||
| 1393 | } | ||
| 1394 | if (!$found) { | ||
| 1395 | error "Cannot find '$add' in file"; | ||
| 1396 | } | ||
| 1397 | }; | ||
| 1398 | } else { | ||
| 1399 | $_->{action} = sub { my $arrayref = $_[0]; push @$arrayref, $line }; | ||
| 1400 | } | ||
| 1401 | } | ||
| 1402 | } | ||
| 1403 | |||
| 1404 | my @match_actions; | ||
| 1405 | my @unless_match_actions; | ||
| 1406 | for (@changes) { | ||
| 1407 | if ($_->{unless_match}) { | ||
| 1408 | if (ref $_->{unless_match} ne 'Regexp') { | ||
| 1409 | my $rx = '^' . quotemeta($_->{unless_match}) . '$'; | ||
| 1410 | $_->{unless_match} = qr{$rx}; | ||
| 1411 | } | ||
| 1412 | if (!$_->{action}) { | ||
| 1413 | error "action is missing"; | ||
| 1414 | } | ||
| 1415 | if (ref $_->{action} ne 'CODE') { | ||
| 1416 | error "action must be a sub reference"; | ||
| 1417 | } | ||
| 1418 | push @unless_match_actions, $_; | ||
| 1419 | } elsif ($_->{match}) { | ||
| 1420 | if (ref $_->{match} ne 'Regexp') { | ||
| 1421 | my $rx = '^' . quotemeta($_->{match}) . '$'; | ||
| 1422 | $_->{match} = qr{$rx}; | ||
| 1423 | } | ||
| 1424 | my $consequences = ($_->{action}?1:0) + (defined $_->{replace}?1:0) + (defined $_->{delete}?1:0); | ||
| 1425 | if ($consequences != 1) { | ||
| 1426 | error "Exactly one of the following is missing: action, replace, or delete"; | ||
| 1427 | } | ||
| 1428 | if ($_->{action}) { | ||
| 1429 | if (ref $_->{action} ne 'CODE') { | ||
| 1430 | error "action must be a sub reference"; | ||
| 1431 | } | ||
| 1432 | } elsif (defined $_->{replace}) { | ||
| 1433 | # accept | ||
| 1434 | } elsif (defined $_->{delete}) { | ||
| 1435 | # accept | ||
| 1436 | } else { | ||
| 1437 | error "FATAL: should never happen"; | ||
| 1438 | } | ||
| 1439 | push @match_actions, $_; | ||
| 1440 | } else { | ||
| 1441 | error "match or unless_match is missing"; | ||
| 1442 | } | ||
| 1443 | } | ||
| 1444 | |||
| 1445 | require File::Temp; | ||
| 1446 | require File::Basename; | ||
| 1447 | require File::Copy; | ||
| 1448 | my($tmpfh,$tmpfile) = File::Temp::tempfile('doittemp_XXXXXXXX', UNLINK => 1, DIR => File::Basename::dirname($file)); | ||
| 1449 | File::Copy::copy($file, $tmpfile) | ||
| 1450 | or error "failed to copy $file to temporary file $tmpfile: $!"; | ||
| 1451 | Doit::Util::copy_stat($file, $tmpfile); | ||
| 1452 | |||
| 1453 | require Tie::File; | ||
| 1454 | tie my @lines, 'Tie::File', $tmpfile | ||
| 1455 | or error "cannot tie file $file: $!"; | ||
| 1456 | |||
| 1457 | my $no_of_changes = 0; | ||
| 1458 | for my $match_action (@match_actions) { | ||
| 1459 | my $match = $match_action->{match}; | ||
| 1460 | for(my $line_i=0; $line_i<=$#lines; $line_i++) { | ||
| 1461 | if ($debug) { info "change_file check '$lines[$line_i]' =~ '$match'" } | ||
| 1462 | if ($lines[$line_i] =~ $match) { | ||
| 1463 | if (exists $match_action->{replace}) { | ||
| 1464 | my $replace = $match_action->{replace}; | ||
| 1465 | if ($lines[$line_i] ne $replace) { | ||
| 1466 | push @commands, { msg => "replace '$lines[$line_i]' with '$replace' in '$file'" }; | ||
| 1467 | $lines[$line_i] = $replace; | ||
| 1468 | $no_of_changes++; | ||
| 1469 | } | ||
| 1470 | } elsif (exists $match_action->{delete}) { | ||
| 1471 | if ($match_action->{delete}) { | ||
| 1472 | push @commands, { msg => "delete '$lines[$line_i]' in '$file'" }; | ||
| 1473 | splice @lines, $line_i, 1; | ||
| 1474 | $line_i--; | ||
| 1475 | $no_of_changes++; | ||
| 1476 | } | ||
| 1477 | } else { | ||
| 1478 | push @commands, { msg => "matched '$match' on line '$lines[$line_i]' in '$file' -> execute action" }; | ||
| 1479 | my $action = $match_action->{action}; | ||
| 1480 | $action->($lines[$line_i]); | ||
| 1481 | $no_of_changes++; | ||
| 1482 | } | ||
| 1483 | } | ||
| 1484 | } | ||
| 1485 | } | ||
| 1486 | ITER: for my $unless_match_action (@unless_match_actions) { | ||
| 1487 | my $match = $unless_match_action->{unless_match}; | ||
| 1488 | for my $line (@lines) { | ||
| 1489 | if ($line =~ $match) { | ||
| 1490 | next ITER; | ||
| 1491 | } | ||
| 1492 | } | ||
| 1493 | push @commands, { msg => "did not find '$match' in '$file' -> execute action" }; | ||
| 1494 | my $action = $unless_match_action->{action}; | ||
| 1495 | $action->(\@lines); | ||
| 1496 | $no_of_changes++; | ||
| 1497 | } | ||
| 1498 | |||
| 1499 | untie @lines; | ||
| 1500 | close $tmpfh; | ||
| 1501 | |||
| 1502 | if ($no_of_changes) { | ||
| 1503 | push @commands, { | ||
| 1504 | code => sub { | ||
| 1505 | if ($check) { | ||
| 1506 | # XXX maybe it would be good to pass the Doit::Runner object, | ||
| 1507 | # but unfortunately it's not available at this point --- | ||
| 1508 | # maybe the code sub should generally get it as first argument? | ||
| 1509 | $check->($tmpfile) | ||
| 1510 | or error "Check on file $file failed"; | ||
| 1511 | } | ||
| 1512 | rename $tmpfile, $file | ||
| 1513 | or error "Can't rename $tmpfile to $file: $!"; | ||
| 1514 | }, | ||
| 1515 | msg => do { | ||
| 1516 | "Final changes as diff:\n" . _diff_files($file, $tmpfile); | ||
| 1517 | }, | ||
| 1518 | rv => $no_of_changes, | ||
| 1519 | }; | ||
| 1520 | } | ||
| 1521 | |||
| 1522 | if ($no_of_changes) { | ||
| 1523 | Doit::Commands->new(@commands); | ||
| 1524 | } else { | ||
| 1525 | Doit::Commands->return_zero; | ||
| 1526 | } | ||
| 1527 | } | ||
| 1528 | |||
| 1529 | sub _diff_files { | ||
| 1530 | my($file1, $file2) = @_; | ||
| 1531 | |||
| 1532 | my $stdin; | ||
| 1533 | if (ref $file2) { | ||
| 1534 | $stdin = $$file2; | ||
| 1535 | $file2 = '-'; | ||
| 1536 | } | ||
| 1537 | |||
| 1538 | if (!@diff_cmd) { | ||
| 1539 | my @diff_candidates = (['diff', '-u']); | ||
| 1540 | if ($^O eq 'MSWin32') { | ||
| 1541 | push @diff_candidates, ['fc']; | ||
| 1542 | } | ||
| 1543 | for my $diff_candidate (@diff_candidates) { | ||
| 1544 | if (Doit::Util::is_in_path($diff_candidate->[0])) { | ||
| 1545 | @diff_cmd = @$diff_candidate; | ||
| 1546 | last; | ||
| 1547 | } | ||
| 1548 | } | ||
| 1549 | return "(diff not available" . (!$diff_error_shown++ ? ", error: none of the candidates (" . join(", ", map { $_->[0] } @diff_candidates) . ") exist" : "") . ")" | ||
| 1550 | if !@diff_cmd; | ||
| 1551 | } | ||
| 1552 | |||
| 1553 | my $cannot_use_dash; | ||
| 1554 | if ($^O eq 'MSWin32' && $diff_cmd[0] eq 'fc') { # FC cannot handle forward slashes | ||
| 1555 | s{/}{\\}g for ($file1, $file2); | ||
| 1556 | if ($file2 eq '-') { | ||
| 1557 | $cannot_use_dash = 1; | ||
| 1558 | } | ||
| 1559 | } | ||
| 1560 | |||
| 1561 | my($diff, $diff_stderr); | ||
| 1562 | if (!$cannot_use_dash && eval { require IPC::Run; 1 }) { | ||
| 1563 | if (!eval { | ||
| 1564 | IPC::Run::run([@diff_cmd, $file1, $file2], (defined $stdin ? ('<', \$stdin) : ()), '>', \$diff, '2>', \$diff_stderr); 1; | ||
| 1565 | }) { | ||
| 1566 | $diff = "(diff not available" . (!$diff_error_shown++ ? ", error: $@" : "") . ")"; | ||
| 1567 | $diff_stderr = ''; | ||
| 1568 | } | ||
| 1569 | } else { | ||
| 1570 | if ($^O eq 'MSWin32' || $cannot_use_dash) { # list systems with unreliable IPC::Open3 here | ||
| 1571 | my $tmp; | ||
| 1572 | if ($file2 eq '-') { | ||
| 1573 | require File::Temp; | ||
| 1574 | $tmp = File::Temp->new; | ||
| 1575 | binmode($tmp); # XXX yes or no? | ||
| 1576 | $tmp->print($stdin); | ||
| 1577 | $tmp->close; | ||
| 1578 | $file2 = "$tmp"; | ||
| 1579 | } | ||
| 1580 | my $diffref = _qx(@diff_cmd, $file1, $file2); | ||
| 1581 | $diff = $$diffref; | ||
| 1582 | $diff_stderr = ''; | ||
| 1583 | } else { | ||
| 1584 | ($diff, $diff_stderr) = eval { _open3($stdin, @diff_cmd, $file1, $file2) }; | ||
| 1585 | if ($@) { | ||
| 1586 | $diff = "(diff not available" . (!$diff_error_shown++ ? ", error: $@" : "") . ")"; | ||
| 1587 | } | ||
| 1588 | } | ||
| 1589 | } | ||
| 1590 | "$diff$diff_stderr"; | ||
| 1591 | } | ||
| 1592 | |||
| 1593 | } | ||
| 1594 | |||
| 1595 | { | ||
| 1596 | package Doit::Commands; | ||
| 1597 | sub new { | ||
| 1598 | my($class, @commands) = @_; | ||
| 1599 | my $self = bless \@commands, $class; | ||
| 1600 | $self; | ||
| 1601 | } | ||
| 1602 | sub return_zero { | ||
| 1603 | my $class = shift; | ||
| 1604 | $class->new({ code => sub {}, rv => 0 }); | ||
| 1605 | } | ||
| 1606 | sub commands { @{$_[0]} } | ||
| 1607 | sub set_last_rv { | ||
| 1608 | my($self, $rv) = @_; | ||
| 1609 | my @commands = $self->commands; | ||
| 1610 | if (@commands) { | ||
| 1611 | $commands[-1]->{rv} = $rv; | ||
| 1612 | } | ||
| 1613 | } | ||
| 1614 | sub doit { | ||
| 1615 | my($self) = @_; | ||
| 1616 | my $rv; | ||
| 1617 | for my $command ($self->commands) { | ||
| 1618 | if (exists $command->{msg}) { | ||
| 1619 | Doit::Log::info($command->{msg}); | ||
| 1620 | } | ||
| 1621 | if (exists $command->{code}) { | ||
| 1622 | my $this_rv = $command->{code}->(); | ||
| 1623 | if (exists $command->{rv}) { | ||
| 1624 | $rv = $command->{rv}; | ||
| 1625 | } else { | ||
| 1626 | $rv = $this_rv; | ||
| 1627 | } | ||
| 1628 | } | ||
| 1629 | } | ||
| 1630 | $rv; | ||
| 1631 | } | ||
| 1632 | sub show { | ||
| 1633 | my($self) = @_; | ||
| 1634 | my $rv; | ||
| 1635 | for my $command ($self->commands) { | ||
| 1636 | if (exists $command->{msg}) { | ||
| 1637 | Doit::Log::info($command->{msg} . " (dry-run)"); | ||
| 1638 | } | ||
| 1639 | if (exists $command->{code}) { | ||
| 1640 | if (exists $command->{rv}) { | ||
| 1641 | $rv = $command->{rv}; | ||
| 1642 | } else { | ||
| 1643 | # Well, in dry-run mode we have no real return value... | ||
| 1644 | } | ||
| 1645 | } | ||
| 1646 | } | ||
| 1647 | $rv; | ||
| 1648 | } | ||
| 1649 | } | ||
| 1650 | |||
| 1651 | { | ||
| 1652 | package Doit::Runner; | ||
| 1653 | sub new { | ||
| 1654 | my($class, $Doit, %options) = @_; | ||
| 1655 | my $dryrun = delete $options{dryrun}; | ||
| 1656 | die "Unhandled options: " . join(" ", %options) if %options; | ||
| 1657 | bless { Doit => $Doit, dryrun => $dryrun, components => [] }, $class; | ||
| 1658 | } | ||
| 1659 | sub is_dry_run { shift->{dryrun} } | ||
| 1660 | |||
| 1661 | sub can_ipc_run { eval { require IPC::Run; 1 } } | ||
| 1662 | |||
| 1663 | sub install_generic_cmd { | ||
| 1664 | my($self, $name, @args) = @_; | ||
| 1665 | $self->{Doit}->install_generic_cmd($name, @args); | ||
| 1666 | __PACKAGE__->install_cmd($name); | ||
| 1667 | } | ||
| 1668 | |||
| 1669 | sub install_cmd { | ||
| 1670 | shift; # $class unused | ||
| 1671 | my $cmd = shift; | ||
| 1672 | my $meth = 'cmd_' . $cmd; | ||
| 1673 | my $code = sub { | ||
| 1674 | my($self, @args) = @_; | ||
| 1675 | if ($self->{dryrun}) { | ||
| 1676 | $self->{Doit}->$meth(@args)->show; | ||
| 1677 | } else { | ||
| 1678 | $self->{Doit}->$meth(@args)->doit; | ||
| 1679 | } | ||
| 1680 | }; | ||
| 1681 | no strict 'refs'; | ||
| 1682 | *{$cmd} = $code; | ||
| 1683 | } | ||
| 1684 | |||
| 1685 | sub add_component { | ||
| 1686 | my($self, $component_or_module) = @_; | ||
| 1687 | my $module; | ||
| 1688 | if ($component_or_module =~ /::/) { | ||
| 1689 | $module = $component_or_module; | ||
| 1690 | } else { | ||
| 1691 | $module = 'Doit::' . ucfirst($component_or_module); | ||
| 1692 | } | ||
| 1693 | |||
| 1694 | for (@{ $self->{components} }) { | ||
| 1695 | return if $_->{module} eq $module; | ||
| 1696 | } | ||
| 1697 | |||
| 1698 | if (!eval qq{ require $module; 1 }) { | ||
| 1699 | Doit::Log::error("Cannot load $module: $@"); | ||
| 1700 | } | ||
| 1701 | my $o = $module->new | ||
| 1702 | or Doit::Log::error("Error while calling $module->new"); | ||
| 1703 | for my $function ($o->functions) { | ||
| 1704 | my $fullqual = $module.'::'.$function; | ||
| 1705 | my $code = sub { | ||
| 1706 | my($self, @args) = @_; | ||
| 1707 | $self->$fullqual(@args); | ||
| 1708 | }; | ||
| 1709 | no strict 'refs'; | ||
| 1710 | *{$function} = $code; | ||
| 1711 | } | ||
| 1712 | my $mod_file = do { | ||
| 1713 | (my $relpath = $module) =~ s{::}{/}; | ||
| 1714 | $relpath .= '.pm'; | ||
| 1715 | }; | ||
| 1716 | push @{ $self->{components} }, { module => $module, path => $INC{$mod_file}, relpath => $mod_file }; | ||
| 1717 | |||
| 1718 | if ($o->can('add_components')) { | ||
| 1719 | for my $sub_component ($o->add_components) { | ||
| 1720 | $self->add_component($sub_component); | ||
| 1721 | } | ||
| 1722 | } | ||
| 1723 | } | ||
| 1724 | |||
| 1725 | for my $cmd ( | ||
| 1726 | qw(chmod chown mkdir rename rmdir symlink unlink utime), | ||
| 1727 | qw(make_path remove_tree), # File::Path | ||
| 1728 | qw(copy move), # File::Copy | ||
| 1729 | qw(run), # IPC::Run | ||
| 1730 | qw(qx info_qx), # qx// and variant which even runs in dry-run mode, both using list syntax | ||
| 1731 | qw(open2 info_open2), # IPC::Open2 | ||
| 1732 | qw(open3 info_open3), # IPC::Open3 | ||
| 1733 | qw(system info_system), # builtin system with variant | ||
| 1734 | qw(cond_run), # conditional run | ||
| 1735 | qw(touch), # like unix touch | ||
| 1736 | qw(ln_nsf), # like unix ln -nsf | ||
| 1737 | qw(which), # like unix which | ||
| 1738 | qw(create_file_if_nonexisting), # does the half of touch | ||
| 1739 | qw(write_binary), # like File::Slurper | ||
| 1740 | qw(change_file), # own invention | ||
| 1741 | qw(setenv unsetenv), # $ENV manipulation | ||
| 1742 | ) { | ||
| 1743 | __PACKAGE__->install_cmd($cmd); | ||
| 1744 | } | ||
| 1745 | |||
| 1746 | sub call_wrapped_method { | ||
| 1747 | my($self, $context, $method, @args) = @_; | ||
| 1748 | my @ret; | ||
| 1749 | if ($context eq 'a') { | ||
| 1750 | @ret = eval { $self->$method(@args) }; | ||
| 1751 | } else { | ||
| 1752 | $ret[0] = eval { $self->$method(@args) }; | ||
| 1753 | } | ||
| 1754 | if ($@) { | ||
| 1755 | ('e', $@); | ||
| 1756 | } else { | ||
| 1757 | ('r', @ret); | ||
| 1758 | } | ||
| 1759 | } | ||
| 1760 | |||
| 1761 | # XXX call vs. call_with_runner ??? | ||
| 1762 | sub call { | ||
| 1763 | my($self, $sub, @args) = @_; | ||
| 1764 | $sub = 'main::' . $sub if $sub !~ /::/; | ||
| 1765 | no strict 'refs'; | ||
| 1766 | &$sub(@args); | ||
| 1767 | } | ||
| 1768 | |||
| 1769 | sub call_with_runner { | ||
| 1770 | my($self, $sub, @args) = @_; | ||
| 1771 | $sub = 'main::' . $sub if $sub !~ /::/; | ||
| 1772 | no strict 'refs'; | ||
| 1773 | &$sub($self, @args); | ||
| 1774 | } | ||
| 1775 | |||
| 1776 | # XXX does this belong here? | ||
| 1777 | sub do_ssh_connect { | ||
| 1778 | my($self, $host, %opts) = @_; | ||
| 1779 | my $remote = Doit::SSH->do_connect($host, dry_run => $self->is_dry_run, components => $self->{components}, %opts); | ||
| 1780 | $remote; | ||
| 1781 | } | ||
| 1782 | |||
| 1783 | # XXX does this belong here? | ||
| 1784 | sub do_sudo { | ||
| 1785 | my($self, %opts) = @_; | ||
| 1786 | my $sudo = Doit::Sudo->do_connect(dry_run => $self->is_dry_run, components => $self->{components}, %opts); | ||
| 1787 | $sudo; | ||
| 1788 | } | ||
| 1789 | } | ||
| 1790 | |||
| 1791 | { | ||
| 1792 | package Doit::RPC; | ||
| 1793 | |||
| 1794 | require Storable; | ||
| 1795 | require IO::Handle; | ||
| 1796 | |||
| 1797 | use Doit::Log; | ||
| 1798 | |||
| 1799 | sub new { | ||
| 1800 | die "Please use either Doit::RPC::Client, Doit::RPC::Server or Doit::RPC::SimpleServer"; | ||
| 1801 | } | ||
| 1802 | |||
| 1803 | sub runner { shift->{runner} } | ||
| 1804 | |||
| 1805 | sub receive_data { | ||
| 1806 | my($self) = @_; | ||
| 1807 | my $fh = $self->{infh}; | ||
| 1808 | my $buf; | ||
| 1809 | my $ret = read $fh, $buf, 4; | ||
| 1810 | if (!defined $ret) { | ||
| 1811 | die "receive_data failed (getting length): $!"; | ||
| 1812 | } elsif (!$ret) { | ||
| 1813 | return; # eof | ||
| 1814 | } | ||
| 1815 | my $length = unpack("N", $buf); | ||
| 1816 | read $fh, $buf, $length or die "receive_data failed (getting data): $!"; | ||
| 1817 | @{ Storable::thaw($buf) }; | ||
| 1818 | } | ||
| 1819 | |||
| 1820 | sub send_data { | ||
| 1821 | my($self, @cmd) = @_; | ||
| 1822 | my $fh = $self->{outfh}; | ||
| 1823 | my $data = Storable::nfreeze(\@cmd); | ||
| 1824 | print $fh pack("N", length($data)) . $data; | ||
| 1825 | } | ||
| 1826 | |||
| 1827 | { | ||
| 1828 | my $done_POSIX_warning; | ||
| 1829 | sub _reap_process { | ||
| 1830 | my($self, $pid) = @_; | ||
| 1831 | return if !defined $pid; | ||
| 1832 | if (eval { require POSIX; defined &POSIX::WNOHANG }) { | ||
| 1833 | if ($self->{debug}) { | ||
| 1834 | info "Reaping process $pid..."; | ||
| 1835 | } | ||
| 1836 | my $start_time = time; | ||
| 1837 | my $got_pid = Doit::RPC::gentle_retry( | ||
| 1838 | code => sub { | ||
| 1839 | waitpid $pid, &POSIX::WNOHANG; | ||
| 1840 | }, | ||
| 1841 | retry_msg_code => sub { | ||
| 1842 | my($seconds) = @_; | ||
| 1843 | if (time - $start_time >= 2) { | ||
| 1844 | info "can't reap process $pid, sleep for $seconds seconds"; | ||
| 1845 | } | ||
| 1846 | }, | ||
| 1847 | fast_sleep => 0.01, | ||
| 1848 | ); | ||
| 1849 | if (!$got_pid) { | ||
| 1850 | warning "Could not reap process $pid..."; | ||
| 1851 | } | ||
| 1852 | } else { | ||
| 1853 | if (!$done_POSIX_warning++) { | ||
| 1854 | warning "Can't require POSIX, cannot reap zombies..." | ||
| 1855 | } | ||
| 1856 | } | ||
| 1857 | } | ||
| 1858 | } | ||
| 1859 | |||
| 1860 | sub gentle_retry { | ||
| 1861 | my(%opts) = @_; | ||
| 1862 | my $code = delete $opts{code} || die "code is mandatory"; | ||
| 1863 | my $tries = delete $opts{tries} || 20; | ||
| 1864 | my $fast_tries = delete $opts{fast_tries} || int($tries/2); | ||
| 1865 | my $slow_sleep = delete $opts{slow_sleep} || 1; | ||
| 1866 | my $fast_sleep = delete $opts{fast_sleep} || 0.1; | ||
| 1867 | my $retry_msg_code = delete $opts{retry_msg_code}; | ||
| 1868 | my $fail_info_ref = delete $opts{fail_info_ref}; | ||
| 1869 | die "Unhandled options: " . join(" ", %opts) if %opts; | ||
| 1870 | |||
| 1871 | for my $try (1..$tries) { | ||
| 1872 | my $ret = $code->(fail_info_ref => $fail_info_ref, try => $try); | ||
| 1873 | return $ret if $ret; | ||
| 1874 | my $sleep_sub; | ||
| 1875 | if ($fast_tries && eval { require Time::HiRes; 1 }) { | ||
| 1876 | $sleep_sub = \&Time::HiRes::sleep; | ||
| 1877 | } else { | ||
| 1878 | $sleep_sub = sub { sleep $_[0] }; | ||
| 1879 | } | ||
| 1880 | my $seconds = $try <= $fast_tries && defined &Time::HiRes::sleep ? $fast_sleep : $slow_sleep; | ||
| 1881 | $retry_msg_code->($seconds) if $retry_msg_code; | ||
| 1882 | $sleep_sub->($seconds); | ||
| 1883 | } | ||
| 1884 | |||
| 1885 | undef; | ||
| 1886 | } | ||
| 1887 | |||
| 1888 | } | ||
| 1889 | |||
| 1890 | { | ||
| 1891 | package Doit::RPC::Client; | ||
| 1892 | our @ISA = ('Doit::RPC'); | ||
| 1893 | |||
| 1894 | sub new { | ||
| 1895 | my($class, $infh, $outfh, %options) = @_; | ||
| 1896 | |||
| 1897 | my $debug = delete $options{debug}; | ||
| 1898 | my $label = delete $options{label}; | ||
| 1899 | die "Unhandled options: " . join(" ", %options) if %options; | ||
| 1900 | |||
| 1901 | $outfh->autoflush(1); | ||
| 1902 | bless { | ||
| 1903 | infh => $infh, | ||
| 1904 | outfh => $outfh, | ||
| 1905 | label => $label, | ||
| 1906 | debug => $debug, | ||
| 1907 | }, $class; | ||
| 1908 | } | ||
| 1909 | |||
| 1910 | # Call for every command on client | ||
| 1911 | sub call_remote { | ||
| 1912 | my($self, @args) = @_; | ||
| 1913 | my $context = wantarray ? 'a' : 's'; # XXX more possible context (void...)? | ||
| 1914 | $self->send_data($context, @args); | ||
| 1915 | my($rettype, @ret) = $self->receive_data(@args); | ||
| 1916 | if (defined $rettype && $rettype eq 'e') { | ||
| 1917 | die $ret[0]; | ||
| 1918 | } elsif (defined $rettype && $rettype eq 'r') { | ||
| 1919 | if ($context eq 'a') { | ||
| 1920 | return @ret; | ||
| 1921 | } else { | ||
| 1922 | return $ret[0]; | ||
| 1923 | } | ||
| 1924 | } else { | ||
| 1925 | die "Unexpected return type " . (defined $self->{label} ? "in connection '$self->{label}' " : "") . (defined $rettype ? "'$rettype'" : "<undefined>") . " (should be 'e' or 'r')"; | ||
| 1926 | } | ||
| 1927 | } | ||
| 1928 | } | ||
| 1929 | |||
| 1930 | { | ||
| 1931 | package Doit::RPC::Server; | ||
| 1932 | our @ISA = ('Doit::RPC'); | ||
| 1933 | |||
| 1934 | sub new { | ||
| 1935 | my($class, $runner, $sockpath, %options) = @_; | ||
| 1936 | |||
| 1937 | my $debug = delete $options{debug}; | ||
| 1938 | my $excl = delete $options{excl}; | ||
| 1939 | die "Unhandled options: " . join(" ", %options) if %options; | ||
| 1940 | |||
| 1941 | bless { | ||
| 1942 | runner => $runner, | ||
| 1943 | sockpath => $sockpath, | ||
| 1944 | debug => $debug, | ||
| 1945 | excl => $excl, | ||
| 1946 | }, $class; | ||
| 1947 | } | ||
| 1948 | |||
| 1949 | sub run { | ||
| 1950 | my($self) = @_; | ||
| 1951 | |||
| 1952 | require IO::Socket::UNIX; | ||
| 1953 | IO::Socket::UNIX->VERSION('1.18'); # autoflush | ||
| 1954 | IO::Socket::UNIX->import(qw(SOCK_STREAM)); | ||
| 1955 | use IO::Select; | ||
| 1956 | |||
| 1957 | my $d; | ||
| 1958 | if ($self->{debug}) { | ||
| 1959 | $d = sub ($) { | ||
| 1960 | Doit::Log::info("WORKER: $_[0]"); | ||
| 1961 | }; | ||
| 1962 | } else { | ||
| 1963 | $d = sub ($) { }; | ||
| 1964 | } | ||
| 1965 | |||
| 1966 | $d->("Start worker ($$)..."); | ||
| 1967 | my $sockpath = $self->{sockpath}; | ||
| 1968 | if (!$self->{excl} && -e $sockpath) { | ||
| 1969 | $d->("unlink socket $sockpath"); | ||
| 1970 | unlink $sockpath; | ||
| 1971 | } | ||
| 1972 | my $sock = IO::Socket::UNIX->new( | ||
| 1973 | Type => SOCK_STREAM(), | ||
| 1974 | Local => $sockpath, | ||
| 1975 | Listen => 1, | ||
| 1976 | ) or die "WORKER: Can't create socket: $!"; | ||
| 1977 | $d->("socket was created"); | ||
| 1978 | |||
| 1979 | my $sel = IO::Select->new($sock); | ||
| 1980 | $d->("waiting for client"); | ||
| 1981 | my @ready = $sel->can_read(); | ||
| 1982 | die "WORKER: unexpected filehandle @ready" if $ready[0] != $sock; | ||
| 1983 | $d->("accept socket"); | ||
| 1984 | my $fh = $sock->accept; | ||
| 1985 | $self->{infh} = $self->{outfh} = $fh; | ||
| 1986 | while () { | ||
| 1987 | $d->(" waiting for line from comm"); | ||
| 1988 | my($context, @data) = $self->receive_data; | ||
| 1989 | if (!defined $context) { | ||
| 1990 | $d->(" got eof"); | ||
| 1991 | $fh->close; | ||
| 1992 | return; | ||
| 1993 | } elsif ($data[0] =~ m{^exit$}) { | ||
| 1994 | $d->(" got exit command"); | ||
| 1995 | $self->send_data('r', 'bye-bye'); | ||
| 1996 | $fh->close; | ||
| 1997 | return; | ||
| 1998 | } | ||
| 1999 | $d->(" calling method $data[0]"); | ||
| 2000 | my($rettype, @ret) = $self->runner->call_wrapped_method($context, @data); | ||
| 2001 | $d->(" sending result back"); | ||
| 2002 | $self->send_data($rettype, @ret); | ||
| 2003 | } | ||
| 2004 | } | ||
| 2005 | |||
| 2006 | } | ||
| 2007 | |||
| 2008 | { | ||
| 2009 | package Doit::RPC::SimpleServer; | ||
| 2010 | our @ISA = ('Doit::RPC'); | ||
| 2011 | |||
| 2012 | sub new { | ||
| 2013 | my($class, $runner, $infh, $outfh, %options) = @_; | ||
| 2014 | my $debug = delete $options{debug}; | ||
| 2015 | die "Unhandled options: " . join(" ", %options) if %options; | ||
| 2016 | |||
| 2017 | $infh = \*STDIN if !$infh; | ||
| 2018 | $outfh = \*STDOUT if !$outfh; | ||
| 2019 | $outfh->autoflush(1); | ||
| 2020 | bless { | ||
| 2021 | runner => $runner, | ||
| 2022 | infh => $infh, | ||
| 2023 | outfh => $outfh, | ||
| 2024 | debug => $debug, | ||
| 2025 | }, $class; | ||
| 2026 | } | ||
| 2027 | |||
| 2028 | sub run { | ||
| 2029 | my $self = shift; | ||
| 2030 | while() { | ||
| 2031 | my($context, @data) = $self->receive_data; | ||
| 2032 | if (!defined $context) { | ||
| 2033 | return; | ||
| 2034 | } elsif ($data[0] =~ m{^exit$}) { | ||
| 2035 | $self->send_data('r', 'bye-bye'); | ||
| 2036 | return; | ||
| 2037 | } | ||
| 2038 | open my $oldout, ">&STDOUT" or die $!; | ||
| 2039 | if (Doit::IS_WIN) { | ||
| 2040 | open STDOUT, '>', 'CON:' or die $!; # XXX???? | ||
| 2041 | } else { | ||
| 2042 | open STDOUT, '>', "/dev/stderr" or die $!; # XXX???? | ||
| 2043 | } | ||
| 2044 | my($rettype, @ret) = $self->runner->call_wrapped_method($context, @data); | ||
| 2045 | open STDOUT, ">&", $oldout or die $!; | ||
| 2046 | $self->send_data($rettype, @ret); | ||
| 2047 | } | ||
| 2048 | } | ||
| 2049 | } | ||
| 2050 | |||
| 2051 | { | ||
| 2052 | package Doit::_AnyRPCImpl; | ||
| 2053 | sub call_remote { | ||
| 2054 | my($self, @args) = @_; | ||
| 2055 | $self->{rpc}->call_remote(@args); | ||
| 2056 | } | ||
| 2057 | |||
| 2058 | our $AUTOLOAD; | ||
| 2059 | sub AUTOLOAD { | ||
| 2060 | (my $method = $AUTOLOAD) =~ s{.*::}{}; | ||
| 2061 | my $self = shift; | ||
| 2062 | $self->call_remote($method, @_); # XXX or use goto? | ||
| 2063 | } | ||
| 2064 | |||
| 2065 | sub _can_LANS { | ||
| 2066 | require POSIX; | ||
| 2067 | $^O eq 'linux' && (POSIX::uname())[2] !~ m{^([01]\.|2\.[01]\.)} # osvers >= 2.2, earlier versions did not have LANS | ||
| 2068 | } | ||
| 2069 | |||
| 2070 | } | ||
| 2071 | |||
| 2072 | { | ||
| 2073 | package Doit::_ScriptTools; | ||
| 2074 | |||
| 2075 | sub add_components { | ||
| 2076 | my(@components) = @_; | ||
| 2077 | q|for my $component_module (qw(| . join(" ", map { qq{$_->{module}} } @components) . q|)) { $d->add_component($component_module) } |; | ||
| 2078 | } | ||
| 2079 | |||
| 2080 | sub self_require (;$) { | ||
| 2081 | my $realscript = shift; | ||
| 2082 | if (!defined $realscript) { $realscript = $0 } | ||
| 2083 | if ($realscript ne '-e') { # not a oneliner | ||
| 2084 | q{$ENV{DOIT_IN_REMOTE} = 1; } . | ||
| 2085 | q{require "} . File::Basename::basename($realscript) . q{"; }; | ||
| 2086 | } else { | ||
| 2087 | q{use Doit; }; | ||
| 2088 | } | ||
| 2089 | } | ||
| 2090 | } | ||
| 2091 | |||
| 2092 | { | ||
| 2093 | package Doit::Sudo; | ||
| 2094 | |||
| 2095 | our @ISA = ('Doit::_AnyRPCImpl'); | ||
| 2096 | |||
| 2097 | use Doit::Log; | ||
| 2098 | |||
| 2099 | my $socket_count = 0; | ||
| 2100 | |||
| 2101 | sub do_connect { | ||
| 2102 | my($class, %opts) = @_; | ||
| 2103 | my @sudo_opts = @{ delete $opts{sudo_opts} || [] }; | ||
| 2104 | my $dry_run = delete $opts{dry_run}; | ||
| 2105 | my $debug = delete $opts{debug}; | ||
| 2106 | my @components = @{ delete $opts{components} || [] }; | ||
| 2107 | my $perl = delete $opts{perl} || $^X; | ||
| 2108 | die "Unhandled options: " . join(" ", %opts) if %opts; | ||
| 2109 | |||
| 2110 | my $self = bless { }, $class; | ||
| 2111 | |||
| 2112 | require File::Basename; | ||
| 2113 | require IPC::Open2; | ||
| 2114 | require POSIX; | ||
| 2115 | require Symbol; | ||
| 2116 | |||
| 2117 | # Socket pathname, make it possible to find out | ||
| 2118 | # old outdated sockets easily by including a | ||
| 2119 | # timestamp. Also need to maintain a $socket_count, | ||
| 2120 | # if the same script opens multiple sockets quickly. | ||
| 2121 | my $sock_path = "/tmp/." . join(".", "doit", "sudo", POSIX::strftime("%Y%m%d_%H%M%S", gmtime), $<, $$, (++$socket_count)) . ".sock"; | ||
| 2122 | |||
| 2123 | # Make sure password has to be entered only once (if at all) | ||
| 2124 | # Using 'sudo --validate' would be more correct, however, | ||
| 2125 | # mysterious "sudo: ignoring time stamp from the future" | ||
| 2126 | # errors may happen every now and then. Seen on a | ||
| 2127 | # debian/jessie system, possibly related to | ||
| 2128 | # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=762465 | ||
| 2129 | { | ||
| 2130 | my @cmd = ('sudo', @sudo_opts, 'true'); | ||
| 2131 | system @cmd; | ||
| 2132 | if ($? != 0) { | ||
| 2133 | # Possible cases: | ||
| 2134 | # - sudo is not installed | ||
| 2135 | # - sudo authentication is not possible or user entered wrong password | ||
| 2136 | # - true is not installed (hopefully this never happens on Unix systems) | ||
| 2137 | error "Command '@cmd' failed"; | ||
| 2138 | } | ||
| 2139 | } | ||
| 2140 | |||
| 2141 | # On linux use Linux Abstract Namespace Sockets --- | ||
| 2142 | # invisible and automatically cleaned up. See man 7 unix. | ||
| 2143 | my $LANS_PREFIX = $class->_can_LANS ? '\0' : ''; | ||
| 2144 | |||
| 2145 | # Run the server | ||
| 2146 | my @cmd_worker = | ||
| 2147 | ( | ||
| 2148 | 'sudo', @sudo_opts, $perl, "-I".File::Basename::dirname(__FILE__), "-I".File::Basename::dirname($0), "-e", | ||
| 2149 | Doit::_ScriptTools::self_require() . | ||
| 2150 | q{my $d = Doit->init; } . | ||
| 2151 | Doit::_ScriptTools::add_components(@components) . | ||
| 2152 | q{Doit::RPC::Server->new($d, "} . $LANS_PREFIX . $sock_path . q{", excl => 1, debug => } . ($debug?1:0) . q{)->run();} . | ||
| 2153 | ($LANS_PREFIX ? '' : q<END { unlink "> . $sock_path . q<" }>), # cleanup socket file, except if Linux Abstract Namespace Sockets are used | ||
| 2154 | "--", ($dry_run? "--dry-run" : ()) | ||
| 2155 | ); | ||
| 2156 | my $worker_pid = fork; | ||
| 2157 | if (!defined $worker_pid) { | ||
| 2158 | die "fork failed: $!"; | ||
| 2159 | } elsif ($worker_pid == 0) { | ||
| 2160 | warn "worker perl cmd: @cmd_worker\n" if $debug; | ||
| 2161 | exec @cmd_worker; | ||
| 2162 | die "Failed to run '@cmd_worker': $!"; | ||
| 2163 | } | ||
| 2164 | |||
| 2165 | # Run the client --- must also run under root for socket | ||
| 2166 | # access. | ||
| 2167 | my($in, $out); | ||
| 2168 | my @cmd_comm = ('sudo', @sudo_opts, $perl, "-I".File::Basename::dirname(__FILE__), "-MDoit", "-e", | ||
| 2169 | q{Doit::Comm->comm_to_sock("} . $LANS_PREFIX . $sock_path . q{", debug => shift)}, !!$debug); | ||
| 2170 | warn "comm perl cmd: @cmd_comm\n" if $debug; | ||
| 2171 | my $comm_pid = IPC::Open2::open2($out, $in, @cmd_comm); | ||
| 2172 | $self->{rpc} = Doit::RPC::Client->new($out, $in, label => "sudo:", debug => $debug); | ||
| 2173 | |||
| 2174 | $self; | ||
| 2175 | } | ||
| 2176 | |||
| 2177 | sub DESTROY { } | ||
| 2178 | |||
| 2179 | } | ||
| 2180 | |||
| 2181 | { | ||
| 2182 | package Doit::SSH; | ||
| 2183 | |||
| 2184 | our @ISA = ('Doit::_AnyRPCImpl'); | ||
| 2185 | |||
| 2186 | use Doit::Log; | ||
| 2187 | |||
| 2188 | sub do_connect { | ||
| 2189 | require File::Basename; | ||
| 2190 | require Net::OpenSSH; | ||
| 2191 | require FindBin; | ||
| 2192 | my($class, $ssh_or_host, %opts) = @_; | ||
| 2193 | my $dry_run = delete $opts{dry_run}; | ||
| 2194 | my @components = @{ delete $opts{components} || [] }; | ||
| 2195 | my $debug = delete $opts{debug}; | ||
| 2196 | my $as = delete $opts{as}; | ||
| 2197 | my $forward_agent = delete $opts{forward_agent}; | ||
| 2198 | my $tty = delete $opts{tty}; | ||
| 2199 | my $port = delete $opts{port}; | ||
| 2200 | my $master_opts = delete $opts{master_opts}; | ||
| 2201 | my $dest_os = delete $opts{dest_os}; | ||
| 2202 | $dest_os = 'unix' if !defined $dest_os; | ||
| 2203 | my $put_to_remote = delete $opts{put_to_remote} || 'rsync_put'; # XXX ideally this should be determined automatically | ||
| 2204 | $put_to_remote =~ m{^(rsync_put|scp_put)$} | ||
| 2205 | or error "Valid values for put_to_remote: rsync_put or scp_put"; | ||
| 2206 | my $perl = delete $opts{perl} || 'perl'; | ||
| 2207 | my $umask = delete $opts{umask}; | ||
| 2208 | if (defined $umask && $umask !~ m{^\d+$}) { | ||
| 2209 | error "The umask '$umask' does not look correct, it should be a (possibly octal) number"; | ||
| 2210 | } | ||
| 2211 | my $bootstrap = delete $opts{bootstrap}; | ||
| 2212 | error "Unhandled options: " . join(" ", %opts) if %opts; | ||
| 2213 | |||
| 2214 | my $self = bless { debug => $debug }, $class; | ||
| 2215 | my %ssh_run_opts = ( | ||
| 2216 | ($forward_agent ? (forward_agent => $forward_agent) : ()), | ||
| 2217 | ($tty ? (tty => $tty) : ()), | ||
| 2218 | ); | ||
| 2219 | my %ssh_new_opts = ( | ||
| 2220 | ($forward_agent ? (forward_agent => $forward_agent) : ()), | ||
| 2221 | ($master_opts ? (master_opts => $master_opts) : ()), | ||
| 2222 | ); | ||
| 2223 | |||
| 2224 | my($host, $ssh); | ||
| 2225 | if (UNIVERSAL::isa($ssh_or_host, 'Net::OpenSSH')) { | ||
| 2226 | $ssh = $ssh_or_host; | ||
| 2227 | $host = $ssh->get_host; # XXX what about username/port/...? | ||
| 2228 | } else { | ||
| 2229 | $host = $ssh_or_host; | ||
| 2230 | $ssh = Net::OpenSSH->new($host, %ssh_new_opts); | ||
| 2231 | $ssh->error | ||
| 2232 | and error "Connection error to $host: " . $ssh->error; | ||
| 2233 | } | ||
| 2234 | $self->{ssh} = $ssh; | ||
| 2235 | |||
| 2236 | if (($bootstrap||'') eq 'perl') { | ||
| 2237 | require Doit::Bootstrap; | ||
| 2238 | Doit::Bootstrap::_bootstrap_perl($self, dry_run => $dry_run); | ||
| 2239 | } | ||
| 2240 | |||
| 2241 | { | ||
| 2242 | my $remote_cmd; | ||
| 2243 | if ($dest_os eq 'MSWin32') { | ||
| 2244 | $remote_cmd = 'if not exist .doit\lib\ mkdir .doit\lib'; | ||
| 2245 | } else { | ||
| 2246 | $remote_cmd = "[ ! -d .doit/lib ] && mkdir -p .doit/lib"; | ||
| 2247 | } | ||
| 2248 | if ($debug) { | ||
| 2249 | info "Running '$remote_cmd' on remote"; | ||
| 2250 | } | ||
| 2251 | $ssh->system(\%ssh_run_opts, $remote_cmd); | ||
| 2252 | } | ||
| 2253 | if ($FindBin::RealScript ne '-e') { | ||
| 2254 | no warnings 'once'; | ||
| 2255 | $ssh->$put_to_remote({verbose => $debug}, "$FindBin::RealBin/$FindBin::RealScript", ".doit/"); # XXX verbose? | ||
| 2256 | } | ||
| 2257 | $ssh->$put_to_remote({verbose => $debug}, __FILE__, ".doit/lib/"); | ||
| 2258 | { | ||
| 2259 | my %seen_dir; | ||
| 2260 | for my $component ( | ||
| 2261 | @components, | ||
| 2262 | ( # add additional RPC components | ||
| 2263 | $dest_os ne 'MSWin32' ? () : | ||
| 2264 | do { | ||
| 2265 | (my $srcpath = __FILE__) =~ s{\.pm}{/WinRPC.pm}; | ||
| 2266 | {relpath => "Doit/WinRPC.pm", path => $srcpath}, | ||
| 2267 | } | ||
| 2268 | ) | ||
| 2269 | ) { | ||
| 2270 | my $from = $component->{path}; | ||
| 2271 | my $to = $component->{relpath}; | ||
| 2272 | my $full_target = ".doit/lib/$to"; | ||
| 2273 | my $target_dir = File::Basename::dirname($full_target); | ||
| 2274 | if (!$seen_dir{$target_dir}) { | ||
| 2275 | my $remote_cmd; | ||
| 2276 | if ($dest_os eq 'MSWin32') { | ||
| 2277 | (my $win_target_dir = $target_dir) =~ s{/}{\\}g; | ||
| 2278 | $remote_cmd = "if not exist $win_target_dir mkdir $win_target_dir"; # XXX is this equivalent to mkdir -p? | ||
| 2279 | } else { | ||
| 2280 | $remote_cmd = "[ ! -d $target_dir ] && mkdir -p $target_dir"; | ||
| 2281 | } | ||
| 2282 | $ssh->system(\%ssh_run_opts, $remote_cmd); | ||
| 2283 | $seen_dir{$target_dir} = 1; | ||
| 2284 | } | ||
| 2285 | $ssh->$put_to_remote({verbose => $debug}, $from, $full_target); | ||
| 2286 | } | ||
| 2287 | } | ||
| 2288 | |||
| 2289 | my $sock_path = ( | ||
| 2290 | $dest_os eq 'MSWin32' | ||
| 2291 | ? join("-", "doit", "ssh", POSIX::strftime("%Y%m%d_%H%M%S", gmtime), int(rand(99999999))) | ||
| 2292 | : do { | ||
| 2293 | require POSIX; | ||
| 2294 | "/tmp/." . join(".", "doit", "ssh", POSIX::strftime("%Y%m%d_%H%M%S", gmtime), $<, $$, int(rand(99999999))) . ".sock"; | ||
| 2295 | } | ||
| 2296 | ); | ||
| 2297 | |||
| 2298 | my @cmd; | ||
| 2299 | if (defined $as) { | ||
| 2300 | if ($as eq 'root') { | ||
| 2301 | @cmd = ('sudo'); | ||
| 2302 | } else { | ||
| 2303 | @cmd = ('sudo', '-u', $as); | ||
| 2304 | } | ||
| 2305 | } # XXX add ssh option -t? for password input? | ||
| 2306 | |||
| 2307 | my @cmd_worker; | ||
| 2308 | if ($dest_os eq 'MSWin32') { | ||
| 2309 | @cmd_worker = | ||
| 2310 | ( | ||
| 2311 | # @cmd not used here (no sudo) | ||
| 2312 | $perl, "-I.doit", "-I.doit\\lib", "-e", | ||
| 2313 | Doit::_ScriptTools::self_require($FindBin::RealScript) . | ||
| 2314 | q{use Doit::WinRPC; } . | ||
| 2315 | q{my $d = Doit->init; } . | ||
| 2316 | Doit::_ScriptTools::add_components(@components) . | ||
| 2317 | # XXX server cleanup? on signals? on END? | ||
| 2318 | q{Doit::WinRPC::Server->new($d, "} . $sock_path . q{", debug => } . ($debug?1:0).q{)->run();}, | ||
| 2319 | "--", ($dry_run? "--dry-run" : ()) | ||
| 2320 | ); | ||
| 2321 | @cmd_worker = Doit::Win32Util::win32_quote_list(@cmd_worker); | ||
| 2322 | } else { | ||
| 2323 | @cmd_worker = | ||
| 2324 | ( | ||
| 2325 | @cmd, $perl, "-I.doit", "-I.doit/lib", "-e", | ||
| 2326 | (defined $umask ? qq{umask $umask; } : q{}) . | ||
| 2327 | Doit::_ScriptTools::self_require($FindBin::RealScript) . | ||
| 2328 | q{my $d = Doit->init; } . | ||
| 2329 | Doit::_ScriptTools::add_components(@components) . | ||
| 2330 | q<sub _server_cleanup { unlink "> . $sock_path . q<" }> . | ||
| 2331 | q<$SIG{PIPE} = \&_server_cleanup; > . | ||
| 2332 | q<END { _server_cleanup() } > . | ||
| 2333 | q{Doit::RPC::Server->new($d, "} . $sock_path . q{", excl => 1, debug => } . ($debug?1:0).q{)->run();}, | ||
| 2334 | "--", ($dry_run? "--dry-run" : ()) | ||
| 2335 | ); | ||
| 2336 | } | ||
| 2337 | warn "remote perl cmd: @cmd_worker\n" if $debug; | ||
| 2338 | my $worker_pid = $ssh->spawn(\%ssh_run_opts, @cmd_worker); # XXX what to do with worker pid? | ||
| 2339 | $self->{worker_pid} = $worker_pid; | ||
| 2340 | |||
| 2341 | my @cmd_comm; | ||
| 2342 | if ($dest_os eq 'MSWin32') { | ||
| 2343 | @cmd_comm = | ||
| 2344 | ($perl, "-I.doit\\lib", "-MDoit", "-MDoit::WinRPC", "-e", | ||
| 2345 | q{Doit::WinRPC::Comm->new("} . $sock_path . q{", debug => shift)->run}, | ||
| 2346 | !!$debug, | ||
| 2347 | ); | ||
| 2348 | @cmd_comm = Doit::Win32Util::win32_quote_list(@cmd_comm); | ||
| 2349 | } else { | ||
| 2350 | @cmd_comm = | ||
| 2351 | ( | ||
| 2352 | @cmd, $perl, "-I.doit/lib", "-MDoit", "-e", | ||
| 2353 | q{Doit::Comm->comm_to_sock("} . $sock_path . q{", debug => shift);}, | ||
| 2354 | !!$debug, | ||
| 2355 | ); | ||
| 2356 | } | ||
| 2357 | warn "comm perl cmd: @cmd_comm\n" if $debug; | ||
| 2358 | my($out, $in, $comm_pid) = $ssh->open2(@cmd_comm); | ||
| 2359 | $self->{comm_pid} = $comm_pid; | ||
| 2360 | $self->{rpc} = Doit::RPC::Client->new($in, $out, label => "ssh:$host", debug => $debug); | ||
| 2361 | |||
| 2362 | $self; | ||
| 2363 | } | ||
| 2364 | |||
| 2365 | sub ssh { $_[0]->{ssh} } | ||
| 2366 | |||
| 2367 | sub DESTROY { | ||
| 2368 | my $self = shift; | ||
| 2369 | local $?; # XXX Net::OpenSSH::_waitpid sets $?=0 | ||
| 2370 | if ($self->{ssh}) { | ||
| 2371 | $self->{ssh}->disconnect if $self->{ssh}->can('disconnect'); | ||
| 2372 | delete $self->{ssh}; | ||
| 2373 | } | ||
| 2374 | if ($self->{rpc}) { | ||
| 2375 | $self->{rpc}->_reap_process($self->{comm_pid}); | ||
| 2376 | $self->{rpc}->_reap_process($self->{worker_pid}); | ||
| 2377 | } | ||
| 2378 | } | ||
| 2379 | |||
| 2380 | } | ||
| 2381 | |||
| 2382 | { | ||
| 2383 | package Doit::Comm; | ||
| 2384 | |||
| 2385 | sub comm_to_sock { | ||
| 2386 | my(undef, $peer, %options) = @_; | ||
| 2387 | die "Please specify path to unix domain socket" if !defined $peer; | ||
| 2388 | my $debug = delete $options{debug}; | ||
| 2389 | die "Unhandled options: " . join(" ", %options) if %options; | ||
| 2390 | |||
| 2391 | my $infh = \*STDIN; | ||
| 2392 | my $outfh = \*STDOUT; | ||
| 2393 | |||
| 2394 | require IO::Socket::UNIX; | ||
| 2395 | IO::Socket::UNIX->VERSION('1.18'); # autoflush | ||
| 2396 | IO::Socket::UNIX->import(qw(SOCK_STREAM)); | ||
| 2397 | |||
| 2398 | my $d; | ||
| 2399 | if ($debug) { | ||
| 2400 | $d = sub ($) { | ||
| 2401 | Doit::Log::info("COMM: $_[0]"); | ||
| 2402 | }; | ||
| 2403 | } else { | ||
| 2404 | $d = sub ($) { }; | ||
| 2405 | } | ||
| 2406 | |||
| 2407 | $d->("Start communication process (pid $$)..."); | ||
| 2408 | |||
| 2409 | my $tries = 20; | ||
| 2410 | my $sock_err; | ||
| 2411 | my $sock = Doit::RPC::gentle_retry( | ||
| 2412 | code => sub { | ||
| 2413 | my(%opts) = @_; | ||
| 2414 | my $sock = IO::Socket::UNIX->new( | ||
| 2415 | Type => SOCK_STREAM(), | ||
| 2416 | Peer => $peer, | ||
| 2417 | ); | ||
| 2418 | return $sock if $sock; | ||
| 2419 | ${$opts{fail_info_ref}} = "(peer=$peer, errno=$!)"; | ||
| 2420 | undef; | ||
| 2421 | }, | ||
| 2422 | retry_msg_code => sub { | ||
| 2423 | my($seconds) = @_; | ||
| 2424 | $d->("can't connect, sleep for $seconds seconds"); | ||
| 2425 | }, | ||
| 2426 | fail_info_ref => \$sock_err, | ||
| 2427 | ); | ||
| 2428 | if (!$sock) { | ||
| 2429 | die "COMM: Can't connect to socket (after $tries retries) $sock_err"; | ||
| 2430 | } | ||
| 2431 | $d->("socket to worker was created"); | ||
| 2432 | |||
| 2433 | my $get_and_send = sub ($$$$) { | ||
| 2434 | my($infh, $outfh, $inname, $outname) = @_; | ||
| 2435 | |||
| 2436 | my $length_buf; | ||
| 2437 | read $infh, $length_buf, 4 or die "COMM: reading data from $inname failed (getting length): $!"; | ||
| 2438 | my $length = unpack("N", $length_buf); | ||
| 2439 | $d->("starting getting data from $inname, length is $length"); | ||
| 2440 | my $buf = ''; | ||
| 2441 | while (1) { | ||
| 2442 | my $got = read($infh, $buf, $length, length($buf)); | ||
| 2443 | last if $got == $length; | ||
| 2444 | die "COMM: Unexpected error $got > $length" if $got > $length; | ||
| 2445 | $length -= $got; | ||
| 2446 | } | ||
| 2447 | $d->("finished reading data from $inname"); | ||
| 2448 | |||
| 2449 | print $outfh $length_buf; | ||
| 2450 | print $outfh $buf; | ||
| 2451 | $d->("finished sending data to $outname"); | ||
| 2452 | }; | ||
| 2453 | |||
| 2454 | $outfh->autoflush(1); | ||
| 2455 | $d->("about to enter loop"); | ||
| 2456 | while () { | ||
| 2457 | $d->("seen eof from local"), last if eof($infh); | ||
| 2458 | $get_and_send->($infh, $sock, "local", "worker"); | ||
| 2459 | $get_and_send->($sock, $outfh, "worker", "local"); | ||
| 2460 | } | ||
| 2461 | $d->("exited loop"); | ||
| 2462 | } | ||
| 2463 | |||
| 2464 | } | ||
| 2465 | |||
| 2466 | 1; | ||
| 2467 | |||
| 2468 | __END__ |