2016-06-14 10:47:07 +00:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class FileImporter
|
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
|
|
|
|
2016-07-27 15:56:25 +00:00
|
|
|
MAX_RETRIES = 8
|
|
|
|
|
2016-06-14 10:47:07 +00:00
|
|
|
def self.import(*args)
|
|
|
|
new(*args).import
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(archive_file:, shared:)
|
|
|
|
@archive_file = archive_file
|
|
|
|
@shared = shared
|
|
|
|
end
|
|
|
|
|
|
|
|
def import
|
2016-09-29 15:17:22 +00:00
|
|
|
mkdir_p(@shared.export_path)
|
2016-07-27 15:56:25 +00:00
|
|
|
|
|
|
|
wait_for_archived_file do
|
|
|
|
decompress_archive
|
|
|
|
end
|
2016-06-14 10:47:07 +00:00
|
|
|
rescue => e
|
|
|
|
@shared.error(e)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-07-27 15:56:25 +00:00
|
|
|
# Exponentially sleep until I/O finishes copying the file
|
|
|
|
def wait_for_archived_file
|
|
|
|
MAX_RETRIES.times do |retry_number|
|
2016-08-01 09:07:06 +00:00
|
|
|
break if File.exist?(@archive_file)
|
2016-07-27 15:56:25 +00:00
|
|
|
|
2016-08-01 09:07:06 +00:00
|
|
|
sleep(2**retry_number)
|
2016-07-27 15:56:25 +00:00
|
|
|
end
|
2016-08-01 09:07:06 +00:00
|
|
|
|
|
|
|
yield
|
2016-07-27 15:56:25 +00:00
|
|
|
end
|
|
|
|
|
2016-06-14 10:47:07 +00:00
|
|
|
def decompress_archive
|
2016-06-24 08:50:23 +00:00
|
|
|
result = untar_zxf(archive: @archive_file, dir: @shared.export_path)
|
|
|
|
|
|
|
|
raise Projects::ImportService::Error.new("Unable to decompress #{@archive_file} into #{@shared.export_path}") unless result
|
|
|
|
|
2016-10-27 15:10:19 +00:00
|
|
|
remove_symlinks!
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_symlinks!
|
2017-08-08 21:18:02 +00:00
|
|
|
extracted_files.each do |path|
|
2016-10-27 15:10:19 +00:00
|
|
|
FileUtils.rm(path) if File.lstat(path).symlink?
|
|
|
|
end
|
|
|
|
|
2016-06-24 08:50:23 +00:00
|
|
|
true
|
2016-06-14 10:47:07 +00:00
|
|
|
end
|
2017-08-08 21:18:02 +00:00
|
|
|
|
|
|
|
def extracted_files
|
|
|
|
Dir.glob("#{@shared.export_path}/**/*", File::FNM_DOTMATCH).reject { |f| f =~ /.*\/\.{1,2}$/ }
|
|
|
|
end
|
2016-06-14 10:47:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|