Skip to content

Commit 08fcbea

Browse files
committed
Windows Failing Regressions
1 parent a7021f1 commit 08fcbea

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

src/rust/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ fn main() {
2121
"ccx_mxf_init", // shall be removed after mxf
2222
"ccx_gxf_probe", // shall be removed after gxf
2323
"ccx_gxf_init", // shall be removed after gxf
24+
#[cfg(windows)]
25+
"_open_osfhandle",
26+
#[cfg(windows)]
27+
"_get_osfhandle",
2428
#[cfg(feature = "enable_ffmpeg")]
2529
"init_ffmpeg",
2630
]);

src/rust/src/demuxer/demux.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ use std::io::{Seek, SeekFrom};
1111

1212
use crate::demuxer::common_structs::*;
1313
use crate::file_functions::file::init_file_buffer;
14+
#[cfg(windows)]
15+
use crate::{bindings::_open_osfhandle, file_functions::file::open_windows};
1416
use cfg_if::cfg_if;
1517
#[cfg(unix)]
1618
use std::os::fd::{FromRawFd, IntoRawFd};
1719
use std::os::raw::{c_char, c_uint};
1820
#[cfg(windows)]
19-
use std::os::windows::io::{FromRawHandle, IntoRawHandle};
21+
use std::os::windows::io::IntoRawHandle;
2022
use std::path::Path;
2123
use std::ptr::{null, null_mut};
2224

@@ -44,7 +46,7 @@ impl CcxDemuxer<'_> {
4446
#[cfg(unix)]
4547
let mut file = unsafe { File::from_raw_fd(in_fd) };
4648
#[cfg(windows)]
47-
let mut file = unsafe { File::from_raw_handle(in_fd as _) };
49+
let mut file = open_windows(in_fd);
4850

4951
// Get current position: equivalent to LSEEK(in, 0, SEEK_CUR);
5052
let current = match file.stream_position() {
@@ -147,7 +149,7 @@ impl CcxDemuxer<'_> {
147149
}
148150
#[cfg(windows)]
149151
{
150-
let file = unsafe { File::from_raw_handle(self.infd as _) };
152+
let file = open_windows(self.infd);
151153
drop(file); // This closes the file descriptor
152154
self.infd = -1;
153155
ccx_options.activity_input_file_closed();
@@ -253,7 +255,12 @@ impl CcxDemuxer<'_> {
253255
}
254256
#[cfg(windows)]
255257
{
256-
self.infd = file.into_raw_handle() as _;
258+
let handle = file.into_raw_handle();
259+
let fd = unsafe { _open_osfhandle(handle as isize, 0) };
260+
if fd == -1 {
261+
return -1;
262+
}
263+
self.infd = fd;
257264
}
258265
}
259266
Err(_) => return -1,

src/rust/src/file_functions/file.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(windows)]
2+
use crate::bindings::_get_osfhandle;
13
use crate::bindings::{lib_ccx_ctx, print_file_report};
24
use crate::demuxer::common_structs::*;
35
use crate::libccxr_exports::demuxer::copy_demuxer_from_c_to_rust;
@@ -15,8 +17,6 @@ use std::io::{Read, Seek, SeekFrom};
1517
use std::os::fd::FromRawFd;
1618
#[cfg(windows)]
1719
use std::os::windows::io::FromRawHandle;
18-
#[cfg(windows)]
19-
use std::os::windows::io::IntoRawHandle;
2020
use std::ptr::{copy, copy_nonoverlapping};
2121
use std::time::{SystemTime, UNIX_EPOCH};
2222
use std::{mem, ptr, slice};
@@ -52,18 +52,29 @@ pub fn init_file_buffer(ctx: &mut CcxDemuxer) -> i32 {
5252
0
5353
}
5454

55+
#[cfg(windows)]
56+
pub fn open_windows(infd: i32) -> File {
57+
// Convert raw fd to a File without taking ownership
58+
let handle = unsafe { _get_osfhandle(infd) };
59+
if handle == -1 {
60+
fatal!(cause = ExitCause::Bug; "Invalid file descriptor for Windows handle.");
61+
}
62+
unsafe { File::from_raw_handle(handle as *mut _) }
63+
}
64+
5565
/// This function checks that the current file position matches the expected value.
5666
#[allow(unused_variables)]
5767
pub fn position_sanity_check(ctx: &mut CcxDemuxer) {
5868
#[cfg(feature = "sanity_check")]
5969
{
70+
use std::os::windows::io::IntoRawHandle;
6071
if ctx.infd != -1 {
6172
let fd = ctx.infd;
6273
// Convert raw fd to a File without taking ownership
6374
#[cfg(unix)]
6475
let mut file = unsafe { File::from_raw_fd(fd) };
6576
#[cfg(windows)]
66-
let mut file = unsafe { File::from_raw_handle(fd as *mut _) };
77+
let mut file = open_windows(fd);
6778
let realpos_result = file.seek(SeekFrom::Current(0));
6879
let realpos = match realpos_result {
6980
Ok(pos) => pos as i64, // Convert to i64 to match C's LLONG
@@ -304,7 +315,7 @@ pub unsafe fn buffered_read_opt(
304315
#[cfg(unix)]
305316
let mut file = File::from_raw_fd(ctx.infd);
306317
#[cfg(windows)]
307-
let mut file = File::from_raw_handle(ctx.infd as _);
318+
let mut file = open_windows(ctx.infd);
308319
let slice = slice::from_raw_parts_mut(buffer_ptr, bytes);
309320
match file.read(slice) {
310321
Ok(n) => i = n as isize,
@@ -323,7 +334,7 @@ pub unsafe fn buffered_read_opt(
323334
#[cfg(unix)]
324335
let mut file = File::from_raw_fd(ctx.infd);
325336
#[cfg(windows)]
326-
let mut file = File::from_raw_handle(ctx.infd as _);
337+
let mut file = open_windows(ctx.infd);
327338
let mut op = file.stream_position().unwrap_or(i64::MAX as u64) as i64; // Get current pos
328339
if op == i64::MAX {
329340
op = -1;
@@ -396,7 +407,7 @@ pub unsafe fn buffered_read_opt(
396407
#[cfg(unix)]
397408
let mut file = File::from_raw_fd(ctx.infd);
398409
#[cfg(windows)]
399-
let mut file = File::from_raw_handle(ctx.infd as _);
410+
let mut file = open_windows(ctx.infd);
400411
let slice = slice::from_raw_parts_mut(
401412
ctx.filebuffer.add(keep as usize),
402413
FILEBUFFERSIZE - keep as usize,
@@ -479,7 +490,7 @@ pub unsafe fn buffered_read_opt(
479490
#[cfg(unix)]
480491
let mut file = File::from_raw_fd(ctx.infd);
481492
#[cfg(windows)]
482-
let mut file = File::from_raw_handle(ctx.infd as _);
493+
let mut file = open_windows(ctx.infd);
483494
let slice = slice::from_raw_parts_mut(buffer_ptr, bytes);
484495
i = file.read(slice).unwrap_or(i64::MAX as usize) as isize;
485496
if i == i64::MAX as usize as isize {
@@ -523,7 +534,7 @@ pub unsafe fn buffered_read_opt(
523534
#[cfg(unix)]
524535
let mut file = File::from_raw_fd(ctx.infd);
525536
#[cfg(windows)]
526-
let mut file = File::from_raw_handle(ctx.infd as _);
537+
let mut file = open_windows(ctx.infd);
527538

528539
// Try to get current position and seek
529540
let op_result = file.stream_position(); // Get current pos
@@ -875,7 +886,7 @@ mod tests {
875886
#[cfg(unix)]
876887
let mut file = unsafe { File::from_raw_fd(fd) };
877888
#[cfg(windows)]
878-
let mut file = unsafe { File::from_raw_handle(fd as *mut _) };
889+
let mut file = unsafe { open_windows(fd) };
879890
file.seek(SeekFrom::Start(130)).unwrap();
880891

881892
// Prevent double-closing when 'file' drops

0 commit comments

Comments
 (0)