2014-12-04 10:34:54 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2014 Johannes Schauer <j.schauer@email.de>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License version 2, as published by
|
|
|
|
|
* the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
|
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define _POSIX_C_SOURCE 200809L // for PATH_MAX
|
2014-12-29 23:34:33 -06:00
|
|
|
#define _GNU_SOURCE // for strdup
|
2014-12-04 10:34:54 +01:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <libgen.h>
|
|
|
|
|
|
|
|
|
|
#include "../../mfapi/apicalls.h"
|
2014-12-29 13:57:53 +01:00
|
|
|
#include "../../mfapi/mfconn.h"
|
2014-12-04 10:34:54 +01:00
|
|
|
#include "../mfshell.h"
|
|
|
|
|
#include "../../mfapi/folder.h"
|
|
|
|
|
#include "../commands.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
|
|
int mfshell_cmd_put(mfshell * mfshell, int argc, char *const argv[])
|
|
|
|
|
{
|
|
|
|
|
int retval;
|
|
|
|
|
const char *file_path;
|
|
|
|
|
char *temp;
|
|
|
|
|
char *file_name;
|
|
|
|
|
char *upload_key;
|
2014-12-04 16:07:12 +01:00
|
|
|
FILE *fh;
|
2014-12-04 10:34:54 +01:00
|
|
|
|
|
|
|
|
if (mfshell == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (mfshell->conn == NULL) {
|
|
|
|
|
fprintf(stderr, "conn is NULL\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
|
fprintf(stderr, "Invalid number of arguments\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
file_path = argv[1];
|
|
|
|
|
|
2014-12-04 16:07:12 +01:00
|
|
|
fh = fopen(file_path, "r");
|
|
|
|
|
if (fh == NULL) {
|
|
|
|
|
perror("fopen");
|
|
|
|
|
fprintf(stderr, "cannot open %s\n", file_path);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2014-12-04 10:34:54 +01:00
|
|
|
// create copies because basename modifies it
|
|
|
|
|
temp = strdup(argv[1]);
|
|
|
|
|
file_name = basename(temp);
|
|
|
|
|
|
|
|
|
|
retval = mfconn_api_upload_simple(mfshell->conn,
|
|
|
|
|
folder_get_key(mfshell->folder_curr),
|
2014-12-04 16:07:12 +01:00
|
|
|
fh, file_name, &upload_key);
|
2014-12-04 10:34:54 +01:00
|
|
|
|
2014-12-04 16:07:12 +01:00
|
|
|
fclose(fh);
|
2014-12-04 10:34:54 +01:00
|
|
|
free(temp);
|
|
|
|
|
|
2014-12-04 16:07:12 +01:00
|
|
|
if (retval != 0 || upload_key == NULL) {
|
2014-12-04 10:34:54 +01:00
|
|
|
fprintf(stderr, "mfconn_api_upload_simple failed\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "upload_key: %s\n", upload_key);
|
|
|
|
|
|
2014-12-04 16:07:12 +01:00
|
|
|
// poll for completion
|
2014-12-29 13:57:53 +01:00
|
|
|
retval = mfconn_upload_poll_for_completion(mfshell->conn, upload_key);
|
2014-12-04 10:34:54 +01:00
|
|
|
free(upload_key);
|
|
|
|
|
|
2014-12-29 13:57:53 +01:00
|
|
|
if (retval != 0) {
|
|
|
|
|
fprintf(stderr, "mfconn_upload_poll_for_completion failed\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 10:34:54 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|