Skip to content
Browse files

Allow specifying a maximum length in mythlink.pl.

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...
1 parent 0504ee8 commit a5325e3c2eaaaaaef4b4a1d9d0ef993665110642 @sphery sphery committed
Showing with 49 additions and 4 deletions.
  1. +49 −4 mythtv/contrib/user_jobs/mythlink.pl
View
53 mythtv/contrib/user_jobs/mythlink.pl
@@ -22,7 +22,7 @@
use MythTV;
# 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 ($dformat, $dseparator, $dreplacement, $separator, $replacement);
our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose);
@@ -39,6 +39,7 @@
$format = $dformat;
$separator = $dseparator;
$replacement = $dreplacement;
+ $maxlength = -1;
# Load the cli options
GetOptions('link|destination|path:s' => \$dest,
@@ -50,6 +51,7 @@
'separator=s' => \$separator,
'replacement=s' => \$replacement,
'rename' => \$rename,
+ 'maxlength=i' => \$maxlength,
'usage|help' => \$usage,
'underscores' => \$underscores,
'verbose' => \$verbose
@@ -229,6 +231,19 @@
represents recordings using human-readable file names or use mythlink.pl to
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
Print debug info.
@@ -250,6 +265,12 @@
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
if ($separator =~ /(?:[\/\\:*?<>|"])/) {
die "The separator cannot contain any of the following characters: /\\:*?<>|\"\n";
@@ -356,21 +377,29 @@
$safe_file = "'$safe_file'";
# Figure out the suffix
my ($suffix) = ($show->{'basename'} =~ /(\.\w+)$/);
+ # Check the link name's length
+ $name = cut_down_name($name, $suffix);
# Link destination
# Check for duplicates
- if (-e "$dest/$name$suffix") {
+ if (($name) and -e "$dest/$name$suffix") {
if ((!defined($filename) and !defined($chanid)) or
(! -l "$dest/$name$suffix")) {
$count = 2;
- while (-e "$dest/$name.$count$suffix") {
+ $name = cut_down_name($name, ".$count$suffix");
+ while (($name) and -e "$dest/$name.$count$suffix") {
$count++;
+ $name = cut_down_name($name, ".$count$suffix");
}
- $name .= ".$count";
+ $name .= ".$count" if (($name));
} else {
unlink "$dest/$name$suffix" or die "Couldn't remove ".
"old symlink $dest/$name$suffix: $!\n";
}
}
+ if (!($name)) {
+ vprint("Unable to represent recording; maxlength too small.");
+ next;
+ }
$name .= $suffix;
# Create the link
my $directory = dirname("$dest/$name");
@@ -383,6 +412,22 @@
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
sub vprint {
return unless (defined($verbose));

0 comments on commit a5325e3

Please sign in to comment.
Something went wrong with that request. Please try again.