cake
/
libp7
Archived
1
0
Fork 1
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libp7/src/storage/send.c

208 lines
6.1 KiB
C
Raw Normal View History

/* *****************************************************************************
* 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>
/**
2017-02-05 20:12:25 +01:00
* p7_send:
* Sends a file using a buffer.
*
2016-09-25 22:01:42 +02:00
* @arg handle the libp7 handle
2017-02-05 20:12:25 +01:00
* @arg buffer the libp7 buffer.
* @arg dirname the directory name
* @arg filename the file name
* @arg devname the device name
2016-09-05 01:43:26 +02:00
* @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.
2016-09-06 00:38:33 +02:00
* @arg disp callback display
* @return if it worked
*/
2017-02-05 20:12:25 +01:00
int p7_send(p7_handle_t *handle, const p7_buffer_t *buffer,
2016-10-05 22:31:01 +02:00
const char *dirname, const char *filename, const char *devname,
int overwrite, p7_confirm_t confirm, p7_disp_t disp)
{
2016-09-25 22:01:42 +02:00
int err;
2016-08-24 16:08:47 +02:00
2016-10-09 16:26:04 +02:00
/* make checks */
2017-02-05 20:12:25 +01:00
chk_filesize(buffer->size);
2017-01-27 10:17:29 +01:00
chk_required_filename(filename);
2016-10-09 16:26:04 +02:00
chk_dirname(dirname);
2017-02-05 20:12:25 +01:00
chk_bufread(buffer);
2016-10-09 16:26:04 +02:00
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
2016-10-09 16:26:04 +02:00
* return no. */
p7_commandoverwrite_t owmode;
if (confirm || !overwrite) owmode = p7_ow_confirm;
2017-01-04 13:38:20 +01:00
else owmode = p7_ow_force;
2016-10-09 16:26:04 +02:00
/* send command packet and check answer */
2016-08-24 16:08:47 +02:00
log_info("sending file transfer command");
2017-02-05 20:12:25 +01:00
if ((err = p7_send_cmdfls_sendfile(handle, owmode, buffer->size,
2016-09-25 22:01:42 +02:00
dirname, filename, devname))) {
2016-08-24 16:08:47 +02:00
log_fatal("couldn't send file transfer packet/didn't receive answer");
2016-10-09 16:26:04 +02:00
return (err);
} else if (response.type != p7_pt_ack && response.type != p7_pt_error) {
2016-08-24 16:08:47 +02:00
log_fatal("file transfer answer was unknown");
2016-10-09 16:26:04 +02:00
return (p7_error_unknown);
}
2016-08-24 16:08:47 +02:00
/* - checking what happened if it is an error packet - */
2017-01-04 13:38:20 +01:00
if (response.type == p7_pt_error) switch (response.code) {
/* file exists, confirm or not */
2017-01-04 13:38:20 +01:00
case p7_err_overwrite:
log_info("calc wanted overwrite confirmation");
/* check whether to ask */
if ((confirm || overwrite) && (!confirm || (*confirm)())) {
2016-10-09 16:26:04 +02:00
/* confirm! */
log_info("sending yes to overwrite confirmation");
2017-01-04 13:38:20 +01:00
if ((err = p7_confirm_ow(handle)))
2016-10-09 16:26:04 +02:00
return (err);
else if (response.type != p7_pt_ack) {
log_fatal("response packet wasn't ack");
return (p7_error_unknown);
}
} else {
2016-10-09 16:26:04 +02:00
/* WE DON'T WANT TO IN THE END */
log_info("sending no to overwrite confirmation");
2017-01-04 13:38:20 +01:00
if ((err = p7_deny_ow(handle)))
2016-10-09 16:26:04 +02:00
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 */
2017-01-04 13:38:20 +01:00
case p7_err_dont_overwrite:
case p7_err_other:
2016-08-20 19:47:31 +02:00
log_error("overwrite impossible");
err = p7_error_unsupported_device;
2016-10-09 16:26:04 +02:00
return (err);
/* memory full */
2017-01-04 13:38:20 +01:00
case p7_err_fullmem:
2016-08-20 19:47:31 +02:00
log_error("distant memory is full");
2016-09-25 22:01:42 +02:00
err = p7_error_fullmem;
2016-10-09 16:26:04 +02:00
return (err);
2016-09-25 22:01:42 +02:00
/* stfu warning */
2017-01-04 13:38:20 +01:00
case p7_err_resend: break;
}
2016-10-09 16:26:04 +02:00
/* send data */
2017-02-05 20:12:25 +01:00
if ((err = p7_send_buffer(handle, buffer, 1, disp)))
2016-10-09 16:26:04 +02:00
return (err);
2016-09-25 22:01:42 +02:00
/* we're done */
return (0);
}
2016-08-21 15:32:52 +02:00
#ifndef P7_DISABLED_FILE
2016-08-21 15:32:52 +02:00
/**
* p7_sendfile:
* Sends a file.
*
2016-09-25 22:01:42 +02:00
* @arg handle the libp7 handle
2016-08-21 15:32:52 +02:00
* @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,
2016-08-21 15:32:52 +02:00
* call it. If answer is 1, overwrite, otherwise, not.
2016-09-06 00:38:33 +02:00
* @arg disp callback display
2016-08-21 15:32:52 +02:00
* @return if it worked
*/
2016-09-25 22:01:42 +02:00
int p7_sendfile(p7_handle_t *handle,
2016-10-05 22:31:01 +02:00
FILE *file, const char *dirname, const char *filename,
const char *devname, int overwrite, p7_confirm_t confirm, p7_disp_t disp)
2016-08-21 15:32:52 +02:00
{
2017-02-05 20:12:25 +01:00
chk_isread(file);
2016-10-08 22:30:35 +02:00
2016-08-21 15:32:52 +02:00
/* calculate file size */
log_info("calculating given file size");
2016-10-08 22:30:35 +02:00
p7uint_t size;
2016-08-21 15:32:52 +02:00
if (fseek(file, 0, SEEK_END)) {
2016-10-08 22:30:35 +02:00
log_fatal("could not seek filestream");
2016-09-25 22:01:42 +02:00
return (p7_error_noseek);
2016-08-21 15:32:52 +02:00
}
2016-10-08 22:30:35 +02:00
size = (p7uint_t)ftell(file);
2016-08-21 15:32:52 +02:00
rewind(file);
/* - log - */
2017-01-05 22:58:23 +01:00
log_info("filesize is %" PRIuP7INT "o", size);
2016-08-21 15:32:52 +02:00
2017-02-05 20:12:25 +01:00
/* prepare the buffer */
p7_buffer_t buffer = {
.cookie = file,
.size = size,
.read = p7_filebuffer_read
};
2016-08-21 15:32:52 +02:00
/* send file */
2017-02-05 20:12:25 +01:00
return (p7_send(handle, &buffer, dirname, filename, devname,
overwrite, confirm, disp));
2016-08-21 15:32:52 +02:00
}
#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));
}