208 lines
6.1 KiB
C
208 lines
6.1 KiB
C
/* *****************************************************************************
|
|
* storage/send.c -- send a file to the calculator.
|
|
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libp7.
|
|
* libp7 is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libp7 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 Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libp7; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************** */
|
|
#include <libp7/internals.h>
|
|
|
|
/**
|
|
* p7_send:
|
|
* Sends a file using a buffer.
|
|
*
|
|
* @arg handle the libp7 handle
|
|
* @arg buffer the libp7 buffer.
|
|
* @arg dirname the directory name
|
|
* @arg filename the file name
|
|
* @arg devname the device name
|
|
* @arg overwrite if no confirmation callback, should overwrite or not
|
|
* @arg confirm if file exists and this callback is not NULL,
|
|
* call it. If answer is 1, overwrite, otherwise, not.
|
|
* @arg disp callback display
|
|
* @return if it worked
|
|
*/
|
|
|
|
int p7_send(p7_handle_t *handle, const p7_buffer_t *buffer,
|
|
const char *dirname, const char *filename, const char *devname,
|
|
int overwrite, p7_confirm_t confirm, p7_disp_t disp)
|
|
{
|
|
int err;
|
|
|
|
/* make checks */
|
|
chk_filesize(buffer->size);
|
|
chk_required_filename(filename);
|
|
chk_dirname(dirname);
|
|
chk_bufread(buffer);
|
|
chk_handle(handle);
|
|
chk_active(handle);
|
|
|
|
/* choose overwrite mode
|
|
* If we want to never overwrite, we don't want the calculator to terminate
|
|
* communication, so we ask confirmation and we will systematically
|
|
* return no. */
|
|
p7_commandoverwrite_t owmode;
|
|
if (confirm || !overwrite) owmode = p7_ow_confirm;
|
|
else owmode = p7_ow_force;
|
|
|
|
/* send command packet and check answer */
|
|
log_info("sending file transfer command");
|
|
if ((err = p7_send_cmdfls_sendfile(handle, owmode, buffer->size,
|
|
dirname, filename, devname))) {
|
|
log_fatal("couldn't send file transfer packet/didn't receive answer");
|
|
return (err);
|
|
} else if (response.type != p7_pt_ack && response.type != p7_pt_error) {
|
|
log_fatal("file transfer answer was unknown");
|
|
return (p7_error_unknown);
|
|
}
|
|
|
|
/* - checking what happened if it is an error packet - */
|
|
if (response.type == p7_pt_error) switch (response.code) {
|
|
/* file exists, confirm or not */
|
|
case p7_err_overwrite:
|
|
log_info("calc wanted overwrite confirmation");
|
|
/* check whether to ask */
|
|
if ((confirm || overwrite) && (!confirm || (*confirm)())) {
|
|
/* confirm! */
|
|
log_info("sending yes to overwrite confirmation");
|
|
if ((err = p7_confirm_ow(handle)))
|
|
return (err);
|
|
else if (response.type != p7_pt_ack) {
|
|
log_fatal("response packet wasn't ack");
|
|
return (p7_error_unknown);
|
|
}
|
|
} else {
|
|
/* WE DON'T WANT TO IN THE END */
|
|
log_info("sending no to overwrite confirmation");
|
|
if ((err = p7_deny_ow(handle)))
|
|
return (err);
|
|
else if (response.type != p7_pt_ack) {
|
|
log_fatal("response packet wasn't ack");
|
|
return (p7_error_unknown);
|
|
}
|
|
/* we have denied */
|
|
return (p7_error_denied_overwrite);
|
|
}
|
|
break;
|
|
|
|
/* impossible overwrite */
|
|
case p7_err_dont_overwrite:
|
|
case p7_err_other:
|
|
log_error("overwrite impossible");
|
|
err = p7_error_unsupported_device;
|
|
return (err);
|
|
|
|
/* memory full */
|
|
case p7_err_fullmem:
|
|
log_error("distant memory is full");
|
|
err = p7_error_fullmem;
|
|
return (err);
|
|
|
|
/* stfu warning */
|
|
case p7_err_resend: break;
|
|
}
|
|
|
|
/* send data */
|
|
if ((err = p7_send_buffer(handle, buffer, 1, disp)))
|
|
return (err);
|
|
|
|
/* we're done */
|
|
return (0);
|
|
}
|
|
|
|
#ifndef P7_DISABLED_FILE
|
|
/**
|
|
* p7_sendfile:
|
|
* Sends a file.
|
|
*
|
|
* @arg handle the libp7 handle
|
|
* @arg file the file stream (must be seekable !)
|
|
* @arg dirname the directory name
|
|
* @arg filename the file name
|
|
* @arg devname the device name
|
|
* @arg overwrite if no confirmation required, should overwrite or not
|
|
* @arg confirm if file exists and this callback is not NULL,
|
|
* call it. If answer is 1, overwrite, otherwise, not.
|
|
* @arg disp callback display
|
|
* @return if it worked
|
|
*/
|
|
|
|
int p7_sendfile(p7_handle_t *handle,
|
|
FILE *file, const char *dirname, const char *filename,
|
|
const char *devname, int overwrite, p7_confirm_t confirm, p7_disp_t disp)
|
|
{
|
|
chk_isread(file);
|
|
|
|
/* calculate file size */
|
|
log_info("calculating given file size");
|
|
p7uint_t size;
|
|
if (fseek(file, 0, SEEK_END)) {
|
|
log_fatal("could not seek filestream");
|
|
return (p7_error_noseek);
|
|
}
|
|
size = (p7uint_t)ftell(file);
|
|
rewind(file);
|
|
/* - log - */
|
|
log_info("filesize is %" PRIuP7INT "o", size);
|
|
|
|
/* prepare the buffer */
|
|
p7_buffer_t buffer = {
|
|
.cookie = file,
|
|
.size = size,
|
|
.read = p7_filebuffer_read
|
|
};
|
|
|
|
/* send file */
|
|
return (p7_send(handle, &buffer, dirname, filename, devname,
|
|
overwrite, confirm, disp));
|
|
}
|
|
#endif
|
|
|
|
/**
|
|
* p7_sendmem:
|
|
* Sends a file, using memory.
|
|
*
|
|
* @arg handle the libp7 handle
|
|
* @arg mem the memory buffer
|
|
* @arg size the memory buffer size
|
|
* @arg dirname the directory name
|
|
* @arg filename the file name
|
|
* @arg devname the device name
|
|
* @arg overwrite if no confirmation required, should overwrite or not
|
|
* @arg confirm if file exists and this callback is not NULL,
|
|
* call it. If answer is 1, overwrite, otherwise, not.
|
|
* @arg disp callback display
|
|
* @return if it worked
|
|
*/
|
|
|
|
int p7_sendmem(p7_handle_t *handle, const void *mem, size_t size,
|
|
const char *dirname, const char *filename, const char *devname,
|
|
int overwrite, p7_confirm_t confirm, p7_disp_t disp)
|
|
{
|
|
chk_mem(mem);
|
|
chk_filesize(size);
|
|
|
|
/* make the buffer */
|
|
p7_cursor_t cursor = {.mem = mem};
|
|
p7_buffer_t buffer = {
|
|
.cookie = &cursor,
|
|
.size = size,
|
|
.read = p7_membuffer_read
|
|
};
|
|
|
|
/* real func */
|
|
return (p7_send(handle, &buffer, dirname, filename, devname,
|
|
overwrite, confirm, disp));
|
|
}
|