Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions lib/linux_admin/fstab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,95 @@ class FSTabEntry < LinuxAdmin
attr_accessor :mount_options
attr_accessor :dumpable
attr_accessor :fsck_order
attr_accessor :comment

def initialize(args = {})
@device = args[:device]
@mount_point = args[:mount_point]
@fs_type = args[:fs_type]
@mount_options = args[:mount_options]
@dumpable = args[:dumpable]
@fsck_order = args[:fsck_order]
@dumpable = args[:dumpable].to_i unless args[:dumpable].nil?
@fsck_order = args[:fsck_order].to_i unless args[:fsck_order].nil?
@comment = args[:comment]
end

def self.from_line(fstab_line)
columns = fstab_line.chomp.split
columns, comment = fstab_line.split('#')
comment = "##{comment}" unless comment.blank?
columns = columns.chomp.split

FSTabEntry.new :device => columns[0],
:mount_point => columns[1],
:fs_type => columns[2],
:mount_options => columns[3],
:dumpable => columns[4].to_i,
:fsck_order => columns[5].to_i

:dumpable => columns[4],
:fsck_order => columns[5],
:comment => comment
end

def has_content?
!self.columns.first.nil?
end

def columns
[self.device, self.mount_point, self.fs_type,
self.mount_options, self.dumpable, self.fsck_order, self.comment]
end

def column_lengths
self.columns.collect { |c| c ? c.size : 0 }
end

def formatted_columns(max_lengths)
self.columns.collect.
with_index { |col, i| col.to_s.rjust(max_lengths[i]) }.join(" ")
end
end

class FSTab < LinuxAdmin
include Singleton

attr_accessor :entries
attr_accessor :maximum_column_lengths

def initialize
refresh
end

def write!
content = ''
comment_index = 0
@entries.each do |entry|
content += "#{entry.device} #{entry.mount_point} #{entry.fs_type} #{entry.mount_options} #{entry.dumpable} #{entry.fsck_order}\n"
if entry.has_content?
content << entry.formatted_columns(@maximum_column_lengths) << "\n"
else
content << "#{entry.comment}"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this loop doing? I can't understand the else condition specifically.

end

File.write('/etc/fstab', content)
self
end

private

def read
File.read('/etc/fstab').lines.find_all {|line| !line.blank? && !line.strip.starts_with?("#")}
File.read('/etc/fstab').lines
end

def refresh
@entries =
read.collect { |line|
FSTabEntry.from_line line
}
@entries = []
@maximum_column_lengths = Array.new(7, 0) # # of columns
read.each do |line|
entry = FSTabEntry.from_line(line)
@entries << entry

lengths = entry.column_lengths
lengths.each_index do |i|
@maximum_column_lengths[i] =
lengths[i] if lengths[i] > @maximum_column_lengths[i]
end
end
end
end
end
42 changes: 25 additions & 17 deletions spec/fstab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,47 @@
Singleton.send :__init__, LinuxAdmin::FSTab
end

it "newline, single spaces, tab" do
it "has newline, single spaces, tab" do
fstab = <<eos



eos
File.should_receive(:read).with('/etc/fstab').and_return(fstab)
LinuxAdmin::FSTab.instance.entries.size.should == 0
LinuxAdmin::FSTab.instance.entries.size.should == 3
LinuxAdmin::FSTab.instance.entries.any? { |e| e.has_content? }.should be_false
end

it "creates FSTabEntry for each line in fstab" do
fstab = <<eos
# Comment, indented comment, comment with device information
# /dev/sda1 / ext4 defaults 1 1
# /dev/sda1 / ext4 defaults 1 1

/dev/sda1 / ext4 defaults 1 1
/dev/sda2 swap swap defaults 0 0
eos
File.should_receive(:read).with('/etc/fstab').and_return(fstab)
entries = LinuxAdmin::FSTab.instance.entries
entries.size.should == 2
entries.size.should == 6

entries[0].device.should == '/dev/sda1'
entries[0].mount_point.should == '/'
entries[0].fs_type.should == 'ext4'
entries[0].mount_options.should == 'defaults'
entries[0].dumpable.should == 1
entries[0].fsck_order.should == 1
entries[0].comment.should == "# Comment, indented comment, comment with device information\n"
entries[1].comment.should == "# /dev/sda1 / ext4 defaults 1 1\n"
entries[2].comment.should == "# /dev/sda1 / ext4 defaults 1 1\n"
entries[3].comment.should == nil
entries[4].device.should == '/dev/sda1'
entries[4].mount_point.should == '/'
entries[4].fs_type.should == 'ext4'
entries[4].mount_options.should == 'defaults'
entries[4].dumpable.should == 1
entries[4].fsck_order.should == 1

entries[1].device.should == '/dev/sda2'
entries[1].mount_point.should == 'swap'
entries[1].fs_type.should == 'swap'
entries[1].mount_options.should == 'defaults'
entries[1].dumpable.should == 0
entries[1].fsck_order.should == 0
entries[5].device.should == '/dev/sda2'
entries[5].mount_point.should == 'swap'
entries[5].fs_type.should == 'swap'
entries[5].mount_options.should == 'defaults'
entries[5].dumpable.should == 0
entries[5].fsck_order.should == 0
end

describe "#write!" do
Expand All @@ -54,10 +60,12 @@
entry.mount_options = 'defaults'
entry.dumpable = 1
entry.fsck_order = 1
entry.comment = "# more"
LinuxAdmin::FSTab.any_instance.stub(:refresh) # don't read /etc/fstab
LinuxAdmin::FSTab.instance.entries = [entry]
LinuxAdmin::FSTab.instance.maximum_column_lengths = [9, 1, 4, 8, 1, 1, 1]
LinuxAdmin::FSTab.instance.entries = [entry]

File.should_receive(:write).with('/etc/fstab', "/dev/sda1 / ext4 defaults 1 1\n")
File.should_receive(:write).with('/etc/fstab', "/dev/sda1 / ext4 defaults 1 1 # more\n")
LinuxAdmin::FSTab.instance.write!
end
end
Expand Down