Skip to content

Commit

Permalink
linux_fs: add listdir
Browse files Browse the repository at this point in the history
  • Loading branch information
yotamolenik committed Feb 5, 2022
1 parent bae7d35 commit 4c92d83
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/pyzshell/pyzshell/linux_fs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
from struct import Struct
from typing import List

from pyzshell.fs import Fs

from pyzshell.exceptions import ZShellError
from pyzshell.structs.linux import dirent


class LinuxFs(Fs):
CHUNK_SIZE = 1024

def listdir(self, dirname: str) -> list:
""" get directory listing for a given dirname """
raise NotImplementedError()
def listdir(self, dirname='.') -> List[Struct]:
""" list directory contents(at remote).
calls readdir in a loop """
errno = self._client.symbols.errno
errno[0] = 0
dir_list = []
folder = self._client.symbols.opendir(dirname)
if folder == 0:
raise ZShellError('cannot open folder to listdir')
diren = self._client.symbols.readdir(folder)
while diren != 0:
entry = dirent.parse_stream(diren)
dir_list.append(entry)
diren = self._client.symbols.readdir(folder)
if errno[0] != 0:
raise ZShellError(f'readdir for listdir failed. ({self._client.errno})')
self._client.symbols.closedir(folder)
return dir_list
13 changes: 12 additions & 1 deletion src/pyzshell/pyzshell/structs/linux.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from construct import Struct, PaddedString
from construct import Struct, PaddedString, Int32ul, Int64ul, Int16ul, Int8ul, Bytes, Padding, Computed

_UTSNAME_LENGTH = 65
_D_NAME_LENGTH = 256

utsname = Struct(
'sysname' / PaddedString(_UTSNAME_LENGTH, 'utf8'),
Expand All @@ -9,3 +10,13 @@
'version' / PaddedString(_UTSNAME_LENGTH, 'utf8'),
'machine' / PaddedString(_UTSNAME_LENGTH, 'utf8'),
)

dirent = Struct(
'd_ino' / Int32ul,
Padding(4),
'd_off' / Int64ul,
'd_reclen' / Int16ul,
'd_type' / Int8ul,
'_d_name_bytes' / Bytes(_D_NAME_LENGTH),
'd_name' / Computed(lambda x: x._d_name_bytes.split(b'\x00', 1)[0].decode())
)

0 comments on commit 4c92d83

Please sign in to comment.