Skip to content

Commit

Permalink
Allow specifying a maximum length in mythlink.pl.
Browse files Browse the repository at this point in the history
This provides a way for users who use inappropriate data to construct a
link name (such as a description) to specify a maximum length of the
link name to work around limitations imposed by the file system.  Note
that the names will be truncated and a count variable added to them to
keep within the --maxlength specified.  The names aren't necessarily
useful or descriptive, but the feature was requested and was easy enough
to add--and may actually convince those users to choose more approrpiate
data for use in creating file names.
  • Loading branch information
sphery committed Feb 7, 2012
1 parent 0504ee8 commit a5325e3
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions mythtv/contrib/user_jobs/mythlink.pl
Expand Up @@ -22,7 +22,7 @@
use MythTV; use MythTV;


# Some variables we'll use here # Some variables we'll use here
our ($dest, $format, $usage, $underscores, $live, $rename); our ($dest, $format, $usage, $underscores, $live, $rename, $maxlength);
our ($chanid, $starttime, $filename); our ($chanid, $starttime, $filename);
our ($dformat, $dseparator, $dreplacement, $separator, $replacement); our ($dformat, $dseparator, $dreplacement, $separator, $replacement);
our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose); our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose);
Expand All @@ -39,6 +39,7 @@
$format = $dformat; $format = $dformat;
$separator = $dseparator; $separator = $dseparator;
$replacement = $dreplacement; $replacement = $dreplacement;
$maxlength = -1;


# Load the cli options # Load the cli options
GetOptions('link|destination|path:s' => \$dest, GetOptions('link|destination|path:s' => \$dest,
Expand All @@ -50,6 +51,7 @@
'separator=s' => \$separator, 'separator=s' => \$separator,
'replacement=s' => \$replacement, 'replacement=s' => \$replacement,
'rename' => \$rename, 'rename' => \$rename,
'maxlength=i' => \$maxlength,
'usage|help' => \$usage, 'usage|help' => \$usage,
'underscores' => \$underscores, 'underscores' => \$underscores,
'verbose' => \$verbose 'verbose' => \$verbose
Expand Down Expand Up @@ -229,6 +231,19 @@
represents recordings using human-readable file names or use mythlink.pl to represents recordings using human-readable file names or use mythlink.pl to
create links with human-readable names to the recording files. create links with human-readable names to the recording files.
--maxlength length
Ensure the link name is length or fewer characters. If the link name is
longer than length, truncate the name. Zero or any negative value for
length disables length checking.
Note that this option does not take into account the path length, so on a
file system used by applications with small path limitations (such as
Windows Explorer and Windows Command Prompt), you should specify a length
that takes into account characters used by the path to the dest directory.
default: Unlimited
--verbose --verbose
Print debug info. Print debug info.
Expand All @@ -250,6 +265,12 @@
die "The arguments --chanid and --starttime must be used together.\n"; die "The arguments --chanid and --starttime must be used together.\n";
} }


# Ensure --maxlength specifies a reasonable value (though filenames may
# still be useless at such short lengths)
if ($maxlength > 0 and $maxlength < 19) {
die "The --maxlength must be 20 or higher.\n";
}

# Check the separator and replacement characters for illegal characters # Check the separator and replacement characters for illegal characters
if ($separator =~ /(?:[\/\\:*?<>|"])/) { if ($separator =~ /(?:[\/\\:*?<>|"])/) {
die "The separator cannot contain any of the following characters: /\\:*?<>|\"\n"; die "The separator cannot contain any of the following characters: /\\:*?<>|\"\n";
Expand Down Expand Up @@ -356,21 +377,29 @@
$safe_file = "'$safe_file'"; $safe_file = "'$safe_file'";
# Figure out the suffix # Figure out the suffix
my ($suffix) = ($show->{'basename'} =~ /(\.\w+)$/); my ($suffix) = ($show->{'basename'} =~ /(\.\w+)$/);
# Check the link name's length
$name = cut_down_name($name, $suffix);
# Link destination # Link destination
# Check for duplicates # Check for duplicates
if (-e "$dest/$name$suffix") { if (($name) and -e "$dest/$name$suffix") {
if ((!defined($filename) and !defined($chanid)) or if ((!defined($filename) and !defined($chanid)) or
(! -l "$dest/$name$suffix")) { (! -l "$dest/$name$suffix")) {
$count = 2; $count = 2;
while (-e "$dest/$name.$count$suffix") { $name = cut_down_name($name, ".$count$suffix");
while (($name) and -e "$dest/$name.$count$suffix") {
$count++; $count++;
$name = cut_down_name($name, ".$count$suffix");
} }
$name .= ".$count"; $name .= ".$count" if (($name));
} else { } else {
unlink "$dest/$name$suffix" or die "Couldn't remove ". unlink "$dest/$name$suffix" or die "Couldn't remove ".
"old symlink $dest/$name$suffix: $!\n"; "old symlink $dest/$name$suffix: $!\n";
} }
} }
if (!($name)) {
vprint("Unable to represent recording; maxlength too small.");
next;
}
$name .= $suffix; $name .= $suffix;
# Create the link # Create the link
my $directory = dirname("$dest/$name"); my $directory = dirname("$dest/$name");
Expand All @@ -383,6 +412,22 @@
vprint("$dest/$name"); vprint("$dest/$name");
} }


# Check the length of the link name
sub cut_down_name {
my $name = shift;
my $suffix = shift;
if ($maxlength > 0) {
my $charsavailable = $maxlength - length($suffix);
if ($charsavailable > 0) {
$name = substr($name, 0, $charsavailable);
}
else {
$name = '';
}
}
return $name;
}

# Print the message, but only if verbosity is enabled # Print the message, but only if verbosity is enabled
sub vprint { sub vprint {
return unless (defined($verbose)); return unless (defined($verbose));
Expand Down

0 comments on commit a5325e3

Please sign in to comment.