Class: Cabriolet::CAB::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/cab/extractor.rb

Overview

Extractor handles the extraction of files from cabinets

Defined Under Namespace

Classes: BlockReader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_system, decompressor) ⇒ Extractor

Initialize a new extractor



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cabriolet/cab/extractor.rb', line 16

def initialize(io_system, decompressor)
  @io_system = io_system
  @decompressor = decompressor

  # State reuse for multi-file extraction (like libmspack self->d)
  @current_folder = nil
  @current_decomp = nil
  @current_input = nil
  @current_offset = 0

  # Cache ENV lookups once at initialization
  @debug_block = ENV.fetch("DEBUG_BLOCK", nil)
end

Instance Attribute Details

#decompressorObject (readonly)

Returns the value of attribute decompressor.



10
11
12
# File 'lib/cabriolet/cab/extractor.rb', line 10

def decompressor
  @decompressor
end

#io_systemObject (readonly)

Returns the value of attribute io_system.



10
11
12
# File 'lib/cabriolet/cab/extractor.rb', line 10

def io_system
  @io_system
end

Instance Method Details

#extract_all(cabinet, output_dir, **options) ⇒ Integer

Extract all files from a cabinet

Options Hash (**options):

  • :preserve_paths (Boolean)

    Preserve directory structure (default: true)

  • :set_timestamps (Boolean)

    Set file modification times (default: true)

  • :progress (Proc)

    Progress callback

  • :salvage (Boolean)

    Skip files that fail to extract (default: false)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cabriolet/cab/extractor.rb', line 90

def extract_all(cabinet, output_dir, **options)
  preserve_paths = options.fetch(:preserve_paths, true)
  set_timestamps = options.fetch(:set_timestamps, true)
  progress = options[:progress]
  salvage = options[:salvage] || false

  # Create output directory
  FileUtils.mkdir_p(output_dir) unless ::File.directory?(output_dir)

  count = 0
  failed_count = 0
  cabinet.files.each do |file|
    # Determine output path
    output_path = if preserve_paths
                    ::File.join(output_dir, file.filename)
                  else
                    ::File.join(output_dir,
                                ::File.basename(file.filename))
                  end

    # Extract file (skip if salvage mode and extraction fails)
    begin
      extract_file(file, output_path, **options)
    rescue DecompressionError => e
      if salvage
        warn "Salvage: skipping #{file.filename}: #{e.message}"
        failed_count += 1
        next
      else
        raise
      end
    end

    # Set timestamp if requested
    if set_timestamps && file.modification_time
      ::File.utime(file.modification_time, file.modification_time,
                   output_path)
    end

    # Set file permissions based on attributes
    set_file_attributes(output_path, file)

    count += 1
    progress&.call(file, count, cabinet.files.size)
  end

  warn "Salvage: #{failed_count} file(s) skipped due to extraction errors" if failed_count.positive?

  count
end

#extract_file(file, output_path, **options) ⇒ Integer

Extract a single file from the cabinet

Options Hash (**options):

  • :salvage (Boolean)

    Enable salvage mode



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
# File 'lib/cabriolet/cab/extractor.rb', line 37

def extract_file(file, output_path, **options)
  salvage = options[:salvage] || @decompressor.salvage
  folder = file.folder
  filelen = validate_file_for_extraction(file, folder, salvage)

  # Check for merge requirements
  if folder.needs_prev_merge?
    raise DecompressionError,
          "File requires previous cabinet, cabinet set is incomplete"
  end

  validate_file_in_folder(folder, file.offset, filelen, salvage)

  # Create output directory if needed
  output_dir = ::File.dirname(output_path)
  FileUtils.mkdir_p(output_dir) unless ::File.directory?(output_dir)

  setup_decompressor_for_folder(folder, salvage, file.offset)
  skip_to_file_offset(file.offset, salvage, file.filename)

  # Extract actual file (libmspack lines 1137-1141)
  output_fh = @io_system.open(output_path, Constants::MODE_WRITE)

  begin
    write_file_data(output_fh, filelen)
  rescue DecompressionError
    handle_extraction_error(output_fh, output_path, file.filename, salvage, filelen)
  ensure
    output_fh.close
  end

  filelen
end

#reset_stateObject

Reset extraction state (used in salvage mode to recover from errors)



72
73
74
75
76
77
78
# File 'lib/cabriolet/cab/extractor.rb', line 72

def reset_state
  @current_input&.close
  @current_input = nil
  @current_decomp = nil
  @current_folder = nil
  @current_offset = 0
end