Module: Decimate
- Defined in:
- lib/decimate.rb,
lib/decimate/version.rb
Constant Summary collapse
- VERSION =
"0.0.4"
Class Method Summary collapse
-
.dir!(path, opts = {}) ⇒ Object
Securely deletes given directory recursively using shred.
- .fail_unless_shred ⇒ Object
-
.file!(path, opts = {}) ⇒ Object
Securely deletes given file using shred.
-
.run(cmd) ⇒ Object
Executes given command using Open3.capture3 Raises exception if non-zero status call returned, writes to error log.
- .shred_cmd ⇒ Object
-
.validate(path, required_regex = nil) ⇒ Object
Check for shred, escape path, ensure path isn’t root dir or fails regexp.
- .validate_path(path, required_regex = nil) ⇒ Object
Class Method Details
.dir!(path, opts = {}) ⇒ Object
Securely deletes given directory recursively using shred.
- Returns nil if directory does not exist
- Returns stdout from shred operation if dir exists and shredded successfully
- If optional regex sanity check is included, exception will be raised if match against given path fails
- Raises if shred or find command triggers any status code other than zero
- Raises if shred command not found
Usage: Decimate.dir! ‘my-unloved-dirctory’ Decimate.dir! ‘my-unloved-dirctory’, path_must_match: /unloved/
65 66 67 68 69 70 71 72 |
# File 'lib/decimate.rb', line 65 def self.dir! path, opts={} return unless Dir.exist?(path) path = validate path, opts[:path_must_match] stdout = run "find #{path} -type f -exec #{shred_cmd} {} +" FileUtils.rm_rf path stdout end |
.fail_unless_shred ⇒ Object
32 33 34 |
# File 'lib/decimate.rb', line 32 def self.fail_unless_shred raise if `which shred`.chomp.empty? end |
.file!(path, opts = {}) ⇒ Object
Securely deletes given file using shred.
- Returns nil if file does not exist
- Returns stdout from shred operation if file exists and shredded successfully
- If optional regex sanity check is included, exception will be raised if match against given path fails
- Raises if shred or find command triggers any status code other than zero
- Raises if shred command not found
45 46 47 48 49 50 |
# File 'lib/decimate.rb', line 45 def self.file! path, opts={} return unless File.exist?(path) path = validate path, opts[:path_must_match] run "#{shred_cmd} #{path}" end |
.run(cmd) ⇒ Object
Executes given command using Open3.capture3 Raises exception if non-zero status call returned, writes to error log
12 13 14 15 16 |
# File 'lib/decimate.rb', line 12 def self.run cmd stdout,stderr,status = Open3.capture3 cmd raise "Failed executing #{cmd}: stdout: #{stdout}, stderr: #{stderr}, status #{status}" unless status.nil? || status == 0 stdout end |
.shred_cmd ⇒ Object
7 |
# File 'lib/decimate.rb', line 7 def self.shred_cmd; "shred -uv"; end |
.validate(path, required_regex = nil) ⇒ Object
Check for shred, escape path, ensure path isn’t root dir or fails regexp
19 20 21 22 23 |
# File 'lib/decimate.rb', line 19 def self.validate path, required_regex=nil fail_unless_shred escaped_path = Shellwords.escape path validate_path escaped_path, required_regex end |
.validate_path(path, required_regex = nil) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/decimate.rb', line 25 def self.validate_path path, required_regex=nil File.(path).tap do |path| raise "Trying to remove root dir? Path: #{path}" if path == '/' raise "Path #{path} does not match #{required_regex}" if required_regex && !path.match(required_regex) end end |