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

#exist?(filename) ⇒ Boolean Also known as: exists?

Returns:

  • (Boolean)


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

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

#glob(s) ⇒ Object



38
39
40
# File 'lib/drb_fileserver.rb', line 38

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

#ls(rawpath) ⇒ Object

path can include a wildcard and the switch -ltr



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

def ls(rawpath)
  
  path, switch = rawpath.split(/\s+/,2)
  wildcard = path.include?('*') ? '' : '*'
  path = File.join(path, wildcard) if File.exist? 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



63
64
65
# File 'lib/drb_fileserver.rb', line 63

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

#mkdir_p(path) ⇒ Object



67
68
69
# File 'lib/drb_fileserver.rb', line 67

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

#mv(path, path2) ⇒ Object



71
72
73
# File 'lib/drb_fileserver.rb', line 71

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

#read(filename) ⇒ Object



75
76
77
# File 'lib/drb_fileserver.rb', line 75

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

#rm(filename) ⇒ Object



79
80
81
# File 'lib/drb_fileserver.rb', line 79

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

#rm_r(filename, force: false) ⇒ Object



83
84
85
# File 'lib/drb_fileserver.rb', line 83

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

#ru(path) ⇒ Object

recently updated



89
90
91
92
93
# File 'lib/drb_fileserver.rb', line 89

def ru(path)
  found = DirToXML.new(File.join(@path, path), recursive: false,
                       verbose: false).latest
  found.sub(/^#{@path}/,'') if found
end

#ru_r(path) ⇒ Object

recently updated; checks subdirectories recursively



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

def ru_r(path)

  found = DirToXML.new(File.join(@path, path), recursive: true,
                       verbose: false).latest
  found.sub(/^#{@path}/,'') if found

end

#stopObject



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

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

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



111
112
113
# File 'lib/drb_fileserver.rb', line 111

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

#write(filename, s) ⇒ Object



115
116
117
# File 'lib/drb_fileserver.rb', line 115

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.



122
123
124
125
126
127
128
129
130
131
# File 'lib/drb_fileserver.rb', line 122

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