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
49 # Maintain an fid for the root of the file server and one for what we49 # Maintain an fid for the root of the file server and one for what we
50 # consider to be the current directory.50 # consider to be the current directory.
51 self.root_fid = 051 self.root_fid = 0
52 self.first_fid = 1
53 self.last_fid = 3
54 self.current_fid = None52 self.current_fid = None
55 53
56 # Keep a collection of replies in case they arrive in an order we don't54 # Keep a collection of replies in case they arrive in an order we don't
@@ -80,6 +78,7 @@ class Client: · 23 unmodified lines
80 reply = self.send(styx.Tattach(tag=1, fid=0, afid=0, uname=uname, aname=aname))78 reply = self.send(styx.Tattach(tag=1, fid=0, afid=0, uname=uname, aname=aname))
81 79
82 self.root_fid = self.current_fid = 080 self.root_fid = self.current_fid = 0
81 self.fids = set([self.root_fid])
83 82
84 def disconnect(self):83 def disconnect(self):
85 84
@@ -114,6 +113,7 @@ class Client: · 28 unmodified lines
114 def _clunk(self, fid):113 def _clunk(self, fid):
115 114
116 self.send(styx.Tclunk(tag=2, fid=fid))115 self.send(styx.Tclunk(tag=2, fid=fid))
116 self.fids.remove(fid)
117 117
118 def _clunk_old(self, fid):118 def _clunk_old(self, fid):
119 119
@@ -163,23 +163,18 @@ class Client: · 43 unmodified lines
163 163
164 def _next_fid(self, fid):164 def _next_fid(self, fid):
165 165
166 # Find the position of the existing fid in the available range.166 next_fid = fid + 1
167 i = fid - self.first_fid
168 167
169 # If the current fid is the same as the root fid then allocate the next168 # If the set of fids is sparse then start at zero in order to find a gap.
170 # fid in the available range.169 if next_fid > len(self.fids):
171 if self.current_fid == self.root_fid:170 next_fid = 0
172 return self.first_fid + ((i + 1) % 3)
173 171
174 # Find the remaining available fid.172 # Find the first fid that is not in the set.
175 j = self.current_fid - self.first_fid173 while next_fid in self.fids:
174 next_fid += 1
176 175
177 i = (i + 1) % 3176 self.fids.add(next_fid)
178 if i != j:177 return next_fid
179 return self.first_fid + i
180 else:
181 i = (i + 1) % 3
182 return self.first_fid + i
183 178
184 def ls(self, path = "", details = False):179 def ls(self, path = "", details = False):
185 180
@@ -263,3 +258,17 @@ class Client: · 77 unmodified lines
263 258
264 # Release the fid so that it can be reused.259 # Release the fid so that it can be reused.
265 self._clunk_old(fid)260 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
637 return encode_data(data)637 return encode_data(data)
638 638
639 639
640class 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
640MessageTypes = {660MessageTypes = {
641 Tversion.code: Tversion, 661 Tversion.code: Tversion,
642 Rversion.code: Rversion,662 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