linux_stat 0.4.2 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62a54b165b53e0fe6fddad20ab6b39b8a0d57e1a02186f995fcf0341cc69aae0
4
- data.tar.gz: 8bc58cc3c8b72e117892cd8f987c924337d2ab10995d14b3fb7735064e4b3685
3
+ metadata.gz: 349ddabe649c7cdec58e01537c6c28ab47f9d30b413208baf5fae4c0c7a26804
4
+ data.tar.gz: e66ba391b2264c53f6d2619ee64d8907e5435079b29bcc76bf2d54516ea82949
5
5
  SHA512:
6
- metadata.gz: a5490c31315132793840e3e849996f0545b08b3b416eadef3ba6979dcbad89d31329b69dad403295f8cc7e17a677f6d8df032c42706162b251b6ed921a3d11e4
7
- data.tar.gz: 0100faa162b904042e8143b8dec85b45a3cb82543c693514049df17834890b12034cb1561d8c69ba606e9ebc81fba1550b255ad6d17d70a552011eb9aaf5300f
6
+ metadata.gz: 1f7274982f6330ed5023b6e7adf33f325163e382c9fdc81be8181c65ad182ba1e77433af792e1167d603673af147ff7e0601a42975f25ac2393206db755a27f5
7
+ data.tar.gz: 61d48ba5bcf4598d8df63173bfbecfe4fd6a91552f32195a540e344961ee4e2ebcb9decf492d092b41ca034908b82948b9e6d727e678e3b41f434c5771794070
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Sourav Goswami
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -288,7 +288,7 @@ LinuxStat::Process.zombie
288
288
  ### LinuxStat::ProcessInfo
289
289
  ```
290
290
  LinuxStat::ProcessInfo.cmdline
291
- => "ruby bin/linuxstat.rb -md"
291
+ => "ruby exe/linuxstat.rb -md"
292
292
 
293
293
  LinuxStat::ProcessInfo.command_name
294
294
  => "ruby"
@@ -399,6 +399,59 @@ LinuxStat::Uname.version
399
399
  => "#1 SMP PREEMPT Wed, 21 Oct 2020 01:11:20 +0000"
400
400
 
401
401
  ```
402
+
403
+ ### LinuxStat::User
404
+ ```
405
+ LinuxStat::User.get_euid
406
+ => 1000
407
+
408
+ LinuxStat::User.get_gid
409
+ => 1000
410
+
411
+ LinuxStat::User.get_uid
412
+ => 1000
413
+
414
+ LinuxStat::User.get_user
415
+ => "sourav"
416
+
417
+ LinuxStat::User.gid_by_username
418
+ => 1000
419
+
420
+ LinuxStat::User.gids
421
+ => {:root=>0, :bin=>1, :daemon=>2, :mail=>12, :ftp=>11, :http=>33, :nobody=>65534, :dbus=>81, :"systemd-journal-remote"=>982, :"systemd-network"=>981, :"systemd-resolve"=>980, :"systemd-timesync"=>979, :"systemd-coredump"=>978, :uuidd=>68, :avahi=>977, :...
422
+
423
+ LinuxStat::User.home_by_gid
424
+ => "/home/sourav"
425
+
426
+ LinuxStat::User.home_by_username
427
+ => "/home/sourav"
428
+
429
+ LinuxStat::User.home_directories
430
+ => {:root=>"/root", :bin=>"/", :daemon=>"/", :mail=>"/var/spool/mail", :ftp=>"/srv/ftp", :http=>"/srv/http", :nobody=>"/", :dbus=>"/", :"systemd-journal-remote"=>"/", :"systemd-network"=>"/", :"systemd-resolve"=>"/", :"systemd-timesync"=>"/", :"systemd-c...
431
+
432
+ LinuxStat::User.homes_by_uid
433
+ => ["/home/sourav"]
434
+
435
+ LinuxStat::User.ids
436
+ => {:root=>{:uid=>0, :gid=>0}, :bin=>{:uid=>1, :gid=>1}, :daemon=>{:uid=>2, :gid=>2}, :mail=>{:uid=>8, :gid=>12}, :ftp=>{:uid=>14, :gid=>11}, :http=>{:uid=>33, :gid=>33}, :nobody=>{:uid=>65534, :gid=>65534}, :dbus=>{:uid=>81, :gid=>81}, :"systemd-journal...
437
+
438
+ LinuxStat::User.list
439
+ => ["root", "bin", "daemon", "mail", "ftp", "http", "nobody", "dbus", "systemd-journal-remote", "systemd-network", "systemd-resolve", "systemd-timesync", "systemd-coredump", "uuidd", "avahi", "colord", "git", "lxdm", "polkitd", "rtkit", "usbmux", "sourav...
440
+
441
+ LinuxStat::User.uid_by_username
442
+ => 1000
443
+
444
+ LinuxStat::User.uids
445
+ => {:root=>0, :bin=>1, :daemon=>2, :mail=>8, :ftp=>14, :http=>33, :nobody=>65534, :dbus=>81, :"systemd-journal-remote"=>982, :"systemd-network"=>981, :"systemd-resolve"=>980, :"systemd-timesync"=>979, :"systemd-coredump"=>978, :uuidd=>68, :avahi=>977, :c...
446
+
447
+ LinuxStat::User.username_by_gid
448
+ => "sourav"
449
+
450
+ LinuxStat::User.usernames_by_uid
451
+ => ["sourav"]
452
+
453
+ ```
454
+
402
455
  ---
