Sha256: f35376157afc69896e0bf90d12d49ef6adf9c38263a285d979ad640d20cff2ec

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

# LinuxAdmin fstab Representation
#
# Copyright (C) 2013 Red Hat Inc.
# Licensed under the MIT License

require 'singleton'

class LinuxAdmin
  class FSTabEntry < LinuxAdmin
    attr_accessor :device
    attr_accessor :mount_point
    attr_accessor :fs_type
    attr_accessor :mount_options
    attr_accessor :dumpable
    attr_accessor :fsck_order

    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]
    end

    def self.from_line(fstab_line)
      columns = fstab_line.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
      
    end
  end

  class FSTab < LinuxAdmin
    include Singleton

    attr_accessor :entries

    def initialize
      refresh
    end

    def write!
      content = ''
      @entries.each do |entry|
        content += "#{entry.device} #{entry.mount_point} #{entry.fs_type} #{entry.mount_options} #{entry.dumpable} #{entry.fsck_order}\n"
      end
      File.write('/etc/fstab', content)
      self
    end

    private

    def read
      contents = File.read('/etc/fstab')
      contents = contents.lines.to_a
      contents.reject! { |line|
        first_char = line.strip[0]
        first_char == '#' || first_char =~ /\s/
      }
      contents
    end

    def refresh
      @entries = 
        read.collect { |line|
          FSTabEntry.from_line line
        }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
linux_admin-0.2.1 lib/linux_admin/fstab.rb