Ticket #6726: mythrename.pl.diff

File mythrename.pl.diff, 6.7 KB (added by scott@…, 4 years ago)

Patch to add '--hardlink' option

  • mythrename.pl

    old new  
    2222    use MythTV; 
    2323 
    2424# Some variables we'll use here 
    25     our ($dest, $format, $usage, $underscores, $live); 
     25    our ($dest, $dest_hard, $format, $usage, $underscores, $live); 
    2626    our ($dformat, $dseparator, $dreplacement, $separator, $replacement); 
    2727    our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose); 
    2828    our ($hostname, $dbh, $sh, $q, $count, $base_dir); 
     
    4141 
    4242# Load the cli options 
    4343    GetOptions('link|dest|destination|path:s' => \$dest, 
     44               'hardlink:s'                   => \$dest_hard, 
    4445               'format=s'                     => \$format, 
    4546               'live'                         => \$live, 
    4647               'separator=s'                  => \$separator, 
     
    6970    WARNING: ALL symlinks within the destination directory and its 
    7071    subdirectories (recursive) will be removed when using the --link option. 
    7172 
     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 
    7281--live 
    7382 
    7483    Include live tv recordings, affects both linking and renaming. 
     
    263272        finddepth sub { rmdir $_; }, $dest; 
    264273    } 
    265274 
     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 
    266304# Only if we're renaming files 
    267     unless ($dest) { 
     305    unless ($dest or $dest_hard) { 
    268306        $q  = 'UPDATE recorded SET basename=? WHERE chanid=? AND starttime=FROM_UNIXTIME(?)'; 
    269307        $sh = $dbh->prepare($q); 
    270308    } 
    271309 
    272 # Create symlinks for the files on this machine 
     310# Create symlinks/hardlinks for the files on this machine 
    273311    my %rows = $Myth->backend_rows('QUERY_RECORDINGS Delete'); 
    274312    foreach my $row (@{$rows{'rows'}}) { 
    275313        my $show = new MythTV::Recording(@$row); 
     
    277315        next unless (defined($live) || $show->{'recgroup'} ne 'LiveTV'); 
    278316    # File doesn't exist locally 
    279317        next unless (-e $show->{'local_path'}); 
    280     # Format the name 
    281         my $name = $show->format_name($format,$separator,$replacement,$dest,$underscores); 
    282318    # 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) 
    283319        my $safe_file = $show->{'local_path'}; 
    284320        $safe_file =~ s/'/'\\''/sg; 
     
    286322    # Figure out the suffix 
    287323        my ($suffix) = ($show->{'basename'} =~ /(\.\w+)$/); 
    288324    # 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"; 
    295343                } 
    296                 $name .= ".$count"; 
     344                symlink $show->{'local_path'}, "$dest/$name" 
     345                    or die "Can't create symlink $dest/$name:  $!\n"; 
     346                vprint("$dest/$name"); 
    297347            } 
    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"); 
    304369            } 
    305             symlink $show->{'local_path'}, "$dest/$name" 
    306                 or die "Can't create symlink $dest/$name:  $!\n"; 
    307             vprint("$dest/$name"); 
    308         } 
     370       } 
    309371    # Rename the file, but only if it's a real file 
    310372        elsif (-f $show->{'local_path'}) { 
     373            my $name = $show->format_name($format,$separator,$replacement,undef,$underscores); 
    311374            if ($show->{'basename'} ne $name.$suffix) { 
    312375            # Check for duplicates 
    313376                $video_dir = $sgroup->FindRecordingDir($show->{'basename'});