Added support for handling binary files.
The problem was I was using XDR strings for reading data, storing it
and transferring to the client from the server. But since this is no
better than null-terminated string, whenever data had \000 the data
used to become corrupt from that point. So now I changed all data to
be integer arrays so there is no question of corruption.
#include <rpc/rpc.h>
#include <stdio.h>
#include "ftp.h"
extern __thread int errno;
readfile_res* retrieve_file_1_svc(request *req, struct svc_req *rqstp)
{
FILE *file;
filechunk data;
static readfile_res res;
file = fopen(req->name, "rb");
if (file == NULL) {
res.errno = errno;
return (&res);
}
xdr_free((xdrproc_t)xdr_readfile_res, (char *)&res);
fseek (file, req->start, SEEK_SET);
res.readfile_res_u.chunk.items = fread(res.readfile_res_u.chunk.data, sizeof(int), MAXLEN, file);
/*
* Return the result
*/
res.errno = 0;
fclose(file);
return (&res);
}
int* send_file_1_svc(chunksend *rec, struct svc_req *rqstp)
{
FILE *file;
int write_items;
static int result;
file = fopen(rec->name, "ab");
if (file == NULL) {
result = errno;
return &result;
}
write_items = fwrite(rec->data, sizeof(int), rec->items, file);
fclose(file);
result = 0;
return &result;
}