From e20ab483df30563c8aa7f54942021a46e2dfe3cc Mon Sep 17 00:00:00 2001 From: shinokaro Date: Sat, 1 Jun 2024 08:00:39 +0900 Subject: [PATCH] RefinePathname#subpath? implementation for strict path comparison Updated to use Pathname#relative_path_from for strict path comparison. This allows comparison of relative paths as well. Additionally, it returns false when comparing an absolute path with a relative path. --- bin/ocran | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bin/ocran b/bin/ocran index 64dff15..5a7d23c 100644 --- a/bin/ocran +++ b/bin/ocran @@ -43,13 +43,14 @@ module Ocran alias == eql? alias === eql? - # Determines if 'src' is contained in 'tgt' (i.e. it is a subpath of - # 'tgt'). Both must be absolute paths and not contain '..' + # Checks if the current path is a sub path of the specified base_directory. + # Both paths must be either absolute paths or relative paths; otherwise, this + # method returns false. def subpath?(base_directory) - base_directory = Pathname.new(base_directory) unless base_directory.is_a?(Pathname) - src_normalized = to_posix - tgt_normalized = base_directory.to_posix - src_normalized =~ /^#{Regexp.escape tgt_normalized}#{Pathname::SEPARATOR_PAT}/i + s = relative_path_from(base_directory).each_filename.first + s != '.' && s != ".." + rescue ArgumentError + false end # Appends the given suffix to the filename, preserving the file extension.