403
456
 
404
457
  ## Note 1: Filesystem
@@ -513,6 +566,71 @@ irb(main):003:0> t = Time.now ; puts LinuxStat::FS.stat('/') ; Time.now - t
513
566
 
514
567
  To learn more about them, just run ri and the method name. To see all available methods:
515
568
 
569
+ ## Note 4: User
570
+ Most of the LinuxStat::User supports arguments.
571
+
572
+ For example, to get a user's home by the username:
573
+
574
+ ```
575
+ $ irb
576
+ irb(main):001:0> require 'linux_stat'
577
+ => true
578
+
579
+ irb(main):002:0> LinuxStat::User.home_by_username('root')
580
+ => "/root"
581
+
582
+ irb(main):003:0> LinuxStat::User.home_by_username('ftp')
583
+ => "/srv/ftp"
584
+
585
+ irb(main):004:0> LinuxStat::User.home_by_username('mail')
586
+ => "/var/spool/mail"
587
+ ```
588
+
589
+ Or to get the user's home by the GID/UID:
590
+
591
+ ```
592
+ $ irb
593
+ irb(main):001:0> require 'linux_stat'
594
+ => true
595
+
596
+ irb(main):002:0> LinuxStat::User.homes_by_uid(1001)
597
+ => ["/home/userx", "/home/userz"]
598
+
599
+ irb(main):003:0> LinuxStat::User.homes_by_uid(1000)
600
+ => ["/home/sourav"]
601
+
602
+ irb(main):004:0> LinuxStat::User.home_by_gid(1001)
603
+ => "/home/userx"
604
+
605
+ irb(main):005:0> LinuxStat::User.home_by_gid(1000)
606
+ => "/home/sourav"
607
+
608
+ irb(main):006:0> LinuxStat::User.home_by_gid(0)
609
+ => "/root"
610
+ ```
611
+
612
+ Or to get the UID/GID by username:
613
+
614
+ ```
615
+ $ irb
616
+ irb(main):001:0> require 'linux_stat'
617
+ => true
618
+
619
+ irb(main):002:0> LinuxStat::User.uid_by_username('root')
620
+ => 0
621
+
622
+ irb(main):003:0> LinuxStat::User.uid_by_username('ftp')
623
+ => 14
624
+
625
+ irb(main):004:0> LinuxStat::User.gid_by_username('ftp')
626
+ => 11
627
+
628
+ irb(main):005:0> LinuxStat::User.gid_by_username('InvalidUser')
629
+ => nil
630
+ ```
631
+
632
+ Read the ri documentation for more info.
633
+
516
634
  ---
