Class: DRbFileServer::FileX

Inherits:
Object
  • Object
show all
Defined in:
lib/drb_fileserver.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = '.') ⇒ FileX

Returns a new instance of FileX.



15
16
17
# File 'lib/drb_fileserver.rb', line 15

def initialize(path='.')
  @path = path
end

Instance Method Details

#chmod(permissions, filename) ⇒ Object



19
20
21
# File 'lib/drb_fileserver.rb', line 19

def chmod(permissions, filename)
  FileUtils.chmod permissions, File.join(@path, filename)
end

#cp(path, path2) ⇒ Object



23
24
25
26
# File 'lib/drb_fileserver.rb', line 23

def cp(path, path2)
  #puts 'cp: ' + [File.join(@path, path), File.join(@path, path2)].inspect
  FileUtils.cp File.join(@path, path), File.join(@path, path2)
end

#directory?(filename) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/drb_fileserver.rb', line 28

def directory?(filename)
  File.directory? File.join(@path, filename)
end

#exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/drb_fileserver.rb', line 32

def exists?(filename)
  File.exists? File.join(@path, filename)
end

#glob(s) ⇒ Object



36
37
38
# File 'lib/drb_fileserver.rb', line 36

def glob(s)
  Dir.glob(File.join(@path, s))
end

#ls(rawpath) ⇒ Object

path can include a wildcard and the switch -ltr



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/drb_fileserver.rb', line 42

def ls(rawpath)

  path, switch = rawpath.split(/\s+/,2)
  wildcard = path.include?('*') ? '' : '*'
  path = File.join(path, wildcard) if File.exists? File.join(@path, path)

  a = Dir[File.join(@path, path)].map do |x|

    File.basename(x)
  end

  if switch == '-ltr' then
    a.sort_by {|x| File.mtime(File.join(@path, File.dirname(path), x)) }
  else
    a
  end

end

#mkdir(name) ⇒ Object



61
62
63
# File 'lib/drb_fileserver.rb', line 61

def mkdir(name)
  FileUtils.mkdir File.join(@path, name)
end

#mkdir_p(path) ⇒ Object



65
66
67
# File 'lib/drb_fileserver.rb', line 65

def mkdir_p(path)
  FileUtils.mkdir_p File.join(@path, path)
end

#mv(path, path2) ⇒ Object



69
70
71
# File 'lib/drb_fileserver.rb', line 69

def mv(path, path2)
  FileUtils.mv File.join(@path, path), File.join(@path, path2)
end

#read(filename) ⇒ Object



73
74
75
# File 'lib/drb_fileserver.rb', line 73

def read(filename)
  File.read File.join(@path, filename)
end

#rm(filename) ⇒ Object



77
78
79
# File 'lib/drb_fileserver.rb', line 77

def rm(filename)
  FileUtils.rm File.join(@path, filename)
end

#rm_r(filename, force: false) ⇒ Object



81
82
83
# File 'lib/drb_fileserver.rb', line 81

def rm_r(filename, force: false)
  FileUtils.rm_r File.join(@path, filename), force: force
end

#ru(path) ⇒ Object

recently updated



87
88
89
# File 'lib/drb_fileserver.rb', line 87

def ru(path)
  DirToXML.new(path, recursive: false, verbose: false).latest
end

#ru_r(path) ⇒ Object

recently updated; checks subdirectories recursively



93
94
95
# File 'lib/drb_fileserver.rb', line 93

def ru_r(path)
  DirToXML.new(path, recursive: true, verbose: false).latest
end

#stopObject



97
98
99
100
101
# File 'lib/drb_fileserver.rb', line 97

def stop()
  puts 'stopping DFS service ...'
  DRb.stop_service
  'connection closed'
end

#touch(filename, mtime: Time.now) ⇒ Object



103
104
105
# File 'lib/drb_fileserver.rb', line 103

def touch(filename, mtime: Time.now)
  FileUtils.touch File.join(@path, filename), mtime: mtime
end

#write(filename, s) ⇒ Object



107
108
109
# File 'lib/drb_fileserver.rb', line 107

def write(filename, s)
  File.write File.join(@path, filename), s
end

#zip(filename_zip, a) ⇒ Object

zips a file. Each array items contains a filename, and content to be written to the file.



114
115
116
117
118
119
120
121
122
123
# File 'lib/drb_fileserver.rb', line 114

def zip(filename_zip, a)

  Zip::File.open(filename_zip, Zip::File::CREATE) do |x|

    a.each do |filename, buffer|
      x.get_output_stream(filename) {|os| os.write buffer }
    end

  end
end