diff options
author | Derek Schuff <dschuff@chromium.org> | 2023-09-14 14:08:40 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-14 14:08:40 -0700 |
commit | f774effa54c6a40448487033a28a47caa3394f61 (patch) | |
tree | ee9764205ec4b6b916cc7e6e1ad94b59eb5f853d /src/support/file.cpp | |
parent | 11dba9b1c2ad988500b329727f39f4d8786918c5 (diff) | |
download | binaryen-f774effa54c6a40448487033a28a47caa3394f61.tar.gz binaryen-f774effa54c6a40448487033a28a47caa3394f61.tar.bz2 binaryen-f774effa54c6a40448487033a28a47caa3394f61.zip |
Encode command line to UTF8 on Windows (#5671)
This PR changes how file paths and the command line are handled. On startup on Windows,
we process the wstring version of the command line (including the file paths) and re-encode
it to UTF8 before handing it off to the rest of the command line handling logic. This means
that all paths are stored in UTF8-encoded std::strings as they go through the program, right
up until they are used to open files. At that time, they are converted to the appropriate native
format with the new to_path function before passing to the stdlib open functions.
This has the advantage that all of the non-file-opening code can use a single type to hold paths
(which is good since std::filesystem::path has proved problematic in some cases), but has the
disadvantage that someone could add new code that forgets to convert to_path before
opening. That's somewhat mitigated by the fact that most of the code uses the ModuleIOBase
classes for opening files.
Fixes #4995
Diffstat (limited to 'src/support/file.cpp')
-rw-r--r-- | src/support/file.cpp | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/support/file.cpp b/src/support/file.cpp index cfd656391..feb05e136 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -16,6 +16,7 @@ #include "support/file.h" #include "support/debug.h" +#include "support/path.h" #include "support/utilities.h" #include <cstdint> @@ -57,7 +58,7 @@ T wasm::read_file(const std::string& filename, Flags::BinaryOption binary) { if (binary == Flags::Binary) { flags |= std::ifstream::binary; } - infile.open(filename, flags); + infile.open(wasm::Path::to_path(filename), flags); if (!infile.is_open()) { Fatal() << "Failed opening '" << filename << "'"; } @@ -108,13 +109,15 @@ wasm::Output::Output(const std::string& filename, Flags::BinaryOption binary) buffer = std::cout.rdbuf(); } else { BYN_TRACE("Opening '" << filename << "'\n"); - auto flags = std::ofstream::out | std::ofstream::trunc; + std::ios_base::openmode flags = + std::ofstream::out | std::ofstream::trunc; if (binary == Flags::Binary) { flags |= std::ofstream::binary; } - outfile.open(filename, flags); + outfile.open(wasm::Path::to_path(filename), flags); if (!outfile.is_open()) { - Fatal() << "Failed opening '" << filename << "'"; + Fatal() << "Failed opening output file '" << filename + << "': " << strerror(errno); } buffer = outfile.rdbuf(); } @@ -122,12 +125,13 @@ wasm::Output::Output(const std::string& filename, Flags::BinaryOption binary) }()) {} void wasm::copy_file(std::string input, std::string output) { - std::ifstream src(input, std::ios::binary); - std::ofstream dst(output, std::ios::binary); + std::ifstream src(wasm::Path::to_path(input), std::ios::binary); + std::ofstream dst(wasm::Path::to_path(output), std::ios::binary); dst << src.rdbuf(); } size_t wasm::file_size(std::string filename) { - std::ifstream infile(filename, std::ifstream::ate | std::ifstream::binary); + std::ifstream infile(wasm::Path::to_path(filename), + std::ifstream::ate | std::ifstream::binary); return infile.tellg(); } |