Ticket #6726: mythrename.pl.diff
File mythrename.pl.diff, 6.7 KB (added by , 15 years ago) |
---|
-
mythrename.pl
old new 22 22 use MythTV; 23 23 24 24 # Some variables we'll use here 25 our ($dest, $ format, $usage, $underscores, $live);25 our ($dest, $dest_hard, $format, $usage, $underscores, $live); 26 26 our ($dformat, $dseparator, $dreplacement, $separator, $replacement); 27 27 our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose); 28 28 our ($hostname, $dbh, $sh, $q, $count, $base_dir); … … 41 41 42 42 # Load the cli options 43 43 GetOptions('link|dest|destination|path:s' => \$dest, 44 'hardlink:s' => \$dest_hard, 44 45 'format=s' => \$format, 45 46 'live' => \$live, 46 47 'separator=s' => \$separator, … … 69 70 WARNING: ALL symlinks within the destination directory and its 70 71 subdirectories (recursive) will be removed when using the --link option. 71 72 73 --hardlink [destination directory] 74 75 Exactly the same as --link, only hardlinks are created, not symlinks. 76 77 WARNING: It's allowed to use both --link and --hardlink in the same 78 command, but strange things may happen if you attempt to use the same 79 destination for both! 80 72 81 --live 73 82 74 83 Include live tv recordings, affects both linking and renaming. … … 263 272 finddepth sub { rmdir $_; }, $dest; 264 273 } 265 274 275 # Hardlink destination 276 if (defined($dest_hard)) { 277 # Double-check the destination 278 $dest_hard ||= "$base_dir/show_names"; 279 # Alert the user 280 vprint("Hardlink destination directory: $dest_hard"); 281 # Create nonexistent paths 282 unless (-e $dest_hard) { 283 mkpath($dest_hard, 0, 0755) or die "Failed to create $dest_hard: $!\n"; 284 } 285 # Bad path 286 die "$dest_hard is not a directory.\n" unless (-d $dest_hard); 287 # Delete any old hardlinks (symlinks too!) 288 find sub { if (-l $_) { 289 unlink $_ or die "Couldn't remove old symlink $_: $!\n"; 290 } 291 elsif (! -d _) { 292 # hardlinks have a link count > 1 293 my $nlink = (stat _)[3]; 294 if ($nlink > 1) { 295 unlink $_ or die "Couldn't remove old hardlink $_: $!\n"; 296 } 297 } 298 }, $dest_hard; 299 # Delete empty directories (should this be an option?) 300 # Let this fail silently for non-empty directories 301 finddepth sub { rmdir $_; }, $dest_hard; 302 } 303 266 304 # Only if we're renaming files 267 unless ($dest ) {305 unless ($dest or $dest_hard) { 268 306 $q = 'UPDATE recorded SET basename=? WHERE chanid=? AND starttime=FROM_UNIXTIME(?)'; 269 307 $sh = $dbh->prepare($q); 270 308 } 271 309 272 # Create symlinks for the files on this machine310 # Create symlinks/hardlinks for the files on this machine 273 311 my %rows = $Myth->backend_rows('QUERY_RECORDINGS Delete'); 274 312 foreach my $row (@{$rows{'rows'}}) { 275 313 my $show = new MythTV::Recording(@$row); … … 277 315 next unless (defined($live) || $show->{'recgroup'} ne 'LiveTV'); 278 316 # File doesn't exist locally 279 317 next unless (-e $show->{'local_path'}); 280 # Format the name281 my $name = $show->format_name($format,$separator,$replacement,$dest,$underscores);282 318 # Get a shell-safe version of the filename (yes, I know it's not needed in this case, but I'm anal about such things) 283 319 my $safe_file = $show->{'local_path'}; 284 320 $safe_file =~ s/'/'\\''/sg; … … 286 322 # Figure out the suffix 287 323 my ($suffix) = ($show->{'basename'} =~ /(\.\w+)$/); 288 324 # Link destination 289 if ($dest) { 290 # Check for duplicates 291 if (-e "$dest/$name$suffix") { 292 $count = 2; 293 while (-e "$dest/$name.$count$suffix") { 294 $count++; 325 if ($dest or $dest_hard) { 326 if ($dest) { 327 # Format the name 328 my $name = $show->format_name($format,$separator,$replacement,$dest,$underscores); 329 # Check for duplicates 330 if (-e "$dest/$name$suffix") { 331 $count = 2; 332 while (-e "$dest/$name.$count$suffix") { 333 $count++; 334 } 335 $name .= ".$count"; 336 } 337 $name .= $suffix; 338 # Create the link 339 my $directory = dirname("$dest/$name"); 340 unless (-e $directory) { 341 mkpath($directory, 0, 0755) 342 or die "Failed to create $directory: $!\n"; 295 343 } 296 $name .= ".$count"; 344 symlink $show->{'local_path'}, "$dest/$name" 345 or die "Can't create symlink $dest/$name: $!\n"; 346 vprint("$dest/$name"); 297 347 } 298 $name .= $suffix; 299 # Create the link 300 my $directory = dirname("$dest/$name"); 301 unless (-e $directory) { 302 mkpath($directory, 0, 0755) 303 or die "Failed to create $directory: $!\n"; 348 if ($dest_hard) { 349 # Format the name 350 my $name = $show->format_name($format,$separator,$replacement,$dest_hard,$underscores); 351 # Check for duplicates 352 if (-e "$dest_hard/$name$suffix") { 353 $count = 2; 354 while (-e "$dest_hard/$name.$count$suffix") { 355 $count++; 356 } 357 $name .= ".$count"; 358 } 359 $name .= $suffix; 360 # Create the link 361 my $directory = dirname("$dest_hard/$name"); 362 unless (-e $directory) { 363 mkpath($directory, 0, 0755) 364 or die "Failed to create $directory: $!\n"; 365 } 366 link $show->{'local_path'}, "$dest_hard/$name" 367 or die "Can't create symlink $dest_hard/$name: $!\n"; 368 vprint("$dest_hard/$name"); 304 369 } 305 symlink $show->{'local_path'}, "$dest/$name" 306 or die "Can't create symlink $dest/$name: $!\n"; 307 vprint("$dest/$name"); 308 } 370 } 309 371 # Rename the file, but only if it's a real file 310 372 elsif (-f $show->{'local_path'}) { 373 my $name = $show->format_name($format,$separator,$replacement,undef,$underscores); 311 374 if ($show->{'basename'} ne $name.$suffix) { 312 375 # Check for duplicates 313 376 $video_dir = $sgroup->FindRecordingDir($show->{'basename'});