Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/torquebox/vfs/ext/file.rb

Class Method Summary collapse

Class Method Details

.chmod(mode_int, *files) ⇒ Object



223
224
225
226
227
# File 'lib/torquebox/vfs/ext/file.rb', line 223

def chmod(mode_int, *files)
  writable_operation(*files) do |filename| 
    chmod_without_vfs( mode_int, filename )
  end
end

.chmod_without_vfsObject



38
# File 'lib/torquebox/vfs/ext/file.rb', line 38

alias_method :chmod_without_vfs,       :chmod

.chown(owner, group, *files) ⇒ Object



229
230
231
232
233
# File 'lib/torquebox/vfs/ext/file.rb', line 229

def chown(owner, group, *files)
  writable_operation(*files) do |filename|
    chown_without_vfs( owner, group, filename )
  end
end

.chown_without_vfsObject



39
# File 'lib/torquebox/vfs/ext/file.rb', line 39

alias_method :chown_without_vfs,       :chown

.directory?(filename) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
178
179
180
# File 'lib/torquebox/vfs/ext/file.rb', line 175

def directory?(filename)
  return directory_without_vfs?( filename ) if exist_without_vfs?( filename )

  virtual_file = virtual_file(filename)
  !virtual_file.nil? && virtual_file.is_directory?
end

.directory_without_vfs?Object



32
# File 'lib/torquebox/vfs/ext/file.rb', line 32

alias_method :directory_without_vfs?,  :directory?

.dirname(filename) ⇒ Object



189
190
191
192
# File 'lib/torquebox/vfs/ext/file.rb', line 189

def dirname(filename)
  dirname = dirname_without_vfs(name_without_vfs(filename))
  vfs_path?(filename) ? TorqueBox::VFS.resolve_path_url(dirname) : dirname
end

.dirname_without_vfsObject



33
# File 'lib/torquebox/vfs/ext/file.rb', line 33

alias_method :dirname_without_vfs,     :dirname

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

Returns:

  • (Boolean)


159
160
161
162
163
164
# File 'lib/torquebox/vfs/ext/file.rb', line 159

def exist?(filename)
  return true if exist_without_vfs?( filename )

  virtual_file = virtual_file(filename)
  !virtual_file.nil? && virtual_file.exists?
end

.exist_without_vfs?Object



31
# File 'lib/torquebox/vfs/ext/file.rb', line 31

alias_method :exist_without_vfs?,      :exist?

.expand_path(*args) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/torquebox/vfs/ext/file.rb', line 58

def expand_path(*args)
  return args[0].to_s.dup if ( vfs_path?(args[0]) )
  if ( vfs_path?(args[1]) )
    expanded = expand_path_without_vfs(args[0], name_without_vfs(args[1].to_s))
    return TorqueBox::VFS.resolve_path_url(expanded)
  end
  expand_path_without_vfs(*args)
end

.expand_path_without_vfsObject



35
# File 'lib/torquebox/vfs/ext/file.rb', line 35

alias_method :expand_path_without_vfs, :expand_path

.file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
186
187
# File 'lib/torquebox/vfs/ext/file.rb', line 182

def file?(filename)
  return true if file_without_vfs?( filename )

  virtual_file = virtual_file(filename)
  !virtual_file.nil? && virtual_file.is_leaf?
end

.file_without_vfs?Object



34
# File 'lib/torquebox/vfs/ext/file.rb', line 34

alias_method :file_without_vfs?,       :file?

.join(*args) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/torquebox/vfs/ext/file.rb', line 194

def join(*args)
  prefix = vfs_path?(args[0]) ? "vfs:" : ""
  args = args.map do |arg|
    if arg.is_a?(Array)
      raise ArgumentError if arg.include?(arg)
      join(*arg)
    else
      arg
    end
  end
  prefix + join_without_vfs(*args.map{|x|name_without_vfs(x)})
end

.join_without_vfsObject



43
# File 'lib/torquebox/vfs/ext/file.rb', line 43

alias_method :join_without_vfs,        :join

.lstat(filename) ⇒ Object



149
150
151
152
153
# File 'lib/torquebox/vfs/ext/file.rb', line 149

def lstat(filename)
  name = name_without_vfs(filename)
  return lstat_without_vfs(name) if ( File.exist_without_vfs?( name ) )
  stat(filename)
end

.lstat_without_vfsObject



