From 4c92d8313594c2a797892024a282533ab4f16b49 Mon Sep 17 00:00:00 2001 From: yotamolenik <39271599+yotamolenik@users.noreply.github.com> Date: Sat, 5 Feb 2022 16:39:56 +0200 Subject: [PATCH] linux_fs: add listdir --- src/pyzshell/pyzshell/linux_fs.py | 27 +++++++++++++++++++++++--- src/pyzshell/pyzshell/structs/linux.py | 13 ++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/pyzshell/pyzshell/linux_fs.py b/src/pyzshell/pyzshell/linux_fs.py index 80710f38..8bd9aa9c 100644 --- a/src/pyzshell/pyzshell/linux_fs.py +++ b/src/pyzshell/pyzshell/linux_fs.py @@ -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 diff --git a/src/pyzshell/pyzshell/structs/linux.py b/src/pyzshell/pyzshell/structs/linux.py index e93fd0fa..fbaae878 100644 --- a/src/pyzshell/pyzshell/structs/linux.py +++ b/src/pyzshell/pyzshell/structs/linux.py @@ -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'), @@ -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()) +)