Skip to content

Commit

Permalink
#598: fix win32 printing and other improvements:
Browse files Browse the repository at this point in the history
* include sha1 digest of the file data so we can verify integrity
* force binary mode so python on win32 won't screw newlines
* better syslog messages (remove duplicate script name in header)
* fix "no root warning" env var name

git-svn-id: https://xpra.org/svn/Xpra/trunk@8743 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Mar 3, 2015
1 parent b3971bd commit f40e7ac
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
31 changes: 12 additions & 19 deletions src/cups/xpraforwarder
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,19 @@ import urlparse

__version__ = "0.15.0"


#Writes a syslog entry (msg) at the default facility:
def debug(msg):
syslog.syslog(syslog.LOG_DEBUG, "xpraforwarder cups backend: %s" % msg)
syslog.syslog(syslog.LOG_DEBUG, msg)

def info(msg):
syslog.syslog(syslog.LOG_INFO, "xpraforwarder cups backend: %s" % msg)
syslog.syslog(syslog.LOG_INFO, msg)

def err(msg):
syslog.syslog(syslog.LOG_ERR, "xpraforwarder cups backend: %s" % msg)
syslog.syslog(syslog.LOG_ERR, msg)


def exec_command(command, env=os.environ.copy(), shell=True):
#print("exec_command(%s)" % command)
syslog.syslog(syslog.LOG_INFO, "xpraforwarder running: %s" % command)
info("running: %s" % command)
PIPE = subprocess.PIPE
proc = subprocess.Popen(command, stdin=None, stdout=PIPE, stderr=PIPE, env=env, shell=shell)
out,err = proc.communicate()
Expand All @@ -67,12 +65,12 @@ def xpra_print(socket_dir, display, filename, mimetype, source, title, printer,
#in this case, running as root cannot be avoided, so skip the warning:
#(using "su" causes even more problems)
env = os.environ.copy()
env["NO_ROOT_WARNING"] = "1"
env["XPRA_NO_ROOT_WARNING"] = "1"
return exec_command(command, env=env, shell=False)


def do_main():
info("xpraforwarder: %s" % str(sys.argv))
info(" ".join(["'%s'" % x for x in sys.argv]))
if len(sys.argv) == 1:
# Without arguments should give backend info.
# This is also used when lpinfo -v is issued, where it should include "direct this_backend"
Expand All @@ -84,23 +82,19 @@ def do_main():
sys.stdout.flush()
err("Wrong number of arguments. Usage: %s job-id user title copies options [file]" % sys.argv[0])
sys.exit(1)
#job_id = sys.argv[1]
username = sys.argv[2]
title = sys.argv[3]
no_copies = sys.argv[4]
print_options = sys.argv[5]
job_id, username, title, no_copies, print_options = sys.argv[1:6]
if len(sys.argv)==7:
filename = sys.argv[6]
else:
filename = "-"
info("username: %s" % username)
info("title: %s" % title)
info("username: %s, title: %s, filename: %s, job_id: %s" % (username, title, filename, job_id))

dev_uri = os.environ['DEVICE_URI']
parsed_url = urlparse.urlparse(dev_uri)
info("urlparse(%s)=%s" % (dev_uri, parsed_url))
#write_dir = parsed_url.path
attributes = urlparse.parse_qs(parsed_url.query)
info("attributes(%s)=%s" % (parsed_url.query, attributes))
def aget(k, default_value):
v = attributes.get(k)
if v is None:
Expand All @@ -115,10 +109,9 @@ def do_main():
raise Exception("Device URI: client source uuid is missing")
if not display:
raise Exception("Device URI: display number not specified!")
info("display: %s" % display)
#exec_command("cp %s /tmp/print.pdf" % output_file)
#exec_command("ls -la %s /tmp/print.pdf" % output_file)
xpra_print(socket_dir, display, filename, "application/postscript", source, title, printer, no_copies, print_options)
info("xpra display: %s" % display)

xpra_print(socket_dir, display, filename, "application/pdf", source, title, printer, no_copies, print_options)


def main():
Expand Down
38 changes: 27 additions & 11 deletions src/xpra/client/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,29 +612,45 @@ def _process_send_file(self, packet):
else:
assert self.file_transfer
assert filesize>0 and file_data
#check digest if present:
digest = options.get("sha1")
if digest:
import hashlib
u = hashlib.sha1()
u.update(file_data)
filelog("sha1 digest: %s - expected: %s", u.hexdigest(), digest)
assert digest==u.hexdigest(), "invalid file digest %s (expected %s)" % (u.hexdigest(), digest)

#make sure we use a filename that does not exist already:
wanted_filename = os.path.abspath(os.path.join(os.path.expanduser(DOWNLOAD_PATH), os.path.basename(basefilename)))
if mimetype=="application/postscript":
EXTS = {"application/postscript" : "ps",
"application/pdf" : "pdf",
}
ext = EXTS.get(mimetype)
if ext:
#on some platforms (win32),
#we want to force a ".ps" extension for postscript files
#so that the file manager can display them properly when you click on them
if not wanted_filename.endswith(".ps"):
wanted_filename += ".ps"
#we want to force an extension
#so that the file manager can display them properly when you double-click on them
if not wanted_filename.endswith("."+ext):
wanted_filename += "."+ext
filename = wanted_filename
base = 0
while os.path.exists(filename):
filelog("cannot save file as %s: file already exists", filename)
root, ext = os.path.splitext(wanted_filename)
base += 1
filename = root+("-%s" % base)+ext
#maybe use mkstemp instead of EXCL?
fd = os.open(filename, os.O_CREAT | os.O_RDWR | os.O_EXCL)
f = os.fdopen(fd,'wb')
flags = os.O_CREAT | os.O_RDWR | os.O_EXCL
try:
flags |= os.O_BINARY #@UndefinedVariable (win32 only)
except:
pass
fd = os.open(filename, flags)
try:
f.write(file_data)
os.write(fd, file_data)
finally:
f.close()
filelog.info("downloaded %s bytes to %s", filesize, filename)
os.close(fd)
filelog.info("downloaded %s bytes to %s file %s", filesize, mimetype, filename)
if printit:
printer = options.strget("printer")
title = options.strget("title")
Expand Down
3 changes: 2 additions & 1 deletion src/xpra/platform/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def print_files(printer, filenames, title, options):

def printing_finished(printpid):
return True


#default implementation uses pycups:
from xpra.platform import platform_import
Expand Down Expand Up @@ -68,5 +68,6 @@ def print_dict(d):
finally:
clean()


if __name__ == "__main__":
main()
8 changes: 7 additions & 1 deletion src/xpra/server/server_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os.path
import sys
import time
import hashlib

from xpra.log import Logger
log = Logger("server")
Expand Down Expand Up @@ -1380,11 +1381,16 @@ def _process_print(self, proto, packet):
parts = x.split("=", 1)
if len(parts)==2:
print_options[parts[0]] = parts[1]
u = hashlib.sha1()
u.update(file_data)
printlog("sha1 digest: %s", u.hexdigest())
options = {"printer" : printer,
"title" : title,
"copies" : no_copies,
"options" : print_options}
"options" : print_options,
"sha1" : u.hexdigest()}
printlog("parsed printer options: %s", options)

for ss in self._server_sources.values():
if ss.uuid!=source_uuid:
printlog("not sending to %s (wanted uuid=%s)", ss, source_uuid)
Expand Down

0 comments on commit f40e7ac

Please sign in to comment.