Class: RosettAi::Backup::TarXzCompressor

Inherits:
Compressor
  • Object
show all
Defined in:
lib/rosett_ai/backup/compressor.rb

Overview

tar.xz compression using system xz command

Constant Summary

Constants inherited from Compressor

Compressor::ALGORITHMS

Instance Method Summary collapse

Methods inherited from Compressor

for

Instance Method Details

#available?Boolean

Returns true if the +xz+ binary is found on PATH.

Returns:

  • (Boolean)

    true if the +xz+ binary is found on PATH



135
136
137
138
139
# File 'lib/rosett_ai/backup/compressor.rb', line 135

def available?
  ENV.fetch('PATH', '').split(File::PATH_SEPARATOR).any? do |dir|
    File.executable?(File.join(dir, 'xz'))
  end
end

#compress(files, _base_dirs, output_path, level: nil) ⇒ String

Returns the output path.

Parameters:

  • files (Array<Hash>)

    entries with :full_path and :archive_path keys

  • _base_dirs (Array<String>)

    unused

  • output_path (String)

    destination path for the archive

  • level (Integer, nil) (defaults to: nil)

    xz preset level (1-9; nil for xz default)

Returns:

  • (String)

    the output path

Raises:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rosett_ai/backup/compressor.rb', line 152

def compress(files, _base_dirs, output_path, level: nil)
  tar_path = "#{output_path}.tar"

  File.open(tar_path, 'wb', 0o644) do |file|
    Gem::Package::TarWriter.new(file) do |tar|
      files.each do |entry|
        content = File.read(entry[:full_path])
        tar.add_file_simple(entry[:archive_path], 0o644, content.bytesize) do |io|
          io.write(content)
        end
      end
    end
  end

  xz_args = ['xz']
  xz_args.push("-#{level}") if level
  xz_args.push('--stdout', tar_path)
  success = File.open(output_path, 'wb', 0o644) do |out|
    system(*xz_args, out: out)
  end
  FileUtils.rm_f(tar_path)

  raise RosettAi::BackupError, 'xz compression failed' unless success

  output_path
end

#extensionString

Returns ".tar.xz".

Returns:

  • (String)

    ".tar.xz"



142
143
144
# File 'lib/rosett_ai/backup/compressor.rb', line 142

def extension
  '.tar.xz'
end