164 lines
5.1 KiB
C
164 lines
5.1 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* protocol/sendfile.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/10/13 07:33:17 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libp7/internals.h>
|
|
|
|
/**
|
|
* p7_sendstream:
|
|
* Sends a stream.
|
|
*
|
|
* Some I/O subtility at the end, as we shouldn't count on local I/O rapidity.
|
|
*
|
|
* @arg handle the libp7 handle
|
|
* @arg file the file stream
|
|
* @arg size the file size
|
|
* @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 ow_conf 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_sendstream(p7_handle_t *handle,
|
|
FILE *file, p7uint_t size,
|
|
const char *dirname, const char *filename, const char *devname,
|
|
int overwrite, int (*ow_conf)(void),
|
|
void (*disp)(p7ushort_t, p7ushort_t))
|
|
{
|
|
int err;
|
|
|
|
/* make checks */
|
|
chk_filesize(size);
|
|
chk_filename(filename);
|
|
chk_dirname(dirname);
|
|
chk_isread(file);
|
|
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 (ow_conf || !overwrite)
|
|
owmode = p7_cow_request_confirmation_before_overwriting;
|
|
else owmode = p7_cow_force_overwrite;
|
|
|
|
/* send command packet and check answer */
|
|
log_info("sending file transfer command");
|
|
if ((err = p7_send_cmdfls_sendfile(handle, owmode, 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.error.code) {
|
|
/* file exists, confirm or not */
|
|
case p7_ec_overwrite:
|
|
log_info("calc wanted overwrite confirmation");
|
|
/* check whether to ask */
|
|
if ((ow_conf || overwrite) && (!ow_conf || (*ow_conf)())) {
|
|
/* confirm! */
|
|
log_info("sending yes to overwrite confirmation");
|
|
if ((err = p7_send_ack_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_send_err_overwrite_no(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_ec_overwrite_noresponse:
|
|
case p7_ec_overwrite_impossible:
|
|
log_error("overwrite impossible");
|
|
err = p7_error_unsupported_device;
|
|
return (err);
|
|
|
|
/* memory full */
|
|
case p7_ec_fullmem:
|
|
log_error("distant memory is full");
|
|
err = p7_error_fullmem;
|
|
return (err);
|
|
|
|
/* stfu warning */
|
|
case p7_ec_resend: break;
|
|
}
|
|
|
|
/* send data */
|
|
if ((err = p7_send_filestream(handle, file, size, 1, disp)))
|
|
return (err);
|
|
|
|
/* we're done */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* 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 ow_conf 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, int (*ow_conf)(void),
|
|
void (*disp)(p7ushort_t, p7ushort_t))
|
|
{
|
|
/* check if filestream is seekable */
|
|
if (!file) {
|
|
log_fatal("provided filestream was unseekable!");
|
|
return (p7_error_noseek);
|
|
}
|
|
|
|
/* 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 %luo", size);
|
|
|
|
/* send file */
|
|
return (p7_sendstream(handle, file, size, dirname, filename, devname,
|
|
overwrite, ow_conf, disp));
|
|
}
|