Class: Palimpsest::Utility
- Inherits:
-
Object
- Object
- Palimpsest::Utility
- Defined in:
- lib/palimpsest/utility.rb
Overview
Utility functions for Palimpsest.
Class Method Summary collapse
-
.extract_repo(repo, treeish, directory) ⇒ Object
Extracts a git repo to a directory.
-
.make_random_directory(root, prefix, dir = nil) ⇒ String
Make a random directory.
-
.safe_path?(path) ⇒ Boolean
Forbids use of
../
and~/
in path. -
.write(contents, file, preserve: false) ⇒ Object
Write contents to file.
Class Method Details
.extract_repo(repo, treeish, directory) ⇒ Object
Extracts a git repo to a directory.
38 39 40 41 42 |
# 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) ⇒ String
Make a random directory.
13 14 15 16 17 18 19 20 |
# 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) ⇒ Boolean
Forbids use of ../
and ~/
in path.
Forbids absolute paths.
26 27 28 29 30 31 32 |
# 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) ⇒ Object
Write contents to file.
47 48 49 50 51 |
# 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 |