30
# File 'lib/torquebox/vfs/ext/file.rb', line 30

alias_method :lstat_without_vfs,       :lstat

.mtime(filename) ⇒ Object

Raises:

  • (Errno::ENOENT)


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/torquebox/vfs/ext/file.rb', line 91

def mtime(filename)
  return mtime_without_vfs(filename) if ( File.exist_without_vfs?( filename ) )

  vfs_url, child_path = TorqueBox::VFS.resolve_within_archive(filename)
  raise Errno::ENOENT.new(filename) unless vfs_url

  virtual_file = Java::org.jboss.vfs.VFS.child( vfs_url )
  virtual_file = virtual_file.get_child( child_path ) if child_path
  raise Errno::ENOENT.new(filename) unless virtual_file.exists?

  Time.at( virtual_file.getLastModified() / 1000 )
end

.mtime_without_vfsObject



26
# File 'lib/torquebox/vfs/ext/file.rb', line 26

alias_method :mtime_without_vfs,       :mtime

.name_without_vfs(filename) ⇒ Object



252
253
254
255
256
257
258
259
260
# File 'lib/torquebox/vfs/ext/file.rb', line 252

def name_without_vfs(filename)
  name = path_to_str(filename).gsub("\\", "/")
  if vfs_path?(name) 
    result = name[4..-1]
    result.size==0 ? "/" : result
  else
    name
  end
end

.new(*args, &block) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/torquebox/vfs/ext/file.rb', line 207

def new(*args, &block)
  fname = args.size > 0 ? args[0] : nil
  if ( Fixnum === fname )
    return new_without_vfs( *args, &block )
  end
  unless ( vfs_path?(fname) )
    return new_without_vfs( *args, &block )
  end
  if ( File.exist_without_vfs?( name_without_vfs(fname) ) )
    args[0] = name_without_vfs(fname)
    return new_without_vfs( *args, &block )
  end
  # File.new doesn't pass a block through to the opened file
  IO.vfs_open( *args )
end

.new_without_vfsObject



41
# File 'lib/torquebox/vfs/ext/file.rb', line 41

alias_method :new_without_vfs,         :new

