kanrisuru 0.16.3 → 0.16.4

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: af1a581f630e30f0d4e45864f8adba335b5a4c39ba77ca81c3de97e62d1b63bc
4
- data.tar.gz: b34344cd94480bac72c5c78db9fe2b331884366a68c30d81b001c4f6d2d648c8
3
+ metadata.gz: e33daa1aa8d51a7f8a9e6efb4e77933ef5a71e1ce3e428dabc7f44468bde79b3
4
+ data.tar.gz: 8cbc4f168fdf88bdd22c8fb237018c65427010356ed8d1bfe9771f2e8ecfc0ca
5
5
  SHA512:
6
- metadata.gz: 437e92131a90f509f116067b7f0f5fb311c7ddc835d554732479d3895d1ce23b63fec937df6dc6d2ecb048deaaff1411c0c9956019c4d2f26b10e0cfeb781d86
7
- data.tar.gz: 100a3f6f404a6665cf56ab0b6159724f775f8e25c76ab103ba491764916a067d4ce55a4043db671f58251dfed7ba25d09fa5c6b63efee7ae5d9a8facfc612248
6
+ metadata.gz: 992f48fbc7c55a67cd2bfa649cba991620ab0ee6aa97d4c49de918d099a1e9963a30dea99aa71fe738ad39982462e731caf4ab121263a0bbf4870a68ae5a1e83
7
+ data.tar.gz: 208b2389785ed015055a3c67b49b433dae7d5388ee32334af5d74f7a2f94db19900a1dba4396b02edc675987fadead7b90d3f961e3eddb9e5e7c35c946ac28fe
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## Kanrisuru 0.16.4 (December 25, 2021) ##
2
+ * Refactor `Kanrisuru::Fstab::Entry` and `Kanrisuru::Fstab::Options` classes into separate files.
3
+
1
4
  ## Kanrisuru 0.16.3 (December 25, 2021) ##
2
5
  * Refactor `Kanrisuru::Mode::Permission` class into separate file.
