Welcome to mirror list, hosted at ThFree Co, Russian Federation.

extract.rb « safe_zip « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3403e3e429e22245d8e4153333f48eea41e7c18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

module SafeZip
  # SafeZip::Extract provides a safe interface
  # to extract specific directories or files within a `zip` archive.
  #
  # @example Extract directories to destination
  #   SafeZip::Extract.new(archive_file).extract(directories: ['app/', 'test/'], to: destination_path)
  # @example Extract files to destination
  #   SafeZip::Extract.new(archive_file).extract(files: ['index.html', 'app/index.js'], to: destination_path)
  class Extract
    Error = Class.new(StandardError)
    PermissionDeniedError = Class.new(Error)
    SymlinkSourceDoesNotExistError = Class.new(Error)
    UnsupportedEntryError = Class.new(Error)
    EntrySizeError = Class.new(Error)
    AlreadyExistsError = Class.new(Error)
    NoMatchingError = Class.new(Error)
    ExtractError = Class.new(Error)

    attr_reader :archive_path

    def initialize(archive_file)
      @archive_path = archive_file
    end

    # extract given files or directories from the archive into the destination path
    #
    # @param [Hash] opts the options for extraction.
    # @option opts [Array<String] :files list of files to be extracted
    # @option opts [Array<String] :directories list of directories to be extracted
    # @option opts [String] :to destination path
    #
    # @raise [PermissionDeniedError]
    # @raise [SymlinkSourceDoesNotExistError]
    # @raise [UnsupportedEntryError]
    # @raise [EntrySizeError]
    # @raise [AlreadyExistsError]
    # @raise [NoMatchingError]
    # @raise [ExtractError]
    def extract(opts = {})
      params = SafeZip::ExtractParams.new(**opts)

      extract_with_ruby_zip(params)
    end

    private

    def extract_with_ruby_zip(params)
      ::Zip::File.open(archive_path) do |zip_archive| # rubocop:disable Performance/Rubyzip
        # Extract all files in the following order:
        # 1. Directories first,
        # 2. Files next,
        # 3. Symlinks last (or anything else)
        extracted = extract_all_entries(zip_archive, params,
          zip_archive.lazy.select(&:directory?))

        extracted += extract_all_entries(zip_archive, params,
          zip_archive.lazy.select(&:file?))

        extracted += extract_all_entries(zip_archive, params,
          zip_archive.lazy.reject(&:directory?).reject(&:file?))

        raise NoMatchingError, 'No entries extracted' unless extracted > 0
      end
    end

    def extract_all_entries(zip_archive, params, entries)
      entries.count do |zip_entry|
        SafeZip::Entry.new(zip_archive, zip_entry, params)
          .extract
      end
    end
  end
end