Module: File_Checking

Included in:
Configurator, HtmlBuilder, Logging
Defined in:
lib/file_checking.rb

Overview

A module to facilitate frequently occuring checks on file-system objects

Constant Summary collapse

@@last_mime_type =
nil
@@text_messages =
{
  :exist? => "does not exist!",
  :exist => "does not exist!",
  :readable? => "is not readable!",
  :readable => "is not readable!",
  :executable? => "is not executable!",
  :executable => "is not executable!",
  :writable? => "is not writable!",
  :writable => "is not writable!",
  :directory? => "is not a directory!",
  :directory => "is not a directory!",
  :file? => "is not a file!",
  :file => "is not a file!",
# different thing
  :type => "is not of the sought file-type (not '%s', but '%s')!",
  :mime => "does not have the sought mime-type (not '%s', but '%s')!",
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.file_check(file, *messages) ⇒ Object

Checks if the file with the name from the first parameter has the properties, listed in the second. The messages parameter is an array of one or all of :exist?, :readable?, :writable?, :directory? or their string-representations, respectively. Returns nil in case of success, otherwise an informative message, describing the first negative test-result.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/file_checking.rb', line 75

def self.file_check(file, *messages)
  if !File.exist?(file)
    return file.dup << ' ' << ' does not exist!'
  end
  msg = nil
  if(file && messages.respond_to?(:to_ary) && !messages.empty?)
    messages.each do |k|
      if( k != :mime && k != :type) 
        if(! k.to_s.end_with?('?'))	
          k = (k.to_s << '?').to_sym
        end
        @log.debug ('checking ' << k.to_s) if @log
        if(msg == nil && File.respond_to?(k) && ! File.send(k, file.to_s))
          msg = "#{file} #{@@text_messages[k.to_sym]}"
        end
      end
    end
  end
  msg
end

.last_mime_typeObject



51
52
53
# File 'lib/file_checking.rb', line 51

def self::last_mime_type
  @@last_mime_type
end

.magic_check(file, magic) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/file_checking.rb', line 127

def self.magic_check(file, magic)
  if !File.exist?(file)
    return file.dup << ' ' << ' does not exist!'
  end
  fm = FileMagic.fm
  fd = fm.fd(File.new(file) ).split(';')[0]
  if fd != magic.strip  
    return file.dup << ' ' << (@@text_messages[:type] %[magic, fd])
  end
  return nil
end

.mime_check(file, mime_type_s) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/file_checking.rb', line 96

def self.mime_check(file, mime_type_s)
  if !File.exist?(file)
    return file.dup << ' ' << ' does not exist!'
  end

  fm = FileMagic.mime
     if (!File.directory?(file) )
       begin
         fd = fm.fd(File.new(file) ).split(';')[0]
       rescue Exception => ex
         @@last_mime_type = nil
         return ex.message 
       end
       @@last_mime_type = fd
       # list of mime_types
       if mime_type_s.respond_to?(:to_ary) 
         format = mime_type_s.detect {|f| fd == f.strip}
         if !format
           return file.dup << ' is of none of the supported mime-types, but ' << fd
         end
         # one mime_type only
       elsif fd != mime_type.strip  
         return file.dup << ' ' << (@@text_messages[:mime] %[mime_type, fd])
       end
     else 
       @@last_mime_type = "inode/directory"
       return file.dup << " is a directory!"
     end
     return nil
end

Instance Method Details

#file_check(file, *messages) ⇒ Object

Checks if the file with the name from the first parameter has the properties, listed in the second. The messages parameter is an array of one or several of :exist?, :readable?, :writable?, :directory? or their string-representations, respectively. Returns nil in case of success, otherwise an informative message, describing the first negative test-result.



63
64
65
# File 'lib/file_checking.rb', line 63

def file_check(file, *messages)
  File_Checking.file_check(file, *messages)
end