3
6
 
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Remote
5
+ class Fstab
6
+ class Entry
7
+ attr_reader :device, :uuid, :invalid, :label, :type, :opts, :freq, :passno
8
+ attr_accessor :mount_point
9
+
10
+ def initialize(opts = {})
11
+ @host = opts[:host]
12
+ @line = opts[:line]
13
+
14
+ @default = nil
15
+
16
+ @device = opts[:device] || nil
17
+ @opts = opts[:opts] || nil
18
+ @label = opts[:label] || nil
19
+ @uuid = opts[:uuid] || nil
20
+ @mount_point = opts[:mount_point] || nil
21
+ @type = opts[:type] || nil
22
+ @freq = opts[:freq] || nil
23
+ @passno = opts[:passno] || nil
24
+
25
+ @changed = false
26
+
27
+ @ucount = 0
28
+ @special = false
29
+ @invalid = false
30
+
31
+ if Kanrisuru::Util.present?(@line) && @line.instance_of?(String)
32
+ parse_line!
33
+ elsif (Kanrisuru::Util.present?(@opts) && @opts.instance_of?(String)) || @opts.instance_of?(Hash)
34
+ @opts = Kanrisuru::Remote::Fstab::Options.new(@type, @opts)
35
+ end
36
+ end
37
+
38
+ def inspect
39
+ str = '#<Kanrisuru::Remote::Fstab::Entry:0x%<object_id>s ' \
40
+ '@line=%<line>s @device=%<device>s @label=%<label>s' \
41
+ '@uuid=%<uuid>s @freq=%<freq>s @pasno=%<passno>s' \
42
+ '@opts=%<opts>s}>'
43
+
44
+ format(
45
+ str,
46
+ object_id: object_id,
47
+ line: @line,
48
+ device: @device,
49
+ label: @label,
50
+ uuid: @uuid,
51
+ freq: @freq,
52
+ passno: @passno,
53
+ opts: @opts.inspect
54
+ )
55
+ end
56
+
57
+ def valid?
58
+ !@invalid
59
+ end
60
+
61
+ def to_s(override = nil)
62
+ mode = override || @default
63
+
64
+ case mode
65
+ when 'uuid'
66
+ "UUID=#{@uuid} #{@mount_point} #{@type} #{@opts} #{@freq} #{@passno}"
67
+ when 'label'
68
+ "LABEL=#{@label} #{@mount_point} #{@type} #{@opts} #{@freq} #{@passno}"
69
+ else
70
+ "#{@device} #{@mount_point} #{@type} #{@opts} #{@freq} #{@passno}"
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def parse_line!
77
+ fsline, mp, @type, opts, freq, passno = @line.split
78
+
79
+ @mount_point = mp
80
+ @freq = freq || '0'
81
+ @passno = passno || '0'
82
+
83
+ @opts = Fstab::Options.new(@type, opts)
84
+
85
+ case @line
86
+ when /^\s*LABEL=/
87
+ @default = 'label'
88
+ parse_label(fsline)
89
+ when /^\s*UUID=/
90
+ @default = 'uuid'
91
+ parse_uuid(fsline)
92
+ when %r{^\s*/dev}
93
+ @default = 'dev'
94
+ parse_dev(fsline)
95
+ else
96
+ # TODO: somewhat risky to assume that everything else
97
+ # can be considered a special device, but validating this
98
+ # is really tricky.
99
+ @special = true
100
+ @device = fsline
101
+ end
102
+
103
+ # Fstab entries not matching real devices have device unknown
104
+ @invalid = (@line.split.count != 6) # invalid entry if < 6 columns
105
+
106
+ if (@uuid.nil? && @label.nil? && !@special) ||
107
+ @device =~ /^unknown_/ ||
108
+ (!@host.inode?(@device) && !@special)
109
+ @invalid = true
110
+ @ucount += 1
111
+ end
112
+
113
+ @invalid = true unless @freq =~ /0|1|2/ && @passno =~ /0|1|2/
114
+ end
115
+
116
+ def parse_label(fsline)
117
+ @label = fsline.split('=').last.strip.chomp
118
+ path = @host.realpath("/dev/disk/by-label/#{@label}").path
119
+
120
+ @device = begin
121
+ "/dev/#{path.split('/').last}"
122
+ rescue StandardError
123
+ "unknown_#{@ucount}"
124
+ end
125
+
126
+ result = @host.blkid(device: @device)
127
+ @uuid = result.success? ? result[0].uuid : nil
128
+ end
129
+
130
+ def parse_uuid(fsline)
131
+ @uuid = fsline.split('=').last.strip.chomp
132
+ path = @host.realpath("/dev/disk/by-uuid/#{uuid}").path
133
+
134
+ @device = begin
135
+ "/dev/#{path.split('/').last}"
136
+ rescue StandardError
137
+ "unknown_#{@ucount}"
138
+ end
139
+
140
+ result = @host.blkid(device: @device)
141
+ @label = result.success? ? result[0].label : nil
142
+ end
143
+
144
+ def parse_dev(fsline)
145
+ @device = fsline
146
+ result = @host.blkid(device: @device)
147
+
148
+ @label = result.success? ? result[0].label : nil
149
+ @uuid = result.success? ? result[0].uuid : nil
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Remote
5
+ class Fstab
6
+ class Options
7
+ def initialize(type, opts)
8
+ @type = type
9
+ @valid = false
10
+
11
+ if opts.instance_of?(String)
12
+ @opts = parse_opts(opts)
13
+ elsif opts.instance_of?(Hash)
14
+ @opts = opts.transform_keys(&:to_s)
15
+ else
16
+ raise ArgumentError, 'Invalid option type'
17
+ end
18
+
19
+ validate_opts!
20
+ end
21
+
22
+ def inspect
23
+ format('<Kanrisuru::Remote::Fstab::Options:0x%<object_id>s @opts=%<opts>s @type=%<type>s>',
24
+ object_id: object_id, opts: @opts, type: @type)
25
+ end
26
+
27
+ def [](option)
28
+ @opts[option]
29
+ end
30
+
31
+ def []=(option, value)
32
+ option = option.to_s
33
+
34
+ unless Kanrisuru::Remote::Fstab::Options.option_exists?(option, @type)
35
+ raise ArgumentError,
36
+ "Invalid option: #{option} for #{@type} file system."
37
+ end
38
+
39
+ unless Kanrisuru::Remote::Fstab::Options.valid_option?(option, value, @type)
40
+ raise ArgumentError,
41
+ "Invalid option value: #{value} for #{option} on #{@type} file system."
42
+ end
43
+
44
+ @opts[option] = value
45
+ end
46
+
47
+ def to_s
48
+ string = ''
49
+ opts_length = @opts.length
50
+
51
+ @opts.each_with_index do |(key, value), index|
52
+ append_comma = true
53
+
54
+ if value == true
55
+ string += key.to_s
56
+ elsif value.instance_of?(String) || value.instance_of?(Integer) || value.instance_of?(Float)
57
+ string += "#{key}=#{value}"
58
+ else
59
+ append_comma = false
60
+ end
61
+
62
+ string += ',' if append_comma && index < opts_length - 1
63
+ end
64
+
65
+ string
66
+ end
67
+
68
+ def to_h
69
+ @opts
70
+ end
71
+
72
+ def self.option_exists?(value, type = nil)
73
+ value = value.to_sym
74
+ type = type ? type.to_sym : nil
75
+
76
+ common = Kanrisuru::Util::FsMountOpts[:common]
77
+ fs_opts = Kanrisuru::Util::FsMountOpts[type]
78
+
79
+ common.key?(value) ||
80
+ fs_opts&.key?(value)
81
+ end
82
+
83
+ def self.valid_option?(value, field, type = nil)
84
+ value = value.to_sym
85
+ type = type ? type.to_sym : nil
86
+
87
+ common = Kanrisuru::Util::FsMountOpts[:common]
88
+ fs_opts = Kanrisuru::Util::FsMountOpts[type]
89
+
90
+ if common.key?(value)
91
+ case common[value]
92
+ when 'boolean'
93
+ [true, false].include?(field)
94
+ when 'value'
95
+ field.instance_of?(String) || field.instance_of?(Float) || field.instance_of?(Integer)
96
+ else
97
+ false
98
+ end
99
+ elsif fs_opts&.key?(value)
100
+ case fs_opts[value]
101
+ when 'boolean'
102
+ [true, false].include?(field)
103
+ when 'value'
104
+ field.instance_of?(String) || field.instance_of?(Float) || field.instance_of?(Integer)
105
+ else
106
+ false
107
+ end
108
+ else
109
+ raise ArgumentError, 'Invalid option'
110
+ end
111
+ end
112
+
113
+ private
114
+
115
+ def validate_opts!
116
+ @opts.each do |key, value|
117
+ unless Kanrisuru::Remote::Fstab::Options.valid_option?(key, value, @type)
118
+ raise ArgumentError, "Invalid option: #{key} for #{@type}"
119
+ end
120
+ end
121
+
122
+ @valid = true
123
+ end
124
+
125
+ def parse_opts(string)
126
+ opts = {}
127
+
128
+ options = string.split(',')
129
+ options.each do |option|
130
+ if option.include?('=')
131
+ opt, value = option.split('=')
132
+ opts[opt] = value
133
+ else
134
+ opts[option] = true
135
+ end
136
+ end
137
+
138
+ opts
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'fstab/entry'
4
+ require_relative 'fstab/options'
5
+
3
6
  module Kanrisuru
