| 1 | package MToken::Command; # $Id: Command.pm 70 2019-06-09 18:25:29Z minus $ | ||
| 2 | use strict; | ||
| 3 | use feature qw/say/; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | MToken::Command - utilities to replace common UNIX commands in Makefiles etc. | ||
| 11 | |||
| 12 | =head1 VIRSION | ||
| 13 | |||
| 14 | Version 1.01 | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | perl -MMToken::Command -e COMMAND -- ARGUMENS | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | Utilities to replace common UNIX commands in Makefiles etc. | ||
| 23 | |||
| 24 | =head2 add | ||
| 25 | |||
| 26 | perl -MMToken::Command -e add -- 2017/07/03 | ||
| 27 | |||
| 28 | Add file to Device | ||
| 29 | |||
| 30 | =head2 backupdelete | ||
| 31 | |||
| 32 | perl -MMToken::Command -e backupdelete -- myfooproject.20170703 | ||
| 33 | |||
| 34 | Remove backup file from remote server | ||
| 35 | |||
| 36 | =head2 backupupdate | ||
| 37 | |||
| 38 | perl -MMToken::Command -e backupupdate -- backup/myfooproject.20170703 | ||
| 39 | |||
| 40 | Update backup file on remote server | ||
| 41 | |||
| 42 | =head2 check | ||
| 43 | |||
| 44 | perl -MMToken::Command -e check -- 2017/07/03 | ||
| 45 | |||
| 46 | Check files on Device | ||
| 47 | |||
| 48 | =head2 config | ||
| 49 | |||
| 50 | perl -MMToken::Command -e config -- PROJECTNAME | ||
| 51 | |||
| 52 | Configure the Device | ||
| 53 | |||
| 54 | =head2 cpgpgkey | ||
| 55 | |||
| 56 | perl -MMToken::Command -e cpgpgkey -- certs/public.key | ||
| 57 | perl -MMToken::Command -e cpgpgkey -- certs/private.key | ||
| 58 | |||
| 59 | Copy public or private key file to Device | ||
| 60 | |||
| 61 | =head2 decrypt | ||
| 62 | |||
| 63 | perl -MMToken::Command -e decrypt -- *.asc .tar.gz | ||
| 64 | |||
| 65 | GPG decrypt files | ||
| 66 | |||
| 67 | =head2 del | ||
| 68 | |||
| 69 | perl -MMToken::Command -e del | ||
| 70 | |||
| 71 | Remove file from Device | ||
| 72 | |||
| 73 | =head2 encrypt | ||
| 74 | |||
| 75 | perl -MMToken::Command -e encrypt -- file.tar.gz file.asc | ||
| 76 | |||
| 77 | GPG encrypt file | ||
| 78 | |||
| 79 | =head2 fetch | ||
| 80 | |||
| 81 | perl -MMToken::Command -e fetch -- backup/myfooproject.20170703 | ||
| 82 | |||
| 83 | Download backup file from remote server | ||
| 84 | |||
| 85 | =head2 genkey | ||
| 86 | |||
| 87 | perl -MMToken::Command -e genkey -- keys/myfooproject.key | ||
| 88 | |||
| 89 | Generate main key file for Device | ||
| 90 | |||
| 91 | =head2 gpgrecipient | ||
| 92 | |||
| 93 | echo "<mail@example.com>" | perl -MMToken::Command -e gpgrecipient -- etc/gpg.conf | ||
| 94 | |||
| 95 | Get default recipient for GPG encrypting/decrypting | ||
| 96 | |||
| 97 | =head2 info | ||
| 98 | |||
| 99 | perl -MMToken::Command -e info -- 20170731 | ||
| 100 | |||
| 101 | Get information about actual backup file | ||
| 102 | |||
| 103 | =head2 list | ||
| 104 | |||
| 105 | perl -MMToken::Command -e list | ||
| 106 | |||
| 107 | Get list of backup files on server | ||
| 108 | |||
| 109 | =head2 serverconfig | ||
| 110 | |||
| 111 | perl -MMToken::Command -e serverconfig | ||
| 112 | |||
| 113 | Get configuration section for Apache2 server | ||
| 114 | |||
| 115 | =head2 show | ||
| 116 | |||
| 117 | perl -MMToken::Command -e show | ||
| 118 | |||
| 119 | Show current list of the device content | ||
| 120 | |||
| 121 | =head2 store | ||
| 122 | |||
| 123 | perl -MMToken::Command -e store -- backup/myfooproject.20170703 | ||
| 124 | |||
| 125 | Send the backup file to remote server | ||
| 126 | |||
| 127 | =head2 untar | ||
| 128 | |||
| 129 | perl -MMToken::Command -e untar -- restore/*.tar restore | ||
| 130 | |||
| 131 | Decompress downloaded backup from the server | ||
| 132 | |||
| 133 | =head2 update | ||
| 134 | |||
| 135 | perl -MMToken::Command -e update -- 2017/07/03 | ||
| 136 | |||
| 137 | Update backup file on server | ||
| 138 | |||
| 139 | =head1 HISTORY | ||
| 140 | |||
| 141 | See C<Changes> file | ||
| 142 | |||
| 143 | =head1 AUTHOR | ||
| 144 | |||
| 145 | Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt> | ||
| 146 | |||
| 147 | =head1 COPYRIGHT | ||
| 148 | |||
| 149 | Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved | ||
| 150 | |||
| 151 | =head1 LICENSE | ||
| 152 | |||
| 153 | This program is free software; you can redistribute it and/or | ||
| 154 | modify it under the same terms as Perl itself. | ||
| 155 | |||
| 156 | See C<LICENSE> file and L<https://dev.perl.org/licenses/> | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | use vars qw/ $VERSION @EXPORT /; | ||
| 161 | $VERSION = "1.01"; | ||
| 162 | |||
| 163 | use base qw/Exporter/; | ||
| 164 | @EXPORT = qw/ | ||
| 165 | config genkey store fetch list info backupdelete backupupdate | ||
| 166 | gpgrecipient cpgpgkey untar | ||
| 167 | serverconfig | ||
| 168 | show check add update del | ||
| 169 | encrypt decrypt | ||
| 170 | /; | ||
| 171 | |||
| 172 | use Carp; | ||
| 173 | use CTK; | ||
| 174 | use CTK::Util qw/ :ALL /; | ||
| 175 | use CTK::TFVals qw/ :ALL /; | ||
| 176 | use CTK::Crypt::GPG; | ||
| 177 | use MToken::Const qw/ :GENERAL :CRYPT /; | ||
| 178 | use MToken::Util; | ||
| 179 | use MToken::Config; | ||
| 180 | use MToken::Client; | ||
| 181 | use Digest::MD5 qw/md5_hex/; | ||
| 182 | use File::Basename qw/basename/; | ||
| 183 | use File::Spec; | ||
| 184 | use File::Copy qw/cp mv/; | ||
| 185 | use URI; | ||
| 186 | use Archive::Extract; | ||
| 187 | use ExtUtils::Manifest qw/ | ||
| 188 | mkmanifest | ||
| 189 | manicheck filecheck fullcheck skipcheck | ||
| 190 | manifind maniread manicopy maniadd | ||
| 191 | maniskip | ||
| 192 | /; | ||
| 193 | |||
| 194 | use constant { | ||
| 195 | VERBOSE => 0, | ||
| 196 | RESERVED_NAMES => [qw/ | ||
| 197 | MANIFEST MANIFEST.SKIP | ||
| 198 | LICENSE | ||
| 199 | TODO | ||
| 200 | DESCRIPTION | ||
| 201 | Makefile.PL | ||
| 202 | README | ||
| 203 | /], | ||
| 204 | }; | ||
| 205 | |||
| 206 | sub genkey { | ||
| 207 | # Create random key file via openssl | ||
| 208 | my $rndf = shift @ARGV || ''; | ||
| 209 | my $config = new MToken::Config; | ||
| 210 | unless ($rndf) { | ||
| 211 | $rndf = File::Spec->catfile(DIR_KEYS, sprintf("%s%s",$config->get("project") || PROJECT,KEYSUFFIX)); | ||
| 212 | } | ||
| 213 | if (-e $rndf) { | ||
| 214 | skip("File \"%s\" already exists", $rndf); | ||
| 215 | return 1; | ||
| 216 | } | ||
| 217 | my $size = int(rand(KEYMAXSIZE - KEYMINSIZE))+KEYMINSIZE; | ||
| 218 | |||
| 219 | my $opensslbin = $config->get("opensslbin"); | ||
| 220 | unless ($opensslbin) { | ||
| 221 | say STDERR red("Can't find OpenSSL program. Please configure first this device"); | ||
| 222 | exit(1); | ||
| 223 | } | ||
| 224 | |||
| 225 | my $err = ""; | ||
| 226 | #my $cmd = [$opensslbin, "rand", sprintf("-out \"%s\"", $rndf), $size]; | ||
| 227 | my $cmd = [$opensslbin, "rand", "-out", $rndf, $size]; | ||
| 228 | say(join(" ", @$cmd)); | ||
| 229 | my $out = execute( $cmd, undef, \$err ); | ||
| 230 | say STDERR red($err) if $err; | ||
| 231 | say cyan($out) if $out; | ||
| 232 | |||
| 233 | # Check | ||
| 234 | unless (-e $rndf) { | ||
| 235 | say STDERR red(sprintf("File \"%s\" not created", $rndf)); | ||
| 236 | exit(1); | ||
| 237 | } | ||
| 238 | |||
| 239 | return 1; | ||
| 240 | } | ||
| 241 | sub config { | ||
| 242 | my $prj = shift @ARGV || PROJECT; | ||
| 243 | my $cgd = shift @ARGV; | ||
| 244 | if ($cgd && -e $cgd) { | ||
| 245 | skip("Device already configured. Run \"make reconfig\" for forced configuration"); | ||
| 246 | say(yellow("File already exists: %s", $cgd)); | ||
| 247 | return 1; | ||
| 248 | } | ||
| 249 | say "Start configuration device: $prj"; | ||
| 250 | |||
| 251 | #my $config = new MToken::Config( name => 'foo' ); | ||
| 252 | my $config = new MToken::Config; | ||
| 253 | my %before = $config->getall; | ||
| 254 | #say(MToken::Util::explain($config)); | ||
| 255 | |||
| 256 | # Internal use only | ||
| 257 | my $c = new CTK(plugins => [qw/cli/]); | ||
| 258 | |||
| 259 | # Check openssl | ||
| 260 | my $opensslbin = $c->cli_prompt('OpenSSL program:', which($config->get("opensslbin") || OPENSSLBIN) || $config->get("opensslbin") || OPENSSLBIN); | ||
| 261 | unless ($opensslbin) { | ||
| 262 | say STDERR red("Program openssl not found. Please install it and try again later"); | ||
| 263 | exit(1); | ||
| 264 | } else { | ||
| 265 | my $cmd = [$opensslbin, "version"]; | ||
| 266 | say(join(" ", @$cmd)); | ||
| 267 | my $err = ""; | ||
| 268 | my $out = execute( $cmd, undef, \$err ); | ||
| 269 | say STDERR red($err) if $err; | ||
| 270 | unless ($out) { | ||
| 271 | say STDERR red("Program openssl not found. Please install it and try again later"); | ||
| 272 | exit(1); | ||
| 273 | } | ||
| 274 | unless ($out =~ /^OpenSSL\s+[1-9]\.[0-9]/m) { | ||
| 275 | say STDERR yellow("OpenSSL Version is not correctly. May be some problems"); | ||
| 276 | say cyan($out); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | $config->set(opensslbin => $opensslbin); | ||
| 280 | |||
| 281 | # Check GnuPG | ||
| 282 | my $gpgbin = $c->cli_prompt('GnuPG (gpg) program:', which($config->get("gpgbin") || GPGBIN) || $config->get("gpgbin") || GPGBIN); | ||
| 283 | unless ($gpgbin) { | ||
| 284 | say STDERR red("Program GnuPG (gpg) not found. Please install it and try again later"); | ||
| 285 | exit(1); | ||
| 286 | } else { | ||
| 287 | my $cmd = [$gpgbin, "--version"]; | ||
| 288 | say(join(" ", @$cmd)); | ||
| 289 | my $err = ""; | ||
| 290 | my $out = execute( $cmd, undef, \$err ); | ||
| 291 | say STDERR red($err) if $err; | ||
| 292 | unless ($out) { | ||
| 293 | say STDERR red("FATAL ERROR: Program GnuPG (gpg) not found. Please install it and try again later"); | ||
| 294 | exit(1); | ||
| 295 | } | ||
| 296 | unless ($out =~ /^gpg\s+\(GnuPG\)\s+[2-9]\.[0-9]/m) { | ||
| 297 | say STDERR yellow("GnuPG Version is not correctly. May be some problems"); | ||
| 298 | say cyan($out); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | $config->set(gpgbin => $gpgbin); | ||
| 302 | |||
| 303 | # Server URL (server_url) | ||
| 304 | say ""; | ||
| 305 | my $default_url = _get_default_url($prj); | ||
| 306 | my $server_url = $c->cli_prompt('Server URL:', MToken::Util::hide_pasword($config->get("server_url") || $default_url, 1)); | ||
| 307 | my $uri = new URI( $server_url ); | ||
| 308 | |||
| 309 | # Server user & password | ||
| 310 | if ($c->cli_prompt('Ask the credentials interactively (Recommended, t. It\'s safer)?:','yes') =~ /^\s*y/i) { | ||
| 311 | $uri->userinfo(undef); | ||
| 312 | } else { | ||
| 313 | my ($server_user, $server_password) = MToken::Util::parse_credentials($uri); | ||
| 314 | unless ($server_user) { # User | ||
| 315 | $server_user = $c->cli_prompt('Server user:', "anonymous") // ""; | ||
| 316 | $server_user =~ s/%/%25/g; | ||
| 317 | $server_user =~ s/:/%3A/g; | ||
| 318 | } | ||
| 319 | unless ($server_password) { # Password | ||
| 320 | system("stty -echo") unless MSWIN; | ||
| 321 | $server_password = $c->cli_prompt('Server password:') // ""; | ||
| 322 | $server_password =~ s/%/%25/g; | ||
| 323 | system("stty echo") unless MSWIN; | ||
| 324 | print STDERR "\n"; # because we disabled echo | ||
| 325 | } | ||
| 326 | $uri->userinfo(sprintf("%s:%s", $server_user, $server_password)); | ||
| 327 | } | ||
| 328 | |||
| 329 | # Result | ||
| 330 | my $url = $uri->canonical->as_string; | ||
| 331 | say(cyan("Full server URL: %s", MToken::Util::hide_pasword($url))); | ||
| 332 | unless ($c->cli_prompt('It is alright?:','yes') =~ /^\s*y/i) { | ||
| 333 | nope("Aborted"); | ||
| 334 | exit(1); | ||
| 335 | } | ||
| 336 | $config->set(server_url => $url); | ||
| 337 | |||
| 338 | # Hash Diff | ||
| 339 | my %after = $config->getall; | ||
| 340 | #say(Data::Dumper::Dumper({ before => {%before}, after => {%after} })); | ||
| 341 | return skip("Nothing changed") if _hashmd5(%before) eq _hashmd5(%after); | ||
| 342 | |||
| 343 | say "Current configuration:"; | ||
| 344 | eval <<'FORMATTING'; | ||
| 345 | my ($kf,$vf); | ||
| 346 | say ""; | ||
| 347 | say "------------------------+-------------------------------------------------------"; | ||
| 348 | format STDOUT_TOP = | ||
| 349 | Name | Value | ||
| 350 | ------------------------+------------------------------------------------------- | ||
| 351 | . | ||
| 352 | format STDOUT = | ||
| 353 | @>>>>>>>>>>>>>>>>>>>>> | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 354 | $kf,$vf | ||
| 355 | . | ||
| 356 | foreach my $k (sort {$a cmp $b} keys %after) { | ||
| 357 | $kf = $k; | ||
| 358 | my $tv = defined($after{$k}) ? $after{$k} : ''; | ||
| 359 | if ($k =~ /(^ur[il])|(ur[il]$)/i) { | ||
| 360 | $vf = variant_stf(MToken::Util::hide_pasword($tv), 52); | ||
| 361 | } else { | ||
| 362 | $vf = variant_stf($tv, 52); | ||
| 363 | } | ||
| 364 | write; | ||
| 365 | } | ||
| 366 | say ""; | ||
| 367 | FORMATTING | ||
| 368 | |||
| 369 | if ($c->cli_prompt('Are you sure you want to save all changes to local configuration file?:','yes') =~ /^\s*y/i) { | ||
| 370 | if ($config->save) { | ||
| 371 | yep("File \"%s\" successfully saved", $config->get("local_conf_file")); | ||
| 372 | } else { | ||
| 373 | nope("Can't save file \"%s\"", $config->get("local_conf_file")); | ||
| 374 | exit(1); | ||
| 375 | } | ||
| 376 | } else { | ||
| 377 | skip("Aborted. File not saved"); | ||
| 378 | } | ||
| 379 | |||
| 380 | 1; | ||
| 381 | } | ||
| 382 | sub store { | ||
| 383 | # Upload files | ||
| 384 | _expand_wildcards(); | ||
| 385 | my @files = @ARGV; | ||
| 386 | croak("Agrguments missing") unless @files; | ||
| 387 | my $config = new MToken::Config; | ||
| 388 | |||
| 389 | # Client | ||
| 390 | my $client = new MToken::Client( | ||
| 391 | url => $config->get("server_url"), | ||
| 392 | verbose => VERBOSE, # Show data pool | ||
| 393 | ); | ||
| 394 | |||
| 395 | $client->check() or do { | ||
| 396 | nope($client->transaction); | ||
| 397 | say STDERR red($client->error); | ||
| 398 | say STDERR $client->trace; | ||
| 399 | exit(1); | ||
| 400 | }; | ||
| 401 | |||
| 402 | # Send file | ||
| 403 | my $retstat = 1; | ||
| 404 | foreach my $file (@files) { | ||
| 405 | my $filename = basename($file); | ||
| 406 | printf("Sending file %s to %s...\n", $filename, $client->{uri}->host_port); | ||
| 407 | if (-f $file) { | ||
| 408 | unless ($client->upload($file)) { | ||
| 409 | nope($client->transaction); | ||
| 410 | say STDERR red($client->error); | ||
| 411 | say STDERR $client->trace; | ||
| 412 | $retstat = 0; | ||
| 413 | next; | ||
| 414 | } | ||
| 415 | yep($client->transaction); | ||
| 416 | } else { # Skipped | ||
| 417 | skip("It is not file") | ||
| 418 | } | ||
| 419 | } | ||
| 420 | exit(1) unless $retstat; | ||
| 421 | return $retstat; | ||
| 422 | } | ||
| 423 | sub fetch { | ||
| 424 | # Downlod files | ||
| 425 | _expand_wildcards(); | ||
| 426 | my @files = @ARGV; | ||
| 427 | my $dir = shift @files; | ||
| 428 | croak("Agrguments missing") unless $dir; | ||
| 429 | croak("Directory incorrect") unless (-d $dir or -l $dir); | ||
| 430 | |||
| 431 | # Internal use only | ||
| 432 | my $c = new CTK(plugins => [qw/cli/]); | ||
| 433 | my $f = ''; | ||
| 434 | unless (@files) { | ||
| 435 | # Try input FileName | ||
| 436 | $f = MToken::Util::cleanFileName( | ||
| 437 | $c->cli_prompt('Please type filename for fetching from server or (last, all):', "last") | ||
| 438 | ); | ||
| 439 | } | ||
| 440 | unless ($f or @files) { | ||
| 441 | nope("Incorrect filename for fetching"); | ||
| 442 | exit(1); | ||
| 443 | } | ||
| 444 | |||
| 445 | my $config = new MToken::Config; | ||
| 446 | my $distname = $config->get("distname"); | ||
| 447 | |||
| 448 | # Set URI & URL | ||
| 449 | my $client = new MToken::Client( | ||
| 450 | url => $config->get("server_url"), | ||
| 451 | verbose => VERBOSE, # Show data pool | ||
| 452 | ); | ||
| 453 | |||
| 454 | $client->check() or do { | ||
| 455 | nope($client->transaction); | ||
| 456 | say STDERR red($client->error); | ||
| 457 | say STDERR $client->trace; | ||
| 458 | exit(1); | ||
| 459 | }; | ||
| 460 | |||
| 461 | my @list; # File list from server | ||
| 462 | if ($f && $f eq 'all') { # All files | ||
| 463 | @list = $client->list($distname); | ||
| 464 | } elsif ($f && $f eq 'last') { # Last file only | ||
| 465 | @list = sort { ($b->{date_sfx} || 0) <=> ($a->{date_sfx} || 0) } ($client->list($distname)); | ||
| 466 | splice(@list,1) | ||
| 467 | } elsif ($f) { # Specified file | ||
| 468 | @list = $client->list($f); | ||
| 469 | } else { # From args | ||
| 470 | foreach my $l ($client->list($distname)) { | ||
| 471 | push @list, $l if grep { $_ && $l->{filename} && basename($_) eq $l->{filename} } @files; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | # Download files | ||
| 476 | my $retstat = 1; | ||
| 477 | foreach my $l (@list) { | ||
| 478 | my $filename = $l->{filename}; | ||
| 479 | unless ($filename) { | ||
| 480 | nope("Filename incorrect"); | ||
| 481 | next; | ||
| 482 | } | ||
| 483 | my $file = File::Spec->catfile($dir, $filename); | ||
| 484 | printf("Downloading file %s from %s...\n", $filename, $client->{uri}->host_port); | ||
| 485 | if (my $msg = $client->download($file)) { | ||
| 486 | my $in_md5 = $l->{md5}; | ||
| 487 | if ($in_md5) { | ||
| 488 | my $out_md5 = MToken::Util::md5sum($file) || ''; | ||
| 489 | unless ($in_md5 eq $out_md5) { | ||
| 490 | nope("File md5sum mismatch: Expected: %s; Got: %s", $in_md5, $out_md5); | ||
| 491 | $retstat = 0; | ||
| 492 | } | ||
| 493 | } | ||
| 494 | my $in_sha1 = $l->{sha1}; | ||
| 495 | if ($in_sha1) { | ||
| 496 | my $out_sha1 = MToken::Util::sha1sum($file); | ||
| 497 | unless ($in_sha1 eq $out_sha1) { | ||
| 498 | nope("File sha1sum mismatch: Expected: %s; Got: %s", $in_sha1, $out_sha1); | ||
| 499 | $retstat = 0; | ||
| 500 | } | ||
| 501 | } | ||
| 502 | if ($retstat) { | ||
| 503 | yep($msg); | ||
| 504 | } | ||
| 505 | } else { | ||
| 506 | nope($client->transaction); | ||
| 507 | say STDERR red($client->error); | ||
| 508 | say STDERR $client->trace; | ||
| 509 | $retstat = 0; | ||
| 510 | } | ||
| 511 | } | ||
| 512 | exit(1) unless $retstat; | ||
| 513 | return $retstat; | ||
| 514 | } | ||
| 515 | sub list { | ||
| 516 | my $config = new MToken::Config; | ||
| 517 | my $distname = $config->get("distname"); | ||
| 518 | my $client = new MToken::Client( | ||
| 519 | url => $config->get("server_url"), | ||
| 520 | verbose => VERBOSE, # Show data pool | ||
| 521 | ); | ||
| 522 | $client->check() or do { | ||
| 523 | nope($client->transaction); | ||
| 524 | say STDERR red($client->error); | ||
| 525 | say STDERR $client->trace; | ||
| 526 | exit(1); | ||
| 527 | }; | ||
| 528 | |||
| 529 | my @list = sort { ($b->{date_sfx} || 0) <=> ($a->{date_sfx} || 0) } ($client->list($distname)); | ||
| 530 | unless ($client->status) { | ||
| 531 | nope($client->transaction); | ||
| 532 | say STDERR red($client->error); | ||
| 533 | say STDERR $client->trace; | ||
| 534 | exit(1); | ||
| 535 | } | ||
| 536 | return skip("No files found") unless scalar(@list); | ||
| 537 | |||
| 538 | eval <<'FORMATTING'; | ||
| 539 | my @arr; | ||
| 540 | my $total = 0; | ||
| 541 | my $totalf = ""; | ||
| 542 | say ""; | ||
| 543 | say "----------+---------------------------------+------------+--------------"; | ||
| 544 | format STDOUT_TOP = | ||
| 545 | ID (SFX) | FileName | Date | Size (bytes) | ||
| 546 | ----------+---------------------------------+------------+-------------- | ||
| 547 | . | ||
| 548 | format STDOUT = | ||
| 549 | @<<<<<<< | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<< | @>>>>>>>>>>> | ||
| 550 | @arr | ||
| 551 | . | ||
| 552 | format STDOUTBOT = | ||
| 553 | ----------+---------------------------------+------------+-------------- | ||
| 554 | @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> bytes | ||
| 555 | $totalf | ||
| 556 | . | ||
| 557 | foreach my $f (@list) { | ||
| 558 | @arr = (); | ||
| 559 | push @arr, $f->{date_sfx} || 0; | ||
| 560 | push @arr, variant_stf($f->{filename} || '', 30); | ||
| 561 | push @arr, $f->{date_fmt} || ''; | ||
| 562 | push @arr, correct_number($f->{size} || 0); | ||
| 563 | $total += ($f->{size} || 0); | ||
| 564 | write; | ||
| 565 | } | ||
| 566 | $~ = "STDOUTBOT"; | ||
| 567 | $totalf = correct_number($total); | ||
| 568 | write; | ||
| 569 | say ""; | ||
| 570 | FORMATTING | ||
| 571 | return 1; | ||
| 572 | } | ||
| 573 | sub info { | ||
| 574 | my @in = @ARGV; | ||
| 575 | my $fnorid = shift @in; | ||
| 576 | croak("Agrguments missing") unless $fnorid; | ||
| 577 | my $config = new MToken::Config; | ||
| 578 | my $client = new MToken::Client( | ||
| 579 | url => $config->get("server_url"), | ||
| 580 | verbose => VERBOSE, # Show data pool | ||
| 581 | ); | ||
| 582 | $client->check() or do { | ||
| 583 | nope($client->transaction); | ||
| 584 | say STDERR red($client->error); | ||
| 585 | say STDERR $client->trace; | ||
| 586 | exit(1); | ||
| 587 | }; | ||
| 588 | |||
| 589 | my %info = $client->info($fnorid); | ||
| 590 | unless ($client->status) { | ||
| 591 | nope($client->transaction); | ||
| 592 | say STDERR red($client->error); | ||
| 593 | say STDERR $client->trace; | ||
| 594 | exit(1); | ||
| 595 | } | ||
| 596 | return skip("File not found") unless %info; | ||
| 597 | |||
| 598 | eval <<'FORMATTING'; | ||
| 599 | my ($kf,$vf); | ||
| 600 | say ""; | ||
| 601 | say "----------------+---------------------------------------------------------------"; | ||
| 602 | format STDOUT_TOP = | ||
| 603 | Name | Value | ||
| 604 | ----------------+--------------------------------------------------------------- | ||
| 605 | . | ||
| 606 | format STDOUT = | ||
| 607 | @>>>>>>>>>>>>> | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ||
| 608 | $kf,$vf | ||
| 609 | . | ||
| 610 | foreach my $k (qw/ date_sfx filename file date_fmt size md5 sha1 /) { | ||
| 611 | $kf = $k; | ||
| 612 | if ($k eq 'size') { | ||
| 613 | $vf = sprintf("%s bytes", correct_number($info{$k} || 0)); | ||
| 614 | } else { | ||
| 615 | $vf = variant_stf((defined($info{$k}) ? $info{$k} : ''), 60); | ||
| 616 | } | ||
| 617 | write; | ||
| 618 | } | ||
| 619 | say ""; | ||
| 620 | FORMATTING | ||
| 621 | return 1; | ||
| 622 | } | ||
| 623 | sub backupdelete { | ||
| 624 | _expand_wildcards(); | ||
| 625 | my @files = @ARGV; | ||
| 626 | croak("Agrguments missing") unless @files; | ||
| 627 | my $config = new MToken::Config; | ||
| 628 | |||
| 629 | # Client | ||
| 630 | my $client = new MToken::Client( | ||
| 631 | url => $config->get("server_url"), | ||
| 632 | verbose => VERBOSE, # Show data pool | ||
| 633 | ); | ||
| 634 | |||
| 635 | $client->check() or do { | ||
| 636 | nope($client->transaction); | ||
| 637 | say STDERR red($client->error); | ||
| 638 | say STDERR $client->trace; | ||
| 639 | exit(1); | ||
| 640 | }; | ||
| 641 | |||
| 642 | # Delete files | ||
| 643 | my $retstat = 1; | ||
| 644 | foreach my $file (@files) { | ||
| 645 | my $filename = basename($file); | ||
| 646 | printf("Deleting backup file %s from %s...\n", $filename, $client->{uri}->host_port); | ||
| 647 | unless ($client->remove($file)) { | ||
| 648 | nope($client->transaction); | ||
| 649 | say STDERR red($client->error); | ||
| 650 | say STDERR $client->trace; | ||
| 651 | $retstat = 0; | ||
| 652 | next; | ||
| 653 | } | ||
| 654 | yep($client->transaction); | ||
| 655 | } | ||
| 656 | exit(1) unless $retstat; | ||
| 657 | return $retstat; | ||
| 658 | } | ||
| 659 | sub backupupdate { | ||
| 660 | _expand_wildcards(); | ||
| 661 | my @files = @ARGV; | ||
| 662 | croak("Agrguments missing") unless @files; | ||
| 663 | my $config = new MToken::Config; | ||
| 664 | |||
| 665 | # Client | ||
| 666 | my $client = new MToken::Client( | ||
| 667 | url => $config->get("server_url"), | ||
| 668 | verbose => 0, # VERBOSE, # --disabled! binary data! | ||
| 669 | ); | ||
| 670 | |||
| 671 | $client->check() or do { | ||
| 672 | nope($client->transaction); | ||
| 673 | say STDERR red($client->error); | ||
| 674 | say STDERR $client->trace; | ||
| 675 | exit(1); | ||
| 676 | }; | ||
| 677 | |||
| 678 | # Updating files | ||
| 679 | my $retstat = 1; | ||
| 680 | foreach my $file (@files) { | ||
| 681 | my $filename = basename($file); | ||
| 682 | printf("Update backup file %s on %s...\n", $filename, $client->{uri}->host_port); | ||
| 683 | if (-f $file) { | ||
| 684 | unless ($client->update($file)) { | ||
| 685 | nope($client->transaction); | ||
| 686 | say STDERR red($client->error); | ||
| 687 | say STDERR $client->trace; | ||
| 688 | $retstat = 0; | ||
| 689 | next; | ||
| 690 | } | ||
| 691 | yep($client->transaction); | ||
| 692 | } else { # Skipped | ||
| 693 | skip("It is not file") | ||
| 694 | } | ||
| 695 | } | ||
| 696 | exit(1) unless $retstat; | ||
| 697 | return $retstat; | ||
| 698 | } | ||
| 699 | sub gpgrecipient { | ||
| 700 | # Get recipient from stdin and replace default-recipient in file | ||
| 701 | my $fn = shift(@ARGV); | ||
| 702 | croak("Destination file is not specified") unless $fn; | ||
| 703 | |||
| 704 | # Get recipient from STDIN | ||
| 705 | my $pool = scalar(do{local $/ = undef; <STDIN>}) // ""; | ||
| 706 | #say(blue($pool)); | ||
| 707 | |||
| 708 | # Cut a recipient | ||
| 709 | my $recipient = $1 if $pool =~ /^\s+\b([0-9A-F]+)\b/m; | ||
| 710 | $recipient //= ""; | ||
| 711 | unless($recipient) { | ||
| 712 | nope("Can't get recipient from input pool"); | ||
| 713 | say cyan($pool); | ||
| 714 | exit(1); | ||
| 715 | } | ||
| 716 | |||
| 717 | # Get data from file and replacing | ||
| 718 | my $data = fload($fn); | ||
| 719 | $data =~ s/^\s*default-recipient.+$//gm; | ||
| 720 | $data .= sprintf("\ndefault-recipient %s\n", $recipient); | ||
| 721 | $data =~ s/\n{2,}/\n/sg; | ||
| 722 | |||
| 723 | # Save file | ||
| 724 | unless (fsave($fn, $data)) { | ||
| 725 | nope("Can't save file %s", $fn); | ||
| 726 | exit(1); | ||
| 727 | } | ||
| 728 | return yep("Found recipient: %s", $recipient); | ||
| 729 | } | ||
| 730 | sub cpgpgkey { | ||
| 731 | # Copy GPG public and private key files from user directory to standard token-directory (certs) | ||
| 732 | my $dst = shift @ARGV; | ||
| 733 | croak("Destination file is not specified") unless $dst; | ||
| 734 | return skip("Destination file $dst already exists") if -e $dst; | ||
| 735 | |||
| 736 | # Internal use only | ||
| 737 | my $c = new CTK(plugins => [qw/cli/]); | ||
| 738 | my $dflt = File::Spec->catfile(DIR_KEYS, $dst =~ /priv/ ? MY_PRIVATE_KEY : MY_PUBLIC_KEY); | ||
| 739 | my $src = $c->cli_prompt('Please type full path to file:', $dflt); | ||
| 740 | |||
| 741 | unless ($src) { | ||
| 742 | nope("Incorrect path to key file"); | ||
| 743 | exit(1); | ||
| 744 | } | ||
| 745 | unless (-f $src and -r _) { | ||
| 746 | nope("File is not exists. Try again"); | ||
| 747 | exit(1); | ||
| 748 | } | ||
| 749 | unless (cp($src, $dst)) { | ||
| 750 | nope("Can't copy file: %s", $!); | ||
| 751 | exit 1; | ||
| 752 | } | ||
| 753 | |||
| 754 | return yep("File has been successfully copied to %s", $dst); | ||
| 755 | } | ||
| 756 | sub untar { | ||
| 757 | _expand_wildcards(); | ||
| 758 | my @files = @ARGV; | ||
| 759 | my $directory = pop @files; | ||
| 760 | croak("Directory incorrect") unless $directory; | ||
| 761 | croak("No files. Missing arguments") unless @files; | ||
| 762 | |||
| 763 | my $retstat = 1; | ||
| 764 | foreach my $file (@files) { | ||
| 765 | next unless -f $file; | ||
| 766 | my $dir = $1 if $file =~ /\.(\d{8})\./; | ||
| 767 | unless ($dir) { | ||
| 768 | skip("Incorrect file %s", $file); | ||
| 769 | next; | ||
| 770 | } | ||
| 771 | my $ae = Archive::Extract->new( archive => $file ); | ||
| 772 | unless ($ae->extract( to => File::Spec->catdir($directory, $dir) )) { | ||
| 773 | nope("Error extract files from $file"); | ||
| 774 | say STDERR red($ae->error); | ||
| 775 | $retstat = 0; | ||
| 776 | next; | ||
| 777 | } | ||
| 778 | unlink($file); | ||
| 779 | yep("Untar file %s to %s", $file, $dir); | ||
| 780 | } | ||
| 781 | exit(1) unless $retstat; | ||
| 782 | return $retstat; | ||
| 783 | } | ||
| 784 | sub serverconfig { | ||
| 785 | my $config = new MToken::Config; | ||
| 786 | my $pool = ""; | ||
| 787 | |||
| 788 | if (MSWIN) { | ||
| 789 | $pool = <<'EOP'; | ||
| 790 | # Configuration section for Apache 2.2 or latest (Windows platforms) | ||
| 791 | <VirtualHost *:[SERVER_PORT]> | ||
| 792 | ServerName [SERVER_HOST] | ||
| 793 | #DocumentRoot C:\\Apache2\\www | ||
| 794 | |||
| 795 | Options All | ||
| 796 | AddDefaultCharset utf-8 | ||
| 797 | |||
| 798 | <Location [SERVER_PATH]> | ||
| 799 | SetHandler modperl | ||
| 800 | PerlResponseHandler MToken::Server | ||
| 801 | #PerlSetVar Debug 1 | ||
| 802 | PerlSetVar MTokenDir "[SERVER_DIR]" | ||
| 803 | |||
| 804 | AuthName "MToken Server" | ||
| 805 | AuthType Basic | ||
| 806 | AuthUserFile "[SERVER_DIR]\\.htpasswd" | ||
| 807 | require valid-user | ||
| 808 | |||
| 809 | </Location> | ||
| 810 | </VirtualHost> | ||
| 811 | |||
| 812 | # Please also create follow file: | ||
| 813 | # [SERVER_DIR]\\.htpasswd: | ||
| 814 | # And add string: | ||
| 815 | # [SERVER_SS] | ||
| 816 | EOP | ||
| 817 | } else { | ||
| 818 | $pool = <<'EOP'; | ||
| 819 | # Configuration section for Apache 2.2 or latest | ||
| 820 | <VirtualHost *:[SERVER_PORT]> | ||
| 821 | ServerName [SERVER_HOST] | ||
| 822 | #DocumentRoot /var/www | ||
| 823 | |||
| 824 | Options All | ||
| 825 | AddDefaultCharset utf-8 | ||
| 826 | |||
| 827 | <Location [SERVER_PATH]> | ||
| 828 | SetHandler modperl | ||
| 829 | PerlResponseHandler MToken::Server | ||
| 830 | #PerlSetVar Debug 1 | ||
| 831 | PerlSetVar MTokenDir [SERVER_DIR] | ||
| 832 | |||
| 833 | AuthName "MToken Server" | ||
| 834 | AuthType Basic | ||
| 835 | AuthUserFile [SERVER_DIR]/.htpasswd | ||
| 836 | require valid-user | ||
| 837 | |||
| 838 | </Location> | ||
| 839 | </VirtualHost> | ||
| 840 | |||
| 841 | # Please also create follow file: | ||
| 842 | # [SERVER_DIR]/.htpasswd: | ||
| 843 | # And add string: | ||
| 844 | # [SERVER_SS] | ||
| 845 | EOP | ||
| 846 | } | ||
| 847 | my $uri = _mk_uri($config->get("server_url")); | ||
| 848 | my ($suser, $spass) = MToken::Util::parse_credentials($uri); | ||
| 849 | my $ss = sprintf("%s:%s", $suser, $spass); | ||
| 850 | |||
| 851 | my $htpasswd = which('htpasswd'); | ||
| 852 | if ($htpasswd) { | ||
| 853 | my $err = ""; | ||
| 854 | my $cmd = [$htpasswd, "-nb", $suser, $spass]; | ||
| 855 | my $out = execute( $cmd, undef, \$err ); | ||
| 856 | if ($err) { | ||
| 857 | say STDERR join(" ", @$cmd); | ||
| 858 | say STDERR red($err) | ||
| 859 | } | ||
| 860 | if ($out) { | ||
| 861 | chomp $out; | ||
| 862 | $ss = $out; | ||
| 863 | } | ||
| 864 | } | ||
| 865 | |||
| 866 | print dformat($pool, { | ||
| 867 | SERVER_HOST => $uri->host, | ||
| 868 | SERVER_PORT => $uri->port || ($uri->secure ? 443 : 80), | ||
| 869 | SERVER_PATH => $uri->path || "/", | ||
| 870 | SERVER_DIR => File::Spec->catfile(webdir(), $uri->host), | ||
| 871 | SERVER_SS => $ss, | ||
| 872 | }); | ||
| 873 | } | ||
| 874 | sub show { | ||
| 875 | # Manifest | ||
| 876 | my $manifest = maniread(); # Текущий манифест файл => коментарий | ||
| 877 | my $rnames = RESERVED_NAMES; | ||
| 878 | foreach my $f (keys %$manifest) { | ||
| 879 | delete $manifest->{$f} if grep {$_ eq $f} @$rnames; | ||
| 880 | } | ||
| 881 | if (keys %$manifest) { | ||
| 882 | say "Current device structure (without system files):"; | ||
| 883 | _show_list($manifest); | ||
| 884 | } else { | ||
| 885 | return nope("No files in manifest. Please initialize your device correctly"); | ||
| 886 | } | ||
| 887 | return 1; | ||
| 888 | } | ||
| 889 | sub check { | ||
| 890 | # less /usr/share/perl/5.22.2/ExtUtils/Manifest.pm | ||
| 891 | my $date_fmt = shift(@ARGV) || dtf("%YYYY/%MM/%DD", time()); | ||
| 892 | my $status = _check_date($date_fmt); | ||
| 893 | |||
| 894 | # Missing files | ||
| 895 | my @missing = manicheck(); # Отсутствующие файлы на диске но есть в манифесте | ||
| 896 | if (@missing) { | ||
| 897 | $status = 0; | ||
| 898 | say "Missing files:"; | ||
| 899 | my %tmp = (); | ||
| 900 | $tmp{$_} = '' for @missing; | ||
| 901 | _show_list(\%tmp); | ||
| 902 | } | ||
| 903 | |||
| 904 | _show_extra(); | ||
| 905 | |||
| 906 | # Manifest | ||
| 907 | # my $found = manifind(); # Список вообще всех файлов | ||
| 908 | my $manifest = maniread(); # Текущий манифест файл => коментарий | ||
| 909 | if (keys %$manifest) { | ||
| 910 | say "Manifest:"; | ||
| 911 | _show_list($manifest); | ||
| 912 | } else { | ||
| 913 | say STDERR red("No files in manifest. Please initialize your device correctly"); | ||
| 914 | $status = 0; | ||
| 915 | } | ||
| 916 | |||
| 917 | # Checking MD5 conflicts | ||
| 918 | foreach my $k (keys %$manifest) { | ||
| 919 | my $comment = $manifest->{$k} || ''; | ||
| 920 | my $date = $1 if $comment =~ /(\d{4}\/\d{2}\/\d{2})/; $date ||= $date_fmt; | ||
| 921 | my $md5 = $1 if $comment =~ /([a-f0-9]{32})/i; | ||
| 922 | next unless $md5; | ||
| 923 | my $realf = File::Spec->catfile(split(/\//, $k)); | ||
| 924 | next unless -f $realf and -r _; | ||
| 925 | my $got = MToken::Util::md5sum($realf) || ''; | ||
| 926 | next if lc($md5) eq lc($got); | ||
| 927 | |||
| 928 | $status = 0; | ||
| 929 | say STDERR red("MD5 Conflict for file: %s", $k); | ||
| 930 | say STDERR red(" Got : %s", $got); # Got : 745cb2db6dbc5ec03dade96ccbc51628 (получил) | ||
| 931 | say STDERR red(" Expected : %s", $md5); # Expected : 745cb2db6dbc5ec03dade96ccbc51629 (ожидал) | ||
| 932 | print STDERR "\n"; | ||
| 933 | } | ||
| 934 | |||
| 935 | exit(1) unless $status; | ||
| 936 | return 1; | ||
| 937 | } | ||
| 938 | sub add { | ||
| 939 | my $date_fmt = shift(@ARGV) || dtf("%YYYY/%MM/%DD", time()); | ||
| 940 | _check_date($date_fmt); | ||
| 941 | |||
| 942 | # Internal use only | ||
| 943 | my $c = new CTK(plugins => [qw/cli/]); | ||
| 944 | |||
| 945 | my $n = _show_extra(); | ||
| 946 | return skip("Nothing to add. Please first copy physical file for adding") unless $n; | ||
| 947 | |||
| 948 | # Добавление единственного файла, _add(1) | ||
| 949 | return _add(1, $date_fmt) if $n == 1; # 1 | ||
| 950 | my $sel = $c->cli_prompt(sprintf("Please type file's NNN (%d-%d) or (all, cancel):", 1, $n), "cancel"); | ||
| 951 | if ($sel && is_int($sel) && ($sel > 0) && ($sel <= $n)) { # NNN | ||
| 952 | # Добавление конкретного файла, _add($sel) | ||
| 953 | return _add($sel, $date_fmt); | ||
| 954 | } elsif ($sel && $sel =~ /all/i) { # All | ||
| 955 | # Добавление всех файлов, _addall | ||
| 956 | return _addall($date_fmt); | ||
| 957 | } elsif ($sel && $sel =~ /cancel/i) { # Cancel | ||
| 958 | return skip("Aborted"); | ||
| 959 | } else { # Other | ||
| 960 | return nope("Incorrect answer. Try again"); | ||
| 961 | } | ||
| 962 | } | ||
| 963 | sub update { | ||
| 964 | my $date_fmt = shift(@ARGV) || CTK::dtf("%YYYY/%MM/%DD", time()); | ||
| 965 | _check_date($date_fmt); | ||
| 966 | |||
| 967 | # Manifest | ||
| 968 | my $manifest = maniread(); # Текущий манифест файл => коментарий | ||
| 969 | return skip("No files in manifest. Please initialize your device correctly") unless keys %$manifest; | ||
| 970 | |||
| 971 | # Checking MD5 conflicts | ||
| 972 | my $rnames = RESERVED_NAMES; | ||
| 973 | my %updates = (); | ||
| 974 | foreach my $k (keys %$manifest) { | ||
| 975 | next if grep {$_ eq $k} @$rnames; | ||
| 976 | my $comment = $manifest->{$k} || ''; | ||
| 977 | my $date = $1 if $comment =~ /(\d{4}\/\d{2}\/\d{2})/; $date ||= ""; | ||
| 978 | my $md5 = $1 if $comment =~ /([a-f0-9]{32})/i; $md5 ||= ""; | ||
| 979 | |||
| 980 | my $realf = File::Spec->catfile(split(/\//, $k)); | ||
| 981 | unless (-f $realf and -r _) { | ||
| 982 | $updates{$k} = ''; # DELETE | ||
| 983 | skip("Missing file %s", $k); | ||
| 984 | next; | ||
| 985 | } | ||
| 986 | |||
| 987 | # Check date and md5 | ||
| 988 | my $got_md5 = MToken::Util::md5sum($realf) || ''; | ||
| 989 | next if $date && lc($md5) eq lc($got_md5); | ||
| 990 | |||
| 991 | $updates{$k} = sprintf("\%s\t%s", $got_md5, $date_fmt); | ||
| 992 | yep("Updated metadata for %s", $k); | ||
| 993 | } | ||
| 994 | if (%updates) { | ||
| 995 | if (ExtUtils::Manifest::maniupdate(\%updates)) { | ||
| 996 | yep("Device successfully updated"); | ||
| 997 | } else { | ||
| 998 | nope("Can't update device"); | ||
| 999 | } | ||
| 1000 | } else { | ||
| 1001 | skip("Device no need to update"); | ||
| 1002 | } | ||
| 1003 | say(""); | ||
| 1004 | |||
| 1005 | return 1; | ||
| 1006 | } | ||
| 1007 | sub del { | ||
| 1008 | # Internal use only | ||
| 1009 | my $c = new CTK(plugins => [qw/cli/]); | ||
| 1010 | |||
| 1011 | # Manifest (without system files) | ||
| 1012 | # my $found = manifind(); # Список вообще всех файлов | ||
| 1013 | my $manifest = maniread(); # Текущий манифест файл => коментарий | ||
| 1014 | my $rnames = RESERVED_NAMES; | ||
| 1015 | foreach my $f (keys %$manifest) { | ||
| 1016 | delete $manifest->{$f} if grep {$_ eq $f} @$rnames; | ||
| 1017 | } | ||
| 1018 | my @files = sort { lc $a cmp lc $b } keys %$manifest; | ||
| 1019 | if (@files) { | ||
| 1020 | say "Manifest (without system files):"; | ||
| 1021 | _show_list($manifest); | ||
| 1022 | } else { | ||
| 1023 | return skip("No files in manifest. Add files first"); | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | my $n = scalar(@files); | ||
| 1027 | my $sel = $c->cli_prompt(sprintf("Please type file's NNN (%d-%d) or (all, cancel):", 1, $n), "cancel"); | ||
| 1028 | if ($sel && is_int($sel) && ($sel > 0) && ($sel <= $n)) { # NNN | ||
| 1029 | # Удаление конкретного файла | ||
| 1030 | my $file = $files[$sel - 1]; | ||
| 1031 | my $realf = File::Spec->catfile(split(/\//, $file)); | ||
| 1032 | unless ($file) { | ||
| 1033 | nope("File not specified or incorrect"); | ||
| 1034 | exit(1); | ||
| 1035 | } | ||
| 1036 | unless ($c->cli_prompt(sprintf("Are you sure you want to remove \"%s\" file?:", $file),'no') =~ /^\s*y/i) { | ||
| 1037 | return skip("Aborted"); | ||
| 1038 | } | ||
| 1039 | say(sprintf("Deleting %s...", $file)); | ||
| 1040 | unless (-f $realf && -w _) { | ||
| 1041 | nope("File not exists or locked for writing"); | ||
| 1042 | exit(1); | ||
| 1043 | } | ||
| 1044 | unless (unlink($realf)) { | ||
| 1045 | nope("Can't remove file: $!"); | ||
| 1046 | exit(1); | ||
| 1047 | } | ||
| 1048 | if (ExtUtils::Manifest::maniupdate({$file, ''})) { | ||
| 1049 | yep("Device successfully updated"); | ||
| 1050 | } else { | ||
| 1051 | nope("Can't update device"); | ||
| 1052 | exit(1); | ||
| 1053 | } | ||
| 1054 | } elsif ($sel && $sel =~ /all/i) { # All | ||
| 1055 | unless ($c->cli_prompt('Are you sure you want to remove all device files?:','no') =~ /^\s*y/i) { | ||
| 1056 | return skip("Aborted"); | ||
| 1057 | } | ||
| 1058 | my %dels; | ||
| 1059 | foreach my $file (@files) { | ||
| 1060 | my $realf = File::Spec->catfile(split(/\//, $file)); | ||
| 1061 | say(sprintf("Deleting %s...", $file)); | ||
| 1062 | unless (-f $realf && -w _) { | ||
| 1063 | nope("File not exists or locked for writing"); | ||
| 1064 | next; | ||
| 1065 | } | ||
| 1066 | unless (unlink($realf)) { | ||
| 1067 | nope("Can't remove file: $!"); | ||
| 1068 | next; | ||
| 1069 | } | ||
| 1070 | $dels{$file} = ""; | ||
| 1071 | } | ||
| 1072 | if (ExtUtils::Manifest::maniupdate({%dels})) { | ||
| 1073 | yep("Device successfully updated"); | ||
| 1074 | } else { | ||
| 1075 | nope("Can't update device"); | ||
| 1076 | exit(1); | ||
| 1077 | } | ||
| 1078 | } elsif ($sel && $sel =~ /cancel/i) { # Cancel | ||
| 1079 | return skip("Aborted"); | ||
| 1080 | } else { # Other | ||
| 1081 | return nope("Incorrect answer. Try again"); | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | return 1; | ||
| 1085 | } | ||
| 1086 | sub encrypt { | ||
| 1087 | my $src = shift @ARGV; | ||
| 1088 | my $dst = shift @ARGV; | ||
| 1089 | croak("Incorrect source file") unless $src && (-f $src) && (-r $src); | ||
| 1090 | croak("Destination file is not specified") unless $dst; | ||
| 1091 | |||
| 1092 | my $gpg = new CTK::Crypt::GPG( | ||
| 1093 | -publickey => File::Spec->catfile(DIR_KEYS, PUBLIC_GPG_KEY), | ||
| 1094 | ); | ||
| 1095 | unless ($gpg) { | ||
| 1096 | nope("Can't create CTK::Crypt::GPG object"); | ||
| 1097 | exit(1); | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | $gpg->encrypt( | ||
| 1101 | -infile => $src, | ||
| 1102 | -outfile=> $dst, | ||
| 1103 | -armor => "yes", | ||
| 1104 | ) or do { | ||
| 1105 | nope("GPG encrypt error"); | ||
| 1106 | say(red($gpg->error)) if $gpg->error; | ||
| 1107 | exit(1); | ||
| 1108 | }; | ||
| 1109 | return yep("Encrypted") | ||
| 1110 | } | ||
| 1111 | sub decrypt { | ||
| 1112 | _expand_wildcards(); | ||
| 1113 | my @files = @ARGV; | ||
| 1114 | my $suffix = pop @files; | ||
| 1115 | croak("Suffix incorrect") unless $suffix; | ||
| 1116 | croak("No files. Missing arguments") unless @files; | ||
| 1117 | |||
| 1118 | my $gpg = new CTK::Crypt::GPG( | ||
| 1119 | -privatekey => File::Spec->catfile(DIR_KEYS, PRIVATE_GPG_KEY), | ||
| 1120 | ); | ||
| 1121 | unless ($gpg) { | ||
| 1122 | nope("Can't create CTK::Crypt::GPG object"); | ||
| 1123 | exit(1); | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | my $retstat = 1; | ||
| 1127 | foreach my $file (@files) { | ||
| 1128 | next unless -f $file; | ||
| 1129 | next if $file =~ /^\./; | ||
| 1130 | next if $file =~ /tar\.\w+$/; | ||
| 1131 | my $src = sprintf("%s%s.gpg", $file, $suffix); | ||
| 1132 | my $dst = sprintf("%s%s", $file, $suffix); | ||
| 1133 | if (mv($file, $src)) { | ||
| 1134 | $gpg->decrypt( | ||
| 1135 | -infile => $src, | ||
| 1136 | -outfile=> $dst, | ||
| 1137 | ) or do { | ||
| 1138 | nope("GPG decrypt error"); | ||
| 1139 | say(red($gpg->error)) if $gpg->error; | ||
| 1140 | $retstat = 0; | ||
| 1141 | next; | ||
| 1142 | }; | ||
| 1143 | yep("Decripted %s", $dst); | ||
| 1144 | } else { | ||
| 1145 | nope("Can't move file %s to %s: %s", $file, $src, $!); | ||
| 1146 | } | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | #say(MToken::Util::explain($gpg)); | ||
| 1150 | exit(1) unless $retstat; | ||
| 1151 | return $retstat; | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | ##################### | ||
| 1155 | # Internal functions | ||
| 1156 | ##################### | ||
| 1157 | sub _expand_wildcards { | ||
| 1158 | # Original in package ExtUtils::Command | ||
| 1159 | @ARGV = map(/[*?]/o ? glob($_) : $_, @ARGV); | ||
| 1160 | } | ||
| 1161 | sub _hashmd5 { | ||
| 1162 | my %h = @_ ; | ||
| 1163 | my $s = ""; | ||
| 1164 | foreach my $k (sort {$a cmp $b} (keys(%h))) { $s .= uv2null($h{$k}) } | ||
| 1165 | return "" unless $s; | ||
| 1166 | return md5_hex($s); | ||
| 1167 | } | ||
| 1168 | sub _mk_uri { | ||
| 1169 | # URI constructor | ||
| 1170 | my $url = shift || DEFAULT_URL; # $config->get("server_url") | ||
| 1171 | my $uri = new URI( $url ); | ||
| 1172 | return $uri; | ||
| 1173 | } | ||
| 1174 | sub _get_default_url { | ||
| 1175 | my $project = shift || PROJECT; | ||
| 1176 | my $uri = new URI( DEFAULT_URL ); | ||
| 1177 | $uri->scheme('https'); | ||
| 1178 | $uri->host(HOSTNAME); | ||
| 1179 | $uri->path($project); | ||
| 1180 | return $uri->canonical->as_string; | ||
| 1181 | } | ||
| 1182 | sub _show_list { | ||
| 1183 | my $struct = shift; | ||
| 1184 | return unless $struct && ref($struct) eq 'HASH'; | ||
| 1185 | eval <<'FORMATTING'; | ||
| 1186 | my $total = 0; | ||
| 1187 | my $totalf = ""; | ||
| 1188 | my @arr; | ||
| 1189 | |||
| 1190 | say "-----+------------------------------------------+------------+--------------"; | ||
| 1191 | format STDOUT_TOP = | ||
| 1192 | NNN | File | Added on | Size (bytes) | ||
| 1193 | -----+------------------------------------------+------------+-------------- | ||
| 1194 | . | ||
| 1195 | format STDOUT = | ||
| 1196 | @>> | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<<< | @>>>>>>>>>>> | ||
| 1197 | @arr | ||
| 1198 | . | ||
| 1199 | format STDOUTBOT = | ||
| 1200 | -----+------------------------------------------+------------+-------------- | ||
| 1201 | @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> bytes | ||
| 1202 | $totalf | ||
| 1203 | . | ||
| 1204 | |||
| 1205 | #$% = 0; | ||
| 1206 | local $= = scalar(keys(%$struct)) + 4; | ||
| 1207 | local $^L = ''; | ||
| 1208 | |||
| 1209 | my $i = 0; | ||
| 1210 | foreach my $k (sort { lc $a cmp lc $b } keys %$struct) { | ||
| 1211 | @arr = (); | ||
| 1212 | push @arr, ++$i; # NNN | ||
| 1213 | push @arr, variant_stf($k || '', 40); # File | ||
| 1214 | my $comment = $struct->{$k} || ''; | ||
| 1215 | my $datef = $1 if $comment =~ /(\d{4}\/\d{2}\/\d{2})/; $datef ||= ""; | ||
| 1216 | push @arr, $datef; # Date | ||
| 1217 | my $realf = File::Spec->catfile(split(/\//, $k)); | ||
| 1218 | my $fs = (-f $realf and -r _) ? (-s _) : 0; # File size | ||
| 1219 | $total += ($fs || 0); | ||
| 1220 | push @arr, correct_number($fs || 0); | ||
| 1221 | write; | ||
| 1222 | } | ||
| 1223 | local $~ = "STDOUTBOT"; | ||
| 1224 | $totalf = correct_number($total); | ||
| 1225 | write; | ||
| 1226 | say ""; | ||
| 1227 | FORMATTING | ||
| 1228 | warn $@."\n" if $@; | ||
| 1229 | } | ||
| 1230 | sub _check_date { | ||
| 1231 | my $date_fmt = shift; | ||
| 1232 | croak("Incorrect date format") unless $date_fmt && $date_fmt =~ /^\d{4}\/\d{2}\/\d{2}$/; | ||
| 1233 | |||
| 1234 | # Date conflict | ||
| 1235 | my $cur_fmt = dtf("%YYYY/%MM/%DD", time()); | ||
| 1236 | unless ($date_fmt eq $cur_fmt) { | ||
| 1237 | say STDERR red("Makefile too old. Please run first follows commands:\n\tmake clean\n\tperl Makefile.PL\n"); | ||
| 1238 | exit(1); | ||
| 1239 | } | ||
| 1240 | return 1; | ||
| 1241 | } | ||
| 1242 | sub _show_extra { | ||
| 1243 | # Extraneous files | ||
| 1244 | my @extra = filecheck(); # Отсутствующие файла в манифесте но есть на диске | ||
| 1245 | if (@extra) { | ||
| 1246 | say "Extraneous files:"; | ||
| 1247 | my %tmp = (); | ||
| 1248 | $tmp{$_} = '' for @extra; | ||
| 1249 | _show_list(\%tmp); | ||
| 1250 | } else { | ||
| 1251 | return 0; | ||
| 1252 | } | ||
| 1253 | return scalar(@extra); | ||
| 1254 | } | ||
| 1255 | sub _add { | ||
| 1256 | my $n = shift || return 0; | ||
| 1257 | my $d = shift || ''; | ||
| 1258 | |||
| 1259 | # Internal use only | ||
| 1260 | my $c = new CTK(plugins => [qw/cli/]); | ||
| 1261 | |||
| 1262 | # Extraneous files | ||
| 1263 | my @extra = filecheck(); # Отсутствующие файла в манифесте но есть на диске | ||
| 1264 | return skip("No files for add") unless @extra; | ||
| 1265 | |||
| 1266 | my $sel = ""; | ||
| 1267 | my $i = 0; | ||
| 1268 | foreach (@extra) { | ||
| 1269 | $i++; | ||
| 1270 | $sel = $_ if $i == $n; | ||
| 1271 | } | ||
| 1272 | return skip("File not selected") unless $sel; | ||
| 1273 | |||
| 1274 | say(cyan("Selected file for adding:")); | ||
| 1275 | say(cyan(" File : %s", $sel)); | ||
| 1276 | my $realf = File::Spec->catfile(split(/\//, $sel)); | ||
| 1277 | my $fs = (-f $realf and -r _) ? (-s _) : 0; # File size | ||
| 1278 | say(cyan(" Size : %s bytes", correct_number($fs || 0))); | ||
| 1279 | say(cyan(" Date : %s", $d)); | ||
| 1280 | my $md5 = MToken::Util::md5sum($realf); | ||
| 1281 | say(cyan(" MD5 : %s", $md5)); | ||
| 1282 | my $sha1 = MToken::Util::sha1sum($realf); | ||
| 1283 | say(cyan(" SHA1 : %s", $sha1)); | ||
| 1284 | return skip("Aborted") unless $c->cli_prompt('Are you sure you want to add this file?:','no') =~ /^\s*y/i; | ||
| 1285 | |||
| 1286 | my $comment = sprintf("\%s\t%s", $md5, $d); | ||
| 1287 | return nope("Can't add file") unless maniadd({$sel, $comment}); | ||
| 1288 | return yep("Done"); | ||
| 1289 | } | ||
| 1290 | sub _addall { | ||
| 1291 | my $d = shift || ''; | ||
| 1292 | |||
| 1293 | # Extraneous files | ||
| 1294 | my @extra = filecheck(); # Отсутствующие файла в манифесте но есть на диске | ||
| 1295 | return skip("No files for add") unless @extra; | ||
| 1296 | |||
| 1297 | foreach my $f (@extra) { | ||
| 1298 | my $realf = File::Spec->catfile(split(/\//, $f)); | ||
| 1299 | my $comment = sprintf("\%s\t%s", MToken::Util::md5sum($realf), $d); | ||
| 1300 | if (maniadd({$f, $comment})) { | ||
| 1301 | yep("File %s successfully added", $f); | ||
| 1302 | } else { | ||
| 1303 | nope("Can't add file %s", $f); | ||
| 1304 | } | ||
| 1305 | } | ||
| 1306 | return 1; | ||
| 1307 | } | ||
| 1308 | |||
| 1309 | 1; | ||
| 1310 | |||
| 1311 | # | ||
| 1312 | # External functions | ||
| 1313 | # | ||
| 1314 | package # hide me from PAUSE | ||
| 1315 | ExtUtils::Manifest; | ||
| 1316 | sub maniupdate { | ||
| 1317 | my($additions) = shift; | ||
| 1318 | my $manifile = $ExtUtils::Manifest::MANIFEST; | ||
| 1319 | |||
| 1320 | _normalize($additions); | ||
| 1321 | _fix_manifest($manifile); | ||
| 1322 | |||
| 1323 | my $manifest = maniread(); | ||
| 1324 | my @needed = grep { exists $manifest->{$_} } keys %$additions; | ||
| 1325 | return 1 unless @needed; | ||
| 1326 | |||
| 1327 | open(MANIFEST, ">", $manifile) or | ||
| 1328 | die "maniupdate() could not open $manifile: $!"; | ||
| 1329 | binmode MANIFEST, ':raw'; | ||
| 1330 | |||
| 1331 | foreach my $file (@needed) { | ||
| 1332 | $manifest->{$file} = $additions->{$file} || ''; | ||
| 1333 | } | ||
| 1334 | foreach my $file (_sort(keys(%$manifest))) { | ||
| 1335 | my $realf = File::Spec->catfile(split(/\//, $file)); | ||
| 1336 | next unless -f $realf and -r _; | ||
| 1337 | my $cmt = $manifest->{$file} || ''; | ||
| 1338 | if ($file =~ /\s/) { | ||
| 1339 | $file =~ s/([\\'])/\\$1/g; | ||
| 1340 | $file = "'$file'"; | ||
| 1341 | } | ||
| 1342 | if ($cmt) { | ||
| 1343 | printf MANIFEST "%-40s %s\n", $file, $cmt; | ||
| 1344 | } else { | ||
| 1345 | printf MANIFEST "%s\n", $file; | ||
| 1346 | } | ||
| 1347 | } | ||
| 1348 | close MANIFEST or die "Error closing $manifile: $!"; | ||
| 1349 | return 1; | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | 1; | ||
| 1353 | |||
| 1354 | __END__ |