From 5bb0bd8c6dbc91342077f9d81f8ce7b9096ddd1c Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 24 Dec 2020 01:10:04 +0100 Subject: [PATCH] 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(). --- Userland/open.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Userland/open.cpp b/Userland/open.cpp index 0e4b4cc5f6..99c2a7e584 100644 --- a/Userland/open.cpp +++ b/Userland/open.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Sergey Bugaev + * Copyright (c) 2020, Linus Groh * 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)) {