Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to_file_path for relative paths with drive letters #442

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2411,8 +2411,9 @@ fn file_url_segments_to_pathbuf_windows(host: Option<&str>, mut segments: str::S
}
}
let path = PathBuf::from(string);
debug_assert!(path.is_absolute(),
"to_file_path() failed to produce an absolute Path");
if !path.is_absolute() {
return Err(());
}
Ok(path)
}

Expand Down
23 changes: 23 additions & 0 deletions tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ fn new_path_windows_fun() {
}
}

#[test]
fn to_file_path_for_relative_windows_paths() {
if cfg!(windows) {
let url = Url::parse("file://example.com/C:foo").unwrap();
assert!(url.to_file_path().is_ok());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: unlike other tests, this is ok. I would think that it should be err as well, but std::Path disagrees with me and insists that \\exaple.com\C:foo is an absolute path. Is this a bug in stdlib maybe as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@retep998 perhaps you know? Consider this program:

use std::path::PathBuf;

fn main() {
    let p1 = PathBuf::from(r"C:foo");
    let p2 = PathBuf::from(r"\\example.com\C:foo");
    println!("{} {}", p1.is_absolute(), p2.is_absolute());
}

It currently prints false true. Is this the correct behavior?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C:foo is relative to the current directory for the C drive. \\example.com\C:foo is a UNC path which is absolute.

For more information on the types of paths... https://googleprojectzero.blogspot.com/2016/02/the-definitive-guide-on-win32-to-nt.html


let url = Url::parse("file://example.com/C:").unwrap();
assert!(url.to_file_path().is_err());

let url = Url::parse("file:///E:foo").unwrap();
assert!(url.to_file_path().is_err());

let url = Url::parse("file:///E:").unwrap();
assert!(url.to_file_path().is_err());

let url = Url::parse("file://localhost/C:foo").unwrap();
assert!(url.to_file_path().is_err());

let url = Url::parse("file://localhost/C:").unwrap();
assert!(url.to_file_path().is_err());
}
}


#[test]
fn new_directory_paths() {
Expand Down