Changeset View
Changeset View
Standalone View
Standalone View
swh/fuse/fs/entry.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from __future__ import annotations | from __future__ import annotations | ||||
from dataclasses import dataclass, field | from dataclasses import dataclass, field | ||||
from enum import IntEnum | from enum import IntEnum | ||||
from pathlib import Path | from pathlib import Path | ||||
from stat import S_IFDIR, S_IFLNK, S_IFREG | from stat import S_IFDIR, S_IFLNK, S_IFREG | ||||
from typing import Any, Union | from typing import Any, AsyncIterator, Sequence, Union | ||||
# Avoid cycling import | # Avoid cycling import | ||||
Fuse = "Fuse" | Fuse = "Fuse" | ||||
class EntryMode(IntEnum): | class EntryMode(IntEnum): | ||||
""" Default entry mode and permissions for the FUSE. | """ Default entry mode and permissions for the FUSE. | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | |||||
class FuseDirEntry(FuseEntry): | class FuseDirEntry(FuseEntry): | ||||
""" FUSE virtual directory entry """ | """ FUSE virtual directory entry """ | ||||
async def size(self) -> int: | async def size(self) -> int: | ||||
return 0 | return 0 | ||||
async def __aiter__(self): | async def compute_entries(self) -> Sequence[FuseEntry]: | ||||
""" Return the child entries of a directory entry """ | """ Return the child entries of a directory entry """ | ||||
raise NotImplementedError | raise NotImplementedError | ||||
async def get_entries(self, offset: int = 0) -> AsyncIterator[FuseEntry]: | |||||
""" Return the child entries of a directory entry using direntry cache """ | |||||
cache = self.fuse.cache.direntry.get(self) | |||||
if cache: | |||||
entries = cache | |||||
else: | |||||
entries = [x async for x in self.compute_entries()] | |||||
self.fuse.cache.direntry.set(self, entries) | |||||
# Avoid copy by manual iteration (instead of slicing) and use of a | |||||
seirl: You're doing a copy here | |||||
# generator (instead of returning the full list every time) | |||||
for i in range(offset, len(entries)): | |||||
yield entries[i] | |||||
async def lookup(self, name: str) -> FuseEntry: | async def lookup(self, name: str) -> FuseEntry: | ||||
""" Look up a FUSE entry by name """ | """ Look up a FUSE entry by name """ | ||||
async for entry in self: | async for entry in self.get_entries(): | ||||
if entry.name == name: | if entry.name == name: | ||||
return entry | return entry | ||||
return None | return None | ||||
@dataclass | @dataclass | ||||
class FuseSymlinkEntry(FuseEntry): | class FuseSymlinkEntry(FuseEntry): | ||||
""" FUSE virtual symlink entry | """ FUSE virtual symlink entry | ||||
Show All 15 Lines |
You're doing a copy here