Skip to content

Commit

Permalink
fix invalid file accesses: properly raise ENOENT
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Oct 11, 2023
1 parent b25182d commit 39fcc49
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
7 changes: 4 additions & 3 deletions rose/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ def fs() -> None:
"""Manage the virtual library."""


@fs.command()
@fs.command(context_settings={"ignore_unknown_options": True})
@click.argument("mount_args", nargs=-1, type=click.UNPROCESSED)
@click.pass_obj
def mount(ctx: Context) -> None:
def mount(ctx: Context, mount_args: list[str]) -> None:
"""Mount the virtual library."""
mount_virtualfs(ctx.config)
mount_virtualfs(ctx.config, mount_args)


@fs.command()
Expand Down
63 changes: 42 additions & 21 deletions rose/virtualfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import subprocess
from collections.abc import Iterator
from pathlib import Path
from typing import Any, Literal
from typing import IO, Any, Literal

import fuse

Expand Down Expand Up @@ -55,59 +55,59 @@ def mkstat(mode: Literal["dir", "file"], fsize: int = 4096) -> fuse.Stat:
if len(parts) == 1:
return mkstat("dir")
if not release_exists(self.config, parts[1]):
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
if len(parts) == 2:
return mkstat("dir")
if len(parts) == 3 and (tp := track_exists(self.config, parts[1], parts[2])):
return mkstat("file", tp.stat().st_size)
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")

if parts[0] == "artists":
if len(parts) == 1:
return mkstat("dir")
if not artist_exists(self.config, parts[1]):
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
if len(parts) == 2:
return mkstat("dir")
if not release_exists(self.config, parts[2]):
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
if len(parts) == 3:
return mkstat("dir")
if len(parts) == 4 and (tp := track_exists(self.config, parts[2], parts[3])):
return mkstat("file", tp.stat().st_size)
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")

if parts[0] == "genres":
if len(parts) == 1:
return mkstat("dir")
if not genre_exists(self.config, parts[1]):
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
if len(parts) == 2:
return mkstat("dir")
if not release_exists(self.config, parts[2]):
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
if len(parts) == 3:
return mkstat("dir")
if len(parts) == 4 and (tp := track_exists(self.config, parts[2], parts[3])):
return mkstat("file", tp.stat().st_size)
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")

if parts[0] == "labels":
if len(parts) == 1:
return mkstat("dir")
if not label_exists(self.config, parts[1]):
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
if len(parts) == 2:
return mkstat("dir")
if not release_exists(self.config, parts[2]):
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
if len(parts) == 3:
return mkstat("dir")
if len(parts) == 4 and (tp := track_exists(self.config, parts[2], parts[3])):
return mkstat("file", tp.stat().st_size)
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")

raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")

def readdir(self, path: str, _: Any) -> Iterator[fuse.Direntry]:
logger.debug(f"Received readdir for {path}")
Expand Down Expand Up @@ -191,7 +191,7 @@ def readdir(self, path: str, _: Any) -> Iterator[fuse.Direntry]:
return
return

raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")

def read(self, path: str, size: int, offset: int) -> bytes:
logger.debug(f"Received read for {path}")
Expand All @@ -205,24 +205,45 @@ def read_bytes(p: Path) -> bytes:

if parts[0] == "albums":
if len(parts) != 3:
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
for track in list_tracks(self.config, parts[1]):
if track.virtual_filename == parts[2]:
return read_bytes(track.source_path)
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
if parts[0] in ["artists", "genres", "labels"]:
if len(parts) != 4:
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
for track in list_tracks(self.config, parts[2]):
if track.virtual_filename == parts[3]:
return read_bytes(track.source_path)
raise fuse.FuseError(errno.ENOENT)
raise fuse.FuseError(errno.ENOENT)
raise OSError(errno.ENOENT, "No such file or directory")
raise OSError(errno.ENOENT, "No such file or directory")

def open(self, path: str, flags: str) -> IO[Any]:
logger.debug(f"Received open for {path}")

def mount_virtualfs(c: Config) -> None:
parts = path.split("/")[1:] # First part is always empty string.

if parts[0] == "albums":
if len(parts) != 3:
raise OSError(errno.ENOENT, "No such file or directory")
for track in list_tracks(self.config, parts[1]):
if track.virtual_filename == parts[2]:
return track.source_path.open(flags)
raise OSError(errno.ENOENT, "No such file or directory")
if parts[0] in ["artists", "genres", "labels"]:
if len(parts) != 4:
raise OSError(errno.ENOENT, "No such file or directory")
for track in list_tracks(self.config, parts[2]):
if track.virtual_filename == parts[3]:
return track.source_path.open(flags)
raise OSError(errno.ENOENT, "No such file or directory")
raise OSError(errno.ENOENT, "No such file or directory")


def mount_virtualfs(c: Config, mount_args: list[str]) -> None:
server = VirtualFS(c)
server.parse([str(c.fuse_mount_dir)])
server.parse([str(c.fuse_mount_dir), *mount_args])
server.main()


Expand Down

0 comments on commit 39fcc49

Please sign in to comment.