core-admin/appvm/copy_file.c

41 lines
914 B
C
Raw Normal View History

2011-03-15 16:07:00 +01:00
#include <unistd.h>
#include <ioall.h>
#include "filecopy.h"
2011-03-15 16:07:00 +01:00
extern void notify_progress(int, int);
int copy_file(int outfd, int infd, long long size)
2011-03-15 16:07:00 +01:00
{
char buf[4096];
long long written = 0;
int ret;
int count;
while (written < size) {
if (size - written > sizeof(buf))
count = sizeof buf;
else
count = size - written;
ret = read(infd, buf, count);
if (!ret)
return COPY_FILE_READ_EOF;
2011-03-15 16:07:00 +01:00
if (ret < 0)
return COPY_FILE_READ_ERROR;
2011-03-15 16:07:00 +01:00
if (!write_all(outfd, buf, ret))
return COPY_FILE_WRITE_ERROR;
2011-03-15 16:07:00 +01:00
notify_progress(ret, 0);
written += ret;
}
return COPY_FILE_OK;
2011-03-15 16:07:00 +01:00
}
char * copy_file_status_to_str(int status)
{
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 "????????";
}
}