4
7
  module Remote
5
8
  class Fstab
@@ -133,289 +136,6 @@ module Kanrisuru
133
136
  }
134
137
  end
135
138
  end
136
-
137
- class Entry
138
- attr_reader :device, :uuid, :invalid, :label, :type, :opts, :freq, :passno
139
- attr_accessor :mount_point
140
-
141
- def initialize(opts = {})
142
- @host = opts[:host]
143
- @line = opts[:line]
144
-
145
- @default = nil
146
-
147
- @device = opts[:device] || nil
148
- @opts = opts[:opts] || nil
149
- @label = opts[:label] || nil
150
- @uuid = opts[:uuid] || nil
151
- @mount_point = opts[:mount_point] || nil
152
- @type = opts[:type] || nil
153
- @freq = opts[:freq] || nil
154
- @passno = opts[:passno] || nil
155
-
156
- @changed = false
157
-
158
- @ucount = 0
159
- @special = false
160
- @invalid = false
161
-
162
- if Kanrisuru::Util.present?(@line) && @line.instance_of?(String)
163
- parse_line!
164
- elsif (Kanrisuru::Util.present?(@opts) && @opts.instance_of?(String)) || @opts.instance_of?(Hash)
165
- @opts = Kanrisuru::Remote::Fstab::Options.new(@type, @opts)
166
- end
167
- end
168
-
169
- def inspect
170
- str = '#<Kanrisuru::Remote::Fstab::Entry:0x%<object_id>s ' \
171
- '@line=%<line>s @device=%<device>s @label=%<label>s' \
172
- '@uuid=%<uuid>s @freq=%<freq>s @pasno=%<passno>s' \
173
- '@opts=%<opts>s}>'
174
-
175
- format(
176
- str,
177
- object_id: object_id,
178
- line: @line,
179
- device: @device,
180
- label: @label,
181
- uuid: @uuid,
182
- freq: @freq,
183
- passno: @passno,
184
- opts: @opts.inspect
185
- )
186
- end
187
-
188
- def valid?
189
- !@invalid
190
- end
191
-
192
- def to_s(override = nil)
193
- mode = override || @default
194
-
195
- case mode
196
- when 'uuid'
197
- "UUID=#{@uuid} #{@mount_point} #{@type} #{@opts} #{@freq} #{@passno}"
198
- when 'label'
199
- "LABEL=#{@label} #{@mount_point} #{@type} #{@opts} #{@freq} #{@passno}"
200
- else
201
- "#{@device} #{@mount_point} #{@type} #{@opts} #{@freq} #{@passno}"
202
- end
203
- end
204
-
205
- private
206
-
207
- def parse_line!
208
- fsline, mp, @type, opts, freq, passno = @line.split
209
-
210
- @mount_point = mp
211
- @freq = freq || '0'
212
- @passno = passno || '0'
213
-
214
- @opts = Fstab::Options.new(@type, opts)
215
-
216
- case @line
217
- when /^\s*LABEL=/
218
- @default = 'label'
219
- parse_label(fsline)
220
- when /^\s*UUID=/
221
- @default = 'uuid'
222
- parse_uuid(fsline)
223
- when %r{^\s*/dev}
224
- @default = 'dev'
225
- parse_dev(fsline)
226
- else
227
- # TODO: somewhat risky to assume that everything else
228
- # can be considered a special device, but validating this
229
- # is really tricky.
230
- @special = true
231
- @device = fsline
232
- end
233
-
234
- # Fstab entries not matching real devices have device unknown
235
- @invalid = (@line.split.count != 6) # invalid entry if < 6 columns
236
-
237
- if (@uuid.nil? && @label.nil? && !@special) ||
238
- @device =~ /^unknown_/ ||
239
- (!@host.inode?(@device) && !@special)
240
- @invalid = true
241
- @ucount += 1
242
- end
243
-
244
- @invalid = true unless @freq =~ /0|1|2/ && @passno =~ /0|1|2/
245
- end
246
-
247
- def parse_label(fsline)
248
- @label = fsline.split('=').last.strip.chomp
249
- path = @host.realpath("/dev/disk/by-label/#{@label}").path
250
-
251
- @device = begin
252
- "/dev/#{path.split('/').last}"
253
- rescue StandardError
254
- "unknown_#{@ucount}"
255
- end
256
-
257
- result = @host.blkid(device: @device)
258
- @uuid = result.success? ? result[0].uuid : nil
259
- end
260
-
261
- def parse_uuid(fsline)
262
- @uuid = fsline.split('=').last.strip.chomp
263
- path = @host.realpath("/dev/disk/by-uuid/#{uuid}").path
264
-
265
- @device = begin
266
- "/dev/#{path.split('/').last}"
267
- rescue StandardError
268
- "unknown_#{@ucount}"
269
- end
270
-
271
- result = @host.blkid(device: @device)
272
- @label = result.success? ? result[0].label : nil
273
- end
274
-
275
- def parse_dev(fsline)
276
- @device = fsline
277
- result = @host.blkid(device: @device)
278
-
279
- @label = result.success? ? result[0].label : nil
280
- @uuid = result.success? ? result[0].uuid : nil
281
- end
282
- end
283
-
284
- class Options
285
- def initialize(type, opts)
286
- @type = type
287
- @valid = false
288
-
289
- if opts.instance_of?(String)
290
- @opts = parse_opts(opts)
291
- elsif opts.instance_of?(Hash)
292
- @opts = opts.transform_keys(&:to_s)
293
- else
294
- raise ArgumentError, 'Invalid option type'
295
- end
296
-
297
- validate_opts!
298
- end
299
-
300
- def inspect
301
- format('<Kanrisuru::Remote::Fstab::Options:0x%<object_id>s @opts=%<opts>s @type=%<type>s>',
302
- object_id: object_id, opts: @opts, type: @type)
303
- end
304
-
305
- def [](option)
306
- @opts[option]
307
- end
308
-
309
- def []=(option, value)
310
- option = option.to_s
311
-
312
- unless Kanrisuru::Remote::Fstab::Options.option_exists?(option, @type)
313
- raise ArgumentError,
314
- "Invalid option: #{option} for #{@type} file system."
315
- end
316
-
317
- unless Kanrisuru::Remote::Fstab::Options.valid_option?(option, value, @type)
318
- raise ArgumentError,
319
- "Invalid option value: #{value} for #{option} on #{@type} file system."
320
- end
321
-
322
- @opts[option] = value
323
- end
324
-
325
- def to_s
326
- string = ''
327
- opts_length = @opts.length
328
-
329
- @opts.each_with_index do |(key, value), index|
330
- append_comma = true
331
-
332
- if value == true
333
- string += key.to_s
334
- elsif value.instance_of?(String) || value.instance_of?(Integer) || value.instance_of?(Float)
335
- string += "#{key}=#{value}"
336
- else
337
- append_comma = false
338
- end
339
-
340
- string += ',' if append_comma && index < opts_length - 1
341
- end
342
-
343
- string
344
- end
345
-
346
- def to_h
347
- @opts
348
- end
349
-
350
- def self.option_exists?(value, type = nil)
351
- value = value.to_sym
352
- type = type ? type.to_sym : nil
353
-
354
- common = Kanrisuru::Util::FsMountOpts[:common]
355
- fs_opts = Kanrisuru::Util::FsMountOpts[type]
356
-
357
- common.key?(value) ||
358
- fs_opts&.key?(value)
359
- end
360
-
361
- def self.valid_option?(value, field, type = nil)
362
- value = value.to_sym
363
- type = type ? type.to_sym : nil
364
-
365
- common = Kanrisuru::Util::FsMountOpts[:common]
366
- fs_opts = Kanrisuru::Util::FsMountOpts[type]
367
-
368
- if common.key?(value)
369
- case common[value]
370
- when 'boolean'
371
- [true, false].include?(field)
372
- when 'value'
373
- field.instance_of?(String) || field.instance_of?(Float) || field.instance_of?(Integer)
374
- else
375
- false
376
- end
377
- elsif fs_opts&.key?(value)
378
- case fs_opts[value]
379
- when 'boolean'
380
- [true, false].include?(field)
381
- when 'value'
382
- field.instance_of?(String) || field.instance_of?(Float) || field.instance_of?(Integer)
383
- else
384
- false
385
- end
386
- else
387
- raise ArgumentError, 'Invalid option'
388
- end
389
- end
390
-
391
- private
392
-
393
- def validate_opts!
394
- @opts.each do |key, value|
395
- unless Kanrisuru::Remote::Fstab::Options.valid_option?(key, value, @type)
396
- raise ArgumentError, "Invalid option: #{key} for #{@type}"
397
- end
398
- end
399
-
400
- @valid = true
401
- end
402
-
403
- def parse_opts(string)
404
- opts = {}
405
-
406
- options = string.split(',')
407
- options.each do |option|
408
- if option.include?('=')
409
- opt, value = option.split('=')
410
- opts[opt] = value
411
- else
412
- opts[option] = true
413
- end
414
- end
415
-
416
- opts
417
- end
418
- end
419
139
  end
420
140
  end
421
141
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.16.3'
4
+ VERSION = '0.16.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanrisuru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.3
4
+ version: 0.16.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Mammina
@@ -428,6 +428,8 @@ files:
428
428
  - lib/kanrisuru/remote/env.rb
429
429
  - lib/kanrisuru/remote/file.rb
430
430
  - lib/kanrisuru/remote/fstab.rb
431
+ - lib/kanrisuru/remote/fstab/entry.rb
432
+ - lib/kanrisuru/remote/fstab/options.rb
431
433
  - lib/kanrisuru/remote/host.rb
432
434
  - lib/kanrisuru/remote/memory.rb
433
435
  - lib/kanrisuru/remote/os.rb