• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::fs::File;
6 use std::io::ErrorKind as IoErrorKind;
7 use std::os::windows::io::AsRawHandle;
8 use std::os::windows::io::FromRawHandle;
9 use std::os::windows::io::IntoRawHandle;
10 use std::os::windows::io::OwnedHandle;
11 use std::os::windows::io::RawHandle;
12 use std::os::windows::raw::HANDLE;
13 
14 use crate::rutabaga_os::descriptor::AsRawDescriptor;
15 use crate::rutabaga_os::descriptor::FromRawDescriptor;
16 use crate::rutabaga_os::descriptor::IntoRawDescriptor;
17 use crate::rutabaga_os::DescriptorType;
18 
19 pub type RawDescriptor = RawHandle;
20 // Same as winapi::um::handleapi::INVALID_HANDLE_VALUE, but avoids compile issues.
21 pub const DEFAULT_RAW_DESCRIPTOR: RawDescriptor = -1isize as HANDLE;
22 
23 type Error = std::io::Error;
24 type Result<T> = std::result::Result<T, Error>;
25 
26 pub struct OwnedDescriptor {
27     owned: OwnedHandle,
28 }
29 
30 impl OwnedDescriptor {
try_clone(&self) -> Result<OwnedDescriptor>31     pub fn try_clone(&self) -> Result<OwnedDescriptor> {
32         let clone = self.owned.try_clone()?;
33         Ok(OwnedDescriptor { owned: clone })
34     }
35 
determine_type(&self) -> Result<DescriptorType>36     pub fn determine_type(&self) -> Result<DescriptorType> {
37         Err(Error::from(IoErrorKind::Unsupported))
38     }
39 }
40 
41 impl AsRawDescriptor for OwnedDescriptor {
as_raw_descriptor(&self) -> RawDescriptor42     fn as_raw_descriptor(&self) -> RawDescriptor {
43         self.owned.as_raw_handle()
44     }
45 }
46 
47 impl FromRawDescriptor for OwnedDescriptor {
48     // SAFETY: It is caller's responsibility to ensure that the descriptor is valid.
from_raw_descriptor(descriptor: RawDescriptor) -> Self49     unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self {
50         OwnedDescriptor {
51             owned: OwnedHandle::from_raw_handle(descriptor),
52         }
53     }
54 }
55 
56 impl IntoRawDescriptor for OwnedDescriptor {
into_raw_descriptor(self) -> RawDescriptor57     fn into_raw_descriptor(self) -> RawDescriptor {
58         self.owned.into_raw_handle()
59     }
60 }
61 
62 impl IntoRawDescriptor for File {
into_raw_descriptor(self) -> RawDescriptor63     fn into_raw_descriptor(self) -> RawDescriptor {
64         self.into_raw_handle()
65     }
66 }
67 
68 impl From<File> for OwnedDescriptor {
from(f: File) -> OwnedDescriptor69     fn from(f: File) -> OwnedDescriptor {
70         OwnedDescriptor { owned: f.into() }
71     }
72 }
73