open: Handle file:// URLs properly

open(1) was able to handle most URLs as well as paths, but not file://
URLs (which occur when dragging something from the output of ls, for
example). We have to create an URL from the user-supplied argument using
create_with_url_or_path(), check whether it's a file:// URL or not and
*then* use real_path_for() on the URL's path().
This commit is contained in:
Linus Groh 2020-12-24 01:10:04 +01:00 committed by Andreas Kling
parent 46a12e32d3
commit 5bb0bd8c6d

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -44,18 +45,17 @@ int main(int argc, char* argv[])
bool all_ok = true;
for (auto& url_or_path : urls_or_paths) {
String path;
auto realpath_errno = 0;
if (path = Core::File::real_path_for(url_or_path); path.is_null()) {
realpath_errno = errno; // This *should* be preserved from Core::File::real_path_for().
path = url_or_path;
}
auto url = URL::create_with_url_or_path(url_or_path);
URL url = URL::create_with_url_or_path(path);
if (url.protocol() == "file" && realpath_errno) {
warnln("Failed to open '{}': {}", url.path(), strerror(realpath_errno));
all_ok = false;
continue;
if (url.protocol() == "file") {
auto real_path = Core::File::real_path_for(url.path());
if (real_path.is_null()) {
// errno *should* be preserved from Core::File::real_path_for().
warnln("Failed to open '{}': {}", url.path(), strerror(errno));
all_ok = false;
continue;
}
url = URL::create_with_url_or_path(real_path);
}
if (!Desktop::Launcher::open(url)) {