stat_file 1.0.16

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of stat_file might be problematic. Click here for more details.

checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 55994c17fad71ee4da83ae81d55ff027b3f9331528b3434d7de414bb3162b619
4
+ data.tar.gz: ac59ee4d60e01333b432f99b62edd5e64680c5ff3be5dd7394a2688e0c1de6e2
5
+ SHA512:
6
+ metadata.gz: f2e562dd767ae3a207648423b8d0eb08abf05e0a953772a8eb76e1b2091573c0865435074c692b3345058a00b8cf49cbf6514dd016d07f8fed9accfac764e640
7
+ data.tar.gz: abaa7737ac1517180f35cdafa1e158c8e3dd5dee458c9015df6c1cef88246c720eb699e9aa1f45e7a0604d19ae7f3124af35d1e6c76b16e22209dd20866eeece
@@ -0,0 +1,246 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === StatFile
6
+ #
7
+ # require 'stat_file/stat_file.rb'
8
+ # =========================================================================== #
9
+ # Bug:
10
+ # The access entry is not completely correct - the number is ok but
11
+ # the rxw--- format is wrong.
12
+ # =========================================================================== #
13
+ require 'etc'
14
+
15
+ class StatFile
16
+
17
+ begin
18
+ require 'colours'
19
+ include Colours
20
+ rescue LoadError; end
21
+ require 'opn'
22
+
23
+ N = "\n" unless defined? N
24
+
25
+ # ========================================================================= #
26
+ # === SHALL_WE_DEBUG
27
+ # ========================================================================= #
28
+ SHALL_WE_DEBUG = false unless defined? SHALL_WE_DEBUG
29
+
30
+ # ========================================================================= #
31
+ # === RED_ON_BLACK
32
+ # ========================================================================= #
33
+ RED_ON_BLACK = "\e[31;48m" unless defined? RED_ON_BLACK
34
+
35
+ # ========================================================================= #
36
+ # === REVERT
37
+ # ========================================================================= #
38
+ REVERT = '\e[0m' unless defined? REVERT
39
+
40
+ # ========================================================================= #
41
+ # === initialize
42
+ # ========================================================================= #
43
+ def initialize(
44
+ this_file,
45
+ be_verbose = true,
46
+ run_already = true
47
+ )
48
+ reset # This must be called before set_be_verbose()
49
+ be_verbose = false if be_verbose == :dont_be_verbose
50
+ set_be_verbose if be_verbose
51
+ set_file(this_file)
52
+ run if run_already
53
+ end
54
+
55
+ # ========================================================================= #
56
+ # === reset
57
+ # ========================================================================= #
58
+ def reset
59
+ # === @be_verbose
60
+ @be_verbose = false # Default.
61
+ @debug = SHALL_WE_DEBUG
62
+ @padding_left = ' '
63
+ end
64
+
65
+ # ========================================================================= #
66
+ # === file?
67
+ # ========================================================================= #
68
+ def file?
69
+ @file
70
+ end
71
+
72
+ # ========================================================================= #
73
+ # === set_file
74
+ # ========================================================================= #
75
+ def set_file(i, get_rid_of_slashdot = false)
76
+ if get_rid_of_slashdot
77
+ i.delete!('/') if i.include? '/' # Since 06.12.2011 we try to get rid of possible '/' in the string.
78
+ end
79
+ i = i.first if i.is_a? Array
80
+ i = i.to_s.dup
81
+ @file = i
82
+ end
83
+
84
+ # ========================================================================= #
85
+ # === set_be_verbose
86
+ # ========================================================================= #
87
+ def set_be_verbose
88
+ @be_verbose = true
89
+ end
90
+
91
+ # ========================================================================= #
92
+ # === stat_file
93
+ # ========================================================================= #
94
+ def stat_file(f = @file)
95
+ if f.to_s.empty?
96
+ opn; e 'No such file could be found. Perhaps you '
97
+ opn; e 'forgot to pass in a valid filename?'
98
+ end
99
+ if f
100
+ if f.is_a? Array
101
+ f.each { |this_file| stat_file(this_file) }
102
+ else
103
+ @is_symlink = File.symlink?(f) ? true : false
104
+ if @debug
105
+ e "Debugging in stat_file.rb: Is `#{f}` a symlink or not? #{@is_symlink}"
106
+ end
107
+ if is_on_roebe? and !File.exist?(f)
108
+ begin
109
+ require 'beautiful_url'
110
+ f = BeautifulUrl[f]
111
+ f = f.first if f.is_a? Array
112
+ rescue LoadError; end
113
+ end
114
+ string = ' File: `'+f+"'"
115
+ # =================================================================== #
116
+ # If it is a symlink we have to check whether the target exists
117
+ # or not.
118
+ # =================================================================== #
119
+ if @is_symlink
120
+ f = File.readlink(f)
121
+ string << ' -> `'+f+"'"
122
+ end
123
+ # =================================================================== #
124
+ # Now we can retrieve the information.
125
+ # =================================================================== #
126
+ if File.exist?(f)
127
+ _ = File.stat(f)
128
+ file_type = File.ftype(f)
129
+ case file_type
130
+ when 'directory'
131
+ target_type = 'directory'
132
+ when 'file'
133
+ target_type = 'regular file'
134
+ when 'symlink','link'
135
+ target_type = 'symlink'
136
+ end
137
+ # Prepend a newline to it. I think it looks better that way.
138
+ @result = N+string+N # reset again here.
139
+ @result << @padding_left+' Size: '+
140
+ (@size = _.size.to_s).to_s+' Blocks: '+_.blocks.to_s+' '+target_type.to_s+N
141
+ @result << @padding_left+return_device_string+' Inode: '+_.ino.to_s+N
142
+ begin
143
+ gid = File.stat(f).gid.to_s
144
+ gid_name = Etc.getgrgid(File.stat(f).gid).name # i.e. "root"
145
+ uid = File.stat(f).uid.to_s
146
+ uid_name = Etc.getpwuid(File.stat(f).uid).name # i.e. "root"
147
+ # =============================================================== #
148
+ # Next we will add the access entry. The Access entry may
149
+ # have a leading 0 or 1.
150
+ # =============================================================== #
151
+ access_mode = File.stat(f).mode.to_s(8)[-4,4] # Get the last 4 chars only.
152
+ access_entry = 'Access: ('+access_mode+'/-rwxr-xr-x) Uid: ( '+uid+'/ '+uid_name+') Gid: ( '+gid+'/ '+gid_name+')'
153
+ @result << @padding_left+access_entry+N
154
+ rescue Exception => error; pp error; end
155
+ @result << @padding_left+'Created at: '+File.ctime(f).to_s+N
156
+ @result << @padding_left+'Last accessed: '+File.atime(f).to_s+N
157
+ @result << @padding_left+'Last modified: '+File.mtime(f).to_s+N
158
+ @result << N
159
+ e @result if @be_verbose
160
+ else # Else the file does not exit.
161
+ if be_verbose?
162
+ opn; ewarn 'No target file exists at: `'+sfile(f)
163
+ # File.readlink(@file)
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+
170
+ # ========================================================================= #
171
+ # === result?
172
+ # ========================================================================= #
173
+ def result?
174
+ @result
175
+ end; alias result result? # === result
176
+
177
+ # ========================================================================= #
178
+ # === be_verbose?
179
+ # ========================================================================= #
180
+ def be_verbose?
181
+ @be_verbose
182
+ end
183
+
184
+ # ========================================================================= #
185
+ # === size?
186
+ # ========================================================================= #
187
+ def size?
188
+ @size
189
+ end; alias size size? # === size()
190
+
191
+ # ========================================================================= #
192
+ # === is_on_roebe?
193
+ # ========================================================================= #
194
+ def is_on_roebe?
195
+ ENV['IS_ROEBE'].to_s == '1'
196
+ end
197
+
198
+ # ========================================================================= #
199
+ # === return_device_string
200
+ #
201
+ # This method is assumed to return an entry such as the following one:
202
+ #
203
+ # Device: 801h/2049d
204
+ #
205
+ # Note that this may differ from systemd to non-systemd systems.
206
+ # ========================================================================= #
207
+ def return_device_string
208
+ result = 'Device: '.dup
209
+ # ======================================================================= #
210
+ # First, find out the current device:
211
+ # ======================================================================= #
212
+ base_dir = File.dirname(file?)
213
+ cmd = `df #{base_dir}`
214
+ device_number = cmd.split(N)[1].split(' ')[0]
215
+ # ======================================================================= #
216
+ # device_number now has something like /dev/sda1
217
+ # In order to obtain the major and minor version,
218
+ # we must do this:
219
+ # ======================================================================= #
220
+ cmd = "ls -al #{device_number}"
221
+ major_minor = `#{cmd}` # To get major/minor = 8/1
222
+ # ======================================================================= #
223
+ # This may however had fail. We assume that it will fail when it
224
+ # includes the string "by-uuid".
225
+ # ======================================================================= #
226
+ applied_match = major_minor.match(/disk (\d, \d)/)
227
+ if applied_match # Protect against nil values.
228
+ major, minor = applied_match[1].split(',').map(&:strip)
229
+ major_and_minor = major+'0'+minor
230
+ result << major_and_minor+'h/'+major_and_minor.to_i(16).to_s+'d '
231
+ end
232
+ result
233
+ end
234
+
235
+ # ========================================================================= #
236
+ # === run
237
+ # ========================================================================= #
238
+ def run
239
+ stat_file
240
+ end
241
+
242
+ end
243
+
244
+ if __FILE__ == $PROGRAM_NAME
245
+ StatFile.new(ARGV.first, true)
246
+ end # sfile $SYSBIN/ruby
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ class StatFile
6
+
7
+ # ========================================================================= #
8
+ # === VERSION
9
+ # ========================================================================= #
10
+ VERSION = '1.0.16'
11
+
12
+ end
data/lib/stat_file.rb ADDED
@@ -0,0 +1 @@
1
+ require 'stat_file/stat_file.rb'
data/stat_file.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # =========================================================================== #
2
+ # Gemspec for Project StatFile.
3
+ # =========================================================================== #
4
+ require 'stat_file/version/version.rb'
5
+ require 'roebe'
6
+
7
+ Gem::Specification.new { |s|
8
+
9
+ s.name = 'stat_file'
10
+ s.version = StatFile::VERSION
11
+ s.date = Time.now.strftime('%Y-%m-%d')
12
+
13
+ s.summary = <<-EOF
14
+
15
+ This library is called stat_file. It allows you to
16
+ do basic stat functionality, like the Unix Tool.
17
+
18
+ EOF
19
+
20
+ s.description = <<-EOF
21
+
22
+ This library is called stat_file. It allows you to
23
+ do basic stat functionality, like the Unix Tool.
24
+
25
+ EOF
26
+
27
+ s.authors = ['Robert A. Heiler']
28
+ s.email = Roebe.email?
29
+ s.files = Dir['**/*']
30
+ s.license = 'MIT'
31
+ s.homepage = 'https://rubygems.org/gems/stat_file'
32
+
33
+ s.required_ruby_version = '>= '+RUBY_VERSION
34
+ s.required_rubygems_version = '>= '+Gem::VERSION
35
+ s.rubygems_version = '>= '+Gem::VERSION
36
+
37
+ s.add_dependency 'opn'
38
+ s.add_dependency 'colours'
39
+
40
+ }
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stat_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.16
5
+ platform: ruby
6
+ authors:
7
+ - Robert A. Heiler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opn
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colours
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |2+
42
+
43
+ This library is called stat_file. It allows you to
44
+ do basic stat functionality, like the Unix Tool.
45
+
46
+ email: shevy@inbox.lt
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - lib/stat_file.rb
52
+ - lib/stat_file/stat_file.rb
53
+ - lib/stat_file/version/version.rb
54
+ - stat_file.gemspec
55
+ homepage: https://rubygems.org/gems/stat_file
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 3.0.1
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.2.19
73
+ requirements: []
74
+ rubygems_version: 3.2.19
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: This library is called stat_file. It allows you to do basic stat functionality,
78
+ like the Unix Tool.
79
+ test_files: []
80
+ ...