Skip to content

Commit ce61b83

Browse files
authored
Merge pull request #945 from AppImage/improve-mountpoint-generation
Refactor and improve mountpoint name generation
2 parents 2db94af + acaf2f3 commit ce61b83

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/runtime.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,32 @@ bool rm_recursive(const char* const path) {
496496
return rv == 0;
497497
}
498498

499+
bool build_mount_point(char* mount_dir, const char* const argv0, char const* const temp_base, const size_t templen) {
500+
const size_t maxnamelen = 6;
501+
502+
// when running for another AppImage, we should use that for building the mountpoint name instead
503+
char* target_appimage = getenv("TARGET_APPIMAGE");
504+
505+
char* path_basename;
506+
if (target_appimage != NULL) {
507+
path_basename = basename(target_appimage);
508+
} else {
509+
path_basename = basename(argv0);
510+
}
511+
512+
size_t namelen = strlen(path_basename);
513+
// limit length of tempdir name
514+
if (namelen > maxnamelen) {
515+
namelen = maxnamelen;
516+
}
517+
518+
strcpy(mount_dir, temp_base);
519+
strncpy(mount_dir + templen, "/.mount_", 8);
520+
strncpy(mount_dir + templen + 8, path_basename, namelen);
521+
strncpy(mount_dir+templen+8+namelen, "XXXXXX", 6);
522+
mount_dir[templen+8+namelen+6] = 0; // null terminate destination
523+
}
524+
499525
int main(int argc, char *argv[]) {
500526
char appimage_path[PATH_MAX];
501527
char argv0_path[PATH_MAX];
@@ -774,17 +800,7 @@ int main(int argc, char *argv[]) {
774800
// allocate enough memory (size of name won't exceed 60 bytes)
775801
char mount_dir[templen + 60];
776802

777-
size_t namelen = strlen(basename(argv[0]));
778-
// limit length of tempdir name
779-
if(namelen > 6){
780-
namelen = 6;
781-
}
782-
783-
strcpy(mount_dir, temp_base);
784-
strncpy(mount_dir+templen, "/.mount_", 8);
785-
strncpy(mount_dir+templen+8, basename(argv[0]), namelen);
786-
strncpy(mount_dir+templen+8+namelen, "XXXXXX", 6);
787-
mount_dir[templen+8+namelen+6] = 0; // null terminate destination
803+
build_mount_point(mount_dir, argv[0], temp_base, templen);
788804

789805
size_t mount_dir_size = strlen(mount_dir);
790806
pid_t pid;

0 commit comments

Comments
 (0)