diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index de47793..afedc05 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -40,12 +40,10 @@ #include #include -#include extern dev_t sqfs_makedev(int maj, int min); -extern int sqfs_opt_proc(void *data, const char *arg, int key, - struct fuse_args *outargs); +extern int sqfs_opt_proc(void* data, const char* arg, int key, struct fuse_args* outargs); #include #include @@ -72,7 +70,7 @@ typedef struct { uint32_t b; uint32_t c; uint32_t d; - uint8_t buffer[64]; + uint8_t buffer[64]; uint32_t block[16]; } Md5Context; @@ -182,7 +180,7 @@ typedef struct elf32_note { typedef Elf32_Nhdr Elf_Nhdr; -static char *fname; +static char* fname; static Elf64_Ehdr ehdr; #if __BYTE_ORDER == __LITTLE_ENDIAN @@ -193,53 +191,49 @@ static Elf64_Ehdr ehdr; #error "Unknown machine endian" #endif -static uint16_t file16_to_cpu(uint16_t val) -{ +static uint16_t file16_to_cpu(uint16_t val) { if (ehdr.e_ident[EI_DATA] != ELFDATANATIVE) val = bswap_16(val); return val; } -static uint32_t file32_to_cpu(uint32_t val) -{ +static uint32_t file32_to_cpu(uint32_t val) { if (ehdr.e_ident[EI_DATA] != ELFDATANATIVE) val = bswap_32(val); return val; } -static uint64_t file64_to_cpu(uint64_t val) -{ +static uint64_t file64_to_cpu(uint64_t val) { if (ehdr.e_ident[EI_DATA] != ELFDATANATIVE) val = bswap_64(val); return val; } -static off_t read_elf32(FILE* fd) -{ +static off_t read_elf32(FILE* fd) { Elf32_Ehdr ehdr32; Elf32_Shdr shdr32; off_t last_shdr_offset; ssize_t ret; - off_t sht_end, last_section_end; + off_t sht_end, last_section_end; fseeko(fd, 0, SEEK_SET); ret = fread(&ehdr32, 1, sizeof(ehdr32), fd); - if (ret < 0 || (size_t)ret != sizeof(ehdr32)) { + if (ret < 0 || (size_t) ret != sizeof(ehdr32)) { fprintf(stderr, "Read of ELF header from %s failed: %s\n", - fname, strerror(errno)); + fname, strerror(errno)); return -1; } - ehdr.e_shoff = file32_to_cpu(ehdr32.e_shoff); - ehdr.e_shentsize = file16_to_cpu(ehdr32.e_shentsize); - ehdr.e_shnum = file16_to_cpu(ehdr32.e_shnum); + ehdr.e_shoff = file32_to_cpu(ehdr32.e_shoff); + ehdr.e_shentsize = file16_to_cpu(ehdr32.e_shentsize); + ehdr.e_shnum = file16_to_cpu(ehdr32.e_shnum); last_shdr_offset = ehdr.e_shoff + (ehdr.e_shentsize * (ehdr.e_shnum - 1)); fseeko(fd, last_shdr_offset, SEEK_SET); ret = fread(&shdr32, 1, sizeof(shdr32), fd); - if (ret < 0 || (size_t)ret != sizeof(shdr32)) { + if (ret < 0 || (size_t) ret != sizeof(shdr32)) { fprintf(stderr, "Read of ELF section header from %s failed: %s\n", - fname, strerror(errno)); + fname, strerror(errno)); return -1; } @@ -249,8 +243,7 @@ static off_t read_elf32(FILE* fd) return sht_end > last_section_end ? sht_end : last_section_end; } -static off_t read_elf64(FILE* fd) -{ +static off_t read_elf64(FILE* fd) { Elf64_Ehdr ehdr64; Elf64_Shdr shdr64; off_t last_shdr_offset; @@ -259,22 +252,22 @@ static off_t read_elf64(FILE* fd) fseeko(fd, 0, SEEK_SET); ret = fread(&ehdr64, 1, sizeof(ehdr64), fd); - if (ret < 0 || (size_t)ret != sizeof(ehdr64)) { + if (ret < 0 || (size_t) ret != sizeof(ehdr64)) { fprintf(stderr, "Read of ELF header from %s failed: %s\n", - fname, strerror(errno)); + fname, strerror(errno)); return -1; } - ehdr.e_shoff = file64_to_cpu(ehdr64.e_shoff); - ehdr.e_shentsize = file16_to_cpu(ehdr64.e_shentsize); - ehdr.e_shnum = file16_to_cpu(ehdr64.e_shnum); + ehdr.e_shoff = file64_to_cpu(ehdr64.e_shoff); + ehdr.e_shentsize = file16_to_cpu(ehdr64.e_shentsize); + ehdr.e_shnum = file16_to_cpu(ehdr64.e_shnum); last_shdr_offset = ehdr.e_shoff + (ehdr.e_shentsize * (ehdr.e_shnum - 1)); fseeko(fd, last_shdr_offset, SEEK_SET); ret = fread(&shdr64, 1, sizeof(shdr64), fd); if (ret < 0 || ret != sizeof(shdr64)) { fprintf(stderr, "Read of ELF section header from %s failed: %s\n", - fname, strerror(errno)); + fname, strerror(errno)); return -1; } @@ -292,19 +285,19 @@ ssize_t appimage_get_elf_size(const char* fname) { fd = fopen(fname, "rb"); if (fd == NULL) { fprintf(stderr, "Cannot open %s: %s\n", - fname, strerror(errno)); + fname, strerror(errno)); return -1; } ret = fread(ehdr.e_ident, 1, EI_NIDENT, fd); if (ret != EI_NIDENT) { fprintf(stderr, "Read of e_ident from %s failed: %s\n", - fname, strerror(errno)); + fname, strerror(errno)); return -1; } if ((ehdr.e_ident[EI_DATA] != ELFDATA2LSB) && (ehdr.e_ident[EI_DATA] != ELFDATA2MSB)) { fprintf(stderr, "Unknown ELF data order %u\n", - ehdr.e_ident[EI_DATA]); + ehdr.e_ident[EI_DATA]); return -1; } if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) { @@ -321,56 +314,6 @@ ssize_t appimage_get_elf_size(const char* fname) { } /* Return the offset, and the length of an ELF section with a given name in a given ELF file */ -bool appimage_get_elf_section_offset_and_length(const char* fname, const char* section_name, unsigned long* offset, unsigned long* length) { - uint8_t* data; - int i; - int fd = open(fname, O_RDONLY); - size_t map_size = (size_t) lseek(fd, 0, SEEK_END); - - data = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, 0); - close(fd); - - // this trick works as both 32 and 64 bit ELF files start with the e_ident[EI_NINDENT] section - unsigned char class = data[EI_CLASS]; - - if (class == ELFCLASS32) { - Elf32_Ehdr* elf; - Elf32_Shdr* shdr; - - elf = (Elf32_Ehdr*) data; - shdr = (Elf32_Shdr*) (data + ((Elf32_Ehdr*) elf)->e_shoff); - - char* strTab = (char*) (data + shdr[elf->e_shstrndx].sh_offset); - for (i = 0; i < elf->e_shnum; i++) { - if (strcmp(&strTab[shdr[i].sh_name], section_name) == 0) { - *offset = shdr[i].sh_offset; - *length = shdr[i].sh_size; - } - } - } else if (class == ELFCLASS64) { - Elf64_Ehdr* elf; - Elf64_Shdr* shdr; - - elf = (Elf64_Ehdr*) data; - shdr = (Elf64_Shdr*) (data + elf->e_shoff); - - char* strTab = (char*) (data + shdr[elf->e_shstrndx].sh_offset); - for (i = 0; i < elf->e_shnum; i++) { - if (strcmp(&strTab[shdr[i].sh_name], section_name) == 0) { - *offset = shdr[i].sh_offset; - *length = shdr[i].sh_size; - } - } - } else { - fprintf(stderr, "Platforms other than 32-bit/64-bit are currently not supported!"); - munmap(data, map_size); - return false; - } - - munmap(data, map_size); - return true; -} - char* read_file_offset_length(const char* fname, unsigned long offset, unsigned long length) { FILE* f; if ((f = fopen(fname, "r")) == NULL) { @@ -387,36 +330,6 @@ char* read_file_offset_length(const char* fname, unsigned long offset, unsigned return buffer; } -int appimage_print_hex(char* fname, unsigned long offset, unsigned long length) { - char* data; - if ((data = read_file_offset_length(fname, offset, length)) == NULL) { - return 1; - } - - for (long long k = 0; k < length && data[k] != '\0'; k++) { - printf("%x", data[k]); - } - - free(data); - - printf("\n"); - - return 0; -} - -int appimage_print_binary(char* fname, unsigned long offset, unsigned long length) { - char* data; - if ((data = read_file_offset_length(fname, offset, length)) == NULL) { - return 1; - } - - printf("%s\n", data); - - free(data); - - return 0; -} - /* Exit status to use when launching an AppImage fails. * For applications that assign meanings to exit status codes (e.g. rsync), @@ -430,82 +343,80 @@ struct stat st; static ssize_t fs_offset; // The offset at which a filesystem image is expected = end of this ELF -static void die(const char *msg) { +static void die(const char* msg) { fprintf(stderr, "%s\n", msg); exit(EXIT_EXECERROR); } /* Check whether directory is writable */ bool is_writable_directory(char* str) { - if(access(str, W_OK) == 0) { + if (access(str, W_OK) == 0) { return true; } else { return false; } } -bool startsWith(const char *pre, const char *str) -{ +bool startsWith(const char* pre, const char* str) { size_t lenpre = strlen(pre), - lenstr = strlen(str); + lenstr = strlen(str); return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0; } /* Fill in a stat structure. Does not set st_ino */ -sqfs_err private_sqfs_stat(sqfs *fs, sqfs_inode *inode, struct stat *st) { - sqfs_err err = SQFS_OK; - uid_t id; - - memset(st, 0, sizeof(*st)); - st->st_mode = inode->base.mode; - st->st_nlink = inode->nlink; - st->st_mtime = st->st_ctime = st->st_atime = inode->base.mtime; - - if (S_ISREG(st->st_mode)) { - /* FIXME: do symlinks, dirs, etc have a size? */ - st->st_size = inode->xtra.reg.file_size; - st->st_blocks = st->st_size / 512; - } else if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) { - st->st_rdev = sqfs_makedev(inode->xtra.dev.major, - inode->xtra.dev.minor); - } else if (S_ISLNK(st->st_mode)) { - st->st_size = inode->xtra.symlink_size; - } +sqfs_err private_sqfs_stat(sqfs* fs, sqfs_inode* inode, struct stat* st) { + sqfs_err err = SQFS_OK; + uid_t id; + + memset(st, 0, sizeof(*st)); + st->st_mode = inode->base.mode; + st->st_nlink = inode->nlink; + st->st_mtime = st->st_ctime = st->st_atime = inode->base.mtime; + + if (S_ISREG(st->st_mode)) { + /* FIXME: do symlinks, dirs, etc have a size? */ + st->st_size = inode->xtra.reg.file_size; + st->st_blocks = st->st_size / 512; + } else if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) { + st->st_rdev = sqfs_makedev(inode->xtra.dev.major, + inode->xtra.dev.minor); + } else if (S_ISLNK(st->st_mode)) { + st->st_size = inode->xtra.symlink_size; + } - st->st_blksize = fs->sb.block_size; /* seriously? */ + st->st_blksize = fs->sb.block_size; /* seriously? */ - err = sqfs_id_get(fs, inode->base.uid, &id); - if (err) - return err; - st->st_uid = id; - err = sqfs_id_get(fs, inode->base.guid, &id); - st->st_gid = id; - if (err) - return err; + err = sqfs_id_get(fs, inode->base.uid, &id); + if (err) + return err; + st->st_uid = id; + err = sqfs_id_get(fs, inode->base.guid, &id); + st->st_gid = id; + if (err) + return err; - return SQFS_OK; + return SQFS_OK; } /* ================= End ELF parsing */ -extern int fusefs_main(int argc, char *argv[], void (*mounted) (void)); +extern int fusefs_main(int argc, char* argv[], void (* mounted)(void)); // extern void ext2_quit(void); static pid_t fuse_pid; static int keepalive_pipe[2]; -static void * -write_pipe_thread (void *arg) -{ +static void* +write_pipe_thread(void* arg) { char c[32]; int res; // sprintf(stderr, "Called write_pipe_thread"); - memset (c, 'x', sizeof (c)); + memset(c, 'x', sizeof(c)); while (1) { /* Write until we block, on broken pipe, exit */ - res = write (keepalive_pipe[1], c, sizeof (c)); + res = write(keepalive_pipe[1], c, sizeof(c)); if (res == -1) { - kill (fuse_pid, SIGTERM); + kill(fuse_pid, SIGTERM); break; } } @@ -513,18 +424,16 @@ write_pipe_thread (void *arg) } void -fuse_mounted (void) -{ +fuse_mounted(void) { pthread_t thread; fuse_pid = getpid(); pthread_create(&thread, NULL, write_pipe_thread, keepalive_pipe); } -char* getArg(int argc, char *argv[],char chr) -{ +char* getArg(int argc, char* argv[], char chr) { int i; - for (i=1; i sizeof(_path)-1) { + if (len > sizeof(_path) - 1) { errno = ENAMETOOLONG; return -1; } @@ -571,62 +479,59 @@ mkdir_p(const char* const path) return 0; } -void print_help(const char *appimage_path) -{ +void print_help(const char* appimage_path) { // TODO: "--appimage-list List content from embedded filesystem image\n" fprintf(stderr, - "AppImage options:\n\n" - " --appimage-extract [] Extract content from embedded filesystem image\n" - " If pattern is passed, only extract matching files\n" - " --appimage-help Print this help\n" - " --appimage-mount Mount embedded filesystem image and print\n" - " mount point and wait for kill with Ctrl-C\n" - " --appimage-offset Print byte offset to start of embedded\n" - " filesystem image\n" - " --appimage-portable-home Create a portable home folder to use as $HOME\n" - " --appimage-portable-config Create a portable config folder to use as\n" - " $XDG_CONFIG_HOME\n" - " --appimage-signature Print digital signature embedded in AppImage\n" - " --appimage-updateinfo[rmation] Print update info embedded in AppImage\n" - " --appimage-version Print version of AppImageKit\n" - "\n" - "Portable home:\n" - "\n" - " If you would like the application contained inside this AppImage to store its\n" - " data alongside this AppImage rather than in your home directory, then you can\n" - " place a directory named\n" - "\n" - " %s.home\n" - "\n" - " Or you can invoke this AppImage with the --appimage-portable-home option,\n" - " which will create this directory for you. As long as the directory exists\n" - " and is neither moved nor renamed, the application contained inside this\n" - " AppImage to store its data in this directory rather than in your home\n" - " directory\n" - "\n" - "License:\n" - " This executable contains code from\n" - " * runtime, licensed under the terms of\n" - " https://github.com/probonopd/static-tools/blob/master/LICENSE\n" - " * musl libc, licensed under the terms of\n" - " https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT\n" - " * libfuse, licensed under the terms of\n" - " https://github.com/libfuse/libfuse/blob/master/LGPL2.txt\n" - " * squashfuse, licensed under the terms of\n" - " https://github.com/vasi/squashfuse/blob/master/LICENSE\n" - " * libzstd, licensed under the terms of\n" - " https://github.com/facebook/zstd/blob/dev/LICENSE\n" - "Please see https://github.com/probonopd/static-tools/\n" - "for information on how to obtain and build the source code\n" - , appimage_path); + "AppImage options:\n\n" + " --appimage-extract [] Extract content from embedded filesystem image\n" + " If pattern is passed, only extract matching files\n" + " --appimage-help Print this help\n" + " --appimage-mount Mount embedded filesystem image and print\n" + " mount point and wait for kill with Ctrl-C\n" + " --appimage-offset Print byte offset to start of embedded\n" + " filesystem image\n" + " --appimage-portable-home Create a portable home folder to use as $HOME\n" + " --appimage-portable-config Create a portable config folder to use as\n" + " $XDG_CONFIG_HOME\n" + " --appimage-signature Print digital signature embedded in AppImage\n" + " --appimage-updateinfo[rmation] Print update info embedded in AppImage\n" + " --appimage-version Print version of AppImageKit\n" + "\n" + "Portable home:\n" + "\n" + " If you would like the application contained inside this AppImage to store its\n" + " data alongside this AppImage rather than in your home directory, then you can\n" + " place a directory named\n" + "\n" + " %s.home\n" + "\n" + " Or you can invoke this AppImage with the --appimage-portable-home option,\n" + " which will create this directory for you. As long as the directory exists\n" + " and is neither moved nor renamed, the application contained inside this\n" + " AppImage to store its data in this directory rather than in your home\n" + " directory\n" + "\n" + "License:\n" + " This executable contains code from\n" + " * runtime, licensed under the terms of\n" + " https://github.com/probonopd/static-tools/blob/master/LICENSE\n" + " * musl libc, licensed under the terms of\n" + " https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT\n" + " * libfuse, licensed under the terms of\n" + " https://github.com/libfuse/libfuse/blob/master/LGPL2.txt\n" + " * squashfuse, licensed under the terms of\n" + " https://github.com/vasi/squashfuse/blob/master/LICENSE\n" + " * libzstd, licensed under the terms of\n" + " https://github.com/facebook/zstd/blob/dev/LICENSE\n" + "Please see https://github.com/probonopd/static-tools/\n" + "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* appimage_path, const char* name) { char option[32]; sprintf(option, "appimage-portable-%s", name); - if (arg && strcmp(arg, option)==0) { + if (arg && strcmp(arg, option) == 0) { char portable_dir[PATH_MAX]; char fullpath[PATH_MAX]; @@ -647,7 +552,8 @@ void portable_option(const char *arg, const char *appimage_path, const char *nam } } -bool extract_appimage(const char* const appimage_path, const char* const _prefix, const char* const _pattern, const bool overwrite, const bool verbose) { +bool extract_appimage(const char* const appimage_path, const char* const _prefix, const char* const _pattern, + const bool overwrite, const bool verbose) { sqfs_err err = SQFS_OK; sqfs_traverse trv; sqfs fs; @@ -725,7 +631,7 @@ bool extract_appimage(const char* const appimage_path, const char* const _prefix unlink(prefixed_path_to_extract); if (link(existing_path_for_inode, prefixed_path_to_extract) == -1) { fprintf(stderr, "Couldn't create hardlink from \"%s\" to \"%s\": %s\n", - prefixed_path_to_extract, existing_path_for_inode, strerror(errno)); + prefixed_path_to_extract, existing_path_for_inode, strerror(errno)); rv = false; break; } else { @@ -733,7 +639,8 @@ bool extract_appimage(const char* const appimage_path, const char* const _prefix } } else { struct stat st; - if (!overwrite && stat(prefixed_path_to_extract, &st) == 0 && st.st_size == inode.xtra.reg.file_size) { + if (!overwrite && stat(prefixed_path_to_extract, &st) == 0 && + st.st_size == inode.xtra.reg.file_size) { // fprintf(stderr, "File exists and file size matches, skipping\n"); continue; } @@ -781,7 +688,8 @@ bool extract_appimage(const char* const appimage_path, const char* const _prefix if (!rv) break; } - } else if (inode.base.inode_type == SQUASHFS_SYMLINK_TYPE || inode.base.inode_type == SQUASHFS_LSYMLINK_TYPE) { + } else if (inode.base.inode_type == SQUASHFS_SYMLINK_TYPE || + inode.base.inode_type == SQUASHFS_LSYMLINK_TYPE) { size_t size; sqfs_readlink(&fs, &inode, NULL, &size); char buf[size]; @@ -829,7 +737,7 @@ int rm_recursive_callback(const char* path, const struct stat* stat, const int t case FTW_NS: case FTW_DNR: fprintf(stderr, "%s: ftw error: %s\n", - path, strerror(errno)); + path, strerror(errno)); return 1; case FTW_D: @@ -886,11 +794,11 @@ void build_mount_point(char* mount_dir, const char* const argv0, char const* con strcpy(mount_dir, temp_base); strncpy(mount_dir + templen, "/.mount_", 8); strncpy(mount_dir + templen + 8, path_basename, namelen); - strncpy(mount_dir+templen+8+namelen, "XXXXXX", 6); - mount_dir[templen+8+namelen+6] = 0; // null terminate destination + strncpy(mount_dir + templen + 8 + namelen, "XXXXXX", 6); + mount_dir[templen + 8 + namelen + 6] = 0; // null terminate destination } -int fusefs_main(int argc, char *argv[], void (*mounted) (void)) { +int fusefs_main(int argc, char* argv[], void (* mounted)(void)) { struct fuse_args args; sqfs_opts opts; @@ -904,35 +812,37 @@ int fusefs_main(int argc, char *argv[], void (*mounted) (void)) { #endif int err; - sqfs_ll *ll; + sqfs_ll* ll; struct fuse_opt fuse_opts[] = { - {"offset=%zu", offsetof(sqfs_opts, offset), 0}, - {"timeout=%u", offsetof(sqfs_opts, idle_timeout_secs), 0}, - FUSE_OPT_END + {"offset=%zu", offsetof(sqfs_opts, offset), 0}, + {"timeout=%u", offsetof(sqfs_opts, idle_timeout_secs), 0}, + {"fsname=%s", "squashfuse", 0}, + {"subtype=%s", "squashfuse", 0}, + FUSE_OPT_END }; - + struct fuse_lowlevel_ops sqfs_ll_ops; memset(&sqfs_ll_ops, 0, sizeof(sqfs_ll_ops)); - sqfs_ll_ops.getattr = sqfs_ll_op_getattr; - sqfs_ll_ops.opendir = sqfs_ll_op_opendir; - sqfs_ll_ops.releasedir = sqfs_ll_op_releasedir; - sqfs_ll_ops.readdir = sqfs_ll_op_readdir; - sqfs_ll_ops.lookup = sqfs_ll_op_lookup; - sqfs_ll_ops.open = sqfs_ll_op_open; - sqfs_ll_ops.create = sqfs_ll_op_create; - sqfs_ll_ops.release = sqfs_ll_op_release; - sqfs_ll_ops.read = sqfs_ll_op_read; - sqfs_ll_ops.readlink = sqfs_ll_op_readlink; - sqfs_ll_ops.listxattr = sqfs_ll_op_listxattr; - sqfs_ll_ops.getxattr = sqfs_ll_op_getxattr; - sqfs_ll_ops.forget = sqfs_ll_op_forget; - sqfs_ll_ops.statfs = stfs_ll_op_statfs; - + sqfs_ll_ops.getattr = sqfs_ll_op_getattr; + sqfs_ll_ops.opendir = sqfs_ll_op_opendir; + sqfs_ll_ops.releasedir = sqfs_ll_op_releasedir; + sqfs_ll_ops.readdir = sqfs_ll_op_readdir; + sqfs_ll_ops.lookup = sqfs_ll_op_lookup; + sqfs_ll_ops.open = sqfs_ll_op_open; + sqfs_ll_ops.create = sqfs_ll_op_create; + sqfs_ll_ops.release = sqfs_ll_op_release; + sqfs_ll_ops.read = sqfs_ll_op_read; + sqfs_ll_ops.readlink = sqfs_ll_op_readlink; + sqfs_ll_ops.listxattr = sqfs_ll_op_listxattr; + sqfs_ll_ops.getxattr = sqfs_ll_op_getxattr; + sqfs_ll_ops.forget = sqfs_ll_op_forget; + sqfs_ll_ops.statfs = stfs_ll_op_statfs; + /* PARSE ARGS */ args.argc = argc; args.argv = argv; args.allocated = 0; - + opts.progname = argv[0]; opts.image = NULL; opts.mountpoint = 0; @@ -942,12 +852,12 @@ int fusefs_main(int argc, char *argv[], void (*mounted) (void)) { sqfs_usage(argv[0], true); #if FUSE_USE_VERSION >= 30 - if (fuse_parse_cmdline(&args, &fuse_cmdline_opts) != 0) + if (fuse_parse_cmdline(&args, &fuse_cmdline_opts) != 0) #else - if (fuse_parse_cmdline(&args, - &fuse_cmdline_opts.mountpoint, - &fuse_cmdline_opts.mt, - &fuse_cmdline_opts.foreground) == -1) + if (fuse_parse_cmdline(&args, + &fuse_cmdline_opts.mountpoint, + &fuse_cmdline_opts.mt, + &fuse_cmdline_opts.foreground) == -1) #endif sqfs_usage(argv[0], true); if (fuse_cmdline_opts.mountpoint == NULL) @@ -963,33 +873,33 @@ int fusefs_main(int argc, char *argv[], void (*mounted) (void)) { while (true) { int fd = open("/dev/null", O_RDONLY); if (fd == -1) { - /* Can't open /dev/null, how bizarre! However, - * fuse_deamonize won't clobber fds if it can't - * open /dev/null either, so we ought to be OK. - */ - break; + /* Can't open /dev/null, how bizarre! However, + * fuse_deamonize won't clobber fds if it can't + * open /dev/null either, so we ought to be OK. + */ + break; } if (fd > 2) { - /* fds 0-2 are now guaranteed to be open. */ - close(fd); - break; + /* fds 0-2 are now guaranteed to be open. */ + close(fd); + break; } } /* OPEN FS */ err = !(ll = sqfs_ll_open(opts.image, opts.offset)); - + /* STARTUP FUSE */ if (!err) { sqfs_ll_chan ch; err = -1; if (sqfs_ll_mount( - &ch, - fuse_cmdline_opts.mountpoint, - &args, - &sqfs_ll_ops, - sizeof(sqfs_ll_ops), - ll) == SQFS_OK) { + &ch, + fuse_cmdline_opts.mountpoint, + &args, + &sqfs_ll_ops, + sizeof(sqfs_ll_ops), + ll) == SQFS_OK) { if (sqfs_ll_daemonize(fuse_cmdline_opts.foreground) != -1) { if (fuse_set_signal_handlers(ch.session) != -1) { if (opts.idle_timeout_secs) { @@ -1012,7 +922,7 @@ int fusefs_main(int argc, char *argv[], void (*mounted) (void)) { rmdir(fuse_cmdline_opts.mountpoint); free(ll); free(fuse_cmdline_opts.mountpoint); - + return -err; } @@ -1031,18 +941,18 @@ int fusefs_main(int argc, char *argv[], void (*mounted) (void)) { // // The basic MD5 functions. F and G are optimised compared to their RFC 1321 definitions for architectures that lack // an AND-NOT instruction, just like in Colin Plumb's implementation. -#define F( x, y, z ) ( (z) ^ ((x) & ((y) ^ (z))) ) -#define G( x, y, z ) ( (y) ^ ((z) & ((x) ^ (y))) ) -#define H( x, y, z ) ( (x) ^ (y) ^ (z) ) -#define I( x, y, z ) ( (y) ^ ((x) | ~(z)) ) +#define F(x, y, z) ( (z) ^ ((x) & ((y) ^ (z))) ) +#define G(x, y, z) ( (y) ^ ((z) & ((x) ^ (y))) ) +#define H(x, y, z) ( (x) ^ (y) ^ (z) ) +#define I(x, y, z) ( (y) ^ ((x) | ~(z)) ) // STEP // // The MD5 transformation for all four rounds. -#define STEP( f, a, b, c, d, x, t, s ) \ - (a) += f((b), (c), (d)) + (x) + (t); \ - (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ - (a) += (b); +#define STEP(f, a, b, c, d, x, t, s) \ +(a) += f((b), (c), (d)) + (x) + (t); \ +(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ +(a) += (b); // TransformFunction // @@ -1051,114 +961,112 @@ int fusefs_main(int argc, char *argv[], void (*mounted) (void)) { static void* TransformFunction - ( - Md5Context* ctx, - void const* data, - uintmax_t size - ) -{ - uint8_t* ptr; - uint32_t a; - uint32_t b; - uint32_t c; - uint32_t d; - uint32_t saved_a; - uint32_t saved_b; - uint32_t saved_c; - uint32_t saved_d; - - #define GET(n) (ctx->block[(n)]) - #define SET(n) (ctx->block[(n)] = \ - ((uint32_t)ptr[(n)*4 + 0] << 0 ) \ - | ((uint32_t)ptr[(n)*4 + 1] << 8 ) \ - | ((uint32_t)ptr[(n)*4 + 2] << 16) \ - | ((uint32_t)ptr[(n)*4 + 3] << 24) ) - - ptr = (uint8_t*)data; + ( + Md5Context* ctx, + void const* data, + uintmax_t size + ) { + uint8_t* ptr; + uint32_t a; + uint32_t b; + uint32_t c; + uint32_t d; + uint32_t saved_a; + uint32_t saved_b; + uint32_t saved_c; + uint32_t saved_d; + +#define GET(n) (ctx->block[(n)]) +#define SET(n) (ctx->block[(n)] = \ + ((uint32_t)ptr[(n)*4 + 0] << 0 ) \ + | ((uint32_t)ptr[(n)*4 + 1] << 8 ) \ + | ((uint32_t)ptr[(n)*4 + 2] << 16) \ + | ((uint32_t)ptr[(n)*4 + 3] << 24) ) + + ptr = (uint8_t*) data; a = ctx->a; b = ctx->b; c = ctx->c; d = ctx->d; - do - { + do { saved_a = a; saved_b = b; saved_c = c; saved_d = d; // Round 1 - STEP( F, a, b, c, d, SET(0), 0xd76aa478, 7 ) - STEP( F, d, a, b, c, SET(1), 0xe8c7b756, 12 ) - STEP( F, c, d, a, b, SET(2), 0x242070db, 17 ) - STEP( F, b, c, d, a, SET(3), 0xc1bdceee, 22 ) - STEP( F, a, b, c, d, SET(4), 0xf57c0faf, 7 ) - STEP( F, d, a, b, c, SET(5), 0x4787c62a, 12 ) - STEP( F, c, d, a, b, SET(6), 0xa8304613, 17 ) - STEP( F, b, c, d, a, SET(7), 0xfd469501, 22 ) - STEP( F, a, b, c, d, SET(8 ), 0x698098d8, 7 ) - STEP( F, d, a, b, c, SET(9 ), 0x8b44f7af, 12 ) - STEP( F, c, d, a, b, SET(10 ), 0xffff5bb1, 17 ) - STEP( F, b, c, d, a, SET(11 ), 0x895cd7be, 22 ) - STEP( F, a, b, c, d, SET(12 ), 0x6b901122, 7 ) - STEP( F, d, a, b, c, SET(13 ), 0xfd987193, 12 ) - STEP( F, c, d, a, b, SET(14 ), 0xa679438e, 17 ) - STEP( F, b, c, d, a, SET(15 ), 0x49b40821, 22 ) + STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) + STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) + STEP(F, c, d, a, b, SET(2), 0x242070db, 17) + STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22) + STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7) + STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12) + STEP(F, c, d, a, b, SET(6), 0xa8304613, 17) + STEP(F, b, c, d, a, SET(7), 0xfd469501, 22) + STEP(F, a, b, c, d, SET(8), 0x698098d8, 7) + STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12) + STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) + STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22) + STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) + STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) + STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) + STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) // Round 2 - STEP( G, a, b, c, d, GET(1), 0xf61e2562, 5 ) - STEP( G, d, a, b, c, GET(6), 0xc040b340, 9 ) - STEP( G, c, d, a, b, GET(11), 0x265e5a51, 14 ) - STEP( G, b, c, d, a, GET(0), 0xe9b6c7aa, 20 ) - STEP( G, a, b, c, d, GET(5), 0xd62f105d, 5 ) - STEP( G, d, a, b, c, GET(10), 0x02441453, 9 ) - STEP( G, c, d, a, b, GET(15), 0xd8a1e681, 14 ) - STEP( G, b, c, d, a, GET(4), 0xe7d3fbc8, 20 ) - STEP( G, a, b, c, d, GET(9), 0x21e1cde6, 5 ) - STEP( G, d, a, b, c, GET(14), 0xc33707d6, 9 ) - STEP( G, c, d, a, b, GET(3), 0xf4d50d87, 14 ) - STEP( G, b, c, d, a, GET(8), 0x455a14ed, 20 ) - STEP( G, a, b, c, d, GET(13), 0xa9e3e905, 5 ) - STEP( G, d, a, b, c, GET(2), 0xfcefa3f8, 9 ) - STEP( G, c, d, a, b, GET(7), 0x676f02d9, 14 ) - STEP( G, b, c, d, a, GET(12), 0x8d2a4c8a, 20 ) + STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) + STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) + STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) + STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20) + STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5) + STEP(G, d, a, b, c, GET(10), 0x02441453, 9) + STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) + STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20) + STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5) + STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) + STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14) + STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20) + STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) + STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) + STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) + STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) // Round 3 - STEP( H, a, b, c, d, GET(5), 0xfffa3942, 4 ) - STEP( H, d, a, b, c, GET(8), 0x8771f681, 11 ) - STEP( H, c, d, a, b, GET(11), 0x6d9d6122, 16 ) - STEP( H, b, c, d, a, GET(14), 0xfde5380c, 23 ) - STEP( H, a, b, c, d, GET(1), 0xa4beea44, 4 ) - STEP( H, d, a, b, c, GET(4), 0x4bdecfa9, 11 ) - STEP( H, c, d, a, b, GET(7), 0xf6bb4b60, 16 ) - STEP( H, b, c, d, a, GET(10), 0xbebfbc70, 23 ) - STEP( H, a, b, c, d, GET(13), 0x289b7ec6, 4 ) - STEP( H, d, a, b, c, GET(0), 0xeaa127fa, 11 ) - STEP( H, c, d, a, b, GET(3), 0xd4ef3085, 16 ) - STEP( H, b, c, d, a, GET(6), 0x04881d05, 23 ) - STEP( H, a, b, c, d, GET(9), 0xd9d4d039, 4 ) - STEP( H, d, a, b, c, GET(12), 0xe6db99e5, 11 ) - STEP( H, c, d, a, b, GET(15), 0x1fa27cf8, 16 ) - STEP( H, b, c, d, a, GET(2), 0xc4ac5665, 23 ) + STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) + STEP(H, d, a, b, c, GET(8), 0x8771f681, 11) + STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) + STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23) + STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4) + STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11) + STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16) + STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23) + STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) + STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11) + STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16) + STEP(H, b, c, d, a, GET(6), 0x04881d05, 23) + STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4) + STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11) + STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) + STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23) // Round 4 - STEP( I, a, b, c, d, GET(0), 0xf4292244, 6 ) - STEP( I, d, a, b, c, GET(7), 0x432aff97, 10 ) - STEP( I, c, d, a, b, GET(14), 0xab9423a7, 15 ) - STEP( I, b, c, d, a, GET(5), 0xfc93a039, 21 ) - STEP( I, a, b, c, d, GET(12), 0x655b59c3, 6 ) - STEP( I, d, a, b, c, GET(3), 0x8f0ccc92, 10 ) - STEP( I, c, d, a, b, GET(10), 0xffeff47d, 15 ) - STEP( I, b, c, d, a, GET(1), 0x85845dd1, 21 ) - STEP( I, a, b, c, d, GET(8), 0x6fa87e4f, 6 ) - STEP( I, d, a, b, c, GET(15), 0xfe2ce6e0, 10 ) - STEP( I, c, d, a, b, GET(6), 0xa3014314, 15 ) - STEP( I, b, c, d, a, GET(13), 0x4e0811a1, 21 ) - STEP( I, a, b, c, d, GET(4), 0xf7537e82, 6 ) - STEP( I, d, a, b, c, GET(11), 0xbd3af235, 10 ) - STEP( I, c, d, a, b, GET(2), 0x2ad7d2bb, 15 ) - STEP( I, b, c, d, a, GET(9), 0xeb86d391, 21 ) + STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) + STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) + STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) + STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21) + STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) + STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10) + STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) + STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21) + STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6) + STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) + STEP(I, c, d, a, b, GET(6), 0xa3014314, 15) + STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21) + STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6) + STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) + STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) + STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) a += saved_a; b += saved_b; @@ -1166,15 +1074,15 @@ TransformFunction d += saved_d; ptr += 64; - } while( size -= 64 ); + } while (size -= 64); ctx->a = a; ctx->b = b; ctx->c = c; ctx->d = d; - #undef GET - #undef SET +#undef GET +#undef SET return ptr; } @@ -1184,10 +1092,9 @@ TransformFunction // Initialises an MD5 Context. Use this to initialise/reset a context. void Md5Initialise - ( - Md5Context* Context // [out] - ) -{ + ( + Md5Context* Context // [out] + ) { Context->a = 0x67452301; Context->b = 0xefcdab89; Context->c = 0x98badcfe; @@ -1203,48 +1110,43 @@ Md5Initialise // calling this function until all the data has been added. Then call Md5Finalise to calculate the hash. void Md5Update - ( - Md5Context* Context, // [in out] - void const* Buffer, // [in] - uint32_t BufferSize // [in] - ) -{ - uint32_t saved_lo; - uint32_t used; - uint32_t free; + ( + Md5Context* Context, // [in out] + void const* Buffer, // [in] + uint32_t BufferSize // [in] + ) { + uint32_t saved_lo; + uint32_t used; + uint32_t free; saved_lo = Context->lo; - if( (Context->lo = (saved_lo + BufferSize) & 0x1fffffff) < saved_lo ) - { + if ((Context->lo = (saved_lo + BufferSize) & 0x1fffffff) < saved_lo) { Context->hi++; } - Context->hi += (uint32_t)( BufferSize >> 29 ); + Context->hi += (uint32_t) (BufferSize >> 29); used = saved_lo & 0x3f; - if( used ) - { + if (used) { free = 64 - used; - if( BufferSize < free ) - { - memcpy( &Context->buffer[used], Buffer, BufferSize ); + if (BufferSize < free) { + memcpy(&Context->buffer[used], Buffer, BufferSize); return; } - memcpy( &Context->buffer[used], Buffer, free ); - Buffer = (uint8_t*)Buffer + free; + memcpy(&Context->buffer[used], Buffer, free); + Buffer = (uint8_t*) Buffer + free; BufferSize -= free; TransformFunction(Context, Context->buffer, 64); } - if( BufferSize >= 64 ) - { - Buffer = TransformFunction( Context, Buffer, BufferSize & ~(unsigned long)0x3f ); + if (BufferSize >= 64) { + Buffer = TransformFunction(Context, Buffer, BufferSize & ~(unsigned long) 0x3f); BufferSize &= 0x3f; } - memcpy( Context->buffer, Buffer, BufferSize ); + memcpy(Context->buffer, Buffer, BufferSize); } // Md5Finalise @@ -1253,13 +1155,12 @@ Md5Update // calling this, Md5Initialised must be used to reuse the context. void Md5Finalise - ( - Md5Context* Context, // [in out] - MD5_HASH* Digest // [in] - ) -{ - uint32_t used; - uint32_t free; + ( + Md5Context* Context, // [in out] + MD5_HASH* Digest // [in] + ) { + uint32_t used; + uint32_t free; used = Context->lo & 0x3f; @@ -1267,44 +1168,43 @@ Md5Finalise free = 64 - used; - if(free < 8) - { - memset( &Context->buffer[used], 0, free ); - TransformFunction( Context, Context->buffer, 64 ); + if (free < 8) { + memset(&Context->buffer[used], 0, free); + TransformFunction(Context, Context->buffer, 64); used = 0; free = 64; } - memset( &Context->buffer[used], 0, free - 8 ); + memset(&Context->buffer[used], 0, free - 8); Context->lo <<= 3; - Context->buffer[56] = (uint8_t)( Context->lo ); - Context->buffer[57] = (uint8_t)( Context->lo >> 8 ); - Context->buffer[58] = (uint8_t)( Context->lo >> 16 ); - Context->buffer[59] = (uint8_t)( Context->lo >> 24 ); - Context->buffer[60] = (uint8_t)( Context->hi ); - Context->buffer[61] = (uint8_t)( Context->hi >> 8 ); - Context->buffer[62] = (uint8_t)( Context->hi >> 16 ); - Context->buffer[63] = (uint8_t)( Context->hi >> 24 ); - - TransformFunction( Context, Context->buffer, 64 ); - - Digest->bytes[0] = (uint8_t)( Context->a ); - Digest->bytes[1] = (uint8_t)( Context->a >> 8 ); - Digest->bytes[2] = (uint8_t)( Context->a >> 16 ); - Digest->bytes[3] = (uint8_t)( Context->a >> 24 ); - Digest->bytes[4] = (uint8_t)( Context->b ); - Digest->bytes[5] = (uint8_t)( Context->b >> 8 ); - Digest->bytes[6] = (uint8_t)( Context->b >> 16 ); - Digest->bytes[7] = (uint8_t)( Context->b >> 24 ); - Digest->bytes[8] = (uint8_t)( Context->c ); - Digest->bytes[9] = (uint8_t)( Context->c >> 8 ); - Digest->bytes[10] = (uint8_t)( Context->c >> 16 ); - Digest->bytes[11] = (uint8_t)( Context->c >> 24 ); - Digest->bytes[12] = (uint8_t)( Context->d ); - Digest->bytes[13] = (uint8_t)( Context->d >> 8 ); - Digest->bytes[14] = (uint8_t)( Context->d >> 16 ); - Digest->bytes[15] = (uint8_t)( Context->d >> 24 ); + Context->buffer[56] = (uint8_t) (Context->lo); + Context->buffer[57] = (uint8_t) (Context->lo >> 8); + Context->buffer[58] = (uint8_t) (Context->lo >> 16); + Context->buffer[59] = (uint8_t) (Context->lo >> 24); + Context->buffer[60] = (uint8_t) (Context->hi); + Context->buffer[61] = (uint8_t) (Context->hi >> 8); + Context->buffer[62] = (uint8_t) (Context->hi >> 16); + Context->buffer[63] = (uint8_t) (Context->hi >> 24); + + TransformFunction(Context, Context->buffer, 64); + + Digest->bytes[0] = (uint8_t) (Context->a); + Digest->bytes[1] = (uint8_t) (Context->a >> 8); + Digest->bytes[2] = (uint8_t) (Context->a >> 16); + Digest->bytes[3] = (uint8_t) (Context->a >> 24); + Digest->bytes[4] = (uint8_t) (Context->b); + Digest->bytes[5] = (uint8_t) (Context->b >> 8); + Digest->bytes[6] = (uint8_t) (Context->b >> 16); + Digest->bytes[7] = (uint8_t) (Context->b >> 24); + Digest->bytes[8] = (uint8_t) (Context->c); + Digest->bytes[9] = (uint8_t) (Context->c >> 8); + Digest->bytes[10] = (uint8_t) (Context->c >> 16); + Digest->bytes[11] = (uint8_t) (Context->c >> 24); + Digest->bytes[12] = (uint8_t) (Context->d); + Digest->bytes[13] = (uint8_t) (Context->d >> 8); + Digest->bytes[14] = (uint8_t) (Context->d >> 16); + Digest->bytes[15] = (uint8_t) (Context->d >> 24); } // Md5Calculate @@ -1312,17 +1212,16 @@ Md5Finalise // Combines Md5Initialise, Md5Update, and Md5Finalise into one function. Calculates the MD5 hash of the buffer. void Md5Calculate - ( - void const* Buffer, // [in] - uint32_t BufferSize, // [in] - MD5_HASH* Digest // [in] - ) -{ + ( + void const* Buffer, // [in] + uint32_t BufferSize, // [in] + MD5_HASH* Digest // [in] + ) { Md5Context context; - Md5Initialise( &context ); - Md5Update( &context, Buffer, BufferSize ); - Md5Finalise( &context, Digest ); + Md5Initialise(&context); + Md5Update(&context, Buffer, BufferSize); + Md5Finalise(&context, Digest); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1344,10 +1243,10 @@ char* appimage_hexlify(const char* bytes, const size_t numBytes) { return hexlified; } -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { char appimage_path[PATH_MAX]; char argv0_path[PATH_MAX]; - char * arg; + char* arg; /* We might want to operate on a target appimage rather than this file itself, * e.g., for appimaged which must not run untrusted code from random AppImages. @@ -1381,10 +1280,10 @@ int main(int argc, char *argv[]) { exit(EXIT_EXECERROR); } - arg=getArg(argc,argv,'-'); + arg = getArg(argc, argv, '-'); /* Print the help and then exit */ - if(arg && strcmp(arg,"appimage-help")==0) { + if (arg && strcmp(arg, "appimage-help") == 0) { char fullpath[PATH_MAX]; ssize_t length = readlink(appimage_path, fullpath, sizeof(fullpath)); @@ -1399,13 +1298,13 @@ int main(int argc, char *argv[]) { } /* Just print the offset and then exit */ - if(arg && strcmp(arg,"appimage-offset")==0) { + if (arg && strcmp(arg, "appimage-offset") == 0) { printf("%zu\n", fs_offset); exit(0); } /* extract the AppImage */ - if(arg && strcmp(arg,"appimage-extract")==0) { + if (arg && strcmp(arg, "appimage-extract") == 0) { char* pattern; // default use case: use standard prefix @@ -1430,7 +1329,7 @@ int main(int argc, char *argv[]) { int length; char fullpath[PATH_MAX]; - if(getenv("TARGET_APPIMAGE") == NULL) { + 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) { @@ -1532,7 +1431,7 @@ int main(int argc, char *argv[]) { if (!rm_recursive(prefix)) { fprintf(stderr, "Failed to clean up cache directory\n"); if (status == 0) /* avoid messing existing failure exit status */ - status = EXIT_EXECERROR; + status = EXIT_EXECERROR; } } @@ -1542,15 +1441,15 @@ int main(int argc, char *argv[]) { exit(status); } - if(arg && (strcmp(arg,"appimage-updateinformation")==0 || strcmp(arg,"appimage-updateinfo")==0)) { - fprintf(stderr,"--%s is not yet implemented in version %s\n", arg, GIT_COMMIT); - // NOTE: Must be implemented in this .c file with no additional dependencies + if (arg && (strcmp(arg, "appimage-updateinformation") == 0 || strcmp(arg, "appimage-updateinfo") == 0)) { + fprintf(stderr, "--%s is not yet implemented in version %s\n", arg, GIT_COMMIT); + // NOTE: Must be implemented in this .c file with no additional dependencies exit(1); } - if(arg && strcmp(arg,"appimage-signature")==0) { - fprintf(stderr,"--%s is not yet implemented in version %s\n", arg, GIT_COMMIT); - // NOTE: Must be implemented in this .c file with no additional dependencies + if (arg && strcmp(arg, "appimage-signature") == 0) { + fprintf(stderr, "--%s is not yet implemented in version %s\n", arg, GIT_COMMIT); + // NOTE: Must be implemented in this .c file with no additional dependencies exit(1); } @@ -1559,8 +1458,8 @@ int main(int argc, char *argv[]) { // 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 - if((arg && strncmp(arg, "appimage-", 8) == 0) && (arg && strcmp(arg,"appimage-mount")!=0)) { - fprintf(stderr,"--%s is not yet implemented in version %s\n", arg, GIT_COMMIT); + if ((arg && strncmp(arg, "appimage-", 8) == 0) && (arg && strcmp(arg, "appimage-mount") != 0)) { + fprintf(stderr, "--%s is not yet implemented in version %s\n", arg, GIT_COMMIT); exit(1); } @@ -1575,34 +1474,34 @@ int main(int argc, char *argv[]) { size_t mount_dir_size = strlen(mount_dir); pid_t pid; - char **real_argv; + char** real_argv; int i; if (mkdtemp(mount_dir) == NULL) { - perror ("create mount dir error"); - exit (EXIT_EXECERROR); + perror("create mount dir error"); + exit(EXIT_EXECERROR); } - if (pipe (keepalive_pipe) == -1) { - perror ("pipe error"); - exit (EXIT_EXECERROR); + if (pipe(keepalive_pipe) == -1) { + perror("pipe error"); + exit(EXIT_EXECERROR); } - pid = fork (); + pid = fork(); if (pid == -1) { - perror ("fork error"); - exit (EXIT_EXECERROR); + perror("fork error"); + exit(EXIT_EXECERROR); } if (pid == 0) { /* in child */ - char *child_argv[5]; + char* child_argv[5]; /* close read pipe */ - close (keepalive_pipe[0]); + close(keepalive_pipe[0]); - char *dir = realpath(appimage_path, NULL ); + char* dir = realpath(appimage_path, NULL); char options[100]; sprintf(options, "ro,offset=%zu", fs_offset); @@ -1613,14 +1512,14 @@ int main(int argc, char *argv[]) { child_argv[3] = dir; child_argv[4] = mount_dir; - if(0 != fusefs_main (5, child_argv, fuse_mounted)){ - char *title; - char *body; + if (0 != fusefs_main(5, child_argv, fuse_mounted)) { + char* title; + char* body; title = "Cannot mount AppImage, please check your FUSE setup."; body = "You might still be able to extract the contents of this AppImage \n" - "if you run it with the --appimage-extract option. \n" - "See https://github.com/AppImage/AppImageKit/wiki/FUSE \n" - "for more information"; + "if you run it with the --appimage-extract option. \n" + "See https://github.com/AppImage/AppImageKit/wiki/FUSE \n" + "for more information"; printf("\n%s\n", title); printf("%s\n", body); }; @@ -1629,34 +1528,34 @@ int main(int argc, char *argv[]) { int c; /* close write pipe */ - close (keepalive_pipe[1]); + close(keepalive_pipe[1]); /* Pause until mounted */ - read (keepalive_pipe[0], &c, 1); + read(keepalive_pipe[0], &c, 1); /* Fuse process has now daemonized, reap our child */ waitpid(pid, NULL, 0); - dir_fd = open (mount_dir, O_RDONLY); + dir_fd = open(mount_dir, O_RDONLY); if (dir_fd == -1) { - perror ("open dir error"); - exit (EXIT_EXECERROR); + perror("open dir error"); + exit(EXIT_EXECERROR); } - res = dup2 (dir_fd, 1023); + res = dup2(dir_fd, 1023); if (res == -1) { - perror ("dup2 error"); - exit (EXIT_EXECERROR); + perror("dup2 error"); + exit(EXIT_EXECERROR); } - close (dir_fd); + close(dir_fd); - real_argv = malloc (sizeof (char *) * (argc + 1)); + real_argv = malloc(sizeof(char*) * (argc + 1)); for (i = 0; i < argc; i++) { real_argv[i] = argv[i]; } real_argv[i] = NULL; - if(arg && strcmp(arg, "appimage-mount") == 0) { + if (arg && strcmp(arg, "appimage-mount") == 0) { char real_mount_dir[PATH_MAX]; if (realpath(mount_dir, real_mount_dir) == real_mount_dir) { @@ -1676,41 +1575,41 @@ int main(int argc, char *argv[]) { } /* Setting some environment variables that the app "inside" might use */ - setenv( "APPIMAGE", fullpath, 1 ); - setenv( "ARGV0", argv0_path, 1 ); - setenv( "APPDIR", mount_dir, 1 ); + setenv("APPIMAGE", fullpath, 1); + setenv("ARGV0", argv0_path, 1); + setenv("APPDIR", mount_dir, 1); char portable_home_dir[PATH_MAX]; char portable_config_dir[PATH_MAX]; /* If there is a directory with the same name as the AppImage plus ".home", then export $HOME */ - strcpy (portable_home_dir, fullpath); - strcat (portable_home_dir, ".home"); - if(is_writable_directory(portable_home_dir)){ + strcpy(portable_home_dir, fullpath); + strcat(portable_home_dir, ".home"); + if (is_writable_directory(portable_home_dir)) { fprintf(stderr, "Setting $HOME to %s\n", portable_home_dir); - setenv("HOME",portable_home_dir,1); + setenv("HOME", portable_home_dir, 1); } /* If there is a directory with the same name as the AppImage plus ".config", then export $XDG_CONFIG_HOME */ - strcpy (portable_config_dir, fullpath); - strcat (portable_config_dir, ".config"); - if(is_writable_directory(portable_config_dir)){ + strcpy(portable_config_dir, fullpath); + strcat(portable_config_dir, ".config"); + if (is_writable_directory(portable_config_dir)) { fprintf(stderr, "Setting $XDG_CONFIG_HOME to %s\n", portable_config_dir); - setenv("XDG_CONFIG_HOME",portable_config_dir,1); + setenv("XDG_CONFIG_HOME", portable_config_dir, 1); } /* Original working directory */ char cwd[1024]; if (getcwd(cwd, sizeof(cwd)) != NULL) { - setenv( "OWD", cwd, 1 ); + setenv("OWD", cwd, 1); } char filename[mount_dir_size + 8]; /* enough for mount_dir + "/AppRun" */ - strcpy (filename, mount_dir); - strcat (filename, "/AppRun"); + strcpy(filename, mount_dir); + strcat(filename, "/AppRun"); /* TODO: Find a way to get the exit status and/or output of this */ - execv (filename, real_argv); + execv(filename, real_argv); /* Error if we continue here */ perror("execv error"); exit(EXIT_EXECERROR);