diff --git a/.gitignore b/.gitignore index 319317a..5d0470a 100644 --- a/.gitignore +++ b/.gitignore @@ -72,6 +72,8 @@ MANIFEST # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest +# Hand-written, and embedded in the Windows binary +!app/mpqcli.manifest *.spec # Installer logs diff --git a/CHANGELOG.md b/CHANGELOG.md index 01cd9c8..970125d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 0.13.3 - 2026-11-20 + +### Fixed + +- Store, list, extract and read file names with non-ASCII characters correctly on Windows 10 version 1903 and later +- Print non-ASCII file names correctly in the Windows console +- Exit with 130 on Windows when interrupted, as on Linux, instead of 3 +- End by SIGTERM when stopped with SIGTERM, so a shell reports 143 rather than an interrupt's 130 + ## 0.13.2 - 2026-11-20 ### Changed diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c69794..3f78a0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.25) -project(mpqcli VERSION 0.13.2) +project(mpqcli VERSION 0.13.3) # An option() name is global to the CMake cache, including a consumer's, so these # carry the project prefix; a bare BUILD_STATIC claims a name anyone might want. diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 5760955..4d1b294 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -33,4 +33,9 @@ add_executable(mpqcli version.cpp ) +# MSVC merges a listed .manifest into the one its linker writes and embeds the result +if(WIN32) + target_sources(mpqcli PRIVATE mpqcli.manifest) +endif() + target_link_libraries(mpqcli PRIVATE mpqcli::core CLI11::CLI11 mpqcli::warnings) diff --git a/app/main.cpp b/app/main.cpp index e225138..833eb79 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -9,21 +9,65 @@ #include "commands.h" #include "errors.h" +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif + namespace { -// The one piece of mutable global state: a signal handler takes no arguments and -// has nowhere else to write, and it may touch nothing but a lock-free atomic +#ifdef _WIN32 +// The manifest makes UTF-8 the process code page, but a console keeps code pages of +// its own, usually the OEM one, so names would print as mojibake. The console is +// shared with the shell that started mpqcli, so what it had is put back on the way +// out. Without a console both calls fail, which is harmless. +class Utf8Console { +public: + Utf8Console() : input_(GetConsoleCP()), output_(GetConsoleOutputCP()) { + SetConsoleCP(CP_UTF8); + SetConsoleOutputCP(CP_UTF8); + } + Utf8Console(const Utf8Console &) = delete; + Utf8Console &operator=(const Utf8Console &) = delete; + ~Utf8Console() { + if (input_ != 0) { + SetConsoleCP(input_); + } + if (output_ != 0) { + SetConsoleOutputCP(output_); + } + } + +private: + UINT input_; + UINT output_; +}; +#endif + +// The only mutable global state: a signal handler takes no arguments and has nowhere +// else to write, and it may touch nothing but lock-free atomics. interrupted is what +// the core polls; interrupt_signal is which signal set it, so main can end by that +// same signal. It starts as SIGINT, which is what main used before it was recorded. std::atomic interrupted{false}; +std::atomic interrupt_signal{SIGINT}; static_assert(decltype(interrupted)::is_always_lock_free, "a signal handler may only touch a lock-free atomic"); +static_assert(decltype(interrupt_signal)::is_always_lock_free, + "a signal handler may only touch a lock-free atomic"); -extern "C" void OnInterrupt(int) { +extern "C" void OnInterrupt(int signal_number) { + interrupt_signal.store(signal_number, std::memory_order_relaxed); interrupted.store(true, std::memory_order_relaxed); } } // namespace int main(int argc, char **argv) { +#ifdef _WIN32 + const Utf8Console console; +#endif Context context{std::cout, std::cerr, std::cin, interrupted}; CLI::App app{ @@ -64,11 +108,19 @@ int main(int argc, char **argv) { } return app.exit(e, context.out, context.err); } catch (const mpqcli::Interrupted &) { - // The user asked for this, so nothing is reported. Dying of the signal rather - // than returning a code is what lets the shell treat it as an interrupt. - std::signal(SIGINT, SIG_DFL); - std::raise(SIGINT); + // The user asked for this, so nothing is reported +#ifdef _WIN32 + // A re-raised SIGINT would end the process with 3 here, so return what a POSIX + // shell reports for one instead + return 130; +#else + // Dying of the same signal rather than returning a code is what lets a shell or + // a supervisor see how mpqcli ended: 130 after SIGINT, 143 after SIGTERM + const int signal_number = interrupt_signal.load(std::memory_order_relaxed); + std::signal(signal_number, SIG_DFL); + std::raise(signal_number); return 1; // Not reached +#endif } catch (const mpqcli::Error &e) { context.err << "[!] " << e.what() << std::endl; return 1; diff --git a/app/mpqcli.manifest b/app/mpqcli.manifest new file mode 100644 index 0000000..04e0995 --- /dev/null +++ b/app/mpqcli.manifest @@ -0,0 +1,13 @@ + + + + + + UTF-8 + + + diff --git a/docs/advanced.md b/docs/advanced.md index 935e6ee..3c99805 100644 --- a/docs/advanced.md +++ b/docs/advanced.md @@ -47,3 +47,13 @@ PS> mpqcli.exe list wow-patch.mpq | Select-String -Pattern \.exe | ` [*] Extracted: Repair.exe [*] Extracted: WowError.exe ``` + +## Find the archives that fail verification + +`verify` exits `0` when an archive's signature is valid, and `1` when it is not or there is none, so a loop can act on the [exit code](./commands_list.md#exit-codes) alone. The verification result goes to stderr, which is discarded here, and the name of each archive that fails goes to `failed.txt`: + +```bash +$ for f in *.mpq; do mpqcli verify "$f" 2>/dev/null || echo "$f"; done > failed.txt +$ cat failed.txt +unsigned.mpq +``` diff --git a/docs/changelog.md b/docs/changelog.md index 01cd9c8..970125d 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,14 @@ # Changelog +## 0.13.3 - 2026-11-20 + +### Fixed + +- Store, list, extract and read file names with non-ASCII characters correctly on Windows 10 version 1903 and later +- Print non-ASCII file names correctly in the Windows console +- Exit with 130 on Windows when interrupted, as on Linux, instead of 3 +- End by SIGTERM when stopped with SIGTERM, so a shell reports 143 rather than an interrupt's 130 + ## 0.13.2 - 2026-11-20 ### Changed diff --git a/docs/commands_list.md b/docs/commands_list.md index a7e6dce..812f1b2 100644 --- a/docs/commands_list.md +++ b/docs/commands_list.md @@ -20,3 +20,16 @@ The `mpqcli` program has the following subcommands: | [`read`](./commands/read.md) | Read a specific file to stdout | | [`verify`](./commands/verify.md) | Verify an MPQ archive signature | | [`compact`](./commands/compact.md) | Compact (defragment) an MPQ archive | + +## Exit codes + +Every subcommand exits with one of these codes, so a script can tell the outcomes apart. [Find the archives that fail verification](./advanced.md#find-the-archives-that-fail-verification) shows one in use. + +| Code | Meaning | +|---|---| +| `0` | Success. A file `add` skipped because it is already in the archive still counts as success. | +| `1` | The command failed: for example, the archive could not be opened, a file is not in it, or `verify` found no valid signature. The reason is on stderr. | +| `2` | An unexpected internal error. Please report it. | +| `100` to `114` | The command line could not be parsed. The most common are `104` (a value of the wrong type), `105` (a value that is not allowed), `106` (a missing argument) and `109` (an extra argument). | +| `130` | Interrupted with Ctrl+C. On Linux mpqcli ends by the signal itself, which a shell reports as `130`; on Windows it exits with `130`. | +| `143` | Stopped with SIGTERM, on Linux. mpqcli ends by the signal itself, which a shell reports as `143`. | diff --git a/docs/installation.md b/docs/installation.md index 54a7d8a..b094607 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -21,6 +21,8 @@ Both installers put `mpqcli` on the system path and may ask for elevation; pass Check the [latest release with binaries](https://github.com/thegraydot/mpqcli/releases). +On Windows, file names with characters outside ASCII, such as `café.txt`, need Windows 10 version 1903 or later. Earlier versions store and extract them with the wrong bytes. + ## Docker image The Docker image for `mpqcli` is hosted on [GitHub Container Registry (GHCR)](https://ghcr.io). It provides a lightweight and portable way to use `mpqcli` without needing to build or download a binary. diff --git a/test/functional/test_add.cpp b/test/functional/test_add.cpp index 5a14b7b..60cf4cf 100644 --- a/test/functional/test_add.cpp +++ b/test/functional/test_add.cpp @@ -17,6 +17,7 @@ #include "archive_helper.h" #include "compressible_text.h" #include "many_locales_archive.h" +#include "read_file.h" #include "sample_archive.h" #include "sample_files.h" #include "subprocess_helper.h" @@ -456,6 +457,35 @@ TEST_CASE("add: a directory keeps its structure", "[add]") { Plus(sample_entries, {"enUS sub\\a.txt", "enUS b.txt"})); } +TEST_CASE("add: stores a non-ASCII name from a directory", "[add]") { + // "cafe" with an acute e, which the Windows runner's code page has, and two Japanese + // characters, which no single-byte code page has. Written as UTF-8 escapes, since + // MSVC reads a source file in the build machine's code page. + const auto name = + GENERATE(as{}, "caf\xC3\xA9.txt", "\xE6\x97\xA5\xE6\x9C\xAC.txt"); + CAPTURE(name); + SampleArchive sample; + // Inside an ASCII-named directory, so the name never crosses a command line and only + // mpqcli's own conversions are tested. u8path names the file right whatever the test + // binary's code page. + const auto add_dir = sample.files.dir.Path() / "add_dir"; + WriteFile(add_dir / fs::u8path(name), "Non-ASCII file."); + + auto result = Run(mpqcli_binary, {"add", sample.archive.string(), add_dir.string()}); + + REQUIRE(result.returncode == 0); + REQUIRE(LinesToVector(result.stderr_output) == + std::vector{"[+] Adding file: " + name}); + REQUIRE(ArchiveEntries(sample.archive) == Plus(sample_entries, {"enUS " + name})); + + const auto output_dir = sample.files.dir.Path() / "out"; + auto extracted = + Run(mpqcli_binary, {"extract", sample.archive.string(), "-o", output_dir.string()}); + + REQUIRE(extracted.returncode == 0); + REQUIRE(ReadFile(output_dir / fs::u8path(name)) == "Non-ASCII file."); +} + TEST_CASE("add: a directory with --path prefix", "[add]") { SampleArchive sample; const auto add_dir = sample.files.dir.Path() / "add_dir_prefix"; diff --git a/test/functional/test_read.cpp b/test/functional/test_read.cpp index 6d56a80..944afd7 100644 --- a/test/functional/test_read.cpp +++ b/test/functional/test_read.cpp @@ -324,6 +324,25 @@ TEST_CASE("read: falls back to the default locale when the file has no such vari REQUIRE(result.stdout_output == CatsContent("")); } +TEST_CASE("read: reads a non-ASCII name given as an argument", "[read]") { + // The names add's non-ASCII test uses, for the same reason + const auto name = + GENERATE(as{}, "caf\xC3\xA9.txt", "\xE6\x97\xA5\xE6\x9C\xAC.txt"); + CAPTURE(name); + TempDir dir; + const auto input_dir = dir.Path() / "in"; + WriteFile(input_dir / fs::u8path(name), "Non-ASCII file."); + const auto archive = dir.Path() / "names.mpq"; + REQUIRE(Run(mpqcli_binary, {"create", archive.string(), input_dir.string()}).returncode == 0); + + // The name reaches mpqcli through argv + auto result = Run(mpqcli_binary, {"read", archive.string(), name}); + + REQUIRE(result.returncode == 0); + REQUIRE(result.stderr_output.empty()); + REQUIRE(result.stdout_output == "Non-ASCII file."); +} + TEST_CASE("read: says nothing when enUS falls back to the neutral locale", "[read]") { ManyLocalesArchive locales;