Skip to content

Commit d58962c

Browse files
committed
fix(cli): only name an installer that is actually there
`update` hands off to install.sh (install.ps1 on Windows) and prints the command to run. It built that command from cbm_detect_self_path - the BINARY's directory - and treated "I resolved my own location" as "the installer is beside me". Those are different questions. install.sh is placed beside the binary by install.sh itself, but a binary that was moved, packaged by a distro, or built from source has no installer next to it. We printed the path anyway: bash "/home/<user>/.local/bin/install.sh" /usr/bin/bash: /home/<user>/.local/bin/install.sh: No such file or directory Reported on discussion DeusData#1560 (DeusData#1632) by a user who was already three releases deep in install trouble and had just been told, by us, to run a file that does not exist. `update` exists to tell someone how to proceed. Ending the interaction on a command that cannot run is the one outcome it must not produce - and the fallback text was already there and already correct, naming install.sh as shipping in the release archive without asserting a path. The probe goes through cbm_path_info_utf8 so a non-ASCII install directory resolves on Windows, and rejects a DIRECTORY of that name, because `bash <dir>` is not a command either. A symlink still counts: it is reported rather than followed, and the shell runs it perfectly well. The Windows branch gets the same treatment; it had the identical assumption about install.ps1. Reproduce-first and revert-checked: with the probe forced to return true - the old behaviour - the new test fails with "a directory with no installer must not be named as one", and passes once it is restored. cli: 276 passed. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 6ecbc5f commit d58962c

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11972,6 +11972,36 @@ static bool check_already_latest(void) {
1197211972

1197311973
#endif /* CBM_CLI_ENABLE_TEST_API */
1197411974

11975+
/* Is the installer script actually present beside the binary? (#1632)
11976+
*
11977+
* `update` hands off to install.sh / install.ps1 and prints the command to run.
11978+
* It derived that path from the binary's own location, which is not the same
11979+
* question: the installer is placed beside the binary by install.sh, but a
11980+
* binary moved, packaged, or built from source has no installer next to it, and
11981+
* we still printed the path. The user's session then ended on a command that
11982+
* cannot run.
11983+
*
11984+
* Checked with cbm_path_info_utf8 so a non-ASCII install path resolves on
11985+
* Windows. A symlink counts: it is reported rather than followed, and the shell
11986+
* will run it perfectly well. */
11987+
bool cbm_cli_installer_beside_binary(const char *dir) {
11988+
if (!dir || !dir[0]) {
11989+
return false;
11990+
}
11991+
char script[CLI_BUF_1K];
11992+
#ifdef _WIN32
11993+
const char *name = "install.ps1";
11994+
#else
11995+
const char *name = "install.sh";
11996+
#endif
11997+
int written = snprintf(script, sizeof(script), "%s/%s", dir, name);
11998+
if (written <= 0 || (size_t)written >= sizeof(script)) {
11999+
return false;
12000+
}
12001+
cbm_path_info_t info;
12002+
return cbm_path_info_utf8(script, &info) == 0 && !info.is_directory;
12003+
}
12004+
1197512005
int cbm_cmd_update(int argc, char **argv) {
1197612006
parse_auto_answer(argc, argv);
1197712007

‎src/cli/cli.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,12 @@ int cbm_cmd_uninstall(int argc, char **argv);
482482

483483
/* update: check latest release, prompt for index deletion, prompt for ui/standard,
484484
* download and replace binary. */
485+
/* True when the installer script (install.sh / install.ps1 on Windows) is
486+
* present beside the binary. `update` prints a command built from this;
487+
* naming a path that does not exist ends the interaction on a failing
488+
* command (#1632). Exposed for testing. */
489+
bool cbm_cli_installer_beside_binary(const char *dir);
490+
485491
int cbm_cmd_update(int argc, char **argv);
486492

487493
/* config: get/set/list/reset runtime config values. */

‎tests/test_cli.c‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12636,7 +12636,61 @@ TEST(cli_windows_update_hands_off_to_install_script) {
1263612636
* Suite definition
1263712637
* ═══════════════════════════════════════════════════════════════════ */
1263812638

12639+
/* #1632: `update` derives the installer command from the BINARY's directory,
12640+
* which is not the same question as "is the installer there". A binary in
12641+
* ~/.local/bin with no install.sh beside it still produced:
12642+
*
12643+
* bash "/home/<user>/.local/bin/install.sh"
12644+
* /usr/bin/bash: .../install.sh: No such file or directory
12645+
*
12646+
* reported on discussion #1560 by a user already three releases deep in install
12647+
* trouble. `update` exists to tell someone how to proceed; ending on a command
12648+
* that cannot run is the one outcome it must not produce. */
12649+
TEST(cli_update_only_names_an_installer_that_exists_issue1632) {
12650+
char tmpdir[256];
12651+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-installer-probe-XXXXXX");
12652+
if (!cbm_mkdtemp(tmpdir)) {
12653+
FAIL("cbm_mkdtemp failed");
12654+
}
12655+
#ifdef _WIN32
12656+
const char *installer = "install.ps1";
12657+
#else
12658+
const char *installer = "install.sh";
12659+
#endif
12660+
12661+
/* A directory holding the binary but no installer must not be advertised. */
12662+
bool absent_is_refused = !cbm_cli_installer_beside_binary(tmpdir);
12663+
12664+
char script[512];
12665+
snprintf(script, sizeof(script), "%s/%s", tmpdir, installer);
12666+
write_test_file(script, "#!/bin/sh\nexit 0\n");
12667+
bool present_is_accepted = cbm_cli_installer_beside_binary(tmpdir);
12668+
12669+
/* A directory of that name must not count: `bash <dir>` is not a command. */
12670+
char decoy[512];
12671+
snprintf(decoy, sizeof(decoy), "%s/decoy", tmpdir);
12672+
test_mkdirp(decoy);
12673+
char decoy_installer[640];
12674+
snprintf(decoy_installer, sizeof(decoy_installer), "%s/%s", decoy, installer);
12675+
test_mkdirp(decoy_installer);
12676+
bool directory_is_refused = !cbm_cli_installer_beside_binary(decoy);
12677+
12678+
bool empty_is_refused = !cbm_cli_installer_beside_binary("");
12679+
test_rmdir_r(tmpdir);
12680+
12681+
if (!absent_is_refused)
12682+
FAIL("a directory with no installer must not be named as one");
12683+
if (!present_is_accepted)
12684+
FAIL("an installer that is present must be named");
12685+
if (!directory_is_refused)
12686+
FAIL("a DIRECTORY named install.sh is not a runnable installer");
12687+
if (!empty_is_refused)
12688+
FAIL("an empty directory string must be refused");
12689+
PASS();
12690+
}
12691+
1263912692
SUITE(cli) {
12693+
RUN_TEST(cli_update_only_names_an_installer_that_exists_issue1632);
1264012694
RUN_TEST(cli_progress_visibility_policy);
1264112695
RUN_TEST(cli_raw_mcp_result_preserves_tool_error_status);
1264212696
RUN_TEST(cli_maintenance_cancellation_forces_failure_status);

0 commit comments

Comments
 (0)