Class: StatFile

Inherits:
Object
  • Object
show all
Defined in:
lib/stat_file/colours.rb,
lib/stat_file/stat_file.rb,
lib/stat_file/version/version.rb

Overview

#

Constant Summary collapse

N =
"\n"
SHALL_WE_DEBUG =
#

SHALL_WE_DEBUG

#
false
RED_ON_BLACK =
#

RED_ON_BLACK

#
"\e[31;48m"
REVERT =
#

REVERT

#
'\e[0m'
VERSION =
#

VERSION

#
'1.0.19'
LAST_UPDATE =
#

LAST_UPDATE

#
'17.01.2024'

Instance Method Summary collapse

Constructor Details

#initialize(this_file, be_verbose = true, run_already = true) ⇒ StatFile

#

initialize

#


43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#are_the_colours_available?Boolean

#

are_the_colours_available?

#

Returns:

  • (Boolean)


221
222
223
# File 'lib/stat_file/stat_file.rb', line 221

def are_the_colours_available?
  Object.const_defined? :Colours
end

#be_verbose?Boolean

#

be_verbose?

#

Returns:

  • (Boolean)


200
201
202
# File 'lib/stat_file/stat_file.rb', line 200

def be_verbose?
  @be_verbose
end

#file?Boolean

#

file?

#

Returns:

  • (Boolean)


76
77
78
# File 'lib/stat_file/stat_file.rb', line 76

def file?
  @file
end

#is_on_roebe?Boolean

#

is_on_roebe?

#

Returns:

  • (Boolean)


214
215
216
# File 'lib/stat_file/stat_file.rb', line 214

def is_on_roebe?
  ENV['IS_ROEBE'].to_s == '1'
end

#resetObject

#

reset (reset tag)

#


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/stat_file/stat_file.rb', line 58

def reset
  # ======================================================================= #
  # === @be_verbose
  # ======================================================================= #
  @be_verbose = false # Default.
  # ======================================================================= #
  # === @debug
  # ======================================================================= #
  @debug = SHALL_WE_DEBUG
  # ======================================================================= #
  # === @padding_left
  # ======================================================================= #
  @padding_left = '  '
end

#result?Boolean Also known as: result, text?

#

result?

#

Returns:

  • (Boolean)


192
193
194
# File 'lib/stat_file/stat_file.rb', line 192

def result?
  @result
end

#return_device_stringObject

#

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.

#


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/stat_file/stat_file.rb', line 234

def return_device_string
  result = 'Device: '.dup
  # ======================================================================= #
  # First, find out the current device:
  # ======================================================================= #
  base_dir = File.dirname(file?)
  cmd = `df #{base_dir} 2>&1` # The 2>&1 is necessary for windows.
  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} 2>&1"
  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

#revObject

#

rev

#


33
34
35
# File 'lib/stat_file/colours.rb', line 33

def rev
  ::Colours.rev
end

#runObject

#

run

#


265
266
267
# File 'lib/stat_file/stat_file.rb', line 265

def run
  stat_file
end

#set_be_verboseObject

#

set_be_verbose

#


95
96
97
# File 'lib/stat_file/stat_file.rb', line 95

def set_be_verbose
  @be_verbose = true
end

#set_file(i, get_rid_of_slashdot = false) ⇒ Object

#

set_file

#


83
84
85
86
87
88
89
90
# File 'lib/stat_file/stat_file.rb', line 83

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?Boolean Also known as: size

#

size?

#

Returns:

  • (Boolean)


207
208
209
# File 'lib/stat_file/stat_file.rb', line 207

def size?
  @size
end

#stat_file(f = @file) ⇒ Object

#

stat_file

#


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/stat_file/stat_file.rb', line 102

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 << rev+' → `'+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.
        # ================================================================= #
        if are_the_colours_available?
          use_this_regex = /`(.+)`/ # See: https://rubular.com/r/EnETP5vkCYgGEB
          string.prepend(rev)
          string.sub!(
            use_this_regex,
            steelblue('\1')
          )
        end
        @result = N+string+N # reset again here.
        @result << "#{rev}#{@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)}#{N}"
        @result << "#{@padding_left}Last accessed: #{File.atime(f)}#{N}"
        @result << "#{@padding_left}Last modified: #{File.mtime(f)}#{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

#steelblue(i = '') ⇒ Object

#

steelblue

#


26
27
28
# File 'lib/stat_file/colours.rb', line 26

def steelblue(i = '')
  ::Colours::HtmlColoursMethods.steelblue(i)
end