Skip to content

Commit

Permalink
bugfix: continue nfs readdir when it's not done yet (+test it),
Browse files Browse the repository at this point in the history
misc: print some informative prints in `ampm search`,
misc: use `$BROWSER` in `ampm search`,
bump version to v1.5.3
  • Loading branch information
Wazzaps committed May 31, 2023
1 parent 503b757 commit 4e4da9e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ampm/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.5.2'
__version__ = '1.5.3'
9 changes: 8 additions & 1 deletion ampm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ def search(ctx: click.Context):
repos = RepoGroup(remote_uri=ctx.obj['server'])
query = ArtifactQuery('', {})

print("Generating index... (Hint: try `ampm --offline search` for faster generation)", file=sys.stderr)

if query.is_exact:
artifacts = [repos.lookup_single(query)]
else:
Expand All @@ -499,9 +501,14 @@ def search(ctx: click.Context):
with open(fd, 'w') as f:
f.write(_index_web_format_artifact_metadata(artifacts, index_webpage_template.read_text(), ''))

print(f"Opening {filename}", file=sys.stderr)

env = os.environ.copy()
env.pop('LD_LIBRARY_PATH', None)
subprocess.call(['xdg-open', filename], env=env)
if 'BROWSER' in env:
subprocess.call([env['BROWSER'], filename], env=env)
else:
subprocess.call(['xdg-open', filename], env=env)


@cli.command()
Expand Down
55 changes: 33 additions & 22 deletions ampm/repo/nfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,33 +210,44 @@ def _create_with_dirs(self, remote_path: List[str]):
def list_dir(self, remote_path: str):
_validate_path(remote_path)
fh, _attrs = self._open(self._splitpath(remote_path))
readdir_res = self.nfs3.readdir(fh)
if readdir_res["status"] == NFS3_OK:
entry = readdir_res["resok"]["reply"]["entries"]
while entry:
yield entry[0]['name']
entry = entry[0]['nextentry']
elif readdir_res["status"] == NFS3ERR_NOTDIR:
raise NotADirectoryError()
else:
raise IOError(f"NFS readdir failed: code={readdir_res['status']} ({NFSSTAT3[readdir_res['status']]})")
cookie = 0
while True:
readdir_res = self.nfs3.readdir(fh, cookie=cookie)
if readdir_res["status"] == NFS3_OK:
entry = readdir_res["resok"]["reply"]["entries"]
while entry:
yield entry[0]['name']
cookie = entry[0]['cookie']
entry = entry[0]['nextentry']
if readdir_res["resok"]["reply"]["eof"]:
break
elif readdir_res["status"] == NFS3ERR_NOTDIR:
raise NotADirectoryError()
else:
raise IOError(f"NFS readdir failed: code={readdir_res['status']} ({NFSSTAT3[readdir_res['status']]})")

def walk_files(self, remote_path: str, include_dirs: bool = False):
_validate_path(remote_path)
fh, _attrs = self._open(self._splitpath(remote_path))
readdir_res = self.nfs3.readdir(fh)
if readdir_res["status"] == NFS3_OK:
if include_dirs:
cookie = 0
while True:
readdir_res = self.nfs3.readdir(fh, cookie=cookie)
if readdir_res["status"] == NFS3_OK:
if include_dirs:
yield remote_path
entry = readdir_res["resok"]["reply"]["entries"]
while entry:
if not entry[0]['name'].startswith(b'.'):
yield from self.walk_files(remote_path + '/' + entry[0]['name'].decode(), include_dirs)
cookie = entry[0]['cookie']
entry = entry[0]['nextentry']
if readdir_res["resok"]["reply"]["eof"]:
break
elif readdir_res["status"] == NFS3ERR_NOTDIR:
yield remote_path
entry = readdir_res["resok"]["reply"]["entries"]
while entry:
if not entry[0]['name'].startswith(b'.'):
yield from self.walk_files(remote_path + '/' + entry[0]['name'].decode(), include_dirs)
entry = entry[0]['nextentry']
elif readdir_res["status"] == NFS3ERR_NOTDIR:
yield remote_path
else:
raise IOError(f"NFS readdir failed: code={readdir_res['status']} ({NFSSTAT3[readdir_res['status']]})")
return
else:
raise IOError(f"NFS readdir failed: code={readdir_res['status']} ({NFSSTAT3[readdir_res['status']]})")

def walk_files_dirs_at_end(self, remote_path: str):
"""Transform the output of `walk_files` such that the dirs appear after their contents"""
Expand Down
24 changes: 24 additions & 0 deletions tests/test_nfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,27 @@ def test_path_traversal(clean_repos, nfs_repo: NfsRepo, nfs_mount_path: Path):
# Readlink
with pytest.raises(NiceTrySagi):
nfs.readlink(f'{str(remote_path)}/../foo3.txt')


def test_big_readdir(clean_repos, nfs_repo: NfsRepo, nfs_mount_path: Path):
_ = clean_repos
nfs = NfsConnection(nfs_repo.host, nfs_repo.mount_path)

with nfs.connected():
expected_files1 = [b'.', b'..', b'_dir']
expected_files2 = ['/_dir/inner']

(nfs_mount_path / '_dir').mkdir()
(nfs_mount_path / '_dir' / 'inner').write_text('inner')

for i in range(1024):
(nfs_mount_path / f'{i}.txt').write_text(f'{i}')
expected_files1.append(f'{i}.txt'.encode())
expected_files2.append(f'/{i}.txt')

expected_files1.sort()
expected_files2.sort()

assert list(sorted(list(nfs.list_dir('')))) == expected_files1

assert list(sorted(list(nfs.walk_files('')))) == expected_files2

0 comments on commit 4e4da9e

Please sign in to comment.