11 my @monikop_banner = (
12 " _/ _/ _/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/_/_/ ",
13 " _/_/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/ _/",
14 " _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/_/_/ ",
15 " _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ ",
16 "_/ _/ _/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ ",
19 # Version number. Should agree with Pokinom's one.
20 # Format: v<1>.<2>.<3> where
23 # <1> = incompatible change.
24 my $version = 'v0.1.0';
27 # 0 = clean UI; 1 = lots of scrolling junk; anything else = both (pipe to file).
29 $debug = $ARGV[1] if $ARGV[1];
31 # Where to read local configuration:
32 my $monikop_config = '~/monikop/monikop.config';
33 $monikop_config = $ARGV[0] if $ARGV[0];
35 ########################################
37 ########################################
38 # Possible data sources, and by what directory name to represent them in
40 # When the latter is not unique, care must be taken that all pathnames in the
41 # respective sources are unique.
44 # Possible mount points of data destinations. Must be unique.
45 my @usable_mount_points;
47 # Common directory (under a mount point) to put new data in.
48 # Must agree with Pokinom's setting.
49 my $path_under_mount_point;
51 # Directories (under any mount point) of this name will be deleted
52 # Must agree with Pokinom's setting.
53 my $path_under_mount_point_backed_up;
55 # Directory name (under a mount point) while being deleted.
56 # Must agree with Pokinom's setting.
57 my $path_under_mount_point_being_deleted;
59 # Path and file name prefix of rsync's raw logs:
62 # Path and file name prefix of the list of successfully rsynced files:
65 # How to suffix the name of the duplicate of a safe file:
66 my $safe_file_backup_suffix;
68 # How to suffix the name of an unfinished safe file:
69 my $safe_file_unfinished_suffix;
71 # What to do (shutdown) when F3 has been pressed:
74 # What to do (reboot) when F6 has been pressed:
77 # Rsyncs time (in seconds) to wait for a response:
80 # Rsyncs directory (relative to destination) for partially transferred files.
81 # Must agree with Pokinom's setting.
82 my $rsync_partial_dir_name;
84 # Put actual values into the above.
85 eval `cat $monikop_config`;
87 # Time in seconds before rsync is restarted and user information is
89 my $coffee_break = 10;
91 # Places to store run-time information to share between threads:
92 my %speeds :shared
; # rsync output
93 my %progress_ratios :shared
; # rsync output
94 my %destination_usages :shared
; # i.e. used/unused
95 my %destination_usage_ratios :shared
;
96 my %destination_source_is_writing_to :shared
;
97 my %reachable :shared
;
99 sub debug_print
{ if ($debug) { print @_; } };
101 # Return the hash referenced by argument, which is sorted if accessed as an
105 my @sorted_hash = ();
106 foreach my $key (sort keys %hash_table) {
107 push @sorted_hash, $key, $hash_table{$key};
112 # Turn a path into a legal perl identifier:
113 sub make_key_from_path
{
115 ($path) =~ s/\/?(.*)\/?
/$1/g;
122 $source_roots{make_key_from_path
$_} = $_
125 my %source_dirs_in_destination;
127 $source_dirs_in_destination{make_key_from_path
$_} = $sources{$_}
130 # Crudely turn date string(s) into a number. Chronological order is preserved.
132 my $date = join '', @_;
133 $date =~ tr/ \/:-//d
;
137 # Return sorted intersection of arrays which are supposed to have unique
140 my @intersection = ();
143 foreach $element (@_) { $count{$element}++ }
144 foreach $element (keys %count) {
145 push @intersection, $element if $count{$element} > 1;
150 # Write @content to a file with name $filename or a name starting with
151 # $filename and ending with $safe_file_backup_suffix. Leave at least one such
152 # file, even if interrupted.
154 my ($filename, @content) = @_;
155 my $filename_a = $filename;
156 my $filename_b = $filename . $safe_file_backup_suffix;
157 my $filename_unfinished = $filename . $safe_file_unfinished_suffix;
158 local (*FILE_UNFINISHED
);
159 open FILE_UNFINISHED
, '>', $filename_unfinished
160 or die "[" . $$ . "] open $filename_unfinished failed: $!\n";
161 print FILE_UNFINISHED
@content;
162 close FILE_UNFINISHED
;
163 qx(cp
$filename_unfinished $filename_b);
164 qx(mv
$filename_unfinished $filename_a);
167 # Put contents of $filename into an array:
171 open FILE
, '<', $filename
172 or warn "[" . $$ . "] open $filename failed: $!\n";
178 # Read a file written by safe_write
181 my $filename_a = $filename;
182 my $filename_b = $filename . $safe_file_backup_suffix;
183 if (stat $filename_a) { $filename = $filename_a }
184 elsif (stat $filename_b) { $filename = $filename_b }
186 debug_print
"SAFE_READ: $filename";
190 my @destination_roots;
195 my %rsync_dir_exec_form;
196 my %rsync_dir_err_form;
197 my %rsync_worker_thread;
198 my %being_deleted_thread;
199 my $destinations_monitor_thread;
202 sub rsync_preparation_form
{
204 $speeds{$source} = "-";
207 ########## Capture rsync's status messages for use by UI
208 '$rsync_outfun{\'', $source, '\'} = sub {',
209 ' my ($outline, $outputchannel) = @_ ; ',
210 ' my ($speed) = $outline =~ /\d+\s+\d+%\s+(\S+)/; ',
211 ' my ($progress_ratio) = ',
212 ' $outline =~ /.+to-check=(\d+\/\d+)\)$/; ',
213 ' if ($speed and $outputchannel eq \'out\') {',
214 ' $speeds{\'', $source, '\'} = $speed;',
216 ' $speeds{\'', $source, '\'} = "-";',
218 ' if ($progress_ratio and $outputchannel eq \'out\') {',
219 ' $progress_ratios{\'', $source, '\'} = $progress_ratio;',
223 ########## Run rsync: main worker
224 '$rsync{\'', $source, '\'} = File::Rsync->new; ',
225 ########## Return fodder for another eval
226 '$rsync_exec_form{\'', $source, '\'} = sub {',
227 ' my ($complete_destination) = @_;',
228 ' \'$rsync{\\\'', $source, '\\\'}->exec(',
230 ' src => \\\'', $source_roots{$source}, '/\\\', ',
231 ' dest => \\\'\' . $complete_destination . \'/\\\', ',
232 ' outfun => $rsync_outfun{\\\'', $source, '\\\'}, ',
233 ' progress => 1, debug => 0, verbose => 0, ',
234 ' filter => [\\\'merge,- ', $finished_prefix, $source,
237 ' \\\'--recursive\\\', \\\'--times\\\', ',
238 ' \\\'--partial-dir=',
239 $rsync_partial_dir_name, '\\\', ',
240 ' \\\'--timeout=', $rsync_timeout, '\\\', ',
241 ' \\\'--prune-empty-dirs\\\', ',
242 ' \\\'--log-file-format=%i %b %l %M %n\\\', ',
245 '\\\'--compare-dest=' . $_ . '/'
246 . $path_under_mount_point . '/'.
247 $source_dirs_in_destination{$source}
250 ( @destination_roots )),
251 ' , \\\'--log-file=', $rsync_log_prefix, $source, '\\\'] ',
256 ########## Run rsync: get directory from source
257 '$rsync_dir{\'', $source, '\'} = File::Rsync->new; ',
258 ########## Return fodder for another eval: dir
259 '$rsync_dir_exec_form{\'', $source, '\'} = sub {',
260 ' \'$rsync_dir{\\\'', $source, '\\\'}->list(',
262 ' src => \\\'', $source_roots{$source}, '/\\\', ',
263 ' literal => [ \\\'--recursive\\\', ',
264 ' \\\'--timeout=', $rsync_timeout, '\\\'] ',
269 ########## Return fodder for another eval: error code from last rsync call
270 '$rsync_dir_err_form{\'', $source, '\'} = sub {',
271 ' \'$rsync_dir{\\\'', $source, '\\\'}->err();\' ',
276 sub act_on_keypress
{
277 my ($pressed_key) = @_;
278 if ($pressed_key eq 267) { qx($key_f3_action) }
279 elsif ($pressed_key eq 270) { qx($key_f6_action); }
282 # Run rsync for one $source, try all destinations:
283 sub rsync_someplace
{
284 my ($source, @destinations) = @_;
287 my $rsync_log_name = $rsync_log_prefix . $source;
288 my $finished_name = $finished_prefix . $source;
289 foreach (@destinations) {
290 $destination_source_is_writing_to{$source} = $_;
291 my $common_destination = $_ . '/' . $path_under_mount_point;
292 my $complete_destination = $common_destination . '/'
293 . $source_dirs_in_destination{$source};
294 qx(mkdir -p
$common_destination);
295 if ($?
) { die "Fatal: $common_destination is not writable."}
296 if (eval ($rsync_exec_form{$source} ($complete_destination))) {
297 debug_print
"EVAL RSYNC_EXEC_FORM (successful) $source,\ $complete_destination: $@ \n";
299 last; # unnecessary reruns would put empty
300 # dirs into otherwise unused destinations
302 debug_print
"EVAL RSYNC_EXEC_FORM (failed) $source, $complete_destination: $@ \n";
310 $display_thread->kill('TERM')->join;
311 die "Caught signal $_[0]";
315 # Preparations done; sleeves up!
317 # Make sure we have dirs to put our logs in:
319 my ($filename, $directory) = fileparse
$_;
320 qx(mkdir -p
$directory);
321 } ( $rsync_log_prefix, $finished_prefix );
323 # Find usable destinations:
324 my @raw_mount_points = grep (s/\S+ on (.*) type .*/$1/, qx/mount/);
325 chomp @raw_mount_points;
326 @destination_roots = intersection
@raw_mount_points, @usable_mount_points;
327 debug_print
"DESTINATION_ROOTS:\n";
328 debug_print
@destination_roots;
330 # Clean up destinations:
332 my $p_i_d = $_ . '/' . $path_under_mount_point;
333 my $p_i_d_backed_up = $_ . '/' . $path_under_mount_point_backed_up;
334 my $p_i_d_being_deleted = $_ . '/' . $path_under_mount_point_being_deleted;
335 if (-d
$p_i_d_backed_up and -d
$p_i_d_being_deleted) {
336 warn "[" . $$ . "] " .
337 "Both $p_i_d_backed_up and $ p_i_d_being_deleted exist.\n" .
338 "This does not normally happen.\n" .
339 "I'm deleting $p_i_d_being_deleted. Be patient.\n";
340 qx(rm
-rf
$p_i_d_being_deleted);
342 qx(mv
-f
$p_i_d_backed_up $p_i_d_being_deleted 2> /dev/null
);
343 $being_deleted_thread{$_} = async
{
344 $SIG{TERM
} = sub { threads
->exit() };
345 qx(rm
-rf
$p_i_d_being_deleted); };
346 } @destination_roots;
348 if (scalar @destination_roots) {
349 # Set up and start things per source_root:
351 # rotate for crude load balancing:
352 push (@destination_roots, shift (@destination_roots));
353 $progress_ratios{$_} = "?"; # Initialize for UI
354 $rsync_worker_thread{$_} = async
{
355 $SIG{TERM
} = sub { threads
->exit() };
356 my $rsync_log_name = $rsync_log_prefix . $_;
357 my $finished_name = $finished_prefix . $_;
358 debug_print
'rsync_preparation_form:' .
359 rsync_preparation_form
($_). "\n";
360 eval rsync_preparation_form
$_;
361 debug_print
"EVAL RSYNC_PREPARATION_FORM $_: $@ \n";
363 debug_print
'rsync_dir_exec_form $_:'.
364 $rsync_dir_exec_form{$_} () . "\n";
365 my @rsync_ls = eval $rsync_dir_exec_form{$_}();
366 $reachable{$_} = eval $rsync_dir_err_form{$_}() ?
0 : 1;
367 debug_print
"REACHABLE: $reachable{$_}\n";
368 if ($reachable{$_}) {
369 my %old_finished = safe_read
$finished_name;
370 if (-f
$rsync_log_name) {
371 my @rsync_log = read_list
$rsync_log_name;
372 foreach (@rsync_log) {
373 my ($file_length, $modification_time, $filename) =
374 /[\d\/\s
:\
[\
]]+ [>c\
.][fd
]\S
{9} \d
+ (\d
+) ([\d\
/:-]+) (.*)$/;
376 $old_finished{$filename . "\n"} =
377 "### " . $modification_time . " " .
381 safe_write
$finished_name, sort_hash
%old_finished;
382 unlink $rsync_log_name unless $debug;
385 # Delete from %old_finished what has to be re-rsynced.
386 foreach (@rsync_ls) {
387 my ($ls_size, $ls_modification_date,
388 $ls_modification_time, $ls_filename) =
389 /[drwx-]+\s+(\d+) ([\d\/]+) ([\d
:]+) (.*)/;
391 exists $old_finished{$ls_filename . "\n"}) {
392 my ($finished_modification_date, $finished_size) =
393 $old_finished{$ls_filename . "\n"} =~
395 if ( ($finished_size eq $ls_size)
397 ($finished_modification_date)
399 ($ls_modification_date,
400 $ls_modification_time)) )
402 $finished{$ls_filename . "\n"} =
403 $old_finished{$ls_filename . "\n"};
407 safe_write
$finished_name, %finished;
408 if (rsync_someplace
$_, @destination_roots) {
409 $progress_ratios{$_} = '0'; # Clean staleness for UI
415 } keys %source_roots;
418 # Provide some reassuring user information:
419 $destinations_monitor_thread = async
{
420 $SIG{TERM
} = sub { threads
->exit() };
423 my $destination_root = $_;
424 my $destination_usage = 0;
426 my $source_root = $_;
427 my $complete_destination = $destination_root . '/'
428 . $path_under_mount_point . '/'
429 . $source_dirs_in_destination{$source_root};
430 my @dir = qx(ls
-A
$complete_destination/ 2> /dev
/null
);
431 $destination_usage = 1 if scalar @dir; # 0 = no new data
432 } keys %source_roots;
433 $destination_usages{$destination_root} = $destination_usage;
434 my @destination_usage_ratio =
435 grep s/\S+\s+\S+\s+\S+\s+\S+\s+(\d*)%\s+\S+/$1/, qx(df
-P
$_);
436 chomp @destination_usage_ratio;
437 ($destination_usage_ratios{$_}) = @destination_usage_ratio;
438 } @destination_roots;
443 unless ($debug == 1) {
445 $display_thread = async
{
447 endwin
(); # Leave a usable terminal.
451 my $redraw_window_count = 0;
456 my $window_left = newwin
(LINES
() -8, 29, 0, 0);
457 my $window_right = newwin
(LINES
() -8, 50, 0, 29);
458 my $window_center = newwin
(5, 79, LINES
() -8, 0);
459 my $window_bottom = newwin
(3, 79, LINES
() -3, 0);
460 $window_bottom->keypad(1);
461 $window_bottom->nodelay(1);
463 init_pair
1, COLOR_MAGENTA
, COLOR_BLACK
;
464 init_pair
2, COLOR_RED
, COLOR_BLACK
;
465 init_pair
3, COLOR_CYAN
, COLOR_BLACK
;
466 init_pair
4, COLOR_YELLOW
, COLOR_BLACK
;
467 my $MAGENTA = COLOR_PAIR
(1);
468 my $RED = COLOR_PAIR
(2);
469 my $CYAN = COLOR_PAIR
(3);
470 my $YELLOW = COLOR_PAIR
(4);
473 $window_left->attron($CYAN);
474 $window_left->box(0, 0);
475 $window_left->addstr(0, 6, "Data Destinations");
476 $window_left->attroff($CYAN);
477 my $destinations_format = "%-18s%-6s%-3s";
478 $window_left->attron(A_BOLD
);
479 $window_left->addstr(1, 1, sprintf($destinations_format,
480 "Removable", "Fresh", "Usg"));
481 $window_left->addstr(2, 1, sprintf($destinations_format,
482 "Disk", "Data?", "%"));
483 $window_left->attroff(A_BOLD
);
484 my $destination_usage;
487 if ($destination_usages{$_}) {
488 $window_left->attron($RED);
489 $destination_usage = "yes";
491 $window_left->attron($CYAN);
492 $destination_usage = "no";
495 addstr
($line_number, 1,
496 sprintf($destinations_format,
498 substr($destination_usage, -6, 6),
499 substr($destination_usage_ratios{$_}
500 ?
$destination_usage_ratios{$_}
504 $window_left->attroff($RED);
505 $window_left->attroff($CYAN);
506 } sort @destination_roots;
508 $window_right->attron($MAGENTA);
509 $window_right->box(0,0);
510 $window_right->addstr(0, 19, "Data Sources");
511 $window_right->attroff($MAGENTA);
512 my $sources_format = "%-15s%-11s%-9s%-13s";
513 $window_right->attron(A_BOLD
);
515 addstr
(1, 1, sprintf ($sources_format,
516 "Data", "", "Files", " Writing"));
518 addstr
(2, 1, sprintf ($sources_format,
519 "Source", "Speed", "To Copy", " To"));
520 $window_right->attroff(A_BOLD
);
522 $window_right->attron($MAGENTA);
525 my $current_destination = '?';
526 my $progress_ratio = $progress_ratios{$source};
527 if (length $progress_ratio > 9) {
528 $progress_ratio = eval ("100*" . $progress_ratio) . "%";
530 if (exists $destination_source_is_writing_to{$source}) {
531 $current_destination =
532 $destination_source_is_writing_to{$source};
534 if ($reachable{$source}) {
536 addstr
($line_number, 1,
537 sprintf($sources_format,
538 substr($source_roots{$source}, 0, 14),
539 substr($speeds{$source}, 0, 11),
540 substr($progress_ratio,
542 substr($current_destination, -13, 13)));
546 addstr
($line_number, 1,
547 sprintf($sources_format, "", "", "", ""));
548 } sort (keys %source_roots);
549 $window_right->attroff($MAGENTA);
553 $window_center->addstr($line_number, 2, $_);
556 $window_center->addstr(4, 78 - length $version, "$version");
557 $window_center->move(0, 0);
559 $window_bottom->box(0,0);
560 $window_bottom->attron(A_BOLD
);
561 $window_bottom->addstr(1, 3, "[F3]: Turn off computer.");
562 $window_bottom->addstr(1, 53, "[F6]: Restart computer.");
563 $window_bottom->attroff(A_BOLD
);
565 $window_left->noutrefresh();
566 $window_right->noutrefresh();
567 $window_bottom->noutrefresh();
568 $window_center->noutrefresh(); # Last window gets the cursor.
569 act_on_keypress
($window_bottom->getch());
571 if (++ $redraw_window_count > 5) {
572 $redraw_window_count = 0;
583 # Tidy up. (Except we don't reach this.)
585 $being_deleted_thread{$_}->join if $being_deleted_thread{$_};
586 } @destination_roots;
589 $rsync_worker_thread{$_}->join if $rsync_worker_thread{$_};
590 } keys %source_roots;
592 $destinations_monitor_thread->join if $destinations_monitor_thread;
594 $display_thread->join if $display_thread;