From af28b5380cd8666872de165e6b2f261e9da2248c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 12:51:04 -0800 Subject: [PATCH] Print: a remote CUPS scheduler supplies its own paper sizes When CUPS_SERVER or client.conf's ServerName names a remote scheduler, the Print dialog no longer reads a same-named PPD under /etc/cups/ppd. The job goes to the remote queue, so the sizes now come from that server through lpoptions and IPP. A domain socket, localhost, 127.0.0.1 and ::1, with an optional port and /version= suffix, still count as local and keep the PPD lookup. --- apps/vectorcraft/src/printing.rs | 67 ++++++++++++++++++++++++++++---- docs/mcp.md | 3 +- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/apps/vectorcraft/src/printing.rs b/apps/vectorcraft/src/printing.rs index f81ca95306..7bfb550876 100644 --- a/apps/vectorcraft/src/printing.rs +++ b/apps/vectorcraft/src/printing.rs @@ -169,12 +169,17 @@ mod system { if printer.contains(['/', '\\']) || printer.starts_with('.') { return Vec::new(); } - const PPD_CAP: u64 = 16 << 20; - let mut bytes = Vec::new(); - let read = std::fs::File::open(format!("/etc/cups/ppd/{printer}.ppd")).and_then(|f| f.take(PPD_CAP).read_to_end(&mut bytes)); - let papers = if read.is_ok() { vectorcraft_ui_egui::print::ppd_papers(&craft::print::ppd_text(&bytes)) } else { Vec::new() }; - if !papers.is_empty() { - return papers; + let named = cups_server(); + // A local PPD describes a local queue. With a remote scheduler the job goes to that server's queue, + // so its sizes come from the server. + if scheduler_is_local(named.as_deref()) { + const PPD_CAP: u64 = 16 << 20; + let mut bytes = Vec::new(); + let read = std::fs::File::open(format!("/etc/cups/ppd/{printer}.ppd")).and_then(|f| f.take(PPD_CAP).read_to_end(&mut bytes)); + let papers = if read.is_ok() { vectorcraft_ui_egui::print::ppd_papers(&craft::print::ppd_text(&bytes)) } else { Vec::new() }; + if !papers.is_empty() { + return papers; + } } // A driverless or remote queue has no PPD here: `lpoptions` asks the queue's server for its sizes. let mut command = Command::new("lpoptions"); @@ -188,7 +193,7 @@ mod system { // A queue whose server has no PPD either: its IPP `media-supported` attribute, from the scheduler CUPS uses. let port = vectorcraft_ui_egui::print::ipp_port(std::env::var("IPP_PORT").ok().as_deref()); // With no server named, CUPS uses its domain socket when it exists, and `localhost` on `IPP_PORT` otherwise. - let server = cups_server().or_else(|| { + let server = named.or_else(|| { let sockets = ["/private/var/run/cupsd", "/run/cups/cups.sock", "/var/run/cups/cups.sock"]; (!sockets.iter().any(|s| std::path::Path::new(s).exists())).then(|| "localhost".to_string()) }); @@ -242,6 +247,25 @@ mod system { client_conf_server_name(&text) }) } + /// Whether a scheduler named by `cups_server` runs on this machine: none named, a domain socket path, + /// or `localhost`, `127.0.0.1` or `::1`, each with an optional `:port` and `/version=` suffix. + #[cfg(not(windows))] + pub(super) fn scheduler_is_local(server: Option<&str>) -> bool { + let Some(server) = server.map(str::trim) else { return true }; + if server.starts_with('/') { + return true; + } + // A network name may end in `/version=1.1`. + let server = server.split_once('/').map_or(server, |(server, _)| server); + let host = if let Some(rest) = server.strip_prefix('[') { + rest.split_once(']').map_or(rest, |(host, _)| host) + } else if server.matches(':').count() == 1 { + server.split_once(':').map_or(server, |(host, _)| host) + } else { + server + }; + host.eq_ignore_ascii_case("localhost") || host == "127.0.0.1" || host == "::1" + } /// The last `ServerName` in a `client.conf`. CUPS (`cupsFileGetConf`) looks only at the first `#` on a /// line: unescaped, it starts a comment that runs to the end of the line; as `\#`, the backslash is /// dropped and the rest of the line is kept as written. @@ -557,7 +581,34 @@ mod turn_tests { #[cfg(all(test, not(windows)))] mod client_conf_tests { - use super::system::client_conf_server_name; + use super::system::{client_conf_server_name, scheduler_is_local}; + + #[test] + fn only_a_scheduler_on_this_machine_counts_as_local() { + for local in [ + None, + Some("/run/cups/cups.sock"), + Some("localhost"), + Some("LocalHost:631"), + Some("127.0.0.1:8631"), + Some("::1"), + Some("[::1]:631"), + Some("localhost/version=1.1"), + ] { + assert!(scheduler_is_local(local), "{local:?}"); + } + for remote in [ + "print.example.com", + "print.example.com:631", + "192.0.2.7", + "[2001:db8::1]:631", + "2001:db8::1", + "localhost.example.com", + "print.example.com:631/version=1.1", + ] { + assert!(!scheduler_is_local(Some(remote)), "{remote}"); + } + } #[test] fn server_name_drops_a_trailing_comment_and_keeps_an_escaped_hash() { diff --git a/docs/mcp.md b/docs/mcp.md index bf0f467ea4..fb72b22769 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -13252,7 +13252,8 @@ default printer) or, with `toFile`, a `path` or no printing available, saves the driver's keyword for the paper (`Letter`, `CR80`, `iso_a4_210x297mm`), which the job asks the print queue for as its `media` on macOS and Linux. `paper` is an error on Windows, with file output, with `directPdf`, or when the value is not such a keyword. The Print dialog sends the keyword of the Size chosen when the printer's driver lists that paper. A printer whose PPD is not on this machine (a driverless or -remote queue) lists the paper sizes `lpoptions -l` reports for it, and when its server has no PPD either, the sizes +remote queue), or any printer when `CUPS_SERVER` or `ServerName` names a scheduler other than a domain socket, +`localhost`, `127.0.0.1` or `::1`, lists the paper sizes `lpoptions -l` reports for it, and when its server has no PPD either, the sizes of its IPP `media-supported` attribute. That lookup asks the server named by `CUPS_SERVER` or, on Linux, `client.conf`'s `ServerName`; a server named without a port, or `localhost` when no server is named and CUPS has no domain socket, is asked on the port in `IPP_PORT` when it is set, and a port in the server name takes precedence.