From 777a8450f33f1fb514c189e3ed4e73637b171c94 Mon Sep 17 00:00:00 2001 From: TheM14 Date: Wed, 14 Oct 2026 17:35:28 +0800 Subject: [PATCH] Resolve ffmpeg through FFMPEG and PATH instead of a file check --- include/joc_core.h | 2 +- src/cli/joc_cli.cpp | 3 +- src/foundation/fs_utf8.cpp | 97 +++++++++++++++++++++++++++++++++++++- src/foundation/fs_utf8.h | 8 ++++ src/task/task.cpp | 45 ++++++++++++++++-- tests/test_core.cpp | 37 +++++++++++++++ 6 files changed, 184 insertions(+), 8 deletions(-) diff --git a/include/joc_core.h b/include/joc_core.h index bff1ae8..6c3acfe 100644 --- a/include/joc_core.h +++ b/include/joc_core.h @@ -373,7 +373,7 @@ typedef struct joc_task_config { /* input */ const char* input_path; /* .eac3/.ec3/.m4a/... */ - const char* ffmpeg_path; /* NULL = "ffmpeg" from PATH */ + const char* ffmpeg_path; /* NULL = FFMPEG, then "ffmpeg" on PATH */ const char* bed_path; /* NULL = decode the core PCM with ffmpeg */ const char* work_dir; /* NULL = a temporary directory */ double eac3_drc_scale; /* 0 = DRC off (reference default) */ diff --git a/src/cli/joc_cli.cpp b/src/cli/joc_cli.cpp index 4ace304..e510497 100644 --- a/src/cli/joc_cli.cpp +++ b/src/cli/joc_cli.cpp @@ -129,7 +129,8 @@ void print_usage() { " --trajectory-mode M ADM 对象轨迹表示 compact|dense64,默认 compact\n" "\n" "输入与解码:\n" - " --ffmpeg PATH ffmpeg 可执行文件,默认取 FFMPEG 环境变量或 PATH\n" + " --ffmpeg PATH ffmpeg 可执行文件;本选项优先,默认先取 FFMPEG 环境变量,\n" + " 再在 PATH 中查找\n" " --eac3-drc-scale X E-AC-3 解码器 -drc_scale,0=关闭码流 dynrng,默认 0\n" " --eac3-target-level N E-AC-3 解码器 -target_level,0=不施加,默认 0\n" " --backend B JOC/扬声器 DSP 后端 auto|native;本构建无 python 后端\n" diff --git a/src/foundation/fs_utf8.cpp b/src/foundation/fs_utf8.cpp index 9a9ea22..593df37 100644 --- a/src/foundation/fs_utf8.cpp +++ b/src/foundation/fs_utf8.cpp @@ -1,5 +1,6 @@ #include "foundation/fs_utf8.h" +#include #include #include #include @@ -12,7 +13,6 @@ #include #include #else -#include #include #endif @@ -20,6 +20,70 @@ namespace joc::fs_utf8 { namespace fs = std::filesystem; +namespace { + +#if defined(_WIN32) +constexpr char kPathListSeparator = ';'; +#else +constexpr char kPathListSeparator = ':'; +#endif + +// PATH (and PATHEXT) are separator-delimited lists. An empty entry is kept: on both +// platforms it means the current directory. +std::vector split_list(const char* value, char separator) { + std::vector parts; + if (value == nullptr) { + return parts; + } + std::string current; + for (const char c : std::string(value)) { + if (c == separator) { + parts.push_back(current); + current.clear(); + continue; + } + current.push_back(c); + } + parts.push_back(current); + return parts; +} + +bool is_runnable_file(const std::string& utf8_path) { + std::error_code error; + if (!fs::is_regular_file(to_path(utf8_path), error) || error) { + return false; + } +#if defined(_WIN32) + return true; +#else + // Existence is not enough: a file without the execute bit cannot be started, so + // deciding here names the real problem instead of a later "exit code 126". + return ::access(utf8_path.c_str(), X_OK) == 0; +#endif +} + +bool carries_directory(const std::string& utf8_path) { +#if defined(_WIN32) + return utf8_path.find_first_of("/\\") != std::string::npos; +#else + return utf8_path.find('/') != std::string::npos; +#endif +} + +#if defined(_WIN32) +// A bare name only resolves through PATHEXT on Windows. The environment is the +// authority; the classic four are the fallback when it is unset. +std::vector executable_extensions() { + std::vector extensions = split_list(std::getenv("PATHEXT"), ';'); + if (extensions.empty()) { + extensions = {".COM", ".EXE", ".BAT", ".CMD"}; + } + return extensions; +} +#endif + +} // namespace + fs::path to_path(const std::string& utf8) { return fs::path(std::u8string(reinterpret_cast(utf8.data()), utf8.size())); } @@ -112,6 +176,37 @@ std::string executable_path() { #endif } +std::string find_executable(const std::string& reference) { + if (reference.empty()) { + return std::string(); + } + if (carries_directory(reference)) { + // A reference that already names a location is the caller's own decision: + // keep it verbatim rather than searching for a same-named program elsewhere. + return is_runnable_file(reference) ? reference : std::string(); + } +#if defined(_WIN32) + const bool named_with_extension = to_path(reference).has_extension(); +#endif + for (const std::string& entry : split_list(std::getenv("PATH"), kPathListSeparator)) { + const fs::path directory = to_path(entry.empty() ? std::string(".") : entry); + const std::string candidate = from_path(directory / to_path(reference)); + if (is_runnable_file(candidate)) { + return candidate; + } +#if defined(_WIN32) + if (!named_with_extension) { + for (const std::string& extension : executable_extensions()) { + if (is_runnable_file(candidate + extension)) { + return candidate + extension; + } + } + } +#endif + } + return std::string(); +} + std::ifstream open_input(const std::string& utf8_path) { return std::ifstream(to_path(utf8_path), std::ios::binary); } diff --git a/src/foundation/fs_utf8.h b/src/foundation/fs_utf8.h index 55675e2..7a07a94 100644 --- a/src/foundation/fs_utf8.h +++ b/src/foundation/fs_utf8.h @@ -33,6 +33,14 @@ std::string temp_directory(); // report it. Defaults are anchored here so they never depend on the CWD. std::string executable_path(); +// Resolves an executable reference to a concrete path. A reference that carries a +// directory separator is a location the caller chose, so it is kept verbatim when +// it names a runnable file and rejected otherwise; a bare name is looked up on PATH +// (through PATHEXT on Windows). Empty means "could not be resolved", which lets a +// caller report the name it was given instead of starting a child process that the +// shell would have to find. +std::string find_executable(const std::string& reference); + // Streams: std::ifstream/ofstream accept a std::filesystem::path, which is the // portable way to open a UTF-8 path. std::ifstream open_input(const std::string& utf8_path); diff --git a/src/task/task.cpp b/src/task/task.cpp index 83fcd09..658daa6 100644 --- a/src/task/task.cpp +++ b/src/task/task.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -82,7 +83,12 @@ std::string fmt(const char* format, ...) { } struct Settings { - std::string input_path; std::string ffmpeg_path = "ffmpeg"; + std::string input_path; + // ffmpeg is a command rather than a path: the reference is what the caller asked + // for (and what a message names), the path is the executable it resolved to. + std::string ffmpeg_reference = "ffmpeg"; + std::string ffmpeg_path; + bool ffmpeg_explicit = false; std::string bed_path; std::string work_dir; std::string output_path; @@ -120,7 +126,19 @@ struct Settings { Settings normalise(const joc_task_config& raw) { Settings cfg; if (raw.input_path != nullptr) { cfg.input_path = raw.input_path; } - if (raw.ffmpeg_path != nullptr && raw.ffmpeg_path[0] != '\0') { cfg.ffmpeg_path = raw.ffmpeg_path; } + // Resolving ffmpeg here gives every caller the same lookup and gives the run a + // concrete executable instead of a name the child process has to find itself: an + // explicit configuration wins, then FFMPEG, then the plain name on PATH. + if (raw.ffmpeg_path != nullptr && raw.ffmpeg_path[0] != '\0') { + cfg.ffmpeg_reference = raw.ffmpeg_path; + cfg.ffmpeg_explicit = true; + } else { + const char* from_environment = std::getenv("FFMPEG"); + if (from_environment != nullptr && from_environment[0] != '\0') { + cfg.ffmpeg_reference = from_environment; + } + } + cfg.ffmpeg_path = fs_utf8::find_executable(cfg.ffmpeg_reference); if (raw.bed_path != nullptr) { cfg.bed_path = raw.bed_path; } if (raw.work_dir != nullptr) { cfg.work_dir = raw.work_dir; } if (raw.output_path != nullptr) { cfg.output_path = raw.output_path; } @@ -1001,9 +1019,26 @@ Status validate(const joc_task_config& raw, std::vector* i warn(JOC_ERR_INVALID_CONFIG, "native_threads", "native_threads is clamped to the 15 JOC objects"); } - if (cfg.bed_path.empty() && !fs_utf8::exists(cfg.ffmpeg_path)) { - warn(JOC_ERR_LIBRARY_MISSING, "ffmpeg_path", - "bed PCM is not supplied, so ffmpeg must be reachable: " + cfg.ffmpeg_path); + // ffmpeg is needed exactly where the task shells out to it: unpacking a container, + // and decoding the core PCM when no bed is supplied. An unresolvable executable is + // a hard error there, because the alternative is a run that fails inside the child + // process; an explicitly requested one that goes unused is only worth a note. + const bool elementary_stream_input = + ends_with(lower(cfg.input_path), ".eac3") || ends_with(lower(cfg.input_path), ".ec3"); + const bool needs_ffmpeg = + !elementary_stream_input || (cfg.bed_path.empty() && !cfg.metadata_only); + if (cfg.ffmpeg_path.empty()) { + const std::string origin = cfg.ffmpeg_explicit ? " (--ffmpeg)" : ""; + if (needs_ffmpeg) { + fail(JOC_ERR_LIBRARY_MISSING, "ffmpeg_path", + "ffmpeg not found: '" + cfg.ffmpeg_reference + "'" + origin + + " is not an executable file and is not on PATH; pass --ffmpeg PATH, set the " + "FFMPEG environment variable, or supply an already decoded --bed PCM"); + } else if (cfg.ffmpeg_explicit) { + warn(JOC_ERR_LIBRARY_MISSING, "ffmpeg_path", + "ffmpeg not found: '" + cfg.ffmpeg_reference + + "' (--ffmpeg); this task does not need ffmpeg, so the option is unused"); + } } if (error_count != nullptr) { *error_count = errors; diff --git a/tests/test_core.cpp b/tests/test_core.cpp index beb753c..1b3c137 100644 --- a/tests/test_core.cpp +++ b/tests/test_core.cpp @@ -346,6 +346,42 @@ void test_process_runner() { CHECK(result.output.find("boom") != std::string::npos); } +void test_find_executable() { + // An executable reference has to become a concrete path: that is what turns the + // default "ffmpeg" into something a child process starts without a PATH search of + // its own, on both platforms. + const std::string bare = +#if defined(_WIN32) + "cmd"; +#else + "sh"; +#endif + const std::string resolved = joc::fs_utf8::find_executable(bare); + CHECK(!resolved.empty()); + CHECK(resolved.find_first_of("/\\") != std::string::npos); + CHECK(joc::fs_utf8::exists(resolved)); + // An already concrete path is kept verbatim instead of being searched for again. + CHECK(joc::fs_utf8::find_executable(resolved) == resolved); + CHECK(joc::fs_utf8::find_executable("").empty()); + CHECK(joc::fs_utf8::find_executable("joc_no_such_program_9f2c").empty()); + // A reference with a directory separator names a location, so it is never answered + // by a same-named program found somewhere else. + CHECK(joc::fs_utf8::find_executable("joc_no_such_dir/joc_no_such_program_9f2c").empty()); +#if !defined(_WIN32) + // Without the execute bit the kernel refuses the file, so it is not a match. + const std::string plain = joc::fs_utf8::from_path( + joc::fs_utf8::to_path(joc::fs_utf8::temp_directory()) / + joc::fs_utf8::to_path("joc_not_executable_9f2c")); + std::FILE* file = joc::fs_utf8::fopen(plain, "wb"); + CHECK(file != nullptr); + if (file != nullptr) { + std::fclose(file); + } + CHECK(joc::fs_utf8::find_executable(plain).empty()); + CHECK(joc::fs_utf8::remove(plain) == 0); +#endif +} + void test_npy_and_zip_writers() { using joc::io::NpyMember; // Python's repr, which is what json.dumps writes into the cache metadata. @@ -855,6 +891,7 @@ int main() { test_int24_packing(); test_utf8_paths(); test_process_runner(); + test_find_executable(); test_builtin_kernel_tables(); test_npy_and_zip_writers(); test_sofa_canonicalize();