| @@ -49,8 +49,6 @@ class Client: · 48 unmodified lines |
| 49 | 49 | # Maintain an fid for the root of the file server and one for what we |
| 50 | 50 | # consider to be the current directory. |
| 51 | 51 | self.root_fid = 0 |
| 52 | | − self.first_fid = 1 |
| 53 | | − self.last_fid = 3 |
| 54 | 52 | self.current_fid = None |
| 55 | 53 | |
| 56 | 54 | # 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 | 78 | reply = self.send(styx.Tattach(tag=1, fid=0, afid=0, uname=uname, aname=aname)) |
| 81 | 79 | |
| 82 | 80 | self.root_fid = self.current_fid = 0 |
| 81 | + self.fids = set([self.root_fid]) |
| 83 | 82 | |
| 84 | 83 | def disconnect(self): |
| 85 | 84 | |
| @@ -114,6 +113,7 @@ class Client: · 28 unmodified lines |
| 114 | 113 | def _clunk(self, fid): |
| 115 | 114 | |
| 116 | 115 | self.send(styx.Tclunk(tag=2, fid=fid)) |
| 116 | + self.fids.remove(fid) |
| 117 | 117 | |
| 118 | 118 | def _clunk_old(self, fid): |
| 119 | 119 | |
| @@ -163,23 +163,18 @@ class Client: · 43 unmodified lines |
| 163 | 163 | |
| 164 | 164 | def _next_fid(self, fid): |
| 165 | 165 | |
| 166 | | − # Find the position of the existing fid in the available range. |
| 167 | | − i = fid - self.first_fid |
| 166 | + next_fid = fid + 1 |
| 168 | 167 | |
| 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 |
| 173 | 171 | |
| 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 |
| 176 | 175 | |
| 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 |
| 183 | 178 | |
| 184 | 179 | def ls(self, path = "", details = False): |
| 185 | 180 | |
| @@ -263,3 +258,17 @@ class Client: · 77 unmodified lines |
| 263 | 258 | |
| 264 | 259 | # Release the fid so that it can be reused. |
| 265 | 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 |