debian-mirror-gitlab/lib/gitlab/import_export/file_importer.rb

84 lines
1.9 KiB
Ruby
Raw Normal View History

2016-06-22 15:30:34 +05:30
module Gitlab
module ImportExport
class FileImporter
include Gitlab::ImportExport::CommandLineUtil
2016-08-24 12:49:21 +05:30
MAX_RETRIES = 8
2018-08-20 21:37:37 +05:30
IGNORED_FILENAMES = %w(. ..).freeze
2016-08-24 12:49:21 +05:30
2016-06-22 15:30:34 +05:30
def self.import(*args)
new(*args).import
end
2018-11-18 11:00:15 +05:30
def initialize(project:, archive_file:, shared:)
@project = project
2016-06-22 15:30:34 +05:30
@archive_file = archive_file
@shared = shared
end
def import
2016-11-03 12:29:30 +05:30
mkdir_p(@shared.export_path)
2018-11-18 11:00:15 +05:30
mkdir_p(@shared.archive_path)
2016-08-24 12:49:21 +05:30
2018-11-18 11:00:15 +05:30
remove_symlinks
copy_archive
2018-03-17 18:26:18 +05:30
2016-08-24 12:49:21 +05:30
wait_for_archived_file do
decompress_archive
end
2016-06-22 15:30:34 +05:30
rescue => e
@shared.error(e)
false
2018-03-17 18:26:18 +05:30
ensure
2018-11-18 11:00:15 +05:30
remove_import_file
remove_symlinks
2016-06-22 15:30:34 +05:30
end
private
2016-08-24 12:49:21 +05:30
# Exponentially sleep until I/O finishes copying the file
def wait_for_archived_file
MAX_RETRIES.times do |retry_number|
break if File.exist?(@archive_file)
sleep(2**retry_number)
end
yield
end
2016-06-22 15:30:34 +05:30
def decompress_archive
2016-08-24 12:49:21 +05:30
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
2018-03-17 18:26:18 +05:30
result
2016-11-03 12:29:30 +05:30
end
2018-11-18 11:00:15 +05:30
def copy_archive
return if @archive_file
@archive_file = File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(project: @project))
download_or_copy_upload(@project.import_export_upload.import_file, @archive_file)
end
def remove_symlinks
2017-08-17 22:00:37 +05:30
extracted_files.each do |path|
2016-11-03 12:29:30 +05:30
FileUtils.rm(path) if File.lstat(path).symlink?
end
2016-08-24 12:49:21 +05:30
true
2016-06-22 15:30:34 +05:30
end
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
def remove_import_file
FileUtils.rm_rf(@archive_file)
end
2017-08-17 22:00:37 +05:30
def extracted_files
2018-08-20 21:37:37 +05:30
Dir.glob("#{@shared.export_path}/**/*", File::FNM_DOTMATCH).reject { |f| IGNORED_FILENAMES.include?(File.basename(f)) }
2017-08-17 22:00:37 +05:30
end
2016-06-22 15:30:34 +05:30
end
end
end