21
21
import pyboard
22
22
23
23
CMD_STAT = 1
24
- CMD_LISTDIR_START = 2
25
- CMD_LISTDIR_NEXT = 3
24
+ CMD_ILISTDIR_START = 2
25
+ CMD_ILISTDIR_NEXT = 3
26
26
CMD_OPEN = 4
27
27
CMD_CLOSE = 5
28
28
CMD_READ = 6
31
31
fs_hook_code = """\
32
32
import os, io, select, ustruct as struct, micropython
33
33
CMD_STAT = 1
34
- CMD_LISTDIR_START = 2
35
- CMD_LISTDIR_NEXT = 3
34
+ CMD_ILISTDIR_START = 2
35
+ CMD_ILISTDIR_NEXT = 3
36
36
CMD_OPEN = 4
37
37
CMD_CLOSE = 5
38
38
CMD_READ = 6
@@ -153,19 +153,23 @@ def stat(self, path):
153
153
if res < 0:
154
154
raise OSError(-res)
155
155
return tuple(self.cmd.rd_uint32() for _ in range(10))
156
- def listdir(self, path):
157
- l = []
158
- self.cmd.begin(CMD_LISTDIR_START)
156
+ def ilistdir(self, path):
157
+ self.cmd.begin(CMD_ILISTDIR_START)
159
158
self.cmd.wr_str(self.path + path)
160
- while True:
161
- self.cmd.begin(CMD_LISTDIR_NEXT)
162
- entry = self.cmd.rd_str()
163
- if entry:
164
- l.append(entry)
165
- else:
166
- break
167
159
self.cmd.end()
168
- return l
160
+ def ilistdir_next():
161
+ while True:
162
+ self.cmd.begin(CMD_ILISTDIR_NEXT)
163
+ name = self.cmd.rd_str()
164
+ if name:
165
+ type = self.cmd.rd_uint32()
166
+ inode = self.cmd.rd_uint32()
167
+ self.cmd.end()
168
+ yield (name, type, inode)
169
+ else:
170
+ self.cmd.end()
171
+ break
172
+ return ilistdir_next()
169
173
def open(self, path, mode):
170
174
self.cmd.begin(CMD_OPEN)
171
175
self.cmd.wr_str(self.path + path)
@@ -239,7 +243,7 @@ def wr_str(self, s):
239
243
self .fout .write (bytearray ([l ]) + b )
240
244
241
245
root = './'
242
- data_listdir = []
246
+ data_ilistdir = []
243
247
data_files = []
244
248
245
249
def do_stat (cmd ):
@@ -254,15 +258,18 @@ def do_stat(cmd):
254
258
for val in stat :
255
259
cmd .wr_uint32 (val ) # TODO will all values always fit in 32 bits?
256
260
257
- def do_listdir_start (cmd ):
258
- global data_listdir
261
+ def do_ilistdir_start (cmd ):
262
+ global data_ilistdir
259
263
path = root + cmd .rd_str ()
260
- data_listdir = os .listdir (path )
264
+ data_ilistdir = os .listdir (path )
261
265
262
- def do_listdir_next (cmd ):
263
- if data_listdir :
264
- entry = data_listdir .pop (0 )
266
+ def do_ilistdir_next (cmd ):
267
+ if data_ilistdir :
268
+ entry = data_ilistdir .pop (0 )
269
+ stat = os .stat (entry )
265
270
cmd .wr_str (entry )
271
+ cmd .wr_uint32 (stat .st_mode & 0xc000 )
272
+ cmd .wr_uint32 (stat .st_ino )
266
273
else :
267
274
cmd .wr_str ('' )
268
275
@@ -306,8 +313,8 @@ def do_write(cmd):
306
313
307
314
cmd_table = {
308
315
CMD_STAT : do_stat ,
309
- CMD_LISTDIR_START : do_listdir_start ,
310
- CMD_LISTDIR_NEXT : do_listdir_next ,
316
+ CMD_ILISTDIR_START : do_ilistdir_start ,
317
+ CMD_ILISTDIR_NEXT : do_ilistdir_next ,
311
318
CMD_OPEN : do_open ,
312
319
CMD_CLOSE : do_close ,
313
320
CMD_READ : do_read ,
0 commit comments