class StatFile

#

Constants

N
RED_ON_BLACK
#

RED_ON_BLACK

#
REVERT
#

REVERT

#
SHALL_WE_DEBUG
#

SHALL_WE_DEBUG

#
VERSION
#

VERSION

#

Public Class Methods

new( this_file, be_verbose = true, run_already = true ) click to toggle source
#

initialize

#
# File lib/stat_file/stat_file.rb, line 43
def initialize(
    this_file,
    be_verbose  = true,
    run_already = true
  )
  reset # This must be called before set_be_verbose()
  be_verbose = false if be_verbose == :dont_be_verbose
  set_be_verbose if be_verbose
  set_file(this_file)
  run if run_already
end

Public Instance Methods

be_verbose?() click to toggle source
#

be_verbose?

#
# File lib/stat_file/stat_file.rb, line 180
def be_verbose?
  @be_verbose
end
file?() click to toggle source
#

file?

#
# File lib/stat_file/stat_file.rb, line 68
def file?
  @file
end
is_on_roebe?() click to toggle source
#

is_on_roebe?

#
# File lib/stat_file/stat_file.rb, line 194
def is_on_roebe?
  ENV['IS_ROEBE'].to_s == '1'
end
reset() click to toggle source
#

reset

#
# File lib/stat_file/stat_file.rb, line 58
def reset
  # === @be_verbose
  @be_verbose = false # Default.
  @debug = SHALL_WE_DEBUG
  @padding_left = '  '
end
result()
Alias for: result?
result?() click to toggle source
#

result?

#
# File lib/stat_file/stat_file.rb, line 173
def result?
  @result
end
Also aliased as: result
return_device_string() click to toggle source
#

return_device_string

This method is assumed to return an entry such as the following one:

Device: 801h/2049d

Note that this may differ from systemd to non-systemd systems.

#
# File lib/stat_file/stat_file.rb, line 207
def return_device_string
  result = 'Device: '.dup
  # ======================================================================= #
  # First, find out the current device:
  # ======================================================================= #
  base_dir = File.dirname(file?)
  cmd = `df #{base_dir}`
  device_number = cmd.split(N)[1].split(' ')[0]
  # ======================================================================= #
  # device_number now has something like /dev/sda1
  # In order to obtain the major and minor version,
  # we must do this:
  # ======================================================================= #
  cmd = "ls -al #{device_number}"
  major_minor  = `#{cmd}` # To get major/minor = 8/1
  # ======================================================================= #
  # This may however had fail. We assume that it will fail when it
  # includes the string "by-uuid".
  # ======================================================================= #
  applied_match = major_minor.match(/disk (\d, \d)/)
  if applied_match # Protect against nil values.
    major, minor = applied_match[1].split(',').map(&:strip)
    major_and_minor = major+'0'+minor
    result << major_and_minor+'h/'+major_and_minor.to_i(16).to_s+'d '
  end
  result
end
run() click to toggle source
#

run

#
# File lib/stat_file/stat_file.rb, line 238
def run
  stat_file
end
set_be_verbose() click to toggle source
#

set_be_verbose

#
# File lib/stat_file/stat_file.rb, line 87
def set_be_verbose
  @be_verbose = true
end
set_file(i, get_rid_of_slashdot = false) click to toggle source
#

set_file

#
# File lib/stat_file/stat_file.rb, line 75
def set_file(i, get_rid_of_slashdot = false)
  if get_rid_of_slashdot
    i.delete!('/') if i.include? '/' # Since 06.12.2011 we try to get rid of possible '/' in the string.
  end
  i = i.first if i.is_a? Array
  i = i.to_s.dup
  @file = i
end
size()
Alias for: size?
size?() click to toggle source
#

size?

#
# File lib/stat_file/stat_file.rb, line 187
def size?
  @size
end
Also aliased as: size
stat_file(f = @file) click to toggle source
#

stat_file

#
# File lib/stat_file/stat_file.rb, line 94
def stat_file(f = @file)
  if f.to_s.empty?
    opn; e 'No such file could be found. Perhaps you '
    opn; e 'forgot to pass in a valid filename?'
  end
  if f
    if f.is_a? Array
      f.each { |this_file| stat_file(this_file) }
    else
      @is_symlink = File.symlink?(f) ? true : false
      if @debug
        e "Debugging in stat_file.rb: Is `#{f}` a symlink or not? #{@is_symlink}"
      end
      if is_on_roebe? and !File.exist?(f)
        begin
          require 'beautiful_url'
          f = BeautifulUrl[f]
          f = f.first if f.is_a? Array
        rescue LoadError; end
      end
      string = '    File: `'+f+"'"
      # =================================================================== #
      # If it is a symlink we have to check whether the target exists
      # or not.
      # =================================================================== #
      if @is_symlink
        f = File.readlink(f)
        string << ' -> `'+f+"'"
      end
      # =================================================================== #
      # Now we can retrieve the information.
      # =================================================================== #
      if File.exist?(f)
        _ = File.stat(f)
        file_type = File.ftype(f)
        case file_type
        when 'directory'
          target_type = 'directory'
        when 'file'
          target_type = 'regular file'
        when 'symlink','link'
          target_type = 'symlink'
        end
        # Prepend a newline to it. I think it looks better that way.
        @result = N+string+N # reset again here.
        @result << @padding_left+'  Size: '+
                   (@size = _.size.to_s).to_s+'         Blocks: '+_.blocks.to_s+'   '+target_type.to_s+N
        @result << @padding_left+return_device_string+'  Inode: '+_.ino.to_s+N
        begin
          gid = File.stat(f).gid.to_s
          gid_name = Etc.getgrgid(File.stat(f).gid).name # i.e. "root"
          uid = File.stat(f).uid.to_s
          uid_name = Etc.getpwuid(File.stat(f).uid).name # i.e. "root"
          # =============================================================== #
          # Next we will add the access entry. The Access entry may
          # have a leading 0 or 1.
          # =============================================================== #
          access_mode = File.stat(f).mode.to_s(8)[-4,4] # Get the last 4 chars only.
          access_entry = 'Access: ('+access_mode+'/-rwxr-xr-x)  Uid: ( '+uid+'/   '+uid_name+')   Gid: ( '+gid+'/   '+gid_name+')'
          @result << @padding_left+access_entry+N
        rescue Exception => error; pp error; end
        @result << @padding_left+'Created at:    '+File.ctime(f).to_s+N
        @result << @padding_left+'Last accessed: '+File.atime(f).to_s+N
        @result << @padding_left+'Last modified: '+File.mtime(f).to_s+N
        @result << N
        e @result if @be_verbose
      else # Else the file does not exit.
        if be_verbose?
          opn; ewarn 'No target file exists at: `'+sfile(f)
               # File.readlink(@file)
        end
      end
    end
  end
end