|
| 1 | +// Copyright (C) 2020 Matthew Glazar |
| 2 | +// See end of file for extended copyright information. |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <cerrno> |
| 6 | +#include <cstddef> |
| 7 | +#include <cstdio> |
| 8 | +#include <cstdlib> |
| 9 | +#include <cstring> |
| 10 | +#include <quick-lint-js/assert.h> |
| 11 | +#include <quick-lint-js/char8.h> |
| 12 | +#include <quick-lint-js/file.h> |
| 13 | +#include <quick-lint-js/have.h> |
| 14 | +#include <string> |
| 15 | + |
| 16 | +#if QLJS_HAVE_STD_FILESYSTEM |
| 17 | +#include <filesystem> |
| 18 | +#endif |
| 19 | + |
| 20 | +#if QLJS_HAVE_SYS_STAT_H |
| 21 | +#include <sys/stat.h> |
| 22 | +#endif |
| 23 | + |
| 24 | +namespace quick_lint_js { |
| 25 | +canonical_path_result::canonical_path_result() {} |
| 26 | + |
| 27 | +canonical_path_result::canonical_path_result(const char *path) : path_(path) {} |
| 28 | + |
| 29 | +std::string_view canonical_path_result::path() const &noexcept { |
| 30 | + QLJS_ASSERT(this->ok()); |
| 31 | + return this->path_; |
| 32 | +} |
| 33 | + |
| 34 | +std::string &&canonical_path_result::path() && noexcept { |
| 35 | + QLJS_ASSERT(this->ok()); |
| 36 | + return std::move(this->path_); |
| 37 | +} |
| 38 | + |
| 39 | +const char *canonical_path_result::c_str() const noexcept { |
| 40 | + QLJS_ASSERT(this->ok()); |
| 41 | + return this->path_.c_str(); |
| 42 | +} |
| 43 | + |
| 44 | +std::string &&canonical_path_result::error() && noexcept { |
| 45 | + QLJS_ASSERT(!this->ok()); |
| 46 | + return std::move(this->error_); |
| 47 | +} |
| 48 | + |
| 49 | +canonical_path_result canonical_path_result::failure(std::string &&error) { |
| 50 | + canonical_path_result result; |
| 51 | + result.error_ = std::move(error); |
| 52 | + QLJS_ASSERT(!result.ok()); |
| 53 | + return result; |
| 54 | +} |
| 55 | + |
| 56 | +canonical_path_result canonicalize_path(const char *path) { |
| 57 | +#if QLJS_HAVE_STD_FILESYSTEM |
| 58 | + std::error_code error; |
| 59 | + std::filesystem::path canonical = std::filesystem::canonical(path, error); |
| 60 | + if (error) { |
| 61 | + return canonical_path_result::failure( |
| 62 | + std::string("failed to canonicalize path ") + path + ": " + |
| 63 | + error.message()); |
| 64 | + } |
| 65 | + return canonical_path_result(canonical.string().c_str()); |
| 66 | +#elif QLJS_HAVE_REALPATH |
| 67 | + char *allocated_canonical = ::realpath(path, nullptr); |
| 68 | + if (!allocated_canonical) { |
| 69 | + return canonical_path_result::failure( |
| 70 | + std::string("failed to canonicalize path ") + path + ": " + |
| 71 | + std::strerror(errno)); |
| 72 | + } |
| 73 | + canonical_path_result result(allocated_canonical); |
| 74 | + std::free(allocated_canonical); |
| 75 | + return result; |
| 76 | +#else |
| 77 | +#error "Unsupported platform" |
| 78 | +#endif |
| 79 | +} |
| 80 | + |
| 81 | +canonical_path_result canonicalize_path(const std::string &path) { |
| 82 | + return canonicalize_path(path.c_str()); |
| 83 | +} |
| 84 | +} |
| 85 | + |
| 86 | +// quick-lint-js finds bugs in JavaScript programs. |
| 87 | +// Copyright (C) 2020 Matthew Glazar |
| 88 | +// |
| 89 | +// This file is part of quick-lint-js. |
| 90 | +// |
| 91 | +// quick-lint-js is free software: you can redistribute it and/or modify |
| 92 | +// it under the terms of the GNU General Public License as published by |
| 93 | +// the Free Software Foundation, either version 3 of the License, or |
| 94 | +// (at your option) any later version. |
| 95 | +// |
| 96 | +// quick-lint-js is distributed in the hope that it will be useful, |
| 97 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 98 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 99 | +// GNU General Public License for more details. |
| 100 | +// |
| 101 | +// You should have received a copy of the GNU General Public License |
| 102 | +// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>. |
0 commit comments