2018-12-23 12:14:25 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
module CommandLineUtil
|
2018-12-23 12:14:25 +05:30
|
|
|
UNTAR_MASK = 'u+rwX,go+rX,go-w'
|
2018-12-21 20:21:59 +05:30
|
|
|
DEFAULT_DIR_MODE = 0700
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
def tar_czf(archive:, dir:)
|
|
|
|
tar_with_options(archive: archive, dir: dir, options: 'czf')
|
|
|
|
end
|
|
|
|
|
|
|
|
def untar_zxf(archive:, dir:)
|
|
|
|
untar_with_options(archive: archive, dir: dir, options: 'zxf')
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def mkdir_p(path)
|
2018-12-21 20:21:59 +05:30
|
|
|
FileUtils.mkdir_p(path, mode: DEFAULT_DIR_MODE)
|
|
|
|
FileUtils.chmod(DEFAULT_DIR_MODE, path)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
private
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def download_or_copy_upload(uploader, upload_path)
|
|
|
|
if uploader.upload.local?
|
|
|
|
copy_files(uploader.path, upload_path)
|
|
|
|
else
|
|
|
|
download(uploader.url, upload_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def download(url, upload_path)
|
|
|
|
File.open(upload_path, 'w') do |file|
|
|
|
|
# Download (stream) file from the uploader's location
|
|
|
|
IO.copy_stream(URI.parse(url).open, file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
def tar_with_options(archive:, dir:, options:)
|
|
|
|
execute(%W(tar -#{options} #{archive} -C #{dir} .))
|
|
|
|
end
|
|
|
|
|
|
|
|
def untar_with_options(archive:, dir:, options:)
|
|
|
|
execute(%W(tar -#{options} #{archive} -C #{dir}))
|
2018-12-21 20:21:59 +05:30
|
|
|
execute(%W(chmod -R #{UNTAR_MASK} #{dir}))
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute(cmd)
|
2016-08-24 12:49:21 +05:30
|
|
|
output, status = Gitlab::Popen.popen(cmd)
|
2018-03-17 18:26:18 +05:30
|
|
|
@shared.error(Gitlab::ImportExport::Error.new(output.to_s)) unless status.zero? # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-06-22 15:30:34 +05:30
|
|
|
status.zero?
|
|
|
|
end
|
|
|
|
|
|
|
|
def git_bin_path
|
|
|
|
Gitlab.config.git.bin_path
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
def copy_files(source, destination)
|
|
|
|
# if we are copying files, create the destination folder
|
|
|
|
destination_folder = File.file?(source) ? File.dirname(destination) : destination
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
mkdir_p(destination_folder)
|
2016-08-24 12:49:21 +05:30
|
|
|
FileUtils.copy_entry(source, destination)
|
|
|
|
true
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|