History

Added methods to the client to handle opening, reading, writing and closing files. Added a File class to provide a Python-like API for files. Changed the fid allocation strategy to allow for long-lived fids to be used.

David Boddie committed 7 years agomaster

Commit 64385b71 parent 24007502 files · +45 −16Browse files

Unified · Split

2 files
  1. client.py +25 −16
  2. styx.py +20 −0
client.py+25−16
@@ -49,8 +49,6 @@ class Client: · 48 unmodified lines
4949 # Maintain an fid for the root of the file server and one for what we
5050 # consider to be the current directory.
5151 self.root_fid = 0
52− self.first_fid = 1
53− self.last_fid = 3
5452 self.current_fid = None
5553
5654 # Keep a collection of replies in case they arrive in an order we don't
@@ -80,6 +78,7 @@ class Client: · 23 unmodified lines
8078 reply = self.send(styx.Tattach(tag=1, fid=0, afid=0, uname=uname, aname=aname))
8179
8280 self.root_fid = self.current_fid = 0
81+ self.fids = set([self.root_fid])
8382
8483 def disconnect(self):
8584
@@ -114,6 +113,7 @@ class Client: · 28 unmodified lines
114113 def _clunk(self, fid):
115114
116115 self.send(styx.Tclunk(tag=2, fid=fid))
116+ self.fids.remove(fid)
117117
118118 def _clunk_old(self, fid):
119119
@@ -163,23 +163,18 @@ class Client: · 43 unmodified lines
163163
164164 def _next_fid(self, fid):
165165
166− # Find the position of the existing fid in the available range.
167− i = fid - self.first_fid
166+ next_fid = fid + 1
168167
169− # If the current fid is the same as the root fid then allocate the next
170− # fid in the available range.
171− if self.current_fid == self.root_fid:
172− return self.first_fid + ((i + 1) % 3)
168+ # If the set of fids is sparse then start at zero in order to find a gap.
169+ if next_fid > len(self.fids):
170+ next_fid = 0
173171
174− # Find the remaining available fid.
175− j = self.current_fid - self.first_fid
172+ # Find the first fid that is not in the set.
173+ while next_fid in self.fids:
174+ next_fid += 1
176175
177− i = (i + 1) % 3
178− if i != j:
179− return self.first_fid + i
180− else:
181− i = (i + 1) % 3
182− return self.first_fid + i
176+ self.fids.add(next_fid)
177+ return next_fid
183178
184179 def ls(self, path = "", details = False):
185180
@@ -263,3 +258,17 @@ class Client: · 77 unmodified lines
263258
264259 # Release the fid so that it can be reused.
265260 self._clunk_old(fid)
261+
262+ def open(self, path, mode):
263+
264+ fid = self._walk(path)
265+
266+ reply = self.send(styx.Topen(tag=2, fid=fid, mode=mode))
267+
268+ return styx.File(fid, mode, self)
269+
270+ def read(self, fid, offset, count):
271+
272+ reply = self.send(styx.Tread(tag=2, fid=fid, offset=offset, count=count))
273+
274+ return reply.data
styx.py+20−0
@@ -637,6 +637,26 @@ class Stat: · 636 unmodified lines
637637 return encode_data(data)
638638
639639
640+class File:
641+
642+ OREAD = 0
643+ OWRITE = 1
644+ ORDWR = 2
645+ OEXEC = 3
646+
647+ def __init__(self, fid, mode, client):
648+
649+ self.fid = fid
650+ self.mode = mode
651+ self.client = client
652+
653+ def read(self, offset, count):
654+ return self.client.read(self.fid, offset, count)
655+
656+ def close(self):
657+ self.client._clunk(self.fid)
658+
659+
640660 MessageTypes = {
641661 Tversion.code: Tversion,
642662 Rversion.code: Rversion,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t