Runtime: Allow recursive extraction of partial sections of the AppImage
[appimagekit/gsi.git] / src / AppRun.c
blob876e313c6e4b0c0db58970c2e6110205388bb853
1 /**************************************************************************
3 Copyright (c) 2004-18 Simon Peter
4 Portions Copyright (c) 2010 RazZziel
6 All Rights Reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
26 **************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <libgen.h>
32 #include <dirent.h>
33 #include <string.h>
34 #include <errno.h>
36 #define die(...) \
37 do { \
38 fprintf(stderr, "Error: " __VA_ARGS__); \
39 exit(1); \
40 } while(0);
41 #define SET_NEW_ENV(str,len,fmt,...) \
42 format = fmt; \
43 length = strlen(format) + (len); \
44 char *str = calloc(length, sizeof(char)); \
45 snprintf(str, length, format, __VA_ARGS__); \
46 putenv(str);
47 #define MAX(a,b) (a > b ? a : b)
48 #define bool int
49 #define false 0
50 #define true -1
52 #define LINE_SIZE 255
54 int filter(const struct dirent *dir) {
55 char *p = (char*) &dir->d_name;
56 p = strrchr(p, '.');
57 return p && !strcmp(p, ".desktop");
60 int main(int argc, char *argv[]) {
61 char *appdir = dirname(realpath("/proc/self/exe", NULL));
62 if (!appdir)
63 die("Could not access /proc/self/exe\n");
65 int ret;
66 struct dirent **namelist;
67 ret = scandir(appdir, &namelist, filter, NULL);
69 if (ret == 0) {
70 die("No .desktop files found\n");
71 } else if(ret == -1) {
72 die("Could not scan directory %s\n", appdir);
75 /* Extract executable from .desktop file */
76 char *desktop_file = calloc(LINE_SIZE, sizeof(char));
77 snprintf(desktop_file, LINE_SIZE, "%s/%s", appdir, namelist[0]->d_name);
78 FILE *f = fopen(desktop_file, "r");
79 char *line = malloc(LINE_SIZE);
80 size_t n = LINE_SIZE;
82 do {
83 if (getline(&line, &n, f) == -1)
84 die("Executable not found, make sure there is a line starting with 'Exec='\n");
85 } while(strncmp(line, "Exec=", 5));
86 fclose(f);
87 char *exe = line+5;
89 // parse arguments
90 bool in_quotes = 0;
91 for (n = 0; n < LINE_SIZE; n++) {
92 if (!line[n]) // end of string
93 break;
94 else if (line[n] == 10 || line[n] == 13) {
95 line[n] = '\0';
96 line[n+1] = '\0';
97 line[n+2] = '\0';
98 break;
99 } else if (line[n] == '"') {
100 in_quotes = !in_quotes;
101 } else if (line[n] == ' ' && !in_quotes)
102 line[n] = '\0';
105 // count arguments
106 char* arg = exe;
107 int argcount = 0;
108 while ((arg += (strlen(arg)+1)) && *arg)
109 argcount += 1;
111 // merge args
112 char* outargptrs[argcount + argc + 1];
113 outargptrs[0] = exe;
114 int outargindex = 1;
115 arg = exe;
116 int argc_ = argc - 1; // argv[0] is the filename
117 char** argv_ = argv + 1;
118 while ((arg += (strlen(arg)+1)) && *arg) {
119 if (arg[0] == '%' || (arg[0] == '"' && arg[1] == '%')) { // handle desktop file field codes
120 char code = arg[arg[0] == '%' ? 1 : 2];
121 switch(code) {
122 case 'f':
123 case 'u':
124 if (argc_ > 0) {
125 outargptrs[outargindex++] = *argv_++;
126 argc_--;
128 break;
129 case 'F':
130 case 'U':
131 while (argc_ > 0) {
132 outargptrs[outargindex++] = *argv_++;
133 argc_--;
135 break;
136 case 'i':
137 case 'c':
138 case 'k':
139 fprintf(stderr, "WARNING: Desktop file field code %%%c is not currently supported\n", code);
140 break;
141 default:
142 fprintf(stderr, "WARNING: Invalid desktop file field code %%%c\n", code);
143 break;
145 } else {
146 outargptrs[outargindex++] = arg;
149 while (argc_ > 0) {
150 outargptrs[outargindex++] = *argv_++;
151 argc_--;
153 outargptrs[outargindex] = '\0'; // trailing null argument required by execvp()
155 // change directory
156 size_t appdir_s = strlen(appdir);
157 char *usr_in_appdir = malloc(appdir_s + 5);
158 snprintf(usr_in_appdir, appdir_s + 5, "%s/usr", appdir);
159 ret = chdir(usr_in_appdir);
160 if (ret != 0)
161 die("Could not cd into %s\n", usr_in_appdir);
163 // set environment variables
164 char *old_env;
165 size_t length;
166 const char *format;
168 /* https://docs.python.org/2/using/cmdline.html#envvar-PYTHONHOME */
169 SET_NEW_ENV(new_pythonhome, appdir_s, "PYTHONHOME=%s/usr/", appdir);
171 old_env = getenv("PATH") ?: "";
172 SET_NEW_ENV(new_path, appdir_s*5 + strlen(old_env), "PATH=%s/usr/bin/:%s/usr/sbin/:%s/usr/games/:%s/bin/:%s/sbin/:%s", appdir, appdir, appdir, appdir, appdir, old_env);
174 old_env = getenv("LD_LIBRARY_PATH") ?: "";
175 SET_NEW_ENV(new_ld_library_path, appdir_s*10 + strlen(old_env), "LD_LIBRARY_PATH=%s/usr/lib/:%s/usr/lib/i386-linux-gnu/:%s/usr/lib/x86_64-linux-gnu/:%s/usr/lib32/:%s/usr/lib64/:%s/lib/:%s/lib/i386-linux-gnu/:%s/lib/x86_64-linux-gnu/:%s/lib32/:%s/lib64/:%s", appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, old_env);
177 old_env = getenv("PYTHONPATH") ?: "";
178 SET_NEW_ENV(new_pythonpath, appdir_s + strlen(old_env), "PYTHONPATH=%s/usr/share/pyshared/:%s", appdir, old_env);
180 old_env = getenv("XDG_DATA_DIRS") ?: "/usr/local/share/:/usr/share/";
181 SET_NEW_ENV(new_xdg_data_dirs, appdir_s + strlen(old_env), "XDG_DATA_DIRS=%s/usr/share/:%s", appdir, old_env);
183 old_env = getenv("PERLLIB") ?: "";
184 SET_NEW_ENV(new_perllib, appdir_s*2 + strlen(old_env), "PERLLIB=%s/usr/share/perl5/:%s/usr/lib/perl5/:%s", appdir, appdir, old_env);
186 /* http://askubuntu.com/questions/251712/how-can-i-install-a-gsettings-schema-without-root-privileges */
187 old_env = getenv("GSETTINGS_SCHEMA_DIR") ?: "";
188 SET_NEW_ENV(new_gsettings_schema_dir, appdir_s + strlen(old_env), "GSETTINGS_SCHEMA_DIR=%s/usr/share/glib-2.0/schemas/:%s", appdir, old_env);
190 old_env = getenv("QT_PLUGIN_PATH") ?: "";
191 SET_NEW_ENV(new_qt_plugin_path, appdir_s*10 + strlen(old_env), "QT_PLUGIN_PATH=%s/usr/lib/qt4/plugins/:%s/usr/lib/i386-linux-gnu/qt4/plugins/:%s/usr/lib/x86_64-linux-gnu/qt4/plugins/:%s/usr/lib32/qt4/plugins/:%s/usr/lib64/qt4/plugins/:%s/usr/lib/qt5/plugins/:%s/usr/lib/i386-linux-gnu/qt5/plugins/:%s/usr/lib/x86_64-linux-gnu/qt5/plugins/:%s/usr/lib32/qt5/plugins/:%s/usr/lib64/qt5/plugins/:%s", appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, appdir, old_env);
193 /* Otherwise may get errors because Python cannot write __pycache__ bytecode cache */
194 putenv("PYTHONDONTWRITEBYTECODE=1");
196 /* Run */
197 ret = execvp(exe, outargptrs);
199 int error = errno;
201 if (ret == -1)
202 die("Error executing '%s': %s\n", exe, strerror(error));
204 free(line);
205 free(desktop_file);
206 free(usr_in_appdir);
207 free(new_pythonhome);
208 free(new_path);
209 free(new_ld_library_path);
210 free(new_pythonpath);
211 free(new_xdg_data_dirs);
212 free(new_perllib);
213 free(new_gsettings_schema_dir);
214 free(new_qt_plugin_path);
215 return 0;