Skip to content

Commit

Permalink
Merge pull request #1180 from doronz88/feature/afc-chunked-pull
Browse files Browse the repository at this point in the history
afc: use `MAX_READ_SIZE` for pulling
  • Loading branch information
doronz88 authored Sep 3, 2024
2 parents 9f00159 + ca6de9e commit f80ccf6
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pymobiledevice3/services/afc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pygments import formatters, highlight, lexers
from pygnuutils.cli.ls import ls as ls_cli
from pygnuutils.ls import Ls, LsStub
from tqdm import trange
from xonsh.built_ins import XSH
from xonsh.cli_utils import Annotated, Arg, ArgParserAlias
from xonsh.main import main as xonsh_main
Expand Down Expand Up @@ -231,7 +232,16 @@ def pull(self, relative_src: str, dst: str, match: Optional[Pattern] = None, cal
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(relative_src))
with open(dst, 'wb') as f:
f.write(self.get_file_contents(src))
src_size = self.stat(src)['st_size']
if src_size <= MAXIMUM_READ_SIZE:
f.write(self.get_file_contents(src))
else:
left_size = src_size
handle = self.fopen(src)
for _ in trange(src_size // MAXIMUM_READ_SIZE + 1):
f.write(self.fread(handle, min(MAXIMUM_READ_SIZE, left_size)))
left_size -= MAXIMUM_READ_SIZE
self.fclose(handle)
os.utime(dst, (os.stat(dst).st_atime, self.stat(src)['st_mtime'].timestamp()))
if callback is not None:
callback(src, dst)
Expand Down Expand Up @@ -438,15 +448,15 @@ def link(self, target: str, source: str, type_=afc_link_type_t.SYMLINK):
afc_make_link_req_t.build({'type': type_, 'target': target, 'source': source}))

@path_to_str()
def fopen(self, filename: str, mode='r'):
def fopen(self, filename: str, mode: str = 'r') -> int:
if mode not in AFC_FOPEN_TEXTUAL_MODES:
raise ArgumentError(f'mode can be only one of: {AFC_FOPEN_TEXTUAL_MODES.keys()}')

data = self._do_operation(afc_opcode_t.FILE_OPEN,
afc_fopen_req_t.build({'mode': AFC_FOPEN_TEXTUAL_MODES[mode], 'filename': filename}))
return afc_fopen_resp_t.parse(data).handle

def fclose(self, handle):
def fclose(self, handle: int):
return self._do_operation(afc_opcode_t.FILE_CLOSE, afc_fclose_req_t.build({'handle': handle}))

@path_to_str()
Expand All @@ -459,7 +469,7 @@ def rename(self, source: str, target: str):
raise
raise AfcFileNotFoundError(e.args[0], e.status) from e

def fread(self, handle, sz):
def fread(self, handle: int, sz: bytes) -> bytes:
data = b''
while sz > 0:
if sz > MAXIMUM_READ_SIZE:
Expand Down

0 comments on commit f80ccf6

Please sign in to comment.