org.torquebox.vfs 1.0.0.Beta22

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vfs/dir.rb ADDED
@@ -0,0 +1,54 @@
1
+
2
+
3
+ module VFS
4
+ class Dir
5
+ attr_reader :path
6
+ attr_reader :pos
7
+ alias_method :tell, :pos
8
+
9
+ def initialize(path)
10
+ @path = path
11
+ begin
12
+ @virtual_file = org.jboss.vfs.VFS.child( path )
13
+ rescue Java::JavaLang::NullPointerException
14
+ raise Errno::ENOENT.new
15
+ end
16
+ @pos = 0
17
+ @closed = false
18
+ end
19
+
20
+ def close
21
+ @closed = true
22
+ end
23
+
24
+ def each
25
+ @virtual_file.children.each do |child|
26
+ yield child.name
27
+ end
28
+ end
29
+
30
+ def rewind
31
+ @pos = 0
32
+ end
33
+
34
+ def read
35
+ children = @virtual_file.children
36
+ return nil unless ( @pos < children.size )
37
+ child = children[@pos]
38
+ @pos += 1
39
+ child.name
40
+ end
41
+
42
+ def seek(i)
43
+ @pos = i
44
+ self
45
+ end
46
+
47
+ def pos=(i)
48
+ @pos = i
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
@@ -0,0 +1,113 @@
1
+
2
+ class Dir
3
+
4
+ class << self
5
+
6
+ alias_method :open_before_vfs, :open
7
+ alias_method :glob_before_vfs, :glob
8
+ alias_method :mkdir_before_vfs, :mkdir
9
+
10
+ def open(str,&block)
11
+ #if ( ::File.exist_without_vfs?( str.to_str ) && ! Java::OrgJbossVirtualPluginsContextJar::JarUtils.isArchive( str.to_str ) )
12
+ if ( ::File.exist_without_vfs?( str.to_str ) )
13
+ return open_before_vfs(str,&block)
14
+ end
15
+ #puts "open(#{str})"
16
+ result = dir = VFS::Dir.new( str.to_str )
17
+ #puts " result = #{result}"
18
+ if block
19
+ begin
20
+ result = block.call(dir)
21
+ ensure
22
+ dir.close
23
+ end
24
+ end
25
+ #puts "open(#{str}) return #{result}"
26
+ result
27
+ end
28
+
29
+ def [](pattern)
30
+ self.glob( pattern )
31
+ end
32
+
33
+ def glob(pattern,flags=0, &block)
34
+ is_absolute_vfs = false
35
+
36
+ str_pattern = pattern.to_str
37
+ #puts "glob(#{str_pattern})"
38
+
39
+ segments = str_pattern.split( '/' )
40
+
41
+ base_segments = []
42
+ for segment in segments
43
+ if ( segment =~ /[\*\?\[\{\}]/ )
44
+ break
45
+ end
46
+ base_segments << segment
47
+ end
48
+
49
+ base = base_segments.join( '/' )
50
+
51
+ base.gsub!( /\\(.)/, '\1' )
52
+
53
+ #if ( base.empty? || ( ::File.exist_without_vfs?( base ) && ! Java::OrgJbossVirtualPluginsContextJar::JarUtils.isArchive( base ) ) )
54
+ #if ( base.empty? || ( ::File.exist_without_vfs?( base ) ) )
55
+ #puts "doing FS glob"
56
+ #paths = glob_before_vfs( str_pattern, flags, &block )
57
+ #return paths
58
+ #end
59
+
60
+ #puts "base= #{base}"
61
+
62
+ vfs_url, child_path = VFS.resolve_within_archive( base )
63
+ #puts "vfs_url=#{vfs_url}"
64
+ #puts "child_path=#{child_path}"
65
+
66
+ return [] if vfs_url.nil?
67
+ #puts "segments.size==base_segments.size? #{segments.size == base_segments.size}"
68
+ return [ base ] if segments.size == base_segments.size
69
+
70
+ matcher_segments = segments - base_segments
71
+ matcher = matcher_segments.join( '/' )
72
+ #puts "matcher [#{matcher}]"
73
+
74
+ begin
75
+ #puts "0 vfs_url=#{vfs_url}"
76
+ starting_point = root = org.jboss.vfs::VFS.child( vfs_url )
77
+ #puts "A starting_point=#{starting_point.path_name}"
78
+ starting_point = root.get_child( child_path ) unless ( child_path.nil? || child_path == '' )
79
+ #puts "B starting_point=#{starting_point.path_name}"
80
+ return [] if ( starting_point.nil? || ! starting_point.exists? )
81
+ child_path = starting_point.path_name
82
+ #puts "child- #{child_path}"
83
+ unless ( child_path =~ %r(/$) )
84
+ child_path = "#{child_path}/"
85
+ end
86
+ child_path = "" if child_path == "/"
87
+ #puts "child_path=#{child_path}"
88
+ #puts "base=#{base}"
89
+ filter = VFS::GlobFilter.new( child_path, matcher )
90
+ #puts "filter is #{filter}"
91
+ paths = starting_point.getChildrenRecursively( filter ).collect{|e|
92
+ #path_name = e.path_name
93
+ path_name = e.getPathNameRelativeTo( starting_point )
94
+ #puts "(collect) path_name=#{path_name}"
95
+ result = ::File.join( base, path_name )
96
+ #puts "(collect) result=#{result}"
97
+ result
98
+ }
99
+ paths.each{|p| block.call(p)} if block
100
+ #puts "Path=#{paths.inspect}"
101
+ paths
102
+ rescue Java::JavaIo::IOException => e
103
+ return []
104
+ end
105
+ end
106
+
107
+ def mkdir(path, mode=0777)
108
+ real_path = path =~ /^vfs:/ ? path[4..-1] : path
109
+ mkdir_before_vfs( real_path, mode )
110
+ end
111
+ end
112
+ end
113
+
@@ -0,0 +1,178 @@
1
+
2
+
3
+ class File
4
+
5
+ class << self
6
+
7
+ alias_method :open_without_vfs, :open
8
+ alias_method :mtime_without_vfs, :mtime
9
+ alias_method :stat_without_vfs, :stat
10
+ alias_method :exist_without_vfs?, :exist?
11
+ alias_method :directory_without_vfs?, :directory?
12
+ alias_method :dirname_without_vfs, :dirname
13
+ alias_method :file_without_vfs?, :file?
14
+ alias_method :expand_path_without_vfs, :expand_path
15
+ alias_method :unlink_without_vfs, :unlink
16
+ alias_method :readable_without_vfs?, :readable?
17
+ alias_method :chmod_without_vfs, :chmod
18
+ alias_method :new_without_vfs, :new
19
+
20
+ def open(fname,mode_str='r', flags=nil, &block)
21
+ if ( Fixnum === fname )
22
+ return File.open_without_vfs( fname, mode_str, &block )
23
+ end
24
+ unless ( vfs_path?(fname) )
25
+ return File.open_without_vfs(fname, mode_str, flags, &block )
26
+ end
27
+ if ( File.exist_without_vfs?( name_without_vfs(fname) ) )
28
+ return File.open_without_vfs( name_without_vfs(fname), mode_str, flags, &block )
29
+ end
30
+ self.vfs_open( fname.to_s, mode_str, &block )
31
+ end
32
+
33
+ def expand_path(*args)
34
+ if ( vfs_path?(args[1]) )
35
+ return expand_path_without_vfs(args[0], name_without_vfs(args[1].to_s))
36
+ end
37
+ return args[0].to_s.dup if ( vfs_path?(args[0]) )
38
+ expand_path_without_vfs(*args)
39
+ end
40
+
41
+ def readable?(filename)
42
+ readable_without_vfs? name_without_vfs(filename)
43
+ end
44
+
45
+ def unlink(*file_names)
46
+ file_names.each do |file_name|
47
+ if ( vfs_path?(file_name) )
48
+ virtual_file = org.jboss.vfs::VFS.child( file_name.to_s )
49
+ raise Errno::ENOENT.new unless virtual_file.exists()
50
+ virtual_file.delete
51
+ else
52
+ unlink_without_vfs( file_name )
53
+ end
54
+ end
55
+ file_names.size
56
+ end
57
+
58
+ alias_method :delete, :unlink
59
+
60
+ def mtime(filename)
61
+ return mtime_without_vfs(filename) if ( File.exist_without_vfs?( filename ) )
62
+
63
+ vfs_url, child_path = VFS.resolve_within_archive(filename)
64
+ raise Errno::ENOENT.new unless vfs_url
65
+
66
+ virtual_file = Java::org.jboss.vfs.VFS.child( vfs_url )
67
+ virtual_file = virtual_file.get_child( child_path ) if child_path
68
+
69
+ Time.at( virtual_file.getLastModified() / 1000 )
70
+ end
71
+
72
+ def stat(filename)
73
+ return stat_without_vfs(filename) if ( File.exist_without_vfs?( filename ) )
74
+
75
+ vfs_url, child_path = VFS.resolve_within_archive(filename)
76
+ raise Errno::ENOENT.new nil unless vfs_url
77
+
78
+ virtual_file = Java::org.jboss.vfs.VFS.child( vfs_url )
79
+ virtual_file = virtual_file.get_child( child_path ) if child_path
80
+
81
+ VFS::File::Stat.new( virtual_file )
82
+ end
83
+
84
+ def exists?(filename)
85
+ exist?(filename)
86
+ end
87
+
88
+ def exist?(filename)
89
+ return true if exist_without_vfs?( filename )
90
+
91
+ vfs_url, child_path = VFS.resolve_within_archive(filename)
92
+ return false unless vfs_url
93
+
94
+ begin
95
+ virtual_file = Java::org.jboss.vfs.VFS.child( vfs_url )
96
+ virtual_file = virtual_file.get_child( child_path ) if child_path
97
+
98
+ return ( ( ! virtual_file.nil? ) && virtual_file.exists() )
99
+ rescue Java::JavaIo::IOException => e
100
+ return false
101
+ end
102
+ end
103
+
104
+ def writable?(filename)
105
+ stat = stat(filename)
106
+ return stat.writable? if stat
107
+ false
108
+ end
109
+
110
+ def directory?(filename)
111
+ return true if directory_without_vfs?( filename )
112
+
113
+ vfs_url, child_path = VFS.resolve_within_archive(filename)
114
+ return false unless vfs_url
115
+
116
+ begin
117
+ virtual_file = Java::org.jboss.vfs.VFS.child( vfs_url )
118
+ virtual_file = virtual_file.get_child( child_path ) if child_path
119
+
120
+ return ( ( ! virtual_file.nil? ) && ( virtual_file.isDirectory() ) )
121
+ rescue Java::JavaIo::IOException => e
122
+ return false
123
+ end
124
+ end
125
+
126
+ def file?(filename)
127
+ return true if file_without_vfs?( filename )
128
+
129
+ vfs_url, child_path = VFS.resolve_within_archive(filename)
130
+ return false unless vfs_url
131
+
132
+ begin
133
+ virtual_file = Java::org.jboss.vfs.VFS.child( vfs_url )
134
+ virtual_file = virtual_file.get_child( child_path ) if child_path
135
+
136
+ return ( ( ! virtual_file.nil? ) && ( virtual_file.isLeaf() ) )
137
+ rescue Java::JavaIo::IOException => e
138
+ return false
139
+ end
140
+ end
141
+
142
+ def chmod(mode_int, *files)
143
+ files.each do |name|
144
+ chmod_without_vfs( mode_int, name_without_vfs(name) )
145
+ end
146
+ end
147
+
148
+ def new(*args, &block)
149
+ fname = args.size > 0 ? args[0] : nil
150
+ if ( Fixnum === fname )
151
+ return self.new_without_vfs( *args, &block )
152
+ end
153
+ unless ( vfs_path?(fname) )
154
+ return self.new_without_vfs( *args, &block )
155
+ end
156
+ if ( File.exist_without_vfs?( name_without_vfs(fname) ) )
157
+ args[0] = name_without_vfs(fname)
158
+ return File.new_without_vfs( *args, &block )
159
+ end
160
+ # File.new doesn't pass a block through to the opened file
161
+ IO.vfs_open( *args )
162
+ end
163
+
164
+ def dirname(filename)
165
+ dirname = dirname_without_vfs(name_without_vfs(filename))
166
+ vfs_path?(filename) ? "vfs:#{dirname}" : dirname
167
+ end
168
+
169
+ def name_without_vfs(filename)
170
+ vfs_path?(filename) ? filename[4..-1] : filename
171
+ end
172
+
173
+ def vfs_path?(path)
174
+ path.to_s =~ /^vfs:/
175
+ end
176
+ end
177
+
178
+ end
data/lib/vfs/ext/io.rb ADDED
@@ -0,0 +1,123 @@
1
+
2
+ class IO
3
+
4
+ class << self
5
+
6
+ #alias_method :open_without_vfs, :open
7
+ alias_method :read_without_vfs, :read
8
+
9
+ def vfs_open(fd,mode_str='r', &block)
10
+ append = false
11
+ truncate = false
12
+ write = false
13
+ read = false
14
+ create = false
15
+ case ( mode_str )
16
+ when /r/
17
+ read = true
18
+ write = false
19
+ append = false
20
+ create = false
21
+ when /r\+/
22
+ read = true
23
+ write = true
24
+ create = false
25
+ append = false
26
+ when /w/
27
+ read = false
28
+ write = true
29
+ create = true
30
+ append = false
31
+ truncate = true
32
+ when /w\+/
33
+ read = false
34
+ write = true
35
+ create = true
36
+ append = false
37
+ truncate = true
38
+ when /a/
39
+ read = false
40
+ write = true
41
+ create = true
42
+ append = true
43
+ when /a\+/
44
+ read = true
45
+ write = true
46
+ create = true
47
+ when Fixnum
48
+ if ( mode_str & File::RDONLY != 0 )
49
+ read = true
50
+ write = false
51
+ end
52
+ if ( mode_str & File::WRONLY != 0 )
53
+ read = false
54
+ write = true
55
+ end
56
+ if ( mode_str & File::RDWR != 0)
57
+ read = true
58
+ write = true
59
+ end
60
+ if ( mode_str & File::APPEND != 0)
61
+ append = true
62
+ end
63
+ if ( mode_str & File::TRUNC != 0)
64
+ append = false
65
+ truncate = true
66
+ end
67
+ if ( mode_str & File::CREAT != 0)
68
+ create = true
69
+ end
70
+ end
71
+
72
+ virtual_file = org.jboss.vfs.VFS.child( fd )
73
+
74
+ if ( ! create && ! virtual_file.exists )
75
+ raise Errno::ENOENT
76
+ end
77
+
78
+ if ( read && ! write )
79
+ java_in = virtual_file.open_stream()
80
+ ruby_io = java_in.to_io
81
+ elsif ( write && ! read )
82
+ physical_file = virtual_file.physical_file
83
+ java_out = java.io::FileOutputStream.new( physical_file, append )
84
+ ruby_io = java_out.to_io
85
+ elsif ( read && write )
86
+ raise Error.new( "Random-access on VFS not supported" )
87
+ end
88
+
89
+ if ( block )
90
+ begin
91
+ block.call( ruby_io )
92
+ ensure
93
+ ruby_io.close
94
+ end
95
+ else
96
+ return ruby_io
97
+ end
98
+
99
+ end
100
+
101
+ def read(name, length=nil, offset=nil)
102
+ return read_without_vfs(name, length, offset) if ::File.exist_without_vfs?( name )
103
+
104
+ if ( name =~ /^\// || name =~ /^vfs:\// )
105
+ full_path = name
106
+ else
107
+ full_path = File.join( Dir.pwd, name )
108
+ end
109
+ virtual_file = org.jboss.vfs.VFS.child( full_path )
110
+ raise ::Errno::ENOENT.new( "#{name} (#{virtual_file})" ) unless virtual_file.exists()
111
+
112
+ stream = virtual_file.openStream()
113
+ io = stream.to_io
114
+ begin
115
+ s = io.read
116
+ ensure
117
+ io.close()
118
+ end
119
+ s
120
+ end
121
+ end
122
+
123
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module Kernel
3
+
4
+ private
5
+ alias_method :open_without_vfs, :open
6
+
7
+ def open(name, *rest, &block)
8
+ if ( name =~ /^vfs:/ )
9
+ return IO.vfs_open( name, *rest, &block )
10
+ end
11
+ open_without_vfs( name, *rest, &block )
12
+ end
13
+
14
+ end
@@ -0,0 +1,8 @@
1
+
2
+ class Java::org.jboss.vfs::VFS
3
+ def self.child(uri_str)
4
+ uri = java.net.URI.new( uri_str.gsub('#', '%23') )
5
+ child = self.getChild( uri )
6
+ child
7
+ end
8
+ end
data/lib/vfs/file.rb ADDED
@@ -0,0 +1,170 @@
1
+
2
+ module VFS
3
+ module File
4
+ class Stat
5
+ def initialize(virtual_file)
6
+ @virtual_file = virtual_file
7
+ end
8
+
9
+ def cmp(other)
10
+ @virtual_file.last_modified <=> other.last_modified
11
+ end
12
+
13
+ def atime()
14
+ Time.at( @virtual_file.last_modified )
15
+ end
16
+
17
+ def blksize
18
+ nil
19
+ end
20
+
21
+ def blockdev?
22
+ false
23
+ end
24
+
25
+ def blocks
26
+ nil
27
+ end
28
+
29
+ def chardev?
30
+ true
31
+ end
32
+
33
+ def ctime
34
+ Time.at( @virtual_file.last_modified )
35
+ end
36
+
37
+ def dev
38
+ nil
39
+ end
40
+
41
+ def dev_major
42
+ nil
43
+ end
44
+
45
+ def dev_minor
46
+ nil
47
+ end
48
+
49
+ def directory?
50
+ @virtual_file.eixsts && ! @virtual_file.is_leaf
51
+ end
52
+
53
+ def executable?
54
+ false
55
+ end
56
+
57
+ def executable_real?
58
+ false
59
+ end
60
+
61
+ def file?
62
+ true
63
+ end
64
+
65
+ def ftype
66
+ return 'file' if @virtual_file.is_leaf
67
+ 'directory'
68
+ end
69
+
70
+ def gid
71
+ nil
72
+ end
73
+
74
+ def grpowned?
75
+ false
76
+ end
77
+
78
+ def ino
79
+ nil
80
+ end
81
+
82
+ def mode
83
+ 0x444
84
+ end
85
+
86
+ def mtime
87
+ Time.at( @virtual_file.getLastModified() / 1000 )
88
+ end
89
+
90
+ def nlink
91
+ 1
92
+ end
93
+
94
+ def owned?
95
+ false
96
+ end
97
+
98
+ def pipe?
99
+ false
100
+ end
101
+
102
+ def rdev
103
+ nil
104
+ end
105
+
106
+ def rdev_major
107
+ nil
108
+ end
109
+
110
+ def rdev_minor
111
+ nil
112
+ end
113
+
114
+ def readable?
115
+ true
116
+ end
117
+
118
+ def readable_real?
119
+ true
120
+ end
121
+
122
+ def setgid?
123
+ false
124
+ end
125
+
126
+ def setuid?
127
+ false
128
+ end
129
+
130
+ def size
131
+ @virtual_file.size
132
+ end
133
+
134
+ def socket?
135
+ false
136
+ end
137
+
138
+ def sticky?
139
+ false
140
+ end
141
+
142
+ def symlink?
143
+ false
144
+ end
145
+
146
+ def uid
147
+ nil
148
+ end
149
+
150
+ def writable?
151
+ begin
152
+ physical_file = @virtual_file.physical_file
153
+ physical_file.canWrite
154
+ rescue => e
155
+ false
156
+ end
157
+ end
158
+
159
+ def writable_real?
160
+ writable?
161
+ end
162
+
163
+ def zero?
164
+ self.size == 0
165
+ end
166
+
167
+ end
168
+
169
+ end
170
+ end
@@ -0,0 +1,50 @@
1
+
2
+
3
+ module VFS
4
+ class GlobFilter
5
+ include Java::org.jboss.vfs.VirtualFileFilter
6
+
7
+ def initialize(child_path, glob)
8
+ #puts "init #{child_path} #{glob}"
9
+ @child_path = child_path
10
+ glob_segments = glob.split( '/' )
11
+ regexp_segments = []
12
+
13
+ glob_segments.each_with_index do |gs,i|
14
+ if ( gs == '**' )
15
+ regexp_segments << '(.*)'
16
+ else
17
+ gs.gsub!( /\*/, '[^\/]*')
18
+ gs.gsub!( /\?/, '.')
19
+ gs.gsub!( /\{[^\}]+\}/ ) do |m|
20
+ options = m[1..-2].split(',', -1)
21
+ options = options.collect{|e| "(#{e})"}
22
+ "(#{options.join('|')})"
23
+ end
24
+ if ( i < (glob_segments.size()-1))
25
+ gs = "#{gs}/"
26
+ end
27
+ regexp_segments << gs
28
+ end
29
+ end
30
+
31
+ regexp_str = regexp_segments.join
32
+ #puts "regexp_str(1) [#{regexp_str}]"
33
+ if ( @child_path && @child_path != '' )
34
+ regexp_str = ::File.join( "^#{@child_path}", "#{regexp_str}$" )
35
+ else
36
+ regexp_str = "^#{regexp_str}$"
37
+ end
38
+ #puts "regexp_str(2) [#{regexp_str}]"
39
+ @regexp = Regexp.new( regexp_str )
40
+ end
41
+
42
+ def accepts(file)
43
+ #puts "accepts(#{file.path_name}) vs #{@regexp}"
44
+ acceptable = ( !!( file.path_name =~ @regexp ) )
45
+ #puts " -> #{acceptable}"
46
+ !!( file.path_name =~ @regexp )
47
+ end
48
+ end
49
+ end
50
+