-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathauto-unrar.pl
More file actions
executable file
·1790 lines (1386 loc) · 54 KB
/
auto-unrar.pl
File metadata and controls
executable file
·1790 lines (1386 loc) · 54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
use strict;
use warnings;
=head1 NAME
auto-unrar
=head1 ToDo
* fix Archive::Rar atributes bug and send patch to author
* check number of parts from archive
* add was modified check for archives in base dir (root)
* unrar and duplicate names
** unrar to temp directory - go back if error
** compare fname and fname.1 content, remove .1 if same
* password protected archives support
* backup old version of state_fpath files, remove backup after normal end
* unrar/test full paths of files in archive
* refactor to Perl package
* use do_cmd_sub inside do_cmds ?
=cut
use Carp qw(carp croak verbose);
use FindBin qw($RealBin);
use Getopt::Long;
use Pod::Usage;
use File::Spec::Functions qw/:ALL splitpath/;
use File::Copy;
use File::stat;
use Storable;
use Data::Dumper;
use Digest::SHA;
use Filesys::Df;
use lib "$FindBin::Bin/../lib";
use App::KeyPress;
use Archive::Rar;
=head1 NAME
unrar2.pl - Run auto-unrar utility.
=head1 SYNOPSIS
perl unrar2.pl [options]
Options:
--help ... Prints this help informations.
--ver=$NUM ... Verbosity level 0..10 Default 2.
--conf=?
DATA or ../conf/unrar-data-conf.pl
MY or ../conf/unrar-my-conf.pl
--conf_part=?
--conf_part=mydir1
Process command only for given config part name.
--cmd=? ... See availible commands below:
--cmd=unrar
Unrar/process all directories from configuration file.
--cmd=process_action_file --action_fpath=$PATH
--cmd=process_action_file --action_fpath=../conf/clear-done-list.pl
Process action file. E.g. edit/clear auto-unrar database.
--cmd=db_cleanup
Clean up, upgrade and fix db files.
--cmd=refresh_rsync_file
Refresh db and rsync files.
--cmd=db_remove_info
Remove 'info' part from db file.
--cmd=db_export
Export/save db files to other formats.
--cmd=db_remove_item --item_name=$PATH
--cmd=db_remove_item --item_name=/subdir2/subdir2b
Remove this item from db file. Use --conf_part to be more specific.
--cmd=db_remove_dirs_from_src_dir
Remove all directories found in source directory from db file. After this
auto-unrar will process all directories inside source dir again.
=cut
# Global variables.
my $ver = 0;
my $keypress_obj = undef;
# Cache for input directories times of last modification .
my $src_dir_mtimes = {};
=head1 DESCRIPTION
B<This program> run auto-unrar utility. Smart extracting or RAR archives while
respect/dumplicate directory structure.
=head1 METHODS
=head2 main
Parse command line options and run commands.
=cut
sub main {
# See SYNOPSIS part of perldoc above.
my $help = 0;
my $options = {
ver => 3,
conf_fpath => undef,
only_conf_part => undef,
cmd => undef,
};
my $conf_fpath = $ARGV[0];
my $options_ok = GetOptions(
'help|h|?' => \$help,
'ver|v=i' => \$options->{'ver'},
'conf=s' => \$options->{'conf_fpath'},
'conf_part=s' => \$options->{'only_conf_part'},
'cmd=s' => \$options->{'cmd'},
'action_fpath=s' => \$options->{'action_fpath'},
'item_name=s' => \$options->{'item_name'},
);
if ( $help || !$options_ok ) {
pod2usage(1);
return 0 unless $options_ok;
return 1;
}
# Set global variables.
$ver = $options->{ver};
if ( ! $options->{cmd} ) {
print "No command selected. Option 'cmd' is mandatory. Use --help to see more info.\n";
return 0;
}
unless ( $options->{'conf_fpath'} ) {
print "No configuration path/name given. Option 'conf' is mandatory. Use --help to see more info.\n";
return 0;
}
# Process and test conf_fpath.
if ( $options->{'conf_fpath'} eq 'MY' ) {
$options->{'conf_fpath'} = catfile( $RealBin, '..', 'conf', 'unrar-my-conf.pl' );
} elsif ( $options->{'conf_fpath'} eq 'DATA' ) {
$options->{'conf_fpath'} = catfile( $RealBin, '..', 'conf', 'unrar-data-conf.pl' );
}
unless ( -f $options->{'conf_fpath'} ) {
print "Can't find defined config file '$options->{'conf_fpath'}'.\n" if $ver >= 1;
return 0;
}
# Prepare/check 'process_action_file' cmd variables.
my $action_file_data = undef;
if ( $options->{cmd} eq 'process_action_file' ) {
unless ( $options->{action_fpath} ) {
print "No action file path given. Option 'action_fpath' is mandatory. Use --help to see more info.\n";
return 0;
}
print "action_fpath: '$options->{action_fpath}'\n" if $ver >= 5;
unless ( -f $options->{action_fpath} ) {
print "Action file '$options->{action_fpath}' not found.\n" if $ver >= 1;
return 0;
}
$action_file_data = load_perl_data( $options->{action_fpath} );
if ( (not $action_file_data) || ref $action_file_data ne 'ARRAY' ) {
print "Action file '$options->{action_fpath}' data loading error.\n" if $ver >= 1;
return 0;
}
# Prepare/check 'db_remove_item' cmd variables.
} elsif ( $options->{cmd} eq 'db_remove_item' ) {
unless ( $options->{item_name} ) {
print "No item name given. Option 'item_name' is mandatory. Use --help to see more info.\n";
return 0;
}
}
# Init keypress object.
$keypress_obj = App::KeyPress->new(
$ver,
0 # $debug
);
$keypress_obj->set_quit_pressed_sub(
sub {
print "Quit keypressed.\n" if $ver >= 2;
}
);
$keypress_obj->set_return_on_exit( 1 );
my $dirs_conf = load_perl_data( $options->{'conf_fpath'} );
return 0 unless defined $dirs_conf;
# dumper( '$dirs_conf', $dirs_conf ); my_croak(); # debug
# Testing.
if ( 0 ) {
my $dconf = $dirs_conf->[0];
my $base_dir = $dconf->{src_dir};
my $sub_dir = 'subdir6/subdir5A-file';
my $full_path = catdir( $base_dir, $sub_dir );
my $dir_items = load_dir_content( $full_path );
exit unless defined $dir_items;
do_for_rar_file(
$dconf,
[], # $finish_cmds
$base_dir,
$sub_dir,
'test14.part1.rar', # $file_name,
$dir_items
);
return 1;
}
# Main loop.
my $last_dconf_num = $#$dirs_conf;
DCONF: foreach my $dconf_num ( 0..$last_dconf_num ) {
my $dconf = $dirs_conf->[ $dconf_num ];
# skip if only one selected
if ( defined $options->{conf_part} && $dconf->{name} ne $options->{conf_part} ) {
print "Skipping configuration $dconf->{name} (only '$options->{conf_part}' selected).\n" if $ver >= 2;
next DCONF;
}
# Check configuration.
if ( $dconf->{basedir_deep} <= 0 ) {
print "Configuration $dconf->{name} error: 'basedir_deep' must be >= 1.\n" if $ver >= 1;
next DCONF;
}
my $state = load_state( $dconf );
# Cmd 'unrar'.
if ( $options->{cmd} eq 'unrar' ) {
unless ( -d $dconf->{src_dir} ) {
print "Input directory '$dconf->{src_dir}' doesn't exists.\n" if $ver >= 1;
next DCONF;
}
unless ( -d $dconf->{dest_dir} ) {
print "Output directory '$dconf->{dest_dir}' doesn't exists.\n" if $ver >= 1;
next DCONF;
}
dumper( 'dconf', $dconf ) if $ver >= 5;
my $ud_err_code = unrar_dir_start(
$state, # $state
[], # $undo_cmds
[], # $finish_cmds
$dconf, # $dcong
'', # $sub_dir
0 # $deep
);
# Clean up 'info' part.
foreach my $if_name ( keys %{$state->{done}} ) {
if ( exists $state->{info}->{ $if_name } ) {
delete $state->{info}->{ $if_name };
}
}
save_state( $state, $dconf );
if ( $keypress_obj->get_exit_keypressed() ) {
if ( $dconf_num < $last_dconf_num ) {
print "Keypress for Quit - skipping next configuration options.\n" if $ver >= 2;
}
last;
}
dumper( "state for '$dconf->{name}':", $state ) if $ver >= 5;
# Cmd 'process_action_file'.
} elsif ( $options->{cmd} eq 'process_action_file' ) {
ADATA: foreach my $anum ( 0..$#$action_file_data ) {
my $acmd = $action_file_data->[ $anum ];
print "Action " . ($anum+1) . ": '$acmd->{action}'\n" if $ver >= 8;
if ( exists $acmd->{where} ) {
my $where = $acmd->{where};
foreach my $w_key ( keys %$where ) {
my $w_value = $where->{$w_key};
print "Key '$w_key', value '$w_value'\n" if $ver >= 10;
if ( not exists $dconf->{$w_key} ) {
print "Unknown configuration key '$w_key'\n" if $ver >= 2;
next ADATA;
} elsif ( $dconf->{$w_key} ne $w_value ) {
print "Configuration key '$w_key' has value '$dconf->{$w_key}' != '$w_value'.\n" if $ver >= 10;
next ADATA;
}
}
print "Where condition fulfilled.\n" if $ver >= 4;
}
if ( $acmd->{action} eq 'remove_from_done_list' ) {
unless ( exists $acmd->{what} ) {
print "What part not found in action file.\n" if $ver >= 2;
next ADATA;
}
foreach my $item_name ( @{$acmd->{what}} ) {
print "Going to remove item '$item_name'\n" if $ver >= 10;
remove_item_from_state( $state, $item_name );
}
} else {
print "Unknown action '$acmd->{action}'.\n" if $ver >= 2;
}
}
save_state( $state, $dconf );
next DCONF;
# Cmd 'refresh_rsync_file'.
}elsif ( $options->{cmd} eq 'refresh_rsync_file' ) {
save_state( $state, $dconf );
next DCONF;
# Cmd 'db_cleanup'.
} elsif ( $options->{cmd} eq 'db_cleanup' ) {
# Upgrade from prev versions.
delete $state->{err} if exists $state->{err};
# Fix old errors.
delete $state->{done}->{''} if exists $state->{done}->{''};
# Clean up data.
foreach my $name ( keys %{$state->{done}} ) {
if ( exists $state->{info}->{ $name } ) {
delete $state->{info}->{ $name };
}
}
save_state( $state, $dconf );
next DCONF;
# Cmd 'db_remove_info'.
} elsif ( $options->{cmd} eq 'db_remove_info' ) {
delete $state->{info} if exists $state->{info};
save_state( $state, $dconf );
next DCONF;
# Cmd 'db_export'.
} elsif ( $options->{cmd} eq 'db_export' ) {
# Export to other format.
if ( $dconf->{state_store_type} eq 'perl' ) {
$dconf->{state_store_type} = 'storable';
$dconf->{state_fpath} =~ s{\.pl$}{\.db};
} else {
$dconf->{state_store_type} = 'perl';
$dconf->{state_fpath} =~ s{\.db$}{\.pl};
}
save_state( $state, $dconf );
next DCONF;
# Cmd 'db_remove_item'.
} elsif ( $options->{cmd} eq 'db_remove_item' ) {
# Remove some file from done and info parts.
my $item_name = $options->{item_name};
dumper( 'old $state', $state ) if $ver >= 6;
remove_item_from_state( $state, $item_name );
dumper( 'new $state', $state ) if $ver >= 6;
save_state( $state, $dconf );
next DCONF;
# Cmd 'db_remove_dirs_from_src_dir'.
} elsif ( $options->{cmd} eq 'db_remove_dirs_from_src_dir' ) {
# Remove dirs found inside input dir from state:done list.
my $items = load_dir_content( $dconf->{src_dir} );
# dumper( '$items', $items ) if $ver >= 10;
foreach my $item ( @$items ) {
my $i_path = catfile( $dconf->{src_dir}, $item );
next DCONF unless -d $i_path;
my $full_item_name = '/' . $item;
print "full_item_name: '$full_item_name'\n" if $ver >= 10;
remove_item_from_state( $state, $full_item_name );
}
save_state( $state, $dconf );
next DCONF;
} # end of last cmd
} # end foreach
return 1;
}
=head2 my_croak
Do clean up before croak with given message.
=cut
sub my_croak {
my ( $err_msg ) = @_;
$keypress_obj->cleanup_before_exit();
croak $err_msg;
}
sub load_perl_data {
my ( $fpath ) = @_;
print "Loading data from '$fpath'.\n" if $ver >= 4;
my( $exception, $conf );
{
local $@;
$conf = do $fpath;
$exception = $@;
}
if ( $exception ) {
print "Loading data from '$fpath' failed: $exception\n" if $ver >= 2;
return undef;
}
return $conf;
}
sub debug_suffix {
my ( $msg, $caller_back ) = @_;
$caller_back = 1 unless defined $caller_back;
$msg =~ s/[\n\s]+$//;
my $has_new_line = 0;
$has_new_line = 1 if $msg =~ /\n/;
my $caller_line = (caller 0+$caller_back)[2];
my $caller_sub = (caller 1+$caller_back)[3];
$msg .= " ";
$msg .= "(" unless $has_new_line;
$msg .= "$caller_sub " if $caller_sub;
$msg .= "on line $caller_line";
$msg .= ')' unless $has_new_line;
$msg .= ".\n";
$msg .= "\n" if $has_new_line;
return $msg;
}
sub dumper {
my ( $prefix_text, $data, $caller_back ) = @_;
my $ot = '';
if ( (not defined $data) && $prefix_text =~ /^\n$/ ) {
$ot .= $prefix_text;
return 1;
}
$caller_back = 0 unless defined $caller_back;
if ( defined $prefix_text ) {
$prefix_text .= ' ';
} else {
$caller_back = 0;
$prefix_text = '';
}
$ot = $prefix_text;
if ( defined $data ) {
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Purity = 1;
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Sortkeys = 1;
$ot .= Data::Dumper->Dump( [ $data ], [] );
}
if ( $caller_back >= 0 ) {
$ot = debug_suffix( $ot, $caller_back+1 );
}
print $ot;
return 1;
}
sub load_dir_content {
my ( $dir_name ) = @_;
my $dir_h;
if ( not opendir($dir_h, $dir_name) ) {
print STDERR "Directory '$dir_name' not open for read.\n" if $ver >= 1;
return undef;
}
my @all_items = readdir( $dir_h );
close($dir_h);
return [] unless scalar @all_items;
my $items = [];
foreach my $name ( @all_items ) {
next if $name =~ /^\.$/;
next if $name =~ /^\..$/;
next if $name =~ /^\s*$/;
push @$items, $name;
}
return $items;
}
sub do_cmd_sub {
my ( $cmd_sub, $msg ) = @_;
my $done_ok = 0;
my $out_data = undef;
my $sleep_time = 1;
while ( not $done_ok ) {
my $ret_val = $cmd_sub->();
$out_data = undef;
if ( ref $ret_val ) {
( $done_ok, $out_data ) = @$ret_val;
} else {
$done_ok = $ret_val;
}
unless ( $done_ok ) {
if ( $ver >= 5 ) {
print $msg;
print $out_data if defined $out_data;
print " Sleeping $sleep_time s ...\n";
}
$keypress_obj->sleep_and_process_keypress( $sleep_time );
my $max_sleep_time = 10*60; # max 10 minutes
if ( $sleep_time < $max_sleep_time ) {
$sleep_time = $sleep_time * $sleep_time;
$sleep_time = $max_sleep_time if $sleep_time > $max_sleep_time;
}
}
}
return $out_data;
}
sub get_item_info {
my ( $base_path, $item_sub_path ) = @_;
my $item_path = catdir( $base_path, $item_sub_path );
my $stat_obj = get_item_stat_obj( $item_path );
return undef unless $stat_obj;
my $info = {
path => $item_sub_path,
mtime => $stat_obj->mtime,
};
if ( -d $item_path ) {
$info->{is_dir} = 1;
} else {
$info->{size} = $stat_obj->size;
}
return $info;
}
sub get_rec_content_info {
my ( $info, $base_path, $item_sub_path ) = @_;
my $item_info = get_item_info( $base_path, $item_sub_path );
return undef unless defined $item_info;
push @$info, $item_info;
my $item_path = catdir( $base_path, $item_sub_path );
# No directory.
return 1 unless -d $item_path;
my $dir_items = load_dir_content( $item_path );
return undef unless defined $dir_items;
foreach my $sitem_name ( sort @$dir_items ) {
my $sitem_sub_path = catdir( $item_sub_path, $sitem_name );
my $sret_code = get_rec_content_info( $info, $base_path, $sitem_sub_path );
return undef unless $sret_code;
}
return 1;
}
sub get_content_info_and_hash {
my ( $base_path, $item_sub_path ) = @_;
my $info = [];
my $ret_code = get_rec_content_info( $info, $base_path, $item_sub_path );
return ( undef, undef ) unless $ret_code;
my $hash_str = '';
foreach my $item_info ( @$info ) {
map { $hash_str .= '|' . $_ . '|' . $item_info->{$_} } sort keys %$item_info;
$hash_str .= '|';
}
my $hash = Digest::SHA::sha1_hex( $hash_str );
return ( $info, $hash );
}
sub save_item_rec_content_info {
my ( $state, $dconf, $base_path, $item_sub_path, $info, $save_content_info ) = @_;
return 1 unless $item_sub_path;
$info = {} unless defined $info;
my ( $content_info, $hash ) = get_content_info_and_hash( $base_path, $item_sub_path );
$info->{time} = time();
if ( $save_content_info ) {
$info->{content} = $content_info;
}
$info->{content_meta_hash} = $hash;
$state->{info}->{$item_sub_path} = [] unless defined $state->{info}->{$item_sub_path};
push @{ $state->{info}->{$item_sub_path} }, $info;
return save_state( $state, $dconf );
}
sub save_item_done {
my ( $state, $dconf, $item_name ) = @_;
if ( defined $item_name && $item_name ) {
$state->{done}->{$item_name} = time();
}
my $state_store_type = $dconf->{state_store_type};
if ( $state_store_type eq 'storable' ) {
do_cmd_sub(
sub { store( $state, $dconf->{state_fpath} ); },
"Saving state to '$dconf->{state_fpath}' failed (type=$state_store_type)."
);
} elsif ( $state_store_type eq 'perl' ) {
my $dumper_obj = Data::Dumper->new( [ $state ], [ 'state' ] );
$dumper_obj->Purity(1)->Terse(1)->Deepcopy(1)->Sortkeys(1)->Indent(1);
my $state_dump_code = $dumper_obj->Dump;
do_cmd_sub(
sub {
my $fh;
open( $fh, '>', $dconf->{state_fpath} ) or return ( 0, $! );
print $fh $state_dump_code;
close $fh or return ( 0, $! );
},
"Saving state to '$dconf->{state_fpath}' failed (type=$state_store_type)."
);
} else {
print "Unknown state_store_type '$state_store_type'.\n" if $ver >= 2;
return 0;
}
if ( defined $item_name ) {
print "Item '$item_name' saved to state_fpath.\n" if $ver >= 5;
} else {
print "State saved to state_fpath.\n" if $ver >= 5;
}
if ( $dconf->{exclude_list} ) {
my $out_fh = do_cmd_sub(
sub {
my $out_fh = undef;
my $ok = open( $out_fh, '>', $dconf->{exclude_list} );
return [ $ok, $out_fh ];
},
"Open file '$dconf->{exclude_list}' for write."
);
foreach my $item ( sort keys %{ $state->{done} } ) {
my $line = "- $item\n";
print $out_fh $line;
}
do_cmd_sub(
sub { close $out_fh; },
"Closing file '$dconf->{exclude_list}'."
);
}
return 1;
}
sub save_state {
my ( $state, $dconf ) = @_;
return save_item_done( $state, $dconf, undef );
}
sub load_state {
my ( $dconf ) = @_;
my $state_store_type = 'storable';
if ( $dconf->{state_fpath} =~ /\.pl$/ ) {
$state_store_type = 'perl';
}
$dconf->{state_store_type} = $state_store_type;
unless ( -e $dconf->{state_fpath} ) {
return {
'done' => {},
};
}
my $state = undef;
if ( $dconf->{state_store_type} eq 'storable' ) {
$state = retrieve( $dconf->{state_fpath} );
} else {
$state = load_perl_data( $dconf->{state_fpath} );
}
return $state;
}
sub get_item_stat_obj {
my ( $path ) = @_;
my $stat_obj = stat( $path );
unless ( defined $stat_obj ) {
print "Command stat for item '$path' failed.\n" if $ver >= 1;
return undef;
}
return $stat_obj;
}
sub get_item_mtime {
my ( $path ) = @_;
my $stat_obj = get_item_stat_obj( $path );
return undef unless $stat_obj;
return $stat_obj->mtime;
}
sub set_dir_mtime {
my ( $dest_dir_path, $src_mtime ) = @_;
my $act_time = time();
# Do not allow time in future.
$src_mtime = $act_time if $src_mtime > $act_time;
unless ( utime($act_time, $src_mtime, $dest_dir_path) ) {
print "Command utime for '$dest_dir_path' failed: $^E\n" if $ver >= 1;
return 0;
}
print "Finished setting '$dest_dir_path' mtime to " . (localtime $src_mtime) . "\n" if $ver >= 8;
return 1;
}
sub set_saved_dir_mtime {
my ( $dest_dir_path, $src_dir_part ) = @_;
unless ( defined $src_dir_mtimes->{ $src_dir_part } ) {
print "Error: Saved mtime for dir '$src_dir_part' not found.\n" if $ver >= 3;
return 0;
}
return set_dir_mtime( $dest_dir_path, $src_dir_mtimes->{ $src_dir_part } );
}
sub copy_dir_mtime {
my ( $dest_dir_path, $src_dir_path ) = @_;
my $src_mtime = get_item_mtime( $src_dir_path );
return 0 unless defined $src_mtime;
return set_dir_mtime( $dest_dir_path, $src_mtime );
}
sub mkdir_copy_mtime {
my ( $dest_dir_path, $src_dir_path ) = @_;
return 1 if -d $dest_dir_path;
print "mkdir_copy_mtime '$src_dir_path' -> '$dest_dir_path'\n" if $ver >= 8;
unless ( mkdir( $dest_dir_path, 0777 ) ) {
print "Command mkdir '$dest_dir_path' failed: $^E\n" if $ver >= 1;
return 0;
}
return copy_dir_mtime( $dest_dir_path, $src_dir_path );
}
sub mkpath_copy_mtime {
my ( $dest_base_dir, $src_base_dir, $sub_dirs ) = @_;
my $full_dest_dir = catdir( $dest_base_dir, $sub_dirs );
return 1 if -d $full_dest_dir;
unless ( -d $dest_base_dir ) {
print "Error mkpath_copy_mtime dest_base_dir '$dest_base_dir' doesn't exists.\n" if $ver >= 1;
return 0;
}
unless ( -d $src_base_dir ) {
print "Error mkpath_copy_mtime src_base_dir '$src_base_dir' doesn't exists.\n" if $ver >= 1;
return 0;
}
my $full_src_dir = catdir( $src_base_dir, $sub_dirs );
unless ( -d $full_src_dir ) {
print "Error mkpath_copy_mtime full_src_dir '$full_src_dir' doesn't exists.\n" if $ver >= 1;
return 0;
}
my @dir_parts = File::Spec->splitdir( $sub_dirs );
my $tmp_dest_dir = $dest_base_dir;
my $tmp_src_dir = $src_base_dir;
foreach my $dir ( @dir_parts ) {
$tmp_dest_dir = catdir( $tmp_dest_dir, $dir );
$tmp_src_dir = catdir( $tmp_src_dir, $dir );
return 0 unless mkdir_copy_mtime( $tmp_dest_dir, $tmp_src_dir );
}
return 1;
}
sub do_for_dir {
my ( $dconf, $finish_cmds, $undo_cmds, $base_dir, $sub_dir ) = @_;
push @$finish_cmds, [ 'mkpath_copy_mtime', $dconf->{dest_dir}, $base_dir, $sub_dir ];
my $dest_path = catdir( $dconf->{dest_dir}, $sub_dir );
push @$undo_cmds, [ 'rm_rec_empty_dir', $dest_path ];
return 1;
}
sub do_for_rar_file {
my ( $dconf, $finish_cmds, $base_dir, $sub_dir, $file_name, $dir_items ) = @_;
my $base_name_part = undef;
my $is_rar_archive = 0;
my $part_num = undef;
my $multipart_type = undef;
my $part_num_str_length = undef;
if ( $file_name =~ /^(.*)\.part(\d+)\.rar$/ ) {
my $tmp_base_name_part = $1;
my $tmp_part_num = $2;
# See test/subdir11.
my $mr_type_found = 0;
NEXT_FILE: foreach my $next_file_name ( sort @$dir_items ) {
if ( $next_file_name =~ /^\Q${tmp_base_name_part}.part${tmp_part_num}\E\.r(\d+)$/ ) {
$mr_type_found = 1;
last;
}
}
if ( not $mr_type_found ) {
$base_name_part = $tmp_base_name_part;
$part_num = $tmp_part_num;
$part_num_str_length = length( $tmp_part_num );
$is_rar_archive = 1;
$multipart_type = 'part';
}
}
if ( defined $multipart_type ) {
# Is 'part' type.
} elsif ( $file_name =~ /^(.*)\.rar$/ ) {
$base_name_part = $1;
$part_num = 1;
$part_num_str_length = undef;
$is_rar_archive = 1;
# initial value, is set to '' unless other parts found
$multipart_type = 'mr';
} elsif ( $file_name =~ /^(.*)\.r(\d+)$/ ) {
$base_name_part = $1;
$part_num = $2 + 2;
$part_num_str_length = length( $2 );
$is_rar_archive = 1;
# initial value, is set to '' unless other parts found
$multipart_type = 'mr';
} elsif ( $file_name =~ /^(.*)\.(\d{3})$/ ) {
$base_name_part = $1;
$part_num = $2;
$part_num_str_length = length( $2 );
$is_rar_archive = 1;
$multipart_type = 'unsup';
}
return ( 0, "File isn't rar archive.", undef, undef )
unless $is_rar_archive;
return ( 1, "File is part of multiparts archive (type $multipart_type), but isn't first part.", undef, undef )
if $multipart_type && ($part_num != 1);
return -1
unless mkpath_copy_mtime( $dconf->{dest_dir}, $base_dir, $sub_dir );
my $dest_dir = catdir( $dconf->{dest_dir}, $sub_dir );
my $file_sub_path = catfile( $sub_dir, $file_name );
my $file_path = catfile( $base_dir, $sub_dir, $file_name );
my $rar_ver = $ver - 10;
$rar_ver = 0 if $rar_ver < 0;
my %rar_conf = (
'-archive' => $file_path,
'-initial' => $dest_dir,
);
$rar_conf{'-verbose'} = $rar_ver if $rar_ver;
my $rar_obj = Archive::Rar->new( %rar_conf );
$rar_obj->List();
my @files_extracted = $rar_obj->GetBareList();
if ( $ver >= 10 ) {
print "Input file '$file_name':\n";
$rar_obj->PrintList();
dumper( 'rar_obj->list', $rar_obj->{list} );
dumper( '@files_extracted', \@files_extracted );
}
my @rar_parts_list = ();
$rar_parts_list[0] = $file_name;
my %files_extracted = map { $_ => 1 } @files_extracted;
#dumper( '%files_extracted', \%files_extracted );
my $other_part_found = 0;
NEXT_FILE: foreach my $next_file_name ( sort @$dir_items ) {
next NEXT_FILE if $file_name eq $next_file_name;
my $other_part_num = undef;
my $other_part_num_str_length = undef;
if ( $multipart_type eq 'part' ) {
if ( $next_file_name =~ /^\Q$base_name_part\E\.part(\d+)\.rar$/ ) {
$other_part_num = $1;
$other_part_num_str_length = length( $1 );
}
} elsif ( $multipart_type eq 'mr' ) {
if ( $next_file_name =~ /^\Q$base_name_part\E\.r(\d+)$/ ) {
$other_part_num = $1 + 2;
unless ( defined $part_num_str_length ) {
$part_num_str_length = length( $1 );
}
$other_part_num_str_length = length( $1 );
}
} elsif ( $multipart_type eq 'unsup' ) {
if ( $next_file_name =~ /^\Q$base_name_part\E\.(\d+)$/ ) {
$other_part_num = $1;
$other_part_num_str_length = length( $1 );
}
}
next unless defined $other_part_num;
if ( defined $part_num_str_length ) {
if ( $other_part_num_str_length != $part_num_str_length ) {
print "Error: Found other_part_num $other_part_num with length $other_part_num_str_length which isn't same as base part length $part_num_str_length.\n" if $ver >= 2;
next;
}