Skip to content

Commit

Permalink
add outdir parameter to curl_fetch_file
Browse files Browse the repository at this point in the history
files downloaded by curl can be saved in
a safe directory
  • Loading branch information
Julio Montes committed Apr 21, 2016
1 parent b243796 commit 73d1aeb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ bool curl_common_init(CURL** curl) {
return false;
}

gchar* curl_fetch_file(CURL* curl, gchar* url, int attempts, useconds_t u_sleep) {
gchar* curl_fetch_file(CURL* curl, gchar* url, const gchar* outdir, int attempts, useconds_t u_sleep) {
int fd;
FILE* file;
gchar *filename;
filename = g_strdup(DATADIR_PATH "/cloud-init-XXXXXX");
gchar* filename;

filename = malloc(sizeof(gchar)*PATH_MAX);
g_snprintf(filename, PATH_MAX, "%s/cloud-init-XXXXXX", outdir);

fd = mkstemp(filename);
if (fd < 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool curl_common_init(CURL** curl);
* On success, the function returns a path to the file downloaded,
* otherwise NULL is returned.
*/
gchar* curl_fetch_file(CURL* curl, gchar* url, int attempts, useconds_t u_sleep);
gchar* curl_fetch_file(CURL* curl, gchar* url, const gchar* outdir, int attempts, useconds_t u_sleep);


bool curl_ping(CURL* curl, gchar* url, int attempts, useconds_t u_sleep);
6 changes: 3 additions & 3 deletions src/datasources/openstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ bool openstack_start(void) {
case SOURCE_METADATA_SERVICE:
/* download metadata file from metadata service */
LOG(MOD "Fetching metadata file URL %s\n", METADATA_SERVICE_URL OPENSTACK_METADATA_FILE );
data_file = curl_fetch_file(curl, METADATA_SERVICE_URL OPENSTACK_METADATA_FILE, ms_attempts, ms_usleep);
data_file = curl_fetch_file(curl, METADATA_SERVICE_URL OPENSTACK_METADATA_FILE, DATADIR_PATH, ms_attempts, ms_usleep);
if (!data_file) {
LOG(MOD "Fetch metadata failed\n");
return false;
Expand All @@ -199,7 +199,7 @@ bool openstack_start(void) {

/* download userdata file from metadata service */
LOG(MOD "Fetching userdata file URL %s\n", METADATA_SERVICE_URL OPENSTACK_USERDATA_FILE );
data_file = curl_fetch_file(curl, METADATA_SERVICE_URL OPENSTACK_USERDATA_FILE, ms_attempts, ms_usleep);
data_file = curl_fetch_file(curl, METADATA_SERVICE_URL OPENSTACK_USERDATA_FILE, DATADIR_PATH, ms_attempts, ms_usleep);
if (!data_file) {
/* userdata is optional, so it is ok */
LOG(MOD "Userdata file not found\n");
Expand Down Expand Up @@ -600,7 +600,7 @@ static int openstack_metadata_files(GNode* node) {

case SOURCE_METADATA_SERVICE:
g_snprintf(src_content_file, PATH_MAX, "%s/%s", METADATA_SERVICE_URL, content_path );
tmp_content_file = curl_fetch_file(curl, src_content_file, 1, 0);
tmp_content_file = curl_fetch_file(curl, src_content_file, DATADIR_PATH, 1, 0);
if (!tmp_content_file) {
LOG(MOD "Fetch file '%s' failed\n", src_content_file);
break;
Expand Down
2 changes: 1 addition & 1 deletion tests/curl_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ START_TEST(test_curl_fetch_file)
ck_assert(fd != -1);

g_string_append(url, filename);
file_fetched = curl_fetch_file(curl, url->str, 1, 0);
file_fetched = curl_fetch_file(curl, url->str, "/tmp", 1, 0);
g_string_free(url, true);
ck_assert(file_fetched != NULL);
ck_assert(remove(filename) != -1);
Expand Down

0 comments on commit 73d1aeb

Please sign in to comment.