blob: 0e45fd7c1bab6bca61131b8ecbd791dab36beac6 [file] [log] [blame]
/**
*Licensed to the Apache Software Foundation (ASF) under one
*or more contributor license agreements. See the NOTICE file
*distributed with this work for additional information
*regarding copyright ownership. The ASF licenses this file
*to you under the Apache License, Version 2.0 (the
*"License"); you may not use this file except in compliance
*with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing,
*software distributed under the License is distributed on an
*"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
*specific language governing permissions and limitations
*under the License.
*/
/*
* update_command.c
*
* \date Aug 20, 2010
* \author <a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
* \copyright Apache License, Version 2.0
*
*/
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include "array_list.h"
#include "bundle_context.h"
celix_status_t updateCommand_download(bundle_context_pt context, char * url, char **inputFile);
size_t updateCommand_writeData(void *ptr, size_t size, size_t nmemb, FILE *stream);
void updateCommand_execute(void *handle, char * line, FILE *outStream, FILE *errStream) {
bundle_context_pt context = handle;
bundle_pt bundle = NULL;
char delims[] = " ";
char * sub = NULL;
char *save_ptr = NULL;
sub = strtok_r(line, delims, &save_ptr);
sub = strtok_r(NULL, delims, &save_ptr);
if (sub == NULL) {
fprintf(errStream, "Incorrect number of arguments.\n");
} else {
long id = atol(sub);
bundleContext_getBundleById(context, id, &bundle);
if (bundle != NULL) {
char inputFile[256];
sub = strtok_r(NULL, delims, &save_ptr);
inputFile[0] = '\0';
if (sub != NULL) {
char *test = inputFile;
printf("URL: %s\n", sub);
if (updateCommand_download(context, sub, &test) == CELIX_SUCCESS) {
printf("Update bundle with stream\n");
bundle_update(bundle, inputFile);
} else {
fprintf(errStream, "Unable to download from %s\n", sub);
}
} else {
bundle_update(bundle, NULL);
}
} else {
fprintf(errStream, "Bundle id is invalid.\n");
}
}
}
celix_status_t updateCommand_download(bundle_context_pt context, char * url, char **inputFile) {
CURL *curl = NULL;
CURLcode res = CURLE_FILE_COULDNT_READ_FILE;
curl = curl_easy_init();
if (curl) {
FILE *fp = NULL;
snprintf(*inputFile, 13,"updateXXXXXX");
umask(0011);
int fd = mkstemp(*inputFile);
if (fd) {
fp = fopen(*inputFile, "wb+");
if(fp!=NULL){
printf("Temp file: %s\n", *inputFile);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, updateCommand_writeData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
//curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
//curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, updateCommand_downloadProgress);
res = curl_easy_perform(curl);
fclose(fp);
}
/* always cleanup */
curl_easy_cleanup(curl);
if(fp==NULL){
return CELIX_FILE_IO_EXCEPTION;
}
}
}
if (res != CURLE_OK) {
printf("Error: %d\n", res);
*inputFile[0] = '\0';
return CELIX_ILLEGAL_STATE;
} else {
return CELIX_SUCCESS;
}
}
size_t updateCommand_writeData(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}