2013-03-20 05:27:32 +00:00
|
|
|
#include <unistd.h>
|
2017-06-08 23:15:38 +00:00
|
|
|
#include "ioall.h"
|
2013-08-14 19:25:30 +00:00
|
|
|
#include "libqubes-rpc-filecopy.h"
|
2013-03-20 05:27:32 +00:00
|
|
|
#include "crc32.h"
|
|
|
|
|
2014-02-07 04:27:29 +00:00
|
|
|
notify_progress_t *notify_progress_func = NULL;
|
|
|
|
void register_notify_progress(notify_progress_t *func)
|
|
|
|
{
|
2016-06-02 00:32:46 +00:00
|
|
|
notify_progress_func = func;
|
2014-02-07 04:27:29 +00:00
|
|
|
}
|
2013-03-20 05:27:32 +00:00
|
|
|
|
|
|
|
int copy_file(int outfd, int infd, long long size, unsigned long *crc32)
|
|
|
|
{
|
2016-06-02 00:32:46 +00:00
|
|
|
char buf[4096];
|
|
|
|
long long written = 0;
|
|
|
|
int ret;
|
|
|
|
int count;
|
|
|
|
while (written < size) {
|
|
|
|
if (size - written > (int)sizeof(buf))
|
|
|
|
count = sizeof buf;
|
|
|
|
else
|
|
|
|
count = size - written;
|
|
|
|
ret = read(infd, buf, count);
|
|
|
|
if (!ret)
|
|
|
|
return COPY_FILE_READ_EOF;
|
|
|
|
if (ret < 0)
|
|
|
|
return COPY_FILE_READ_ERROR;
|
|
|
|
/* acumulate crc32 if requested */
|
|
|
|
if (crc32)
|
|
|
|
*crc32 = Crc32_ComputeBuf(*crc32, buf, ret);
|
|
|
|
if (!write_all(outfd, buf, ret))
|
|
|
|
return COPY_FILE_WRITE_ERROR;
|
|
|
|
if (notify_progress_func != NULL)
|
|
|
|
notify_progress_func(ret, 0);
|
|
|
|
written += ret;
|
|
|
|
}
|
|
|
|
return COPY_FILE_OK;
|
2013-03-20 05:27:32 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 12:03:33 +00:00
|
|
|
const char * copy_file_status_to_str(int status)
|
2013-03-20 05:27:32 +00:00
|
|
|
{
|
2016-06-02 00:32:46 +00:00
|
|
|
switch (status) {
|
|
|
|
case COPY_FILE_OK: return "OK";
|
|
|
|
case COPY_FILE_READ_EOF: return "Unexpected end of data while reading";
|
|
|
|
case COPY_FILE_READ_ERROR: return "Error reading";
|
|
|
|
case COPY_FILE_WRITE_ERROR: return "Error writing";
|
|
|
|
default: return "????????";
|
|
|
|
}
|
2013-03-20 05:27:32 +00:00
|
|
|
}
|