Skip to content

Commit

Permalink
unify unix file handling
Browse files Browse the repository at this point in the history
i.e. anything that doesnt understand `echo -n` for speedup
  • Loading branch information
arlimus committed Jan 28, 2016
1 parent 4377e7b commit 40a80bb
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 88 deletions.
2 changes: 1 addition & 1 deletion lib/train/extras.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module Train::Extras
autoload :CommandWrapper, 'train/extras/command_wrapper'
autoload :FileCommon, 'train/extras/file_common'
autoload :AixFile, 'train/extras/file_aix'
autoload :UnixFile, 'train/extras/file_unix'
autoload :LinuxFile, 'train/extras/file_linux'
autoload :SolarisFile, 'train/extras/file_solaris'
autoload :WindowsFile, 'train/extras/file_windows'
autoload :OSCommon, 'train/extras/os_common'
autoload :Stat, 'train/extras/stat'
Expand Down
13 changes: 1 addition & 12 deletions lib/train/extras/file_aix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@
require 'train/extras/stat'

module Train::Extras
class AixFile < LinuxFile
def content
@content ||= case
when !exist?, directory?
nil
when size.nil?, size == 0
''
else
@backend.run_command("cat #{@spath}").stdout || ''
end
end

class AixFile < UnixFile
def link_path
return nil unless symlink?
@link_path ||= (
Expand Down
56 changes: 1 addition & 55 deletions lib/train/extras/file_linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
require 'train/extras/stat'

module Train::Extras
class LinuxFile < FileCommon
attr_reader :path
def initialize(backend, path)
@backend = backend
@path = path
@spath = Shellwords.escape(@path)
end

class LinuxFile < UnixFile
def content
return @content if defined?(@content)
@content = @backend.run_command(
Expand All @@ -22,52 +15,5 @@ def content
@content = nil if directory? or size.nil? or size > 0
@content
end

def exist?
@exist ||= (
@backend.run_command("test -e #{@spath}")
.exit_status == 0
)
end

def link_target
return @link_target if defined? @link_target
return @link_target = nil if link_path.nil?
@link_target = @backend.file(link_path)
end

def link_path
return nil unless symlink?
@link_path ||= (
@backend.run_command("readlink #{@spath}").stdout.chomp
)
end

def mounted
@mounted ||= (
@backend.run_command("mount | grep -- ' on #{@spath}'")
)
end

%w{
type mode owner group mtime size selinux_label
}.each do |field|
define_method field.to_sym do
stat[field.to_sym]
end
end

def product_version
nil
end

def file_version
nil
end

def stat
return @stat if defined?(@stat)
@stat = Train::Extras::Stat.stat(@spath, @backend)
end
end
end
18 changes: 0 additions & 18 deletions lib/train/extras/file_solaris.rb

This file was deleted.

75 changes: 75 additions & 0 deletions lib/train/extras/file_unix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# encoding: utf-8
# author: Dominik Richter
# author: Christoph Hartmann

require 'shellwords'
require 'train/extras/stat'

module Train::Extras
class UnixFile < FileCommon
attr_reader :path
def initialize(backend, path)
@backend = backend
@path = path
@spath = Shellwords.escape(@path)
end

def content
@content ||= case
when !exist?, directory?
nil
when size.nil?, size == 0
''
else
@backend.run_command("cat #{@spath}").stdout || ''
end
end

def exist?
@exist ||= (
@backend.run_command("test -e #{@spath}")
.exit_status == 0
)
end

def link_target
return @link_target if defined? @link_target
return @link_target = nil if link_path.nil?
@link_target = @backend.file(link_path)
end

def link_path
return nil unless symlink?
@link_path ||= (
@backend.run_command("readlink #{@spath}").stdout.chomp
)
end

def mounted
@mounted ||= (
@backend.run_command("mount | grep -- ' on #{@spath}'")
)
end

%w{
type mode owner group mtime size selinux_label
}.each do |field|
define_method field.to_sym do
stat[field.to_sym]
end
end

def product_version
nil
end

def file_version
nil
end

def stat
return @stat if defined?(@stat)
@stat = Train::Extras::Stat.stat(@spath, @backend)
end
end
end
4 changes: 2 additions & 2 deletions lib/train/transports/ssh_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def os

def file(path)
@files[path] ||= \
if os[:family] == 'aix'
if os.aix?
AixFile.new(self, path)
elsif os.solaris?
SolarisFile.new(self, path)
UnixFile.new(self, path)
else
LinuxFile.new(self, path)
end
Expand Down

0 comments on commit 40a80bb

Please sign in to comment.