Module: Tmptation::SafeDeletable
Overview
Adds a #safe_delete method that will delete the object’s associated path (either #path or #to_s, if it exists) only if it lives within the system’s temporary directory (as defined by Dir.tmpdir)
Constant Summary collapse
- UnsafeDelete =
Class.new(RuntimeError)
Class Method Summary collapse
- .path_for(obj) ⇒ Object
-
.safe?(path) ⇒ Boolean
Whether ‘path` lives under `Dir.tmpdir`.
Instance Method Summary collapse
-
#safe_delete ⇒ Object
Delete ‘#path` or `#to_s` if it exists, and only if it lives under `Dir.tmpdir`.
-
#safe_delete_contents ⇒ Object
Same as ‘#safe_delete`, but only deletes the contents of the directory, i.e.
Class Method Details
.path_for(obj) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tmptation.rb', line 36 def self.path_for(obj) path = obj.respond_to?(:path) ? obj.path : obj.to_s path = Pathname(path). unless safe?(path) raise UnsafeDelete.new("refusing to remove non-tmp directory '#{path}'") end path end |
.safe?(path) ⇒ Boolean
Whether ‘path` lives under `Dir.tmpdir`
48 49 50 |
# File 'lib/tmptation.rb', line 48 def self.safe?(path) !!path.to_s.match(/^#{Regexp.escape(Dir.tmpdir)}/) end |
Instance Method Details
#safe_delete ⇒ Object
Delete ‘#path` or `#to_s` if it exists, and only if it lives under `Dir.tmpdir`. If the path is a directory, it is deleted recursively.
57 58 59 60 61 |
# File 'lib/tmptation.rb', line 57 def safe_delete FileUtils.remove_entry_secure(SafeDeletable.path_for(self).to_s) rescue Errno::ENOENT # noop end |
#safe_delete_contents ⇒ Object
Same as ‘#safe_delete`, but only deletes the contents of the directory, i.e. files and subdirectories
69 70 71 |
# File 'lib/tmptation.rb', line 69 def safe_delete_contents SafeDeletable.path_for(self).children.each {|entry| FileUtils.remove_entry_secure(entry) } end |