From f807543191306f4bdc54501dc482af30d554b23b Mon Sep 17 00:00:00 2001 From: Robert Kausch Date: Sat, 28 Jun 2025 17:58:10 +0200 Subject: [PATCH] Fix TARGET_APPIMAGE issues - Fix --appimage-help not working when TARGET_APPIMAGE is used - Fix --appimage-portable-* not working when TARGET_APPIMAGE is used --- src/runtime/runtime.c | 73 +++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 41 deletions(-) diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 5e24191..7f2eb77 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -716,20 +716,12 @@ void print_help(const char* appimage_path) { "for information on how to obtain and build the source code\n", appimage_path); } -void portable_option(const char* arg, const char* appimage_path, const char* name) { +void portable_option(const char* arg, const char* fullpath, const char* name) { char option[32]; sprintf(option, "appimage-portable-%s", name); if (arg && strcmp(arg, option) == 0) { char portable_dir[PATH_MAX]; - char fullpath[PATH_MAX]; - - ssize_t length = readlink(appimage_path, fullpath, sizeof(fullpath)); - if (length < 0) { - fprintf(stderr, "Error getting realpath for %s\n", appimage_path); - exit(EXIT_FAILURE); - } - fullpath[length] = '\0'; sprintf(portable_dir, "%s.%s", fullpath, name); if (!mkdir(portable_dir, S_IRWXU)) @@ -1488,6 +1480,35 @@ int main(int argc, char* argv[]) { strcpy(argv0_path, getenv("TARGET_APPIMAGE")); } + // figure out file type + struct stat stat_buf; + + if (lstat(appimage_path, &stat_buf) < 0) { + fprintf(stderr, "Error, cannot stat %s\n", appimage_path); + exit(EXIT_EXECERROR); + } + + // calculate full path of AppImage + char fullpath[PATH_MAX]; + + if (S_ISLNK(stat_buf.st_mode)) { + // If we are operating on a link + ssize_t len = readlink(appimage_path, fullpath, sizeof(fullpath)); + if (len < 0) { + fprintf(stderr, "Error getting realpath for %s\n", appimage_path); + exit(EXIT_EXECERROR); + } + fullpath[len] = '\0'; + } else { + char* abspath = realpath(appimage_path, NULL); + if (abspath == NULL) { + fprintf(stderr, "Error getting realpath for %s\n", appimage_path); + exit(EXIT_EXECERROR); + } + strcpy(fullpath, abspath); + free(abspath); + } + // temporary directories are required in a few places // therefore we implement the detection of the temp base dir at the top of the code to avoid redundancy char temp_base[PATH_MAX] = P_tmpdir; @@ -1510,15 +1531,6 @@ int main(int argc, char* argv[]) { /* Print the help and then exit */ if (arg && strcmp(arg, "appimage-help") == 0) { - char fullpath[PATH_MAX]; - - ssize_t length = readlink(appimage_path, fullpath, sizeof(fullpath)); - if (length < 0) { - fprintf(stderr, "Error getting realpath for %s\n", appimage_path); - exit(EXIT_EXECERROR); - } - fullpath[length] = '\0'; - print_help(fullpath); exit(0); } @@ -1557,27 +1569,6 @@ int main(int argc, char* argv[]) { exit(0); } - // calculate full path of AppImage - char fullpath[PATH_MAX]; - - if (getenv("TARGET_APPIMAGE") == NULL) { - // If we are operating on this file itself - ssize_t len = readlink(appimage_path, fullpath, sizeof(fullpath)); - if (len < 0) { - perror("Failed to obtain absolute path"); - exit(EXIT_EXECERROR); - } - fullpath[len] = '\0'; - } else { - char* abspath = realpath(appimage_path, NULL); - if (abspath == NULL) { - perror("Failed to obtain absolute path"); - exit(EXIT_EXECERROR); - } - strcpy(fullpath, abspath); - free(abspath); - } - if (getenv("APPIMAGE_EXTRACT_AND_RUN") != NULL || (arg && strcmp(arg, "appimage-extract-and-run") == 0)) { char* hexlified_digest = NULL; @@ -1695,8 +1686,8 @@ int main(int argc, char* argv[]) { exit(0); } - portable_option(arg, appimage_path, "home"); - portable_option(arg, appimage_path, "config"); + portable_option(arg, fullpath, "home"); + portable_option(arg, fullpath, "config"); // If there is an argument starting with appimage- (but not appimage-mount which is handled further down) // then stop here and print an error message