Close spooler temp file before PowerShell reads it.
NamedTempFile kept an exclusive handle so ReadAllBytes failed on Windows; into_temp_path closes it first. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -139,14 +139,14 @@ impl WinSpoolerTransport {
139
139
140
140
impl super::PrinterTransport for WinSpoolerTransport {
141
141
fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
142
// Close the handle before PowerShell reads: NamedTempFile keeps the file
143
// open, and on Windows that blocks ReadAllBytes ("used by another process").
142
144
let tmp = tempfile::NamedTempFile::new()?;
143
145
std::fs::write(tmp.path(), data)?;
146
let tmp_path = tmp.into_temp_path();
144
147
145
148
let safe_printer = self.printer_name.replace('\'', "''");
146
let safe_file = tmp
147
.path()
148
.to_string_lossy()
149
.replace('\'', "''");
149
let safe_file = tmp_path.to_string_lossy().replace('\'', "''");
150
150
151
151
let script = format!(
152
152
r#"
@@ -229,6 +229,9 @@ Write-Output 'OK'
229
229
.output()
230
230
.map_err(io::Error::other)?;
231
231
232
// Keep tmp_path alive until PowerShell has finished reading it.
233
drop(tmp_path);
234
232
235
if !output.status.success() {
233
236
let stderr = String::from_utf8_lossy(&output.stderr);
234
237
return Err(io::Error::other(stderr.trim()));
@@ -260,4 +263,14 @@ mod tests {
260
263
assert!(is_com_port("COM3"));
261
264
assert!(!is_com_port("USB001"));
262
265
}
266
267
#[test]
268
fn temp_payload_readable_after_handle_closed() {
269
// Mirrors write_all: PowerShell ReadAllBytes needs the file closed on Windows.
270
let tmp = tempfile::NamedTempFile::new().unwrap();
271
std::fs::write(tmp.path(), b"escpos").unwrap();
272
let path = tmp.into_temp_path();
273
let bytes = std::fs::read(&path).unwrap();
274
assert_eq!(bytes, b"escpos");
275
}
263
276
}
@@ -139,14 +139,14 @@ impl WinSpoolerTransport {139
139
140
impl super::PrinterTransport for WinSpoolerTransport {
140
impl super::PrinterTransport for WinSpoolerTransport {
141
fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
141
fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
142
// Close the handle before PowerShell reads: NamedTempFile keeps the file
143
// open, and on Windows that blocks ReadAllBytes ("used by another process").
142
let tmp = tempfile::NamedTempFile::new()?;
144
let tmp = tempfile::NamedTempFile::new()?;
143
std::fs::write(tmp.path(), data)?;
145
std::fs::write(tmp.path(), data)?;
146
let tmp_path = tmp.into_temp_path();
144
147
145
let safe_printer = self.printer_name.replace('\'', "''");
148
let safe_printer = self.printer_name.replace('\'', "''");
146
let safe_file = tmp
149
let safe_file = tmp_path.to_string_lossy().replace('\'', "''");
147
.path()
148
.to_string_lossy()
149
.replace('\'', "''");
150
150
151
let script = format!(
151
let script = format!(
152
r#"
152
r#"
@@ -229,6 +229,9 @@ Write-Output 'OK'229
.output()
229
.output()
230
.map_err(io::Error::other)?;
230
.map_err(io::Error::other)?;
231
231
232
// Keep tmp_path alive until PowerShell has finished reading it.
233
drop(tmp_path);
234
232
if !output.status.success() {
235
if !output.status.success() {
233
let stderr = String::from_utf8_lossy(&output.stderr);
236
let stderr = String::from_utf8_lossy(&output.stderr);
234
return Err(io::Error::other(stderr.trim()));
237
return Err(io::Error::other(stderr.trim()));
@@ -260,4 +263,14 @@ mod tests {260
assert!(is_com_port("COM3"));
263
assert!(is_com_port("COM3"));
261
assert!(!is_com_port("USB001"));
264
assert!(!is_com_port("USB001"));
262
}
265
}
266
267
#[test]
268
fn temp_payload_readable_after_handle_closed() {
269
// Mirrors write_all: PowerShell ReadAllBytes needs the file closed on Windows.
270
let tmp = tempfile::NamedTempFile::new().unwrap();
271
std::fs::write(tmp.path(), b"escpos").unwrap();
272
let path = tmp.into_temp_path();
273
let bytes = std::fs::read(&path).unwrap();
274
assert_eq!(bytes, b"escpos");
275
}
263
}
276
}