Skip to content

Commit 5504205

Browse files
Enhance directory structure handling
we failed to tackle Errno::ENOTDIR if a parent component of the path is already a file (e.g. index.php) see #45
1 parent 2911e3a commit 5504205

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

lib/wayback_machine_downloader.rb

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class WaybackMachineDownloader
133133
include SubdomainProcessor
134134
include URLRewrite
135135

136-
VERSION = "2.4.5"
136+
VERSION = "2.4.6"
137137
DEFAULT_TIMEOUT = 30
138138
MAX_RETRIES = 3
139139
RETRY_DELAY = 2
@@ -793,26 +793,39 @@ def process_page_requisites(file_path, parent_remote_info)
793793
end
794794
end
795795

796-
def structure_dir_path dir_path
796+
def structure_dir_path dir_path
797797
begin
798-
FileUtils::mkdir_p dir_path unless File.exist? dir_path
799-
rescue Errno::EEXIST => e
800-
error_to_string = e.to_s
801-
puts "# #{error_to_string}"
802-
if error_to_string.include? "File exists @ dir_s_mkdir - "
803-
file_already_existing = error_to_string.split("File exists @ dir_s_mkdir - ")[-1]
804-
elsif error_to_string.include? "File exists - "
805-
file_already_existing = error_to_string.split("File exists - ")[-1]
798+
# check if it's already a directory; if not, try to create it
799+
FileUtils::mkdir_p dir_path unless File.directory? dir_path
800+
rescue Errno::EEXIST, Errno::ENOTDIR => e
801+
file_already_existing = nil
802+
check_path = dir_path
803+
804+
# walk up the path to find the specific file that is blocking directory creation
805+
while check_path != "." && check_path != "/"
806+
if File.exist?(check_path) && !File.directory?(check_path)
807+
file_already_existing = check_path
808+
break
809+
end
810+
parent = File.dirname(check_path)
811+
break if parent == check_path
812+
check_path = parent
813+
end
814+
815+
if file_already_existing
816+
file_already_existing_temporary = file_already_existing + '.temp'
817+
file_already_existing_permanent = file_already_existing + '/index.html'
818+
819+
FileUtils::mv file_already_existing, file_already_existing_temporary
820+
FileUtils::mkdir_p file_already_existing
821+
FileUtils::mv file_already_existing_temporary, file_already_existing_permanent
822+
823+
puts "#{file_already_existing} -> #{file_already_existing_permanent}"
824+
# retry the directory creation now that the path is clear
825+
structure_dir_path dir_path
806826
else
807-
raise "Unhandled directory restructure error # #{error_to_string}"
827+
raise "Unhandled directory restructure error: #{e.message}"
808828
end
809-
file_already_existing_temporary = file_already_existing + '.temp'
810-
file_already_existing_permanent = file_already_existing + '/index.html'
811-
FileUtils::mv file_already_existing, file_already_existing_temporary
812-
FileUtils::mkdir_p file_already_existing
813-
FileUtils::mv file_already_existing_temporary, file_already_existing_permanent
814-
puts "#{file_already_existing} -> #{file_already_existing_permanent}"
815-
structure_dir_path dir_path
816829
end
817830
end
818831

0 commit comments

Comments
 (0)