From 5919607ad09c112b02d24da90ee0ef4edd72494b Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 30 May 2024 23:55:37 +0000 Subject: [PATCH] sanitize fs-paths in archive error summary also gets rid of a dumb debug print i forgot --- copyparty/httpcli.py | 3 +-- copyparty/star.py | 8 ++++---- copyparty/sutil.py | 16 ++++++++++------ copyparty/szip.py | 8 ++++---- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 191c9a59..c6ea483b 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -759,7 +759,6 @@ class HttpCli(object): is_jinja = True if is_jinja: - print("applying jinja") with self.conn.hsrv.mutex: if html not in self.conn.hsrv.j2: j2env = jinja2.Environment() @@ -3423,7 +3422,7 @@ class HttpCli(object): bgen = packer( self.log, - self.args, + self.asrv, fgen, utf8="utf" in uarg, pre_crc="crc" in uarg, diff --git a/copyparty/star.py b/copyparty/star.py index 6d93f442..41ee1a69 100644 --- a/copyparty/star.py +++ b/copyparty/star.py @@ -1,13 +1,13 @@ # coding: utf-8 from __future__ import print_function, unicode_literals -import argparse import re import stat import tarfile from queue import Queue +from .authsrv import AuthSrv from .bos import bos from .sutil import StreamArc, errdesc from .util import Daemon, fsenc, min_ex @@ -45,12 +45,12 @@ class StreamTar(StreamArc): def __init__( self, log: "NamedLogger", - args: argparse.Namespace, + asrv: AuthSrv, fgen: Generator[dict[str, Any], None, None], cmp: str = "", **kwargs: Any ): - super(StreamTar, self).__init__(log, args, fgen) + super(StreamTar, self).__init__(log, asrv, fgen) self.ci = 0 self.co = 0 @@ -148,7 +148,7 @@ class StreamTar(StreamArc): errors.append((f["vp"], ex)) if errors: - self.errf, txt = errdesc(errors) + self.errf, txt = errdesc(self.asrv.vfs, errors) self.log("\n".join(([repr(self.errf)] + txt[1:]))) self.ser(self.errf) diff --git a/copyparty/sutil.py b/copyparty/sutil.py index 01e5f525..a01e2c66 100644 --- a/copyparty/sutil.py +++ b/copyparty/sutil.py @@ -1,15 +1,15 @@ # coding: utf-8 from __future__ import print_function, unicode_literals -import argparse import os import tempfile from datetime import datetime from .__init__ import CORES +from .authsrv import AuthSrv, VFS from .bos import bos from .th_cli import ThumbCli -from .util import UTC, vjoin +from .util import UTC, vjoin, vol_san if True: # pylint: disable=using-constant-test from typing import Any, Generator, Optional @@ -21,12 +21,13 @@ class StreamArc(object): def __init__( self, log: "NamedLogger", - args: argparse.Namespace, + asrv: AuthSrv, fgen: Generator[dict[str, Any], None, None], **kwargs: Any ): self.log = log - self.args = args + self.asrv = asrv + self.args = asrv.args self.fgen = fgen self.stopped = False @@ -103,15 +104,18 @@ def enthumb( return f -def errdesc(errors: list[tuple[str, str]]) -> tuple[dict[str, Any], list[str]]: +def errdesc(vfs: VFS, errors: list[tuple[str, str]]) -> tuple[dict[str, Any], list[str]]: report = ["copyparty failed to add the following files to the archive:", ""] for fn, err in errors: report.extend([" file: {}".format(fn), "error: {}".format(err), ""]) + btxt = "\r\n".join(report).encode("utf-8", "replace") + btxt = vol_san(list(vfs.all_vols.values()), btxt) + with tempfile.NamedTemporaryFile(prefix="copyparty-", delete=False) as tf: tf_path = tf.name - tf.write("\r\n".join(report).encode("utf-8", "replace")) + tf.write(btxt) dt = datetime.now(UTC).strftime("%Y-%m%d-%H%M%S") diff --git a/copyparty/szip.py b/copyparty/szip.py index de6b0851..02b51b5c 100644 --- a/copyparty/szip.py +++ b/copyparty/szip.py @@ -1,12 +1,12 @@ # coding: utf-8 from __future__ import print_function, unicode_literals -import argparse import calendar import stat import time import zlib +from .authsrv import AuthSrv from .bos import bos from .sutil import StreamArc, errdesc from .util import min_ex, sanitize_fn, spack, sunpack, yieldfile @@ -219,13 +219,13 @@ class StreamZip(StreamArc): def __init__( self, log: "NamedLogger", - args: argparse.Namespace, + asrv: AuthSrv, fgen: Generator[dict[str, Any], None, None], utf8: bool = False, pre_crc: bool = False, **kwargs: Any ) -> None: - super(StreamZip, self).__init__(log, args, fgen) + super(StreamZip, self).__init__(log, asrv, fgen) self.utf8 = utf8 self.pre_crc = pre_crc @@ -302,7 +302,7 @@ class StreamZip(StreamArc): mbuf = b"" if errors: - errf, txt = errdesc(errors) + errf, txt = errdesc(self.asrv.vfs, errors) self.log("\n".join(([repr(errf)] + txt[1:]))) for x in self.ser(errf): yield x