Skip to content

Commit

Permalink
Revert "adding support for iOS"
Browse files Browse the repository at this point in the history
This reverts commit 276d522.
  • Loading branch information
abishekgoda committed Dec 26, 2024
1 parent 306205d commit 8e22ed3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,5 @@ description = "Get the filesystem path of a file."
[target.'cfg(target_os="macos")'.dependencies]
libc = "0.2"

[target.'cfg(target_os="ios")'.dependencies]
libc = {version = "0.2", default_features = false}

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["std", "fileapi"] }
winapi = { version = "0.3", features = ["std", "fileapi"] }
39 changes: 18 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,40 @@
//! `filepath` contains an extension trait for `std::fs::File` providing a `path` method.
//!
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[cfg(target_os="macos")]
extern crate libc;

#[cfg(windows)]
extern crate winapi;

use std::fs::File;
use std::io;
#[cfg(target_os = "linux")]
#[cfg(target_os="linux")]
use std::path::Path;
use std::path::PathBuf;
use std::io;
use std::fs::File;
#[cfg(windows)]
use std::ptr;

#[cfg(target_os = "linux")]
#[cfg(target_os="linux")]
use std::fs::read_link;

#[cfg(unix)]
use std::os::unix::io::AsRawFd;
#[cfg(windows)]
use std::os::windows::io::AsRawHandle;

#[cfg(any(target_os = "macos", target_os = "ios", windows))]
#[cfg(any(target_os="macos", windows))]
use std::ffi::OsString;

#[cfg(any(target_os = "macos", target_os = "ios"))]
#[cfg(target_os="macos")]
use std::os::unix::ffi::OsStringExt;
#[cfg(windows)]
use std::os::windows::prelude::*;

#[cfg(windows)]
use winapi::um::fileapi;

#[cfg(any(target_os = "macos", target_os = "ios"))]
const F_GETPATH: i32 = 50;
#[cfg(target_os="macos")]
const F_GETPATH : i32 = 50;

/// An extension trait for `std::fs::File` providing a `path` method.
pub trait FilePath {
Expand Down Expand Up @@ -66,14 +65,14 @@ pub trait FilePath {
}

impl FilePath for File {
#[cfg(target_os = "linux")]
#[cfg(target_os="linux")]
fn path(&self) -> io::Result<PathBuf> {
let fd = self.as_raw_fd();
let path = Path::new("/proc/self/fd/").join(fd.to_string());
read_link(path)
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
#[cfg(target_os="macos")]
fn path(&self) -> io::Result<PathBuf> {
let fd = self.as_raw_fd();
let mut path = vec![0; libc::PATH_MAX as usize + 1];
Expand All @@ -99,29 +98,27 @@ impl FilePath for File {
}

let mut path = Vec::with_capacity(len as usize);
let len2 = unsafe {
fileapi::GetFinalPathNameByHandleW(self.as_raw_handle(), path.as_mut_ptr(), len, 0)
};
let len2 = unsafe { fileapi::GetFinalPathNameByHandleW(self.as_raw_handle(), path.as_mut_ptr(),
len, 0) };
// Handle unlikely case that path length changed between those two calls.
if len2 == 0 || len2 >= len {
return Err(io::Error::last_os_error());
}
unsafe { path.set_len(len2 as usize) };

// Turn the \\?\UNC\ network path prefix into \\.
let prefix = [
'\\' as _, '\\' as _, '?' as _, '\\' as _, 'U' as _, 'N' as _, 'C' as _, '\\' as _,
];
let prefix = ['\\' as _, '\\' as _, '?' as _, '\\' as _, 'U' as _, 'N' as _, 'C' as _,
'\\' as _];
if path.starts_with(&prefix) {
let mut network_path: Vec<u16> = vec!['\\' as u16, '\\' as u16];
network_path.extend_from_slice(&path[prefix.len()..]);
network_path.extend_from_slice(&path[prefix.len() ..]);
return Ok(PathBuf::from(OsString::from_wide(&network_path)));
}

// Remove the \\?\ prefix.
let prefix = ['\\' as _, '\\' as _, '?' as _, '\\' as _];
if path.starts_with(&prefix) {
return Ok(PathBuf::from(OsString::from_wide(&path[prefix.len()..])));
return Ok(PathBuf::from(OsString::from_wide(&path[prefix.len() ..])));
}

Ok(PathBuf::from(OsString::from_wide(&path)))
Expand All @@ -130,8 +127,8 @@ impl FilePath for File {

#[cfg(test)]
mod tests {
use std::fs::{remove_file, File};
use std::io::prelude::*;
use std::fs::{remove_file, File};
use FilePath;

#[test]
Expand Down

0 comments on commit 8e22ed3

Please sign in to comment.