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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

153 lines
4.8 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-06-22 15:30:34 +05:30
module Gitlab
module ImportExport
module CommandLineUtil
2019-02-15 15:39:39 +05:30
UNTAR_MASK = 'u+rwX,go+rX,go-w'
2018-12-21 20:21:59 +05:30
DEFAULT_DIR_MODE = 0700
2023-09-09 18:01:29 +05:30
CLEAN_DIR_IGNORE_FILE_NAMES = %w[. ..].freeze
2016-11-03 12:29:30 +05:30
2023-09-09 18:01:29 +05:30
CommandLineUtilError = Class.new(StandardError)
FileOversizedError = Class.new(CommandLineUtilError)
HardLinkError = Class.new(CommandLineUtilError)
2022-05-07 20:08:51 +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
2021-11-18 22:05:49 +05:30
def tar_cf(archive:, dir:)
tar_with_options(archive: archive, dir: dir, options: 'cf')
end
2022-01-26 12:08:38 +05:30
def untar_xf(archive:, dir:)
untar_with_options(archive: archive, dir: dir, options: 'xf')
end
2021-06-08 01:23:25 +05:30
def gzip(dir:, filename:)
2021-09-04 01:27:46 +05:30
gzip_with_options(dir: dir, filename: filename)
end
def gunzip(dir:, filename:)
gzip_with_options(dir: dir, filename: filename, options: 'd')
end
def gzip_with_options(dir:, filename:, options: nil)
2021-06-08 01:23:25 +05:30
filepath = File.join(dir, filename)
cmd = %W(gzip #{filepath})
2021-09-04 01:27:46 +05:30
cmd << "-#{options}" if options
2021-06-08 01:23:25 +05:30
_, status = Gitlab::Popen.popen(cmd)
if status == 0
status
else
raise Gitlab::ImportExport::Error.file_compression_error
end
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
2022-05-07 20:08:51 +05:30
def download_or_copy_upload(uploader, upload_path, size_limit: nil)
2018-11-18 11:00:15 +05:30
if uploader.upload.local?
copy_files(uploader.path, upload_path)
else
2022-05-07 20:08:51 +05:30
download(uploader.url, upload_path, size_limit: size_limit)
2018-11-18 11:00:15 +05:30
end
end
2022-05-07 20:08:51 +05:30
def download(url, upload_path, size_limit: nil)
File.open(upload_path, 'wb') do |file|
current_size = 0
Gitlab::HTTP.get(url, stream_body: true, allow_object_storage: true) do |fragment|
2022-06-21 17:19:12 +05:30
if [301, 302, 303, 307].include?(fragment.code)
2022-05-07 20:08:51 +05:30
Gitlab::Import::Logger.warn(message: "received redirect fragment", fragment_code: fragment.code)
elsif fragment.code == 200
current_size += fragment.bytesize
raise FileOversizedError if size_limit.present? && current_size > size_limit
file.write(fragment)
else
raise Gitlab::ImportExport::Error, "unsupported response downloading fragment #{fragment.code}"
end
end
2018-11-18 11:00:15 +05:30
end
2022-05-07 20:08:51 +05:30
rescue FileOversizedError
nil
2018-11-18 11:00:15 +05:30
end
2016-06-22 15:30:34 +05:30
def tar_with_options(archive:, dir:, options:)
2021-11-18 22:05:49 +05:30
execute_cmd(%W(tar -#{options} #{archive} -C #{dir} .))
2016-06-22 15:30:34 +05:30
end
def untar_with_options(archive:, dir:, options:)
2021-11-18 22:05:49 +05:30
execute_cmd(%W(tar -#{options} #{archive} -C #{dir}))
execute_cmd(%W(chmod -R #{UNTAR_MASK} #{dir}))
2023-09-09 18:01:29 +05:30
clean_extraction_dir!(dir)
2016-06-22 15:30:34 +05:30
end
2021-11-18 22:05:49 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def execute_cmd(cmd)
2016-08-24 12:49:21 +05:30
output, status = Gitlab::Popen.popen(cmd)
2021-11-18 22:05:49 +05:30
return true if status == 0
2022-04-04 11:22:00 +05:30
output = output&.strip
message = "command exited with error code #{status}"
message += ": #{output}" if output.present?
2021-11-18 22:05:49 +05:30
if @shared.respond_to?(:error)
2022-04-04 11:22:00 +05:30
@shared.error(Gitlab::ImportExport::Error.new(message))
2021-11-18 22:05:49 +05:30
false
else
2022-04-04 11:22:00 +05:30
raise Gitlab::ImportExport::Error, message
2021-11-18 22:05:49 +05:30
end
2016-06-22 15:30:34 +05:30
end
2021-11-18 22:05:49 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2016-06-22 15:30:34 +05:30
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
2023-05-27 22:25:52 +05:30
2023-09-09 18:01:29 +05:30
# Scans and cleans the directory tree.
# Symlinks are considered legal but are removed.
# Files sharing hard links are considered illegal and the directory will be removed
# and a `HardLinkError` exception will be raised.
#
# @raise [HardLinkError] if there multiple hard links to the same file detected.
# @return [Boolean] true
def clean_extraction_dir!(dir)
2023-05-27 22:25:52 +05:30
# Using File::FNM_DOTMATCH to also delete symlinks starting with "."
2023-09-09 18:01:29 +05:30
Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).each do |filepath|
next if CLEAN_DIR_IGNORE_FILE_NAMES.include?(File.basename(filepath))
raise HardLinkError, 'File shares hard link' if Gitlab::Utils::FileInfo.shares_hard_link?(filepath)
FileUtils.rm(filepath) if Gitlab::Utils::FileInfo.linked?(filepath)
end
2023-05-27 22:25:52 +05:30
true
2023-09-09 18:01:29 +05:30
rescue HardLinkError
FileUtils.remove_dir(dir)
raise
2023-05-27 22:25:52 +05:30
end
2016-06-22 15:30:34 +05:30
end
end
end