Ask the disk for room before a download or an unpack starts.

A disk that filled in the middle ended a download after four tries with an error that named no cause, and an unpack
with half a folder. The rest of a download, the build tools with their unpacked size, an update's archive and a
mod's zip are now measured against the free space first, and the refusal says how much is needed and how much is
free; a disk that fills during a download is named as full at once.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-12 22:19:28 -05:00
parent fe041188f5
commit d0e6cd695e
6 changed files with 156 additions and 4 deletions

View File

@@ -17,9 +17,11 @@ Two checks can accept a server's certificate, tried in turn by request():
A certificate both refuse ends the request with CertificateError: what each check said, whether the PC's clock agrees
with the server's, and what to do."""
import email.utils
import errno
import hashlib
import json
import os
import shutil
import ssl
import time
import urllib.error
@@ -240,10 +242,47 @@ def sha256_file(path):
return h.hexdigest()
SPACE_MARGIN = 32 << 20 # bytes a disk keeps free beyond what is about to be written to it (space_problem)
class NoSpace(DownloadError):
"""The disk has no room for the file. Asking again or another server gets the same answer, so download() does
not retry it, and it keeps the part file: the download goes on from it once there is room."""
def free_bytes(folder):
"""The free bytes of the disk that holds folder, asked of the nearest folder above it that exists; None when that
cannot be asked."""
p = Path(folder).absolute()
for d in (p, *p.parents):
try:
return shutil.disk_usage(d).free
except FileNotFoundError:
continue
except OSError:
return None
return None
def space_problem(folder, need, what):
"""'' when the disk of folder has room for need more bytes and SPACE_MARGIN, or when its free space cannot be
asked; else the sentence that says there is no room for what, how much it takes and how much is free. Asked
before a download or an unpack starts: a disk that fills in the middle otherwise ends it with an error that
names no cause and leaves half of it behind."""
free = free_bytes(folder)
if free is None or free >= max(0, need) + SPACE_MARGIN:
return ''
p = Path(folder).absolute()
return (f'there is no room for {what} on {p.anchor or p} ({(max(0, need) + SPACE_MARGIN) / 1e6:,.0f} MB needed, '
f'{free / 1e6:,.0f} MB free): make room there and try again')
def download(url, dest, sha256=None, size=None, progress=None, cancel=None, headers=None, retries=4, version='dev'):
"""Fetch url to dest through dest.part, resuming a part file a cut connection left. progress(done, total) after
every chunk; cancel() returning True raises Cancelled. A wrong size or sha256 deletes the file and raises
DownloadError. A dest already holding the expected sha256 is kept without a request."""
DownloadError. A dest already holding the expected sha256 is kept without a request. NoSpace, before anything
is written, when the disk has no room for the rest of the file (the expected size, else the size the server
names)."""
dest = Path(dest)
dest.parent.mkdir(parents=True, exist_ok=True)
if sha256 and dest.is_file() and sha256_file(dest) == sha256.lower():
@@ -251,6 +290,10 @@ def download(url, dest, sha256=None, size=None, progress=None, cancel=None, head
progress(dest.stat().st_size, dest.stat().st_size)
return dest
part = dest.with_name(dest.name + '.part')
if size:
full = space_problem(dest.parent, size - (part.stat().st_size if part.is_file() else 0), dest.name)
if full:
raise NoSpace(full)
last_error = None
for attempt in range(retries):
have = part.stat().st_size if part.is_file() else 0
@@ -268,6 +311,10 @@ def download(url, dest, sha256=None, size=None, progress=None, cancel=None, head
# no length, and the bar ran past its end.
coming = int(r.headers.get('Content-Length') or 0)
total = size or (coming + have if coming else 0)
if coming and not size:
full = space_problem(dest.parent, coming, dest.name)
if full:
raise NoSpace(full)
with open(part, 'ab' if have else 'wb') as f:
done = have
while True:
@@ -284,7 +331,7 @@ def download(url, dest, sha256=None, size=None, progress=None, cancel=None, head
if progress:
progress(done, total or done)
break
except (Cancelled, CertificateError):
except (Cancelled, CertificateError, NoSpace):
raise
except DownloadError:
part.unlink(missing_ok=True)
@@ -298,6 +345,10 @@ def download(url, dest, sha256=None, size=None, progress=None, cancel=None, head
raise DownloadError(f'{url}: HTTP {e.code}') from e
last_error = e
except (urllib.error.URLError, OSError, IncompleteRead) as e:
if isinstance(e, OSError) and e.errno == errno.ENOSPC: # the disk filled meanwhile: trying again cannot help
p = dest.parent.absolute()
raise NoSpace(f'{p.anchor or p} is full: {dest.name} could not be written. Make room there and try '
'again') from e
last_error = e
time.sleep(min(2 ** attempt, 16))
else: