From d873c13e1626946ca6c450266f03be4a483cfafa Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Tue, 19 Dec 2017 15:20:19 +0100 Subject: [PATCH] fetchurl: create compound dir of downloaded file Fixes #2623 --- repos/libports/src/app/fetchurl/component.cc | 36 +++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/repos/libports/src/app/fetchurl/component.cc b/repos/libports/src/app/fetchurl/component.cc index a4b127024f..35f3c13e0c 100644 --- a/repos/libports/src/app/fetchurl/component.cc +++ b/repos/libports/src/app/fetchurl/component.cc @@ -25,6 +25,7 @@ #include #include #include +#include struct Stats { @@ -95,6 +96,37 @@ void Libc::Component::construct(Libc::Env &env) char const *out_path = path.base(); + /* create compound directories leading to the path */ + for (size_t sub_path_len = 0; ; sub_path_len++) { + + bool const end_of_path = (out_path[sub_path_len] == 0); + bool const end_of_elem = (out_path[sub_path_len] == '/'); + + if (end_of_path) + break; + + if (!end_of_elem) + continue; + + Genode::String<256> sub_path(Genode::Cstring(out_path, sub_path_len)); + + /* skip '/' */ + sub_path_len++; + + /* if sub path is a directory, we are fine */ + struct stat sb; + sb.st_mode = 0; + stat(sub_path.string(), &sb); + if (S_ISDIR(sb.st_mode)) + continue; + + /* create directory for sub path */ + if (mkdir(sub_path.string(), 0777) < 0) { + Genode::error("failed to create directory ", sub_path); + break; + } + } + int fd = open(out_path, O_CREAT | O_RDWR); if (fd == -1) { switch (errno) { @@ -106,9 +138,11 @@ void Libc::Component::construct(Libc::Env &env) Genode::error(out_path, " is a directory"); break; case ENOSPC: Genode::error("cannot create ", out_path, ", out of space"); break; + default: + Genode::error("creation of ", out_path, " failed (errno=", errno, ")"); } env.parent().exit(errno); - return; + throw Genode::Exception(); } CURL *curl = curl_easy_init();