517
635
 
518
636
  ## Return Types
@@ -558,31 +676,37 @@ Issues regarding running LinuxStat on termux are also welcomed.
558
676
  ---
559
677
 
560
678
  ## Development
679
+ After checking out the repo, compile and install this gem onto your local machine with `bundle exec rake install`
561
680
 
562
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
681
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
563
682
 
564
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
683
+ To test all modules, run `rake install` and then `exe/linuxstat.rb`. Also check "Testing" below.
565
684
 
566
685
  ---
567
686
 
568
687
  ## Testing
569
- Like other gems, this doesn't have a test like RSpec. We suggest using the bin/linuxstat.rb file on various systems.
688
+ Like other gems, this doesn't have a test like RSpec.
689
+
690
+ We suggest using the exe/linuxstat.rb file on various Linux systems to test.
691
+
692
+ First you need to execute `bundle exec rake install` to compile and install this gem.
693
+
570
694
  If you need to test a specific module, say the CPU, just run it like this:
571
695
 
572
696
  ```
573
- $ ruby bin/linuxstat.rb CPU
697
+ $ ruby exe/linuxstat.rb CPU
574
698
  ```
575
699
 
576
700
  Or:
577
701
  ```
578
- $ ruby bin/linuxstat.rb cpu
702
+ $ ruby exe/linuxstat.rb cpu
579
703
  ```
580
704
 
581
705
  That is, the argument passed is not case-sensitive.
582
706
  But if the argument passed isn't available and outright wrong, it will run all the module methods. For example, you can't do:
583
707
 
584
708
  ```
585
- $ ruby bin/linuxstat.rb upc
709
+ $ ruby exe/linuxstat.rb upc
586
710
  ```
587
711
  This is not a valid module and can't be run.
588
712
 
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  $-v = true
3
- %w(bundler/setup linux_stat irb).each(&method(:require))
3
+ %w(linux_stat irb).each(&method(:require))
4
4
  IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -4,5 +4,3 @@ IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
6
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,16 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
+ $-v = true
3
+
2
4
  begin
3
5
  require 'linux_stat'
4
6
  rescue LoadError
5
- puts "The Gem needs to be installed before this test can be run!"
7
+ abort "The Gem needs to be installed before this test can be run!"
6
8
  end
7
9
 
8
- $-v = true
9
-
10
- # Print time each method takes unless --no-time or -nt option is passed
11
- markdown = ARGV.any? { |x| x[/^\-(\-markdown|md)$/] }
12
- html = ARGV.any? { |x| x[/^\-(\-html|html)$/] }
13
-
14
10
  # Check which conflicting argument (e.g., `-md -html` together) is passed last
15
11
  # Always use the last argument
