Method: File.safe_unlink

Defined in:
lib/ftools.rb

Removes a list of files. Each parameter should be the name of the file to delete. If the last parameter isn’t a String, verbose mode will be enabled. Returns the number of files deleted.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ftools.rb', line 168

def safe_unlink(*files)
  verbose = if files[-1].is_a? String then false else files.pop end
  files.each do |file|
    begin
      unlink file
      $stderr.print "removing ", file, "\n" if verbose
    rescue Errno::EACCES # for Windows
      continue if symlink? file
      begin
        mode = stat(file).mode
        o_chmod mode | 0200, file
        unlink file
        $stderr.print "removing ", file, "\n" if verbose
      rescue
        o_chmod mode, file rescue nil
      end
    rescue
    end
  end
end