Skip to content

Commit

Permalink
Rename source directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Phan committed Apr 20, 2020
1 parent 4129728 commit 326e867
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 22 additions & 23 deletions pdf-converter/client.py → qubespdfconverter/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class PageError(ValueError):


class ReceiveError(Exception):
"""
"""
"""Raise if an error occurs when reading from STDOUT"""


class RepresentationError(ValueError):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
"""
Expand All @@ -259,28 +271,14 @@ 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


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)
Expand Down Expand Up @@ -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 = []
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions pdf-converter/server.py → qubespdfconverter/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from tempfile import TemporaryDirectory

DEPTH = 8
STDIN_READ_SIZE = 65536

Representation = namedtuple("Representation", ["initial", "final"])

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 326e867

Please sign in to comment.