Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux_fs: listdir #28

Merged
merged 1 commit into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
)