.open(fname, mode_str = 'r', flags = nil, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/torquebox/vfs/ext/file.rb', line 45

def open(fname,mode_str='r', flags=nil, &block)
  if ( Fixnum === fname )
    return open_without_vfs( fname, mode_str, &block )
  end
  unless ( vfs_path?(fname) )
    return open_without_vfs(fname, mode_str, flags, &block )
  end
  if ( File.exist_without_vfs?( name_without_vfs(fname) ) )
    return open_without_vfs( name_without_vfs(fname), mode_str, flags, &block )
  end
  self.vfs_open( fname.to_s, mode_str, &block )
end

.open_without_vfsObject



25
# File 'lib/torquebox/vfs/ext/file.rb', line 25

alias_method :open_without_vfs,        :open

.path_to_str(path) ⇒ Object

Raises:

  • (TypeError)


262
263
264
265
266
267
# File 'lib/torquebox/vfs/ext/file.rb', line 262

def path_to_str(path)
  # 1.9 - to_str became to_path for Pathnames
  return path.to_path if path.respond_to?(:to_path) 
  raise TypeError unless path.respond_to?(:to_str)
  path.to_str
end

.readable?(filename) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# File 'lib/torquebox/vfs/ext/file.rb', line 67

def readable?(filename)
  real_name = name_without_vfs( filename )
  return readable_without_vfs?( real_name ) if exist_without_vfs?( real_name )

  virtual_file = virtual_file( filename )
  # VirtualFile has no readable? so assume we can read it if it exists
  !virtual_file.nil? && virtual_file.exists?
end

.readable_without_vfs?Object



37
# File 'lib/torquebox/vfs/ext/file.rb', line 37

alias_method :readable_without_vfs?,   :readable?

.rename(oldname, newname) ⇒ Object



155
156
157
# File 'lib/torquebox/vfs/ext/file.rb', line 155

def rename(oldname, newname) 
  rename_without_vfs( name_without_vfs(oldname), name_without_vfs(newname) )
end

.rename_without_vfsObject



42
# File 'lib/torquebox/vfs/ext/file.rb', line 42

alias_method :rename_without_vfs,      :rename

.size(filename) ⇒ Object

Raises:

  • (Errno::ENOENT)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/torquebox/vfs/ext/file.rb', line 104

def size(filename)
  return size_without_vfs(filename) if ( File.exist_without_vfs?( filename ) )


  vfs_url, child_path = TorqueBox::VFS.resolve_within_archive(filename)
  raise Errno::ENOENT.new(filename) unless vfs_url

  virtual_file = Java::org.jboss.vfs.VFS.child( vfs_url )

  raise Errno::ENOENT.new(filename) unless virtual_file.exists

  virtual_file = virtual_file.get_child( child_path ) if child_path
  raise Errno::ENOENT.new(filename) unless virtual_file.exists

  virtual_file.size
end

.size?(filename) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/torquebox/vfs/ext/file.rb', line 121

def size?(filename)
  return size_without_vfs?(filename) if ( File.exist_without_vfs?( filename ) )

  vfs_url, child_path = TorqueBox::VFS.resolve_within_archive(filename)
  return nil unless vfs_url

  virtual_file = Java::org.jboss.vfs.VFS.child( vfs_url )
  virtual_file = virtual_file.get_child( child_path ) if child_path

  return nil unless virtual_file.exists

  virtual_file.size
end

.size_without_vfsObject



27
# File 'lib/torquebox/vfs/ext/file.rb', line 27

alias_method :size_without_vfs,        :size

.size_without_vfs?Object



28
# File 'lib/torquebox/vfs/ext/file.rb', line 28

alias_method :size_without_vfs?,       :size?

.stat(filename) ⇒ Object

Raises:

  • (Errno::ENOENT)


135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/torquebox/vfs/ext/file.rb', line 135

def stat(filename)
  name = name_without_vfs(filename)
  return stat_without_vfs(name) if ( File.exist_without_vfs?( name ) )

  vfs_url, child_path = TorqueBox::VFS.resolve_within_archive(filename)
  raise Errno::ENOENT.new(filename) unless vfs_url

  virtual_file = Java::org.jboss.vfs.VFS.child( vfs_url )
  virtual_file = virtual_file.get_child( child_path ) if child_path
  raise Errno::ENOENT.new(filename) unless virtual_file.exists?

  TorqueBox::VFS::File::Stat.new( virtual_file )
end

.stat_without_vfsObject



29
# File 'lib/torquebox/vfs/ext/file.rb', line 29

alias_method :stat_without_vfs,        :stat


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/torquebox/vfs/ext/file.rb', line 76

def unlink(*file_names)
  file_names.each do |file_name|
    if ( vfs_path?(file_name) )
      virtual_file = org.jboss.vfs::VFS.child( file_name.to_s )
      raise Errno::ENOENT.new(file_name) unless virtual_file.exists()
      virtual_file.delete
    else
      unlink_without_vfs( file_name )
    end
  end
  file_names.size
end


36
# File 'lib/torquebox/vfs/ext/file.rb', line 36

alias_method :unlink_without_vfs,      :unlink

.utime(accesstime, modtime, *files) ⇒ Object



235
236
237
238
239
# File 'lib/torquebox/vfs/ext/file.rb', line 235

def utime(accesstime, modtime, *files)
  writable_operation(*files) do |filename|
    utime_without_vfs( accesstime, modtime, filename )
  end
end

.utime_without_vfsObject



40
# File 'lib/torquebox/vfs/ext/file.rb', line 40

alias_method :utime_without_vfs,       :utime

.vfs_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/torquebox/vfs/ext/file.rb', line 269

def vfs_path?(path)
  path.to_s =~ /^vfs:/
end

.virtual_file(filename) ⇒ Object



273
274
275
# File 'lib/torquebox/vfs/ext/file.rb', line 273

def virtual_file(filename)
  TorqueBox::VFS.virtual_file(filename)
end

.writable?(filename) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
170
171
172
173
# File 'lib/torquebox/vfs/ext/file.rb', line 167

def writable?(filename)
  stat = stat(filename)
  return stat.writable? if stat
  false
rescue Errno::ENOENT
  false
end

.writable_operation(*files) ⇒ Object



241
242
243
244
245
246
247
248
249
250
# File 'lib/torquebox/vfs/ext/file.rb', line 241

def writable_operation(*files)
  files.each do |name|
    begin
      yield name_without_vfs(name)
    rescue Errno::ENOENT => e
      yield TorqueBox::VFS.writable_path_or_error( name, e )
    end
  end
  files.size
end