class Palimpsest::Utility

Utility functions for Palimpsest.

Public Class Methods

extract_repo(repo, treeish, directory) click to toggle source

Extracts a git repo to a directory. @param [Grit::Repo] repo @param [String] treeish @param [String] directory

# File lib/palimpsest/utility.rb, line 38
def self.extract_repo repo, treeish, directory
  input = Archive::Tar::Minitar::Input.new StringIO.new(repo.archive_tar treeish)
  input.each { |e| input.extract_entry directory, e }
  FileUtils.remove_entry_secure "#{directory}/pax_global_header"
end
make_random_directory(root, prefix, dir = nil) click to toggle source

Make a random directory. @param [String] root directory to place random directory @param [String] prefix prepended to random directory name @param [String, nil] dir the random directory name (used recursively) @return [String] path to created random directory

# File lib/palimpsest/utility.rb, line 13
def self.make_random_directory root, prefix, dir = nil
  path = "#{root}/#{prefix}#{dir}" unless dir.nil?
  if path.nil? or File.exists? path
    make_random_directory root, prefix, Random.rand(10000000)
  else
    FileUtils.mkdir(path).first
  end
end
safe_path?(path) click to toggle source

Forbids use of ‘../` and `~/` in path. Forbids absolute paths. @param [String] path @return [Boolean]

# File lib/palimpsest/utility.rb, line 26
def self.safe_path? path
  case
  when path[/(\.\.\/|~\/)/] then return false
  when path[/^\//] then return false
  else return true
  end
end
write(contents, file, preserve: false) click to toggle source

Write contents to file. @param contents [String] @param file [String]

# File lib/palimpsest/utility.rb, line 47
def self.write contents, file, preserve: false
  original_time = File.mtime file if preserve
  File.open(file, 'w') { |f| f.write contents }
  File.utime original_time, original_time, file if preserve
end