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/usage/mcs/send.c

121 lines
3.9 KiB
C

/* *****************************************************************************
* usage/mcs/send.c -- send a file to the main memory.
* 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_mcs_send:
* Send a file.
*
* @arg handle the libp7 handle.
* @arg file the file to send.
* @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 the progression display callback.
* @return the libp7 error (0 if ok).
*/
p7_define_ufunc(p7_mcs_send, p7_attrs_mcs_send,
p7_handle_t *handle, g1m_mcsfile_t *file,
int overwrite, p7_confirm_t *confirm, p7_disp_t *disp)
{
int err;
/* make checks */
chk_handle(handle);
chk_seven(handle);
chk_active(handle);
chk_head(&file->head);
/* choose overwrite mode */
p7_seven_ow_t owmode;
if (confirm || !overwrite) owmode = p7_seven_ow_confirm;
else owmode = p7_seven_ow_force;
/* send command */
log_info("sending command");
if ((err = p7_send_cmdmcs_sendfile(handle, owmode, file))) {
log_fatal("couldn't send file transfer pakcet/didn't receive answer");
return (err);
} else if (response.p7_seven_packet_type != p7_seven_type_ack
&& response.p7_seven_packet_type != p7_seven_type_error) {
log_fatal("file transfer answer was unknown");
return (p7_error_unknown);
}
/* check what happened if it is an error packet */
if (response.p7_seven_packet_type == p7_seven_type_error)
switch (response.p7_seven_packet_code) {
/* file exists, confirm or not */
case p7_seven_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_seven_confirm_ow(handle)))
return (err);
else if (response.p7_seven_packet_type != p7_seven_type_ack) {
log_fatal("response packet wasn't ack");
return (p7_error_unknown);
}
} else {
/* nope, don't want to */
log_info("sending no to overwrite confirmation");
if ((err = p7_seven_deny_ow(handle)))
return (err);
else if (response.p7_seven_packet_type != p7_seven_type_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_seven_err_dont_overwrite:
case p7_seven_err_other:
log_error("overwrite impossible");
err = p7_error_unsupported_device;
return (err);
/* memory full */
case p7_seven_err_fullmem:
log_error("distant memory is full");
return (p7_error_fullmem);
}
/* send the file content */
p7_seven_data_cookie_t bcookie;
g1m_buffer_t buffer = {
.cookie = &bcookie,
.announce = (g1m_buffer_announce_t)&p7_seven_data_prepare,
.write = (g1m_buffer_write_t)&p7_seven_data_write
};
int libg1m_err = g1m_encode_mcsfile(file, &buffer);
if (libg1m_err == g1m_error_nowrite)
return (bcookie.p7err);
else return (p7_error_unknown); /* FIXME */
/* no error! */
return (0);
}