Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed copy in parameters, added some prints for debug. #1

Merged
merged 1 commit into from
Mar 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions http-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ static CURL *curl;

static FILE *curr_fd;

static char *local_fn;
static char *remote_path;
static sds local_fn;
static sds remote_path;

static int remote_port;
static char *remote_base_url;
Expand All @@ -31,19 +31,31 @@ int set_global_opts();
int init_file_upload();
int create_full_path();

static off_t GetFileSize( int fd ) {
// Get the size of the input file.
struct stat stat_buf;
int res;
res = fstat( fd, &stat_buf );
if( res < 0 ){
UniError("Failed to get file size.");
}

return stat_buf.st_size;
}

int asynch_send(char *filename, char *rem_path) {
// Check to make sure more than one simultaneous transfer doesn't occur
pthread_mutex_lock(&send_lock);
printf("%d\n", in_progress);
printf("Current upload status: %d\n", in_progress);
if (in_progress) {
fprintf(stderr, "error: attempted to start second transfer\n");
return -1;
}
in_progress = 1;
pthread_mutex_unlock(&send_lock);

local_fn = filename;
remote_path = rem_path;
local_fn = sdsnew(filename);
remote_path = sdsnew(rem_path);

pthread_t id;
// Set thread to detached
Expand Down Expand Up @@ -75,9 +87,13 @@ void *send_worker() {
int still_running=0, msgs_left=0;
double speed_upload, total_time;

printf("Executing upload worker.\n");

// Initialize global curl easy struct
if (init_file_upload() < 0)
if (init_file_upload() < 0) {
printf("Init file upload failed.\n");
return (void*)-1;
}

// Add single handle to multi
curl_multi_add_handle(cm, curl);
Expand Down Expand Up @@ -135,9 +151,11 @@ void *send_worker() {

fclose(curr_fd);
sdsfree(full_remote_path);
sdsfree(local_fn);
pthread_mutex_lock(&send_lock);
in_progress = 0;
cancel_flag = 0;
sdsfree(remote_path);
pthread_mutex_unlock(&send_lock);

printf("thread done\n");
Expand Down Expand Up @@ -303,13 +321,23 @@ int init_file_upload() {
struct stat file_info;
create_full_path();

printf("Local filename is %s\n", local_fn);

curr_fd = fopen(local_fn, "rb"); /* open file to upload */
if(!curr_fd)
return -1; /* can't continue */
if(!curr_fd) {
printf("Failed to open local file\n");
return -1; /* can't continue */
}

/* to get the file size */
if(fstat(fileno(curr_fd), &file_info) != 0)
if(fstat(fileno(curr_fd), &file_info) != 0) {
printf("Failed fstat.\n");
return -1; /* can't continue */
}

printf("Full remote path is: %s\n", full_remote_path);
printf("Size is: %d\n", file_info.st_size);

curl_easy_setopt(curl, CURLOPT_URL, full_remote_path);
/* set where to read from */
curl_easy_setopt(curl, CURLOPT_READDATA, curr_fd);
Expand Down Expand Up @@ -338,6 +366,7 @@ int init_file_upload() {
int create_full_path() {
full_remote_path = sdsnew(remote_base_url);//strcpy(tmp, remote_base_url);
full_remote_path = sdscat(full_remote_path, remote_path);
printf("Full remote: %s\n", full_remote_path);
return 0;
}