Module: VFS

Defined in:
lib/smb/vfs.rb

Overview

module Ambit

Defined Under Namespace

Modules: Dir, File, FileTest

Constant Summary collapse

SCHEMES =
{
	'smb'=> {
		:dir_new => lambda { |uri| SMB::Dir.new(uri.to_s) },
		:file_new => lambda { |uri,flags,mode| SMB::File.new(uri.to_s,flags,mode) },
		:dir_entries => lambda { |uri| SMB::Dir.entries(uri.to_s) },
		:dir_foreach => lambda { |uri,block| SMB::Dir.foreach(uri.to_s, &block) },
		:mkdir => lambda { |uri,mode| SMB::Dir.mkdir(uri.to_s,mode) },
		:rmdir => lambda { |uri| SMB::Dir.rmdir(uri.to_s) },
		:stat => lambda { |uri| SMB::File.stat(uri.to_s) },
		:unlink => lambda { |uri| SMB::File.unlink(uri.to_s) },
		:file_read => lambda { |uri| SMB::File.read(uri.to_s) },
		
		
		:init => lambda { |auth| SMB::init { |srv,shr,wrk,uid,pwd| auth.call(URI.parse("smb://#{srv}/#{shr}"),{:server=>srv, :share=>shr, :workgroup=>wrk, :username=>uid, :password=>pwd}) } },
	},
	'file'=> {
		:dir_new => lambda { |uri| ::Dir.new(unescape(uri.path)) },
		:file_new => lambda { |uri,flags,mode| ::File.new(unescape(uri.path),flags,mode) },
		:dir_entries => lambda { |uri| ::Dir.entries(unescape(uri.path)) },
		:dir_foreach => lambda { |uri,block| ::Dir.foreach(unescape(uri.path), &block) },
		:mkdir => lambda { |uri,mode| ::Dir.mkdir(unescape(uri.path),mode) },
		:rmdir => lambda { |uri| ::Dir.rmdir(unescape(uri.path)) },
		:stat => lambda { |uri| ::File.stat(unescape(uri.path)) },
		:unlink => lambda { |uri| ::File.unlink(unescape(uri.path)) },
		:file_read => lambda { |uri| ::File.read(unescape(uri.path)) },
	}
}

Class Method Summary collapse

Class Method Details

.escape(str) ⇒ Object



74
75
76
# File 'lib/smb/vfs.rb', line 74

def self.escape(str)
	str.gsub(/([^-A-Za-z0-9.\/])/) {|i| sprintf("%%%02X",i[0]) }
end

.init(&auth_callback) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/smb/vfs.rb', line 67

def self.init(&auth_callback)
	SCHEMES.each do |key,val|
		if val[:init]
			val[:init].call(auth_callback)
		end
	end
end

.io_copy(in_fp, out_fp, block_size = (4096 * 16)) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/smb/vfs.rb', line 87

def self.io_copy(in_fp, out_fp, block_size = (4096 * 16))
	rfp, wfp = IO::pipe

	reader = fork {
		rfp.close()
		while str = in_fp.read(block_size)
			wfp.write(str)
			break if in_fp.respond_to?(:eof?) and in_fp.eof?
		end
		wfp.close
		exit(1)
	}
	writer = fork {
		wfp.close()
		while str = rfp.read(block_size)
			out_fp.write(str)
			break if rfp.closed? or rfp.eof? or str.empty?
		end
		rfp.close
		exit(1)
	}
	wfp.close()
	rfp.close()

	ar = aw = nil
	until ar and aw
		ar = Process::waitpid2(reader, Process::WNOHANG) unless ar
		aw = Process::waitpid2(writer, Process::WNOHANG) unless aw
	end
end

.path2uri(path) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/smb/vfs.rb', line 80

def self.path2uri(path)
	"file://" + escape(if path[0] == ?/
		path
	else
		::File::expand_path(path)
	end)
end

.unescape(str) ⇒ Object



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

def self.unescape(str)
	str.gsub(/(\%[A-Fa-f0-9]{2})/) {|i| d=" "; d[0]=Integer("0x"+i[1..-1]); d }
end