16
12
  conflicting, hash = [
@@ -38,6 +34,7 @@ end
38
34
 
39
35
  MARKDOWN, HTML = hash[:markdown], hash[:html]
40
36
 
37
+ # Print time each method takes unless --no-time or -nt option is passed
41
38
  PRINT_TIME = (MARKDOWN || HTML) ? false : !ARGV.any? { |x| x[/^\-\-no-time$/] || x[/^\-nt$/] }
42
39
 
43
40
  %w(--markdown -md --no-time -nt --html -html).each(&ARGV.method(:delete))
@@ -99,10 +96,11 @@ execute.sort.each do |c|
99
96
  puts
100
97
  end
101
98
 
102
- meths.length > 0
99
+ if meths.length > 0
103
100
  if MARKDOWN
104
101
  puts "```\n\n"
105
102
  elsif HTML
106
103
  puts "</pre>"
107
104
  end
105
+ end
108
106
  end
@@ -37,6 +37,23 @@ static VALUE getPosixVersion(VALUE obj) {
37
37
  return INT2FIX(sysconf(_SC_VERSION)) ;
38
38
  }
39
39
 
40
+ static VALUE getUser(VALUE obj) {
41
+ char *name = getlogin() ;
42
+ return name ? rb_str_new_cstr(name) : rb_str_new_cstr("") ;
43
+ }
44
+
45
+ static VALUE getUID(VALUE obj) {
46
+ return INT2FIX(getuid()) ;
47
+ }
48
+
49
+ static VALUE getGID(VALUE obj) {
50
+ return INT2FIX(getgid()) ;
51
+ }
52
+
53
+ static VALUE getEUID(VALUE obj) {
54
+ return INT2FIX(geteuid()) ;
55
+ }
56
+
40
57
  void Init_sysconf() {
41
58
  VALUE _linux_stat = rb_define_module("LinuxStat") ;
42
59
  VALUE _sysconf = rb_define_module_under(_linux_stat, "Sysconf") ;
@@ -50,4 +67,11 @@ void Init_sysconf() {
50
67
  rb_define_module_function(_sysconf, "stream_max", getStreamMax, 0) ;
51
68
  rb_define_module_function(_sysconf, "tty_name_max", getTTYNameMax, 0) ;
52
69
  rb_define_module_function(_sysconf, "posix_version", getPosixVersion, 0) ;
70
+
71
+ rb_define_module_function(_sysconf, "get_uid", getUID, 0) ;
72
+ rb_define_module_function(_sysconf, "get_gid", getGID, 0) ;
73
+ rb_define_module_function(_sysconf, "get_euid", getEUID, 0) ;
74
+
75
+ rb_define_module_function(_sysconf, "get_user", getUser, 0) ;
76
+ rb_define_module_function(_sysconf, "get_login", getUser, 0) ;
53
77
  }
@@ -18,3 +18,4 @@ require "linux_stat/filesystem"
18
18
  require "linux_stat/sysconf"
19
19
  require "linux_stat/kernel"
20
20
  require "linux_stat/process_info"
21
+ require 'linux_stat/user'
@@ -133,8 +133,6 @@ module LinuxStat
133
133
  end
134
134
  end
135
135
 
136
- alias release version
137
-
138
136
  # Reads maximum 1024 bytes from /proc/version and returns the string.
139
137
  # The output is also cached ; as changing the value in runtime is unexpected.
140
138
  def string
@@ -148,6 +146,7 @@ module LinuxStat
148
146
  @@tick ||= LinuxStat::Sysconf.sc_clk_tck
149
147
  end
150
148
 
149
+ alias release version
151
150
  alias clk_tck ticks
152
151
 
153
152
  private
@@ -127,9 +127,10 @@ module LinuxStat
127
127
  #
128
128
  # If the info isn't available it will return nil.
129
129
  def memory(pid = $$)
130
- _rss_anon = IO.readlines("/proc/#{pid}/status")
131
- .find { |x| x.start_with?('RssAnon') }
130
+ file = "/proc/#{pid}/status".freeze
131
+ return nil unless File.readable?(file)
132
132
 
133
+ _rss_anon = IO.readlines(file).find { |x| x.start_with?('RssAnon') }
133
134
  _rss_anon ? _rss_anon.split[1].to_i : nil
134
135
  end
135
136
 
@@ -144,7 +145,10 @@ module LinuxStat
144
145
  #
145
146
  # If the info isn't available it will return nil.
146
147
  def virtual_memory(pid = $$)
147
- _virtual_memory = IO.read("/proc/#{pid}/stat".freeze).split[22]
148
+ file = "/proc/#{pid}/stat".freeze
149
+ return nil unless File.readable?(file)
150
+
151
+ _virtual_memory = IO.read(file).split[22]
148
152
  _virtual_memory ? _virtual_memory.to_i.fdiv(1024).to_i : nil
149
153
  end
150
154
 
@@ -159,7 +163,10 @@ module LinuxStat
159
163
  #
160
164
  # If the info isn't available it will return nil.
161
165
  def resident_memory(pid = $$)
162
- _vm_rss = IO.readlines("/proc/#{pid}/status")
166
+ file = "/proc/#{pid}/status".freeze
167
+ return nil unless File.readable?(file)
168
+
169
+ _vm_rss = IO.readlines(file)
163
170
  .find { |x| x.start_with?('VmRSS') }
164
171
 
165
172
  _vm_rss ? _vm_rss.split[1].to_i : nil
@@ -301,6 +308,10 @@ module LinuxStat
301
308
  IO.read("/proc/#{pid}/stat".freeze).split[38].to_i
302
309
  end
303
310
 
311
+ # def owned_by
312
+
313
+ # end
314
+
304
315
  private
305
316
  def get_ticks
306
317
  @@ticks ||= Sysconf.sc_clk_tck
@@ -0,0 +1,258 @@
1
+ module LinuxStat
2
+ module User
3
+ class << self
4
+ # Returns an array of users as string
5
+ # For example:
6
+ # ["root", "bin", "daemon", "mail", "ftp", "http", "nobody"]
7
+ # But if the status isn't available it will return an empty Array.
8
+ def list
9
+ return [] unless passwd_readable?
10
+ passwd.map { |x| x[/.+?:/][0..-2].freeze }
11
+ end
12
+
13
+ # Returns all the Group ids directories as Hash.
14
+ # For example:
15
+ # {:root=>{:uid=>0, :gid=>0}, :bin=>{:uid=>1, :gid=>1}, :daemon=>{:uid=>2, :gid=>2}, :mail=>{:uid=>8, :gid=>12}, :ftp=>{:uid=>14, :gid=>11}}
16
+ #
17
+ # But if the status isn't available it will return an empty Hash.
18
+ def ids
19
+ return {} unless passwd_readable?
20
+ passwd.reduce({}) { |h, x|
21
+ splitted = x.split(?:)
22
+
23
+ h.merge!(splitted[0].to_sym => {
24
+ uid: splitted[2].to_i, gid: splitted[3].to_i
25
+ })
26
+ }
27
+ end
28
+
29
+ # Returns all the user IDs as Hash.
30
+ # For example:
31
+ # LinuxStat::User.uids
32
+ # => {:root=>0, :bin=>1, :daemon=>2, :mail=>8, :ftp=>14}
33
+ #
34
+ # But if the status isn't available it will return an empty Hash.
35
+ def uids
36
+ return {} unless passwd_readable?
37
+ passwd.reduce({}) { |h, x|
38
+ splitted = x.split(?:)
39
+ h.merge!(splitted[0].to_sym => splitted[2].to_i)
40
+ }
41
+ end
42
+
43
+ # Returns all the Group identifiers as Hash.
44
+ # For example:
45
+ # LinuxStat::User.gids
46
+ # => {:root=>0, :bin=>1, :daemon=>2, :mail=>12, :ftp=>11}
47
+ #
48
+ # But if the status isn't available it will return an empty Hash.
49
+ def gids
50
+ return {} unless passwd_readable?
51
+ passwd.reduce({}) { |h, x|
52
+ splitted = x.split(?:)
53
+ h.merge!(splitted[0].to_sym => splitted[3].to_i)
54
+ }
55
+ end
56
+
57
+ # Returns all the home directories as Hash.
58
+ # For example:
59
+ # LinuxStat::User.home_directories
60
+ # => {:root=>"/root", :bin=>"/", :daemon=>"/", :mail=>"/var/spool/mail", :ftp=>"/srv/ftp", :http=>"/srv/http", :nobody=>"/"}
61
+ #
62
+ # But if the status isn't available it will return an empty Hash.
63
+ def home_directories
64
+ return {} unless passwd_readable?
65
+ passwd.reduce({}) { |h, x|
66
+ splitted = x.split(?:)
67
+ h.merge!(splitted[0].to_sym => splitted[5])
68
+ }
69
+ end
70
+
71
+ # Returns the user ID as integer
72
+ # It directly calls LinuxStat::Sysconf.get_user
73
+ #
74
+ # It doesn't get affected with the assignment of USER environment variable.
75
+ def get_user
76
+ LinuxStat::Sysconf.get_user
77
+ end
78
+
79
+ # Returns the user ID as integer
80
+ # It directly calls LinuxStat::Sysconf.get_uid
81
+ def get_uid
82
+ LinuxStat::Sysconf.get_uid
83
+ end
84
+
85
+ # Returns the group ID as integer
86
+ # It directly calls LinuxStat::Sysconf.get_uid
87
+ def get_gid
88
+ LinuxStat::Sysconf.get_gid
89
+ end
90
+
91
+ # Returns the effective user ID as integer
92
+ # It directly calls LinuxStat::Sysconf.get_euid
93
+ def get_euid
94
+ LinuxStat::Sysconf.get_euid
95
+ end
96
+
97
+ # def usernames_by_uid(gid = get_uid)
98
+ # Where uid is the group id of the user. By default it's the uid of the current user.
99
+ #
100
+ # It returns an Array containing the username corresponding to the uid.
101
+ #
102
+ # For example:
103
+ # LinuxStat::User.usernames_by_uid(1001)
104
+ # => ["userx", "usery"]
105
+ #
106
+ # But if the info isn't available it will return an empty Array.
107
+ def usernames_by_uid(uid = get_uid)
108
+ return [] unless passwd_readable?
109
+
110
+ usernames = []
111
+ passwd.each do |x|
112
+ splitted = x.split(?:.freeze)
113
+ usernames << splitted[0] if splitted[2].to_i == uid
114
+ end
115
+ usernames
116
+ end
117
+
118
+ # def username_by_gid(gid = get_gid)
119
+ # Where gid is the group id of the user. By default it's the gid of the current user.
120
+ #
121
+ # It returns a String cotaining the username corresponding to the gid
122
+ # But if the info isn't available it will return an empty frozen String.
123
+ def username_by_gid(gid = get_gid)
124
+ return ''.freeze unless passwd_readable?
125
+
126
+ username = ''
127
+ passwd.each do |x|
128
+ splitted = x.split(?:.freeze)
129
+ if splitted[2].to_i == gid
130
+ username = splitted[0]
131
+ break
132
+ end
133
+ end
134
+ username
135
+ end
136
+
137
+ # gid_by_username(username = get_user)
138
+ # Where username is the username to look for, by default it is the current user.
139
+ #
140
+ # It returns the gid by the username.
141
+ # For example:
142
+ # LinuxStat::User.gid_by_username('root')
143
+ # => "0"
144
+ #
145
+ # The return type is Integer.
146
+ # But if user passed doesn't exist or if the info isn't available, it will return nil.
147
+ def gid_by_username(username = get_user)
148
+ return nil unless passwd_readable?
149
+
150
+ gid = nil
151
+ passwd.each do |x|
152
+ splitted = x.split(?:.freeze)
153
+ if splitted[0] == username
154
+ gid = splitted[3].to_i
155
+ break
156
+ end
157
+ end
158
+ gid
159
+ end
160
+
161
+ # uid_by_username(username = get_user)
162
+ # Where username is the username to look for, by default it is the current user.
163
+ #
164
+ # It returns the uid by the username.
165
+ # For example:
166
+ # LinuxStat::User.uid_by_username('root')
167
+ # => 0
168
+ #
169
+ # The return type is Integer.
170
+ # But if user passed doesn't exist or if the info isn't available, it will return nil.
171
+ def uid_by_username(username = get_user)
172
+ return nil unless passwd_readable?
173
+
174
+ uid = nil
175
+ passwd.each do |x|
176
+ splitted = x.split(?:.freeze)
177
+ if splitted[0] == username
178
+ uid = splitted[2].to_i
179
+ break
180
+ end
181
+ end
182
+ uid
183
+ end
184
+
185
+ # home_by_username(user = get_user)
186
+ # Where user is the name of the user.
187
+ # Returns the user's home. By default it returns the home of the current user.
188
+ #
189
+ # If the info isn't available, it will return ENV['HOME].to_s.freeze
190
+ def home_by_username(user = get_user)
191
+ return ENV['HOME'].to_s.freeze unless passwd_readable?
192
+
193
+ home = ''
194
+ passwd.each { |x|
195
+ splitted = x.split(?:)
196
+ if splitted[0] == user
197
+ home = splitted[5]
198
+ break
199
+ end
200
+ }
201
+ home
202
+ end
203
+
204
+ # home_by_uid(id = get_uid)
205
+ # Gets all the users home directory with user id.
206
+ # It returns an Array in this format:
207
+ # LinuxStat::User.homes_by_uid(1001)
208
+ # => ["/home/userx", "/home/usery"]
209
+ #
210
+ # Assuming both the users share same UID.
211
+ #
212
+ # If the info isn't available, it will return an empty Array.
213
+ def homes_by_uid(id = get_uid)
214
+ return [] unless passwd_readable?
215
+
216
+ home = []
217
+ passwd.each do |x|
218
+ splitted = x.split(?:.freeze)
219
+ home << splitted[5] if splitted[2].to_i == id
220
+ end
221
+ home
222
+ end
223
+
224
+ # home_by_gid(id = get_uid)
225
+ # Gets the home of the user corresponding to the GID.
226
+ # It returns a String in this format:
227
+ #
228
+ # Assuming both the users share same UID.
229
+ #
230
+ # If the info isn't available, it will return an empty frozen String.
231
+ def home_by_gid(id = get_gid)
232
+ return ''.freeze unless passwd_readable?
233
+
234
+ home = ''
235
+ passwd.each do |x|
236
+ splitted = x.split(?:.freeze)
237
+
238
+ if splitted[3].to_i == id
239
+ home = splitted[5]
240
+ break
241
+ end
242
+ end
243
+ home
244
+ end
245
+
246
+ private
247
+ def passwd
248
+ @@passwd_file ||= '/etc/passwd'.freeze
249
+ IO.readlines(@@passwd_file)
250
+ end
251
+
252
+ def passwd_readable?
253
+ @@passwd_file ||= '/etc/passwd'.freeze
254
+ @@passwd_readable ||= File.readable?(@@passwd_file)
255
+ end
256
+ end
257
+ end
258
+ end
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5.1"
3
3
  end
metadata CHANGED
@@ -1,19 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2020-12-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Efficient linux system reporting gem. Linux Only | Efficient | Reliable
13
+ description: Linux only, efficient linux system utilization reporting and system monitoring
14
+ gem
14
15
  email:
15
16
  - souravgoswami@protonmail.com
16
- executables: []
17
+ executables:
18
+ - linuxstat.rb
17
19
  extensions:
18
20
  - ext/fs_stat/extconf.rb
19
21
  - ext/sysconf/extconf.rb
@@ -21,10 +23,11 @@ extensions:
21
23
  extra_rdoc_files:
22
24
  - README.md
23
25
  files:
26
+ - LICENSE.txt
24
27
  - README.md
25
28
  - bin/console
26
- - bin/linuxstat.rb
27
29
  - bin/setup
30
+ - exe/linuxstat.rb
28
31
  - ext/fs_stat/extconf.rb
29
32
  - ext/fs_stat/fs_stat.c
30
33
  - ext/sysconf/extconf.rb
@@ -44,6 +47,7 @@ files:
44
47
  - lib/linux_stat/process.rb
45
48
  - lib/linux_stat/process_info.rb
46
49
  - lib/linux_stat/swap.rb
50
+ - lib/linux_stat/user.rb
47
51
  - lib/linux_stat/version.rb
48
52
  homepage: https://github.com/Souravgoswami/linux_stat/
49
53
  licenses: