Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer matching fusermount version (to be tested) #86

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions src/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ char* read_file_offset_length(const char* fname, unsigned long offset, unsigned
fseek(f, offset, SEEK_SET);

char* buffer = calloc(length + 1, sizeof(char));
fread(buffer, length, sizeof(char), f);
if (fread(buffer, length, sizeof(char), f) != length) {
free(buffer);
fclose(f);
return NULL;
}

fclose(f);

Expand Down Expand Up @@ -409,11 +413,27 @@ int appimage_print_binary(char* fname, unsigned long offset, unsigned long lengt
return 0;
}

// Function to construct name of the fusermount binary
// matching the version of the libfuse library used; this is the preferred
// fusermount
char* preferred_fusermount_name() {
int version = fuse_version();
char* fusermount_name = malloc(12); // "fusermount" + max 2 digits + null terminator
if (fusermount_name == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
sprintf(fusermount_name, "fusermount%d", version);
return fusermount_name;
}

char* find_fusermount(bool verbose) {
char* fusermount_base = "fusermount";
char* preferred_name = preferred_fusermount_name();

char* fusermount_path = getenv("PATH");
if (fusermount_path == NULL) {
free(preferred_name);
return NULL;
}

Expand All @@ -429,16 +449,16 @@ char* find_fusermount(bool verbose) {

struct dirent* entry;
while ((entry = readdir(dir_ptr)) != NULL) {
// Check if the entry starts with "fusermount"
if (strncmp(entry->d_name, fusermount_base, 10) == 0) {
// Check if the rest of the entry is a digit
// Check if the entry matches the preferred name first
if (strcmp(entry->d_name, preferred_name) == 0 || strncmp(entry->d_name, fusermount_base, 10) == 0) {
// Check if the rest of the entry is a digit (for non-preferred names)
char* suffix = entry->d_name + 10;
int j = 0;
while (suffix[j] != '\0' && isdigit(suffix[j])) {
j++;
}

if (suffix[j] == '\0') {
if (suffix[j] == '\0' || strcmp(entry->d_name, preferred_name) == 0) {
// Construct the full path of the entry
char* fusermount_full_path = malloc(strlen(dir) + strlen(entry->d_name) + 2);
sprintf(fusermount_full_path, "%s/%s", dir, entry->d_name);
Expand Down Expand Up @@ -473,6 +493,9 @@ char* find_fusermount(bool verbose) {
if (pid == 0) {
// Child process
char* args[] = {fusermount_full_path, "--version", NULL};
if (!verbose) {
freopen("/dev/null", "w", stdout); // Redirect stdout to /dev/null if not in verbose mode
}
execvp(fusermount_full_path, args);
// If execvp returns, it means the executable was not found
exit(1);
Expand All @@ -485,6 +508,7 @@ char* find_fusermount(bool verbose) {
// The executable was found and executed successfully
closedir(dir_ptr);
free(path_copy);
free(preferred_name);
return fusermount_full_path;
}

Expand All @@ -499,6 +523,7 @@ char* find_fusermount(bool verbose) {
}

free(path_copy);
free(preferred_name);
return NULL;
}

Expand Down Expand Up @@ -715,7 +740,7 @@ void portable_option(const char* arg, const char* appimage_path, const char* nam
}
fullpath[length] = '\0';

sprintf(portable_dir, "%s.%s", fullpath, name);
snprintf(portable_dir, sizeof(portable_dir), "%s.%s", fullpath, name);
if (!mkdir(portable_dir, S_IRWXU))
fprintf(stderr, "Portable %s directory created at %s\n", name, portable_dir);
else
Expand Down Expand Up @@ -1747,7 +1772,10 @@ int main(int argc, char* argv[]) {
close(keepalive_pipe[1]);

/* Pause until mounted */
read(keepalive_pipe[0], &c, 1);
if (read(keepalive_pipe[0], &c, 1) == -1) {
perror("read error");
exit(EXIT_FAILURE);
}

/* Fuse process has now daemonized, reap our child */
waitpid(pid, NULL, 0);
Expand Down