Skip to content

Crash (NotADirectoryError / OSError) when the per-book output path is a file or symlink #3

Description

@adisakshya

Context

split_book.py, write_chunks.

Problem

Before writing, write_chunks unconditionally does:

if out_path.exists():
    shutil.rmtree(out_path)
out_path.mkdir(parents=True, exist_ok=True)

shutil.rmtree only works on plain directories. If <out>/<input-stem> already exists as something else, the call raises an uncaught exception and the tool dies with a raw Python traceback instead of the clear stderr messages used everywhere else in the tool (e.g. load_reader).

Reproduction (both confirmed)

Case A — plain file at the output path:

  1. mkdir -p out_dir && touch out_dir/mybook (a file, not a directory, named after the input's stem)
  2. python split_book.py mybook.pdf --out out_dir
  3. Crashes with NotADirectoryError: [Errno 20] Not a directory: '.../out_dir/mybook'

Case B — symlink at the output path:

  1. mkdir -p out_dir && ln -s /some/other/dir out_dir/mybook
  2. python split_book.py mybook.pdf --out out_dir
  3. Crashes with OSError: Cannot call rmtree on a symbolic link

Expected

A clear, actionable stderr message and a clean exit (code 1) — e.g. "Error: output path '...' exists and is not a plain directory; remove it or choose a different --out."

Actual

Raw, unhandled traceback in both cases (see above).

Acceptance criteria

  • Both reproduction cases above produce a clear error message instead of a traceback.
  • Regression tests cover both cases.

Note: the same shutil.rmtree call is also the source of a silent-data-loss bug (unrelated files in the output directory get deleted with no warning) — see the follow-up issue for that; a fix here should probably be designed together with it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions