1
+ #[ cfg( windows) ]
2
+ use crate :: bindings:: _get_osfhandle;
1
3
use crate :: bindings:: { lib_ccx_ctx, print_file_report} ;
2
4
use crate :: demuxer:: common_structs:: * ;
3
5
use crate :: libccxr_exports:: demuxer:: copy_demuxer_from_c_to_rust;
@@ -15,8 +17,6 @@ use std::io::{Read, Seek, SeekFrom};
15
17
use std:: os:: fd:: FromRawFd ;
16
18
#[ cfg( windows) ]
17
19
use std:: os:: windows:: io:: FromRawHandle ;
18
- #[ cfg( windows) ]
19
- use std:: os:: windows:: io:: IntoRawHandle ;
20
20
use std:: ptr:: { copy, copy_nonoverlapping} ;
21
21
use std:: time:: { SystemTime , UNIX_EPOCH } ;
22
22
use std:: { mem, ptr, slice} ;
@@ -52,18 +52,29 @@ pub fn init_file_buffer(ctx: &mut CcxDemuxer) -> i32 {
52
52
0
53
53
}
54
54
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
+
55
65
/// This function checks that the current file position matches the expected value.
56
66
#[ allow( unused_variables) ]
57
67
pub fn position_sanity_check ( ctx : & mut CcxDemuxer ) {
58
68
#[ cfg( feature = "sanity_check" ) ]
59
69
{
70
+ use std:: os:: windows:: io:: IntoRawHandle ;
60
71
if ctx. infd != -1 {
61
72
let fd = ctx. infd ;
62
73
// Convert raw fd to a File without taking ownership
63
74
#[ cfg( unix) ]
64
75
let mut file = unsafe { File :: from_raw_fd ( fd) } ;
65
76
#[ cfg( windows) ]
66
- let mut file = unsafe { File :: from_raw_handle ( fd as * mut _ ) } ;
77
+ let mut file = open_windows ( fd) ;
67
78
let realpos_result = file. seek ( SeekFrom :: Current ( 0 ) ) ;
68
79
let realpos = match realpos_result {
69
80
Ok ( pos) => pos as i64 , // Convert to i64 to match C's LLONG
@@ -304,7 +315,7 @@ pub unsafe fn buffered_read_opt(
304
315
#[ cfg( unix) ]
305
316
let mut file = File :: from_raw_fd ( ctx. infd ) ;
306
317
#[ cfg( windows) ]
307
- let mut file = File :: from_raw_handle ( ctx. infd as _ ) ;
318
+ let mut file = open_windows ( ctx. infd ) ;
308
319
let slice = slice:: from_raw_parts_mut ( buffer_ptr, bytes) ;
309
320
match file. read ( slice) {
310
321
Ok ( n) => i = n as isize ,
@@ -323,7 +334,7 @@ pub unsafe fn buffered_read_opt(
323
334
#[ cfg( unix) ]
324
335
let mut file = File :: from_raw_fd ( ctx. infd ) ;
325
336
#[ cfg( windows) ]
326
- let mut file = File :: from_raw_handle ( ctx. infd as _ ) ;
337
+ let mut file = open_windows ( ctx. infd ) ;
327
338
let mut op = file. stream_position ( ) . unwrap_or ( i64:: MAX as u64 ) as i64 ; // Get current pos
328
339
if op == i64:: MAX {
329
340
op = -1 ;
@@ -396,7 +407,7 @@ pub unsafe fn buffered_read_opt(
396
407
#[ cfg( unix) ]
397
408
let mut file = File :: from_raw_fd ( ctx. infd ) ;
398
409
#[ cfg( windows) ]
399
- let mut file = File :: from_raw_handle ( ctx. infd as _ ) ;
410
+ let mut file = open_windows ( ctx. infd ) ;
400
411
let slice = slice:: from_raw_parts_mut (
401
412
ctx. filebuffer . add ( keep as usize ) ,
402
413
FILEBUFFERSIZE - keep as usize ,
@@ -479,7 +490,7 @@ pub unsafe fn buffered_read_opt(
479
490
#[ cfg( unix) ]
480
491
let mut file = File :: from_raw_fd ( ctx. infd ) ;
481
492
#[ cfg( windows) ]
482
- let mut file = File :: from_raw_handle ( ctx. infd as _ ) ;
493
+ let mut file = open_windows ( ctx. infd ) ;
483
494
let slice = slice:: from_raw_parts_mut ( buffer_ptr, bytes) ;
484
495
i = file. read ( slice) . unwrap_or ( i64:: MAX as usize ) as isize ;
485
496
if i == i64:: MAX as usize as isize {
@@ -523,7 +534,7 @@ pub unsafe fn buffered_read_opt(
523
534
#[ cfg( unix) ]
524
535
let mut file = File :: from_raw_fd ( ctx. infd ) ;
525
536
#[ cfg( windows) ]
526
- let mut file = File :: from_raw_handle ( ctx. infd as _ ) ;
537
+ let mut file = open_windows ( ctx. infd ) ;
527
538
528
539
// Try to get current position and seek
529
540
let op_result = file. stream_position ( ) ; // Get current pos
@@ -875,7 +886,7 @@ mod tests {
875
886
#[ cfg( unix) ]
876
887
let mut file = unsafe { File :: from_raw_fd ( fd) } ;
877
888
#[ cfg( windows) ]
878
- let mut file = unsafe { File :: from_raw_handle ( fd as * mut _ ) } ;
889
+ let mut file = unsafe { open_windows ( fd) } ;
879
890
file. seek ( SeekFrom :: Start ( 130 ) ) . unwrap ( ) ;
880
891
881
892
// Prevent double-closing when 'file' drops
0 commit comments