From 326e867fa05df8a30e9129272fe6f8903fd2deab Mon Sep 17 00:00:00 2001 From: Jason Phan Date: Mon, 20 Apr 2020 14:25:36 -0500 Subject: [PATCH] Rename source directory --- Makefile | 4 +- .../client.py | 45 +++++++++---------- .../server.py | 12 ++--- .../tests/__init__.py | 0 .../tests/tests.py | 0 5 files changed, 30 insertions(+), 31 deletions(-) rename {pdf-converter => qubespdfconverter}/client.py (91%) rename {pdf-converter => qubespdfconverter}/server.py (97%) rename {pdf-converter => qubespdfconverter}/tests/__init__.py (100%) rename {pdf-converter => qubespdfconverter}/tests/tests.py (100%) diff --git a/Makefile b/Makefile index 76c3c04..05bc7fe 100644 --- a/Makefile +++ b/Makefile @@ -75,8 +75,8 @@ build: install-vm: make install -C doc - install -D pdf-converter/client.py $(DESTDIR)/usr/bin/qvm-convert-pdf - install -D pdf-converter/server.py $(DESTDIR)/usr/lib/qubes/qpdf-convert-server + install -D qubespdfconverter/client.py $(DESTDIR)/usr/bin/qvm-convert-pdf + install -D qubespdfconverter/server.py $(DESTDIR)/usr/lib/qubes/qpdf-convert-server install -d $(DESTDIR)/etc/qubes-rpc ln -s ../../usr/lib/qubes/qpdf-convert-server $(DESTDIR)/etc/qubes-rpc/qubes.PdfConvert install -D qvm-convert-pdf.gnome $(DESTDIR)/usr/lib/qubes/qvm-convert-pdf.gnome diff --git a/pdf-converter/client.py b/qubespdfconverter/client.py similarity index 91% rename from pdf-converter/client.py rename to qubespdfconverter/client.py index f8863b3..e1230a2 100755 --- a/pdf-converter/client.py +++ b/qubespdfconverter/client.py @@ -52,8 +52,7 @@ class PageError(ValueError): class ReceiveError(Exception): - """ - """ + """Raise if an error occurs when reading from STDOUT""" class RepresentationError(ValueError): @@ -202,11 +201,9 @@ async def get_img_dim(proc): async def send_pdf(loop, proc, path): try: - filesize = (await loop.run_in_executor(None, path.stat)).st_size - await send(proc, f"{filesize}\n") - data = await loop.run_in_executor(None, path.read_bytes) await send(proc, data) + proc.stdin.write_eof() except BrokenPipeError: raise @@ -248,6 +245,21 @@ def get_rep(tmpdir, page, initial, final): async def recv_rep(loop, proc, tmpdir, page): """Receive initial representation from the server + @size bytes is guaranteed to have ben received by the time we write @data to + @rep.initial, so a check on how much is written to isn't needed. + + Also, since the server sends the dimensions and contents of each page in a + simple loop, if the server only sends @size - N bytes for a particular + page, either: + + 1. We'll eventually get @size bytes later on as recv_b() will mistake the + other pages' dimensions and contents as part of the current page's + contents and we end up with a malformed irep, which we'll handle later + during representation's conversion. + + 2. The server exits (the loop is the last thing it does) and we get an + EOF, causing a ReceiveError. + :param proc: Qrexec process to read STDIN from :param path: File path which will store the initial representation """ @@ -259,20 +271,6 @@ async def recv_rep(loop, proc, tmpdir, page): except (DimensionError, ReceiveError, RepresentationError): raise - # @size bytes must have been received if we're here, so a check on how much - # is written to @rep.initial isn't needed. - # - # Also, since the server sends the dimensions and contents of each page in a - # simple loop, if the server only sends @size - N bytes for a particular - # page, either: - # - # 1. We'll eventually get @size bytes later on as recv_b() will mistake the - # other pages' dimensions and contents as part of the current page's - # contents and we end up with a malformed irep, which we'll handle later - # during representation's conversion. - # - # 2. The server exits (the loop is the last thing it does) and we get an - # EOF, causing a ReceiveError. await loop.run_in_executor(None, rep.initial.write_bytes, data) return rep, dim @@ -280,7 +278,7 @@ async def recv_rep(loop, proc, tmpdir, page): async def start_convert(rep, dim): cmd = ["convert", "-size", f"{dim.width}x{dim.height}", "-depth", - f"{dim.depth}", f"rgb:{rep.initial}", f"png:{rep.final}"] + f"{dim.depth}", f"rgb:{rep.initial}", f"png:{rep.final}"] try: proc = await asyncio.create_subprocess_exec(*cmd) @@ -363,9 +361,9 @@ async def sanitize(loop, proc, path): print(f"\nConverted PDF saved as: {pdf}") -# TODO: KeyboardInterrupt async def run(loop, paths): - cmd = ["/usr/bin/qrexec-client-vm", "@dispvm", "qubes.PdfConvert"] + # cmd = ["/usr/bin/qrexec-client-vm", "@dispvm", "qubes.PdfConvert"] + cmd = ["/usr/bin/qrexec-client-vm", "disp4978", "qubes.PdfConvert"] procs = [] send_tasks = [] sanitize_tasks = [] @@ -387,7 +385,8 @@ async def run(loop, paths): sanitize_task, wait_proc(proc)) except (BrokenPipeError, DimensionError, PageError, ReceiveError, - RepresentationError, subprocess.CalledProcessError): + RepresentationError, subprocess.CalledProcessError) as e: + print(type(e).__name__) await asyncio.gather(cancel_task(send_task), cancel_task(sanitize_task)) await terminate_proc(proc) diff --git a/pdf-converter/server.py b/qubespdfconverter/server.py similarity index 97% rename from pdf-converter/server.py rename to qubespdfconverter/server.py index 11c8acd..e84a4dc 100755 --- a/pdf-converter/server.py +++ b/qubespdfconverter/server.py @@ -28,6 +28,7 @@ from tempfile import TemporaryDirectory DEPTH = 8 +STDIN_READ_SIZE = 65536 Representation = namedtuple("Representation", ["initial", "final"]) @@ -87,10 +88,10 @@ async def terminate_proc(proc): ############################### -def recv_b(size): +def recv_b(): """Qrexec wrapper for receiving binary data from the client""" try: - untrusted_data = sys.stdin.buffer.read(size) + untrusted_data = sys.stdin.buffer.read() except EOFError as e: raise ReceiveError from e @@ -210,9 +211,8 @@ async def render(loop, page, pdfpath, rep): def recv_pdf(): try: - filesize = int(recvline()) - data = recv_b(filesize) - except (ReceiveError, ValueError): + data = recv_b() + except ReceiveError: raise return data @@ -270,7 +270,7 @@ def main(): try: data = recv_pdf() - except (ReceiveError, ValueError): + except ReceiveError: sys.exit(1) with TemporaryDirectory(prefix="qvm-sanitize-") as tmpdir: diff --git a/pdf-converter/tests/__init__.py b/qubespdfconverter/tests/__init__.py similarity index 100% rename from pdf-converter/tests/__init__.py rename to qubespdfconverter/tests/__init__.py diff --git a/pdf-converter/tests/tests.py b/qubespdfconverter/tests/tests.py similarity index 100% rename from pdf-converter/tests/tests.py rename to qubespdfconverter/tests/tests.py