diff --git a/runtime/bin/builtin_impl_sources.gni b/runtime/bin/builtin_impl_sources.gni index 385df57a706..97fcb968d28 100644 --- a/runtime/bin/builtin_impl_sources.gni +++ b/runtime/bin/builtin_impl_sources.gni @@ -17,8 +17,6 @@ builtin_impl_sources = [ "crypto_win.cc", "dartutils.cc", "dartutils.h", - "dartdev_utils.cc", - "dartdev_utils.h", "directory.cc", "directory.h", "directory_android.cc", @@ -26,8 +24,6 @@ builtin_impl_sources = [ "directory_linux.cc", "directory_macos.cc", "directory_win.cc", - "exe_utils.h", - "exe_utils.cc", "extensions.h", "extensions.cc", "extensions_android.cc", diff --git a/runtime/bin/dartdev_utils.cc b/runtime/bin/dartdev_utils.cc deleted file mode 100644 index b59f7a81aa2..00000000000 --- a/runtime/bin/dartdev_utils.cc +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -#include "bin/dartdev_utils.h" - -#include - -#include "bin/directory.h" -#include "bin/exe_utils.h" -#include "bin/file.h" -#include "platform/utils.h" - -namespace dart { -namespace bin { - -typedef struct { - const char* command; - const char* snapshot_name; -} DartDevCommandMapping; - -static const DartDevCommandMapping dart_dev_commands[] = { - {"format", "dartfmt.dart.snapshot"}, - {"pub", "pub.dart.snapshot"}, -}; - -static const DartDevCommandMapping* FindCommandMapping(const char* command) { - intptr_t num_commands = - sizeof(dart_dev_commands) / sizeof(dart_dev_commands[0]); - for (intptr_t i = 0; i < num_commands; i++) { - const DartDevCommandMapping& command_mapping = dart_dev_commands[i]; - if (strcmp(command, command_mapping.command) == 0) { - return &command_mapping; - } - } - return nullptr; -} - -bool DartDevUtils::ShouldParseCommand(const char* script_uri) { - return !File::ExistsUri(nullptr, script_uri); -} - -bool DartDevUtils::TryParseCommandFromScriptName(char** script_name) { - const DartDevCommandMapping* command = FindCommandMapping(*script_name); - - // Either the command doesn't exist or we've been given an HTTP resource. - if (command == nullptr) { - return true; - } - - // |dir_prefix| includes the last path seperator. - auto dir_prefix = std::unique_ptr( - EXEUtils::GetDirectoryPrefixFromExeName(), free); - - // First assume we're in dart-sdk/bin. - char* snapshot_path = Utils::SCreate("%s/snapshots/%s", dir_prefix.get(), - command->snapshot_name); - if (File::Exists(nullptr, snapshot_path)) { - free(*script_name); - *script_name = snapshot_path; - return true; - } - free(snapshot_path); - - // If we're not in dart-sdk/bin, we might be in one of the $SDK/out/* - // directories. Try to use a snapshot from a previously built SDK. - snapshot_path = Utils::SCreate("%s/dart-sdk/bin/snapshots/%s", - dir_prefix.get(), command->snapshot_name); - if (File::Exists(nullptr, snapshot_path)) { - free(*script_name); - *script_name = snapshot_path; - return true; - } - free(snapshot_path); - Syslog::PrintErr("Could not find snapshot for command '%s': %s\n", - *script_name, command->snapshot_name); - return false; -} - -} // namespace bin -} // namespace dart diff --git a/runtime/bin/dartdev_utils.h b/runtime/bin/dartdev_utils.h deleted file mode 100644 index d5828cc9362..00000000000 --- a/runtime/bin/dartdev_utils.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -#ifndef RUNTIME_BIN_DARTDEV_UTILS_H_ -#define RUNTIME_BIN_DARTDEV_UTILS_H_ - -#include "platform/globals.h" - -namespace dart { -namespace bin { - -class DartDevUtils { - public: - // Returns true if there does not exist a file at |script_uri|. - static bool ShouldParseCommand(const char* script_uri); - - // Returns true if we were successfully able to parse a DartDev command. - // Returns false if we were unable to find a matching command or a matching - // snapshot does not exist, in which case the VM should exit. - static bool TryParseCommandFromScriptName(char** script_name); - - private: - DISALLOW_ALLOCATION(); - DISALLOW_IMPLICIT_CONSTRUCTORS(DartDevUtils); -}; - -} // namespace bin -} // namespace dart - -#endif // RUNTIME_BIN_DARTDEV_UTILS_H_ diff --git a/runtime/bin/dfe.cc b/runtime/bin/dfe.cc index d27732119b0..2ea68d93ad4 100644 --- a/runtime/bin/dfe.cc +++ b/runtime/bin/dfe.cc @@ -10,7 +10,6 @@ #include "bin/dartutils.h" #include "bin/directory.h" #include "bin/error_exit.h" -#include "bin/exe_utils.h" #include "bin/file.h" #include "bin/main_options.h" #include "bin/platform.h" @@ -42,6 +41,49 @@ DFE dfe; const char kKernelServiceSnapshot[] = "kernel-service.dart.snapshot"; const char kSnapshotsDirectory[] = "snapshots"; +static char* GetDirectoryPrefixFromExeName() { + const char* name = nullptr; + const int kTargetSize = 4096; + char target[kTargetSize]; + intptr_t target_size = + Platform::ResolveExecutablePathInto(target, kTargetSize); + if (target_size > 0 && target_size < kTargetSize - 1) { + target[target_size] = 0; + name = target; + } + if (name == nullptr) { + name = Platform::GetExecutableName(); + target_size = strlen(name); + } + Namespace* namespc = Namespace::Create(Namespace::Default()); + if (File::GetType(namespc, name, false) == File::kIsLink) { + // Resolve the link without creating Dart scope String. + name = File::LinkTarget(namespc, name, target, kTargetSize); + if (name == NULL) { + return strdup(""); + } + target_size = strlen(name); + } + namespc->Release(); + const char* sep = File::PathSeparator(); + const intptr_t sep_length = strlen(sep); + + for (intptr_t i = target_size - 1; i >= 0; --i) { + const char* str = name + i; + if (strncmp(str, sep, sep_length) == 0 +#if defined(HOST_OS_WINDOWS) + // TODO(aam): GetExecutableName doesn't work reliably on Windows, + // the code below is a workaround for that (we would be using + // just single Platform::Separator instead of both slashes if it did). + || *str == '/' +#endif + ) { + return Utils::StrNDup(name, i + 1); + } + } + return strdup(""); +} + DFE::DFE() : use_dfe_(false), use_incremental_compiler_(false), @@ -114,7 +156,7 @@ bool DFE::InitKernelServiceAndPlatformDills(int target_abi_version) { // |dir_prefix| includes the last path seperator. auto dir_prefix = std::unique_ptr( - EXEUtils::GetDirectoryPrefixFromExeName(), free); + GetDirectoryPrefixFromExeName(), free); if (target_abi_version != Options::kAbiVersionUnset) { kernel_service_dill_ = nullptr; diff --git a/runtime/bin/exe_utils.cc b/runtime/bin/exe_utils.cc deleted file mode 100644 index f077ad5ea97..00000000000 --- a/runtime/bin/exe_utils.cc +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -#include "bin/exe_utils.h" - -#include "bin/directory.h" -#include "bin/file.h" -#include "bin/platform.h" -#include "platform/utils.h" - -namespace dart { -namespace bin { - -char* EXEUtils::GetDirectoryPrefixFromExeName() { - const char* name = nullptr; - const int kTargetSize = 4096; - char target[kTargetSize]; - intptr_t target_size = - Platform::ResolveExecutablePathInto(target, kTargetSize); - if (target_size > 0 && target_size < kTargetSize - 1) { - target[target_size] = 0; - name = target; - } - if (name == nullptr) { - name = Platform::GetExecutableName(); - target_size = strlen(name); - } - Namespace* namespc = Namespace::Create(Namespace::Default()); - if (File::GetType(namespc, name, false) == File::kIsLink) { - // Resolve the link without creating Dart scope String. - name = File::LinkTarget(namespc, name, target, kTargetSize); - if (name == NULL) { - return strdup(""); - } - target_size = strlen(name); - } - namespc->Release(); - const char* sep = File::PathSeparator(); - const intptr_t sep_length = strlen(sep); - - for (intptr_t i = target_size - 1; i >= 0; --i) { - const char* str = name + i; - if (strncmp(str, sep, sep_length) == 0 -#if defined(HOST_OS_WINDOWS) - // TODO(aam): GetExecutableName doesn't work reliably on Windows, - // the code below is a workaround for that (we would be using - // just single Platform::Separator instead of both slashes if it did). - || *str == '/' -#endif - ) { - return Utils::StrNDup(name, i + 1); - } - } - return strdup(""); -} - -} // namespace bin -} // namespace dart diff --git a/runtime/bin/exe_utils.h b/runtime/bin/exe_utils.h deleted file mode 100644 index 42d27fe2499..00000000000 --- a/runtime/bin/exe_utils.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -#ifndef RUNTIME_BIN_EXE_UTILS_H_ -#define RUNTIME_BIN_EXE_UTILS_H_ - -#include -#include - -#include "include/dart_api.h" -#include "platform/globals.h" - -namespace dart { -namespace bin { - -class EXEUtils { - public: - // Returns the path to the directory the current executable resides in. - static char* GetDirectoryPrefixFromExeName(); - - private: - DISALLOW_COPY_AND_ASSIGN(EXEUtils); -}; - -} // namespace bin -} // namespace dart - -#endif // RUNTIME_BIN_EXE_UTILS_H_ diff --git a/runtime/bin/file.h b/runtime/bin/file.h index 2716d41328e..4255ba81829 100644 --- a/runtime/bin/file.h +++ b/runtime/bin/file.h @@ -220,7 +220,6 @@ class File : public ReferenceCounted { #endif static bool Exists(Namespace* namespc, const char* path); - static bool ExistsUri(Namespace* namespc, const char* uri); static bool Create(Namespace* namespc, const char* path); static bool CreateLink(Namespace* namespc, const char* path, diff --git a/runtime/bin/file_android.cc b/runtime/bin/file_android.cc index e254d739dfa..c79c1e00dbd 100644 --- a/runtime/bin/file_android.cc +++ b/runtime/bin/file_android.cc @@ -241,23 +241,15 @@ File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) { return new File(new FileHandle(fd)); } -static std::unique_ptr DecodeUri(const char* uri) { +File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) { const char* path = (strlen(uri) >= 8 && strncmp(uri, "file:///", 8) == 0) ? uri + 7 : uri; UriDecoder uri_decoder(path); - if (uri_decoder.decoded() == nullptr) { + if (uri_decoder.decoded() == NULL) { errno = EINVAL; - return nullptr; + return NULL; } - return std::unique_ptr(strdup(uri_decoder.decoded())); -} - -File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) { - auto path = DecodeUri(uri); - if (path == nullptr) { - return nullptr; - } - return File::Open(namespc, path.get(), mode); + return File::Open(namespc, uri_decoder.decoded(), mode); } File* File::OpenStdio(int fd) { @@ -275,14 +267,6 @@ bool File::Exists(Namespace* namespc, const char* name) { } } -bool File::ExistsUri(Namespace* namespc, const char* uri) { - auto path = DecodeUri(uri); - if (path == nullptr) { - return false; - } - return File::Exists(namespc, path.get()); -} - bool File::Create(Namespace* namespc, const char* name) { NamespaceScope ns(namespc, name); const int fd = TEMP_FAILURE_RETRY( diff --git a/runtime/bin/file_fuchsia.cc b/runtime/bin/file_fuchsia.cc index 5f09b2aca7d..9445de63637 100644 --- a/runtime/bin/file_fuchsia.cc +++ b/runtime/bin/file_fuchsia.cc @@ -243,23 +243,19 @@ File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) { return OpenFD(fd); } -static std::unique_ptr DecodeUri(const char* uri) { +File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) { const char* path = (strlen(uri) >= 8 && strncmp(uri, "file:///", 8) == 0) ? uri + 7 : uri; UriDecoder uri_decoder(path); - if (uri_decoder.decoded() == nullptr) { + if (uri_decoder.decoded() == NULL) { errno = EINVAL; - return nullptr; + return NULL; } - return std::unique_ptr(strdup(uri_decoder.decoded())); + return File::Open(namespc, uri_decoder.decoded(), mode); } -File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) { - auto path = DecodeUri(uri); - if (path == nullptr) { - return nullptr; - } - return File::Open(namespc, path.get(), mode); +File* File::OpenStdio(int fd) { + return new File(new FileHandle(fd)); } bool File::Exists(Namespace* namespc, const char* name) { @@ -272,14 +268,6 @@ bool File::Exists(Namespace* namespc, const char* name) { return false; } -bool File::ExistsUri(Namespace* namespc, const char* uri) { - auto path = DecodeUri(uri); - if (path == nullptr) { - return false; - } - return File::Exists(namespc, path.get()); -} - bool File::Create(Namespace* namespc, const char* name) { NamespaceScope ns(namespc, name); const int fd = NO_RETRY_EXPECTED( diff --git a/runtime/bin/file_linux.cc b/runtime/bin/file_linux.cc index e0dd4dbf628..bd2f020e7a6 100644 --- a/runtime/bin/file_linux.cc +++ b/runtime/bin/file_linux.cc @@ -244,23 +244,15 @@ File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) { return OpenFD(fd); } -static std::unique_ptr DecodeUri(const char* uri) { +File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) { const char* path = (strlen(uri) >= 8 && strncmp(uri, "file:///", 8) == 0) ? uri + 7 : uri; UriDecoder uri_decoder(path); - if (uri_decoder.decoded() == nullptr) { + if (uri_decoder.decoded() == NULL) { errno = EINVAL; - return nullptr; + return NULL; } - return std::unique_ptr(strdup(uri_decoder.decoded())); -} - -File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) { - auto path = DecodeUri(uri); - if (path == nullptr) { - return nullptr; - } - return File::Open(namespc, path.get(), mode); + return File::Open(namespc, uri_decoder.decoded(), mode); } File* File::OpenStdio(int fd) { @@ -278,14 +270,6 @@ bool File::Exists(Namespace* namespc, const char* name) { } } -bool File::ExistsUri(Namespace* namespc, const char* uri) { - auto path = DecodeUri(uri); - if (path == nullptr) { - return false; - } - return File::Exists(namespc, path.get()); -} - bool File::Create(Namespace* namespc, const char* name) { NamespaceScope ns(namespc, name); const int fd = TEMP_FAILURE_RETRY( diff --git a/runtime/bin/file_macos.cc b/runtime/bin/file_macos.cc index 6ca2e049e99..25af65b9574 100644 --- a/runtime/bin/file_macos.cc +++ b/runtime/bin/file_macos.cc @@ -286,23 +286,15 @@ File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) { return new File(new FileHandle(fd)); } -static std::unique_ptr DecodeUri(const char* uri) { +File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) { const char* path = (strlen(uri) >= 8 && strncmp(uri, "file:///", 8) == 0) ? uri + 7 : uri; UriDecoder uri_decoder(path); - if (uri_decoder.decoded() == nullptr) { + if (uri_decoder.decoded() == NULL) { errno = EINVAL; - return nullptr; + return NULL; } - return std::unique_ptr(strdup(uri_decoder.decoded())); -} - -File* File::OpenUri(Namespace* namespc, const char* uri, FileOpenMode mode) { - auto path = DecodeUri(uri); - if (path == nullptr) { - return nullptr; - } - return File::Open(namespc, path.get(), mode); + return File::Open(namespc, uri_decoder.decoded(), mode); } File* File::OpenStdio(int fd) { @@ -319,14 +311,6 @@ bool File::Exists(Namespace* namespc, const char* name) { } } -bool File::ExistsUri(Namespace* namespc, const char* uri) { - auto path = DecodeUri(uri); - if (path == nullptr) { - return false; - } - return File::Exists(namespc, path.get()); -} - bool File::Create(Namespace* namespc, const char* name) { int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666)); if (fd < 0) { diff --git a/runtime/bin/file_win.cc b/runtime/bin/file_win.cc index 48c384ae401..975bd12184a 100644 --- a/runtime/bin/file_win.cc +++ b/runtime/bin/file_win.cc @@ -353,15 +353,6 @@ bool File::Exists(Namespace* namespc, const char* name) { return StatHelper(system_name.wide(), &st); } -bool File::ExistsUri(Namespace* namespc, const char* uri) { - UriDecoder uri_decoder(uri); - if (uri_decoder.decoded() == nullptr) { - SetLastError(ERROR_INVALID_NAME); - return false; - } - return File::Exists(namespc, uri_decoder.decoded()); -} - bool File::Create(Namespace* namespc, const char* name) { Utf8ToWideScope system_name(name); int fd = _wopen(system_name.wide(), O_RDONLY | O_CREAT, 0666); diff --git a/runtime/bin/main.cc b/runtime/bin/main.cc index 98d59744966..b98754b068b 100644 --- a/runtime/bin/main.cc +++ b/runtime/bin/main.cc @@ -15,9 +15,9 @@ #include "bin/builtin.h" #include "bin/console.h" #include "bin/crashpad.h" -#include "bin/dartdev_utils.h" #include "bin/dartutils.h" #include "bin/dfe.h" +#include "bin/directory.h" #include "bin/error_exit.h" #include "bin/eventhandler.h" #include "bin/extensions.h" @@ -37,7 +37,6 @@ #include "platform/hashmap.h" #include "platform/syslog.h" #include "platform/text_buffer.h" -#include "platform/utils.h" extern "C" { extern const uint8_t kDartVmSnapshotData[]; @@ -1073,47 +1072,31 @@ void main(int argc, char** argv) { #endif // Parse command line arguments. - if (app_snapshot == nullptr) { - int result = Options::ParseArguments( - argc, argv, vm_run_app_snapshot, &vm_options, &script_name, - &dart_options, &print_flags_seen, &verbose_debug_seen); - if (result < 0) { - if (Options::help_option()) { - Options::PrintUsage(); - Platform::Exit(0); - } else if (Options::version_option()) { - Options::PrintVersion(); - Platform::Exit(0); - } else if (print_flags_seen) { - // Will set the VM flags, print them out and then we exit as no - // script was specified on the command line. - char* error = - Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); - if (error != NULL) { - Syslog::PrintErr("Setting VM flags failed: %s\n", error); - free(error); - Platform::Exit(kErrorExitCode); - } - Platform::Exit(0); - } else { - Options::PrintUsage(); + if (app_snapshot == nullptr && + Options::ParseArguments(argc, argv, vm_run_app_snapshot, &vm_options, + &script_name, &dart_options, &print_flags_seen, + &verbose_debug_seen) < 0) { + if (Options::help_option()) { + Options::PrintUsage(); + Platform::Exit(0); + } else if (Options::version_option()) { + Options::PrintVersion(); + Platform::Exit(0); + } else if (print_flags_seen) { + // Will set the VM flags, print them out and then we exit as no + // script was specified on the command line. + char* error = Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); + if (error != NULL) { + Syslog::PrintErr("Setting VM flags failed: %s\n", error); + free(error); Platform::Exit(kErrorExitCode); } - } - - // Try to parse a DartDev command if script_name doesn't point to a valid - // file. If the command isn't valid we fall through to handle the - // possibility that the script_name points to a HTTP resource. If the - // relevant snapshot can't be found we abort execution. - if (DartDevUtils::ShouldParseCommand(script_name) && - !DartDevUtils::TryParseCommandFromScriptName(&script_name)) { + Platform::Exit(0); + } else { + Options::PrintUsage(); Platform::Exit(kErrorExitCode); } } - - // At this point, script_name now points to either a script or a snapshot - // determined by DartDevUtils above. - DartUtils::SetEnvironment(Options::environment()); if (Options::suppress_core_dump()) { diff --git a/runtime/bin/main_options.cc b/runtime/bin/main_options.cc index a07b0961725..9eaf8c48e53 100644 --- a/runtime/bin/main_options.cc +++ b/runtime/bin/main_options.cc @@ -440,7 +440,7 @@ int Options::ParseArguments(int argc, // Get the script name. if (i < argc) { - *script_name = strdup(argv[i]); + *script_name = argv[i]; i++; } else { return -1;