-
Notifications
You must be signed in to change notification settings - Fork 2
/
transport.rb
479 lines (408 loc) · 11.3 KB
/
transport.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# Transport layer for 9P2000 protocol.
#--
# Copyright protects this work.
# See LICENSE file for details.
#++
require 'rumai/ixp/message'
require 'thread' # for Mutex
module Rumai
module IXP
##
# A thread-safe channel that multiplexes many
# threads onto a single 9P2000 connection.
#
# The send/recv implementation is based on the XCB cookie approach:
# http://www.x.org/releases/X11R7.5/doc/libxcb/tutorial/#requestsreplies
#
class Agent
attr_reader :msize
def initialize stream
@stream = stream
@recv_buf = {} # tag => message
@recv_lock = Mutex.new
@tag_pool = RangedPool.new(0...BYTE2_MASK)
@fid_pool = RangedPool.new(0...BYTE4_MASK)
# establish connection with 9P2000 server
req = Tversion.new(
:tag => Fcall::NOTAG,
:msize => Tversion::MSIZE,
:version => Tversion::VERSION
)
rsp = talk(req)
unless req.version == rsp.version
raise Error, "protocol mismatch: self=#{req.version.inspect} server=#{rsp.version.inspect}"
end
@msize = rsp.msize
# authenticate the connection (not necessary for wmii)
@auth_fid = Fcall::NOFID
# attach to filesystem root
@root_fid = @fid_pool.obtain
attach @root_fid, @auth_fid
end
##
# A finite, thread-safe pool of range members.
#
class RangedPool
# how many new members should be added
# to the pool when the pool is empty?
FILL_RATE = 10
def initialize range
@pos = range.first
@lim = range.last
@lim = @lim.succ unless range.exclude_end?
@pool = Queue.new
end
# Returns an unoccupied range member from the pool.
def obtain
begin
@pool.deq true
rescue ThreadError
# pool is empty, so fill it
FILL_RATE.times do
if @pos != @lim
@pool << @pos
@pos = @pos.succ
else
# range is exhausted, so give other threads
# a chance to fill the pool before retrying
Thread.pass
break
end
end
retry
end
end
# Marks the given member as being unoccupied so
# that it may be occupied again in the future.
def release member
@pool << member
end
end
##
# Sends the given request (Rumai::IXP::Fcall) and returns
# a ticket that you can use later to receive the reply.
#
def send request
tag = @tag_pool.obtain
request.tag = tag
@stream << request.to_9p
tag
end
##
# Returns the reply for the given ticket, which was previously given
# to you when you sent the corresponding request (Rumai::IXP::Fcall).
#
def recv tag
loop do
reply = @recv_lock.synchronize do
if @recv_buf.key? tag
@recv_buf.delete tag
else
# reply was not in the receive buffer, so wait
# for the next reply... hoping that it is ours
msg = Fcall.from_9p(@stream)
if msg.tag == tag
msg
else
# we got someone else's reply, so buffer
# it (for them to receive) and try again
@recv_buf[msg.tag] = msg
nil
end
end
end
if reply
@tag_pool.release tag
if reply.is_a? Rerror
raise Error, reply.ename
end
return reply
else
# give other threads a chance to receive
Thread.pass
end
end
end
##
# Sends the given request (Rumai::IXP::Fcall) and returns its reply.
#
def talk request
tag = send(request)
begin
recv tag
rescue Error => e
e.message << " -- in reply to #{request.inspect}"
raise
end
end
MODES = {
'r' => Topen::OREAD,
'w' => Topen::OWRITE,
't' => Topen::ORCLOSE,
'+' => Topen::ORDWR,
}
##
# Converts the given mode string into an integer.
#
def MODES.parse mode
if mode.respond_to? :split
mode.split(//).inject(0) {|m,c| m | self[c].to_i }
else
mode.to_i
end
end
##
# Opens the given path for I/O access through a FidStream
# object. If a block is given, it is invoked with a
# FidStream object and the stream is closed afterwards.
#
# See File::open in the Ruby documentation.
#
def open path, mode = 'r' # :yields: FidStream
mode = MODES.parse(mode)
# open the file
path_fid = walk(path)
talk Topen.new(
:fid => path_fid,
:mode => mode
)
stream = FidStream.new(self, path_fid, @msize)
# return the file stream
if block_given?
begin
yield stream
ensure
stream.close
end
else
stream
end
end
##
# Encapsulates I/O access over a file handle (fid).
#
# NOTE: this class is NOT thread-safe.
#
class FidStream
attr_reader :fid, :stat
attr_reader :eof
alias eof? eof
attr_accessor :pos
alias tell pos
def initialize agent, path_fid, message_size
@agent = agent
@fid = path_fid
@msize = message_size
@stat = @agent.stat_fid(@fid)
@closed = false
rewind
end
##
# Rewinds the stream to the beginning.
#
def rewind
@pos = 0
@eof = false
end
##
# Closes this stream.
#
def close
unless @closed
@agent.clunk @fid
@closed = true
@eof = true
end
end
##
# Returns true if this stream is closed.
#
def closed?
@closed
end
##
# Reads some data from this stream at the current position.
#
# [partial]
# When false, the entire content of
# this stream is read and returned.
#
# When true, the maximum amount of content that can fit
# inside a single 9P2000 message is read and returned.
#
# If this stream corresponds to a directory, then an Array of
# Stat (one for each file in the directory) will be returned.
#
def read partial = false
raise 'cannot read from a closed stream' if @closed
content = ''
begin
req = Tread.new(
:fid => @fid,
:offset => @pos,
:count => @msize
)
rsp = @agent.talk(req)
content << rsp.data
count = rsp.count
@pos += count
end until @eof = count.zero? or partial
# the content of a directory is a sequence
# of Stat for all files in that directory
if @stat.directory?
buffer = StringIO.new(content)
content = []
until buffer.eof?
content << Stat.from_9p(buffer)
end
end
content
end
##
# Writes the given content at the current position in this stream.
#
def write content
raise 'closed streams cannot be written to' if @closed
raise 'directories cannot be written to' if @stat.directory?
data = content.to_s
limit = data.length + @pos
while @pos < limit
chunk = data[@pos, @msize]
req = Twrite.new(
:fid => @fid,
:offset => @pos,
:count => chunk.length,
:data => chunk
)
rsp = @agent.talk(req)
@pos += rsp.count
end
end
alias << write
end
##
# Returns the content of the file/directory at the given path.
#
def read path, *args
open path do |f|
f.read(*args)
end
end
##
# Returns the names of all files inside the directory whose path is given.
#
def entries path
unless stat(path).directory?
raise ArgumentError, "#{path.inspect} is not a directory"
end
read(path).map! {|t| t.name}
end
##
# Returns the content of the file/directory at the given path.
#
def write path, content
open path, 'w' do |f|
f << content
end
end
##
# Creates a new file at the given path that is accessible using
# the given modes for a user having the given permission bits.
#
def create path, mode = 'rw', perm = 0644
prefix = File.dirname(path)
target = File.basename(path)
mode = MODES.parse(mode)
with_fid do |prefix_fid|
walk_fid prefix_fid, prefix
# create the file
talk Tcreate.new(
:fid => prefix_fid,
:name => target,
:perm => perm,
:mode => mode
)
end
end
##
# Deletes the file at the given path.
#
def remove path
path_fid = walk(path)
remove_fid path_fid # remove also does clunk
end
##
# Deletes the file corresponding to the
# given FID and clunks the given FID.
#
def remove_fid path_fid
talk Tremove.new(:fid => path_fid)
end
##
# Returns information about the file at the given path.
#
def stat path
with_fid do |path_fid|
walk_fid path_fid, path
stat_fid path_fid
end
end
##
# Returns information about the file referenced by the given FID.
#
def stat_fid path_fid
req = Tstat.new(:fid => path_fid)
rsp = talk(req)
rsp.stat
end
##
# Returns an FID corresponding to the given path.
#
def walk path
fid = @fid_pool.obtain
walk_fid fid, path
fid
end
##
# Associates the given FID to the given path.
#
def walk_fid path_fid, path
talk Twalk.new(
:fid => @root_fid,
:newfid => path_fid,
:wname => path.to_s.split(%r{/+}).reject {|s| s.empty? }
)
end
##
# Associates the given FID with the FS root.
#
def attach root_fid, auth_fid = Fcall::NOFID, auth_name = ENV['USER']
talk Tattach.new(
:fid => root_fid,
:afid => auth_fid,
:uname => ENV['USER'],
:aname => auth_name
)
end
##
# Retires the given FID from use.
#
def clunk fid
talk Tclunk.new(:fid => fid)
@fid_pool.release fid
end
private
##
# Invokes the given block with a temporary FID.
#
def with_fid # :yields: fid
begin
fid = @fid_pool.obtain
yield fid
ensure
clunk fid
end
end
end
end
end