BigW Consortium Gitlab

file_importer.rb 1.23 KB
Newer Older
1 2 3 4 5
module Gitlab
  module ImportExport
    class FileImporter
      include Gitlab::ImportExport::CommandLineUtil

6 7
      MAX_RETRIES = 8

8 9 10 11 12 13 14 15 16 17
      def self.import(*args)
        new(*args).import
      end

      def initialize(archive_file:, shared:)
        @archive_file = archive_file
        @shared = shared
      end

      def import
18
        mkdir_p(@shared.export_path)
19 20 21 22

        wait_for_archived_file do
          decompress_archive
        end
23 24 25 26 27 28 29
      rescue => e
        @shared.error(e)
        false
      end

      private

30 31 32
      # Exponentially sleep until I/O finishes copying the file
      def wait_for_archived_file
        MAX_RETRIES.times do |retry_number|
33
          break if File.exist?(@archive_file)
34

35
          sleep(2**retry_number)
36
        end
37 38

        yield
39 40
      end

41
      def decompress_archive
42 43 44 45
        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

46 47 48 49 50 51 52 53
        remove_symlinks!
      end

      def remove_symlinks!
        Dir["#{@shared.export_path}/**/*"].each do |path|
          FileUtils.rm(path) if File.lstat(path).symlink?
        end

54
        true
55 56 57 58
      end
    end
  end
end