Skip to content

Commit 7fa2236

Browse files
authored
Use mkdir_p instead of mkdir when extracting, fixes #333
1 parent e8df637 commit 7fa2236

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

runtime.c

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,47 @@ char* getArg(int argc, char *argv[],char chr)
148148
return NULL;
149149
}
150150

151+
/* mkdir -p implemented in C, needed for https://github.com/probonopd/AppImageKit/issues/333
152+
* https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950 */
153+
int
154+
mkdir_p(const char *path)
155+
{
156+
/* Adapted from http://stackoverflow.com/a/2336245/119527 */
157+
const size_t len = strlen(path);
158+
char _path[PATH_MAX];
159+
char *p;
160+
161+
errno = 0;
162+
163+
/* Copy string so its mutable */
164+
if (len > sizeof(_path)-1) {
165+
errno = ENAMETOOLONG;
166+
return -1;
167+
}
168+
strcpy(_path, path);
169+
170+
/* Iterate the string */
171+
for (p = _path + 1; *p; p++) {
172+
if (*p == '/') {
173+
/* Temporarily truncate */
174+
*p = '\0';
175+
176+
if (mkdir(_path, S_IRWXU) != 0) {
177+
if (errno != EEXIST)
178+
return -1;
179+
}
180+
181+
*p = '/';
182+
}
183+
}
184+
185+
if (mkdir(_path, S_IRWXU) != 0) {
186+
if (errno != EEXIST)
187+
return -1;
188+
}
189+
190+
return 0;
191+
}
151192

152193
int
153194
main (int argc, char *argv[])
@@ -190,8 +231,8 @@ main (int argc, char *argv[])
190231
prefix = "squashfs-root/";
191232

192233
if(access(prefix, F_OK ) == -1 ) {
193-
if (mkdir(prefix, 0777) == -1) {
194-
perror("mkdir error");
234+
if (mkdir_p(prefix) == -1) {
235+
perror("mkdir_p error");
195236
exit(EXIT_FAILURE);
196237
}
197238
}
@@ -218,10 +259,10 @@ main (int argc, char *argv[])
218259
strcat(strcat(prefixed_path_to_extract, prefix), trv.path);
219260
if(inode.base.inode_type == SQUASHFS_DIR_TYPE){
220261
fprintf(stderr, "inode.xtra.dir.parent_inode: %ui\n", inode.xtra.dir.parent_inode);
221-
fprintf(stderr, "mkdir: %s/\n", prefixed_path_to_extract);
262+
fprintf(stderr, "mkdir_p: %s/\n", prefixed_path_to_extract);
222263
if(access(prefixed_path_to_extract, F_OK ) == -1 ) {
223-
if (mkdir(prefixed_path_to_extract, 0777) == -1) {
224-
perror("mkdir error");
264+
if (mkdir_p(prefixed_path_to_extract) == -1) {
265+
perror("mkdir_p error");
225266
exit(EXIT_FAILURE);
226267
}
227268
}

0 commit comments

Comments
 (0)