view rvinterf/etmsync/fileio.c @ 289:244f08f58e51

fc-fsio: error decoding implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 28 Feb 2014 23:12:52 +0000
parents 146e7bf3fa4e
children 69e8ae2b5ba2
line wrap: on
line source

/*
 * FFS2 file descriptor I/O operations
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "etm.h"
#include "ffs.h"
#include "tmffs2.h"
#include "limits.h"
#include "localtypes.h"
#include "localstruct.h"
#include "exitcodes.h"

extern u_char rvi_msg[];
extern int rvi_msg_len;

fd_open(pathname, flags, fdrtn)
	char *pathname;
	int flags, *fdrtn;
{
	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
	int rc, slen;

	slen = strlen(pathname);
	if (slen >= TMFFS_STRING_SIZE) {
		printf("error: pathname arg exceeds string length limit\n");
		return(ERROR_USAGE);
	}
	dp = cmdpkt + 1;
	*dp++ = ETM_FFS2;
	*dp++ = TMFFS_OPEN;
	*dp++ = slen + 1;
	strcpy(dp, pathname);
	dp += slen + 1;
	*dp++ = flags;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("open fd", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 6) {
		printf("error: open fd response has wrong length\n");
		return(ERROR_TARGET);
	}
	*fdrtn = rvi_msg[4];
	return(0);
}

fd_read(fd, databuf, rdsize, rdret)
	u_char *databuf;
	int fd, rdsize, *rdret;
{
	u_char cmdpkt[6];
	int rc, sz;

	if (rdsize > MAX_READ_DATA) {
		printf("error: # of bytes to read may not exceed %d\n",
			MAX_READ_DATA);
		return(ERROR_USAGE);
	}
	cmdpkt[1] = ETM_FFS2;
	cmdpkt[2] = TMFFS_READ;
	cmdpkt[3] = fd;
	cmdpkt[4] = rdsize;
	rc = etm_pkt_exch(cmdpkt, 4);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("read fd", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len < 6) {
		*rdret = 0;
		return(0);
	}
	sz = rvi_msg[4];
	if (rvi_msg_len != sz + 6 || sz > rdsize) {
		printf("error: read fd response has wrong length\n");
		return(ERROR_TARGET);
	}
	bcopy(rvi_msg + 5, databuf, sz);
	*rdret = sz;
	return(0);
}

fd_close(fd)
{
	u_char cmdpkt[5];
	int rc;

	cmdpkt[1] = ETM_FFS2;
	cmdpkt[2] = TMFFS_CLOSE;
	cmdpkt[3] = fd;
	rc = etm_pkt_exch(cmdpkt, 3);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("close fd", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 5) {
		printf("error: close fd response has wrong length\n");
		return(ERROR_TARGET);
	}
	return(0);
}

do_file_read(pathname, databuf, rdsize, rdret)
	char *pathname;
	u_char *databuf;
	int rdsize, *rdret;
{
	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
	int rc, slen, sz;

	slen = strlen(pathname);
	if (slen >= TMFFS_STRING_SIZE) {
		printf("error: pathname arg exceeds string length limit\n");
		return(ERROR_USAGE);
	}
	if (rdsize > MAX_READ_DATA) {
		printf("error: # of bytes to read may not exceed %d\n",
			MAX_READ_DATA);
		return(ERROR_USAGE);
	}
	dp = cmdpkt + 1;
	*dp++ = ETM_FFS2;
	*dp++ = TMFFS_FILE_READ;
	*dp++ = slen + 1;
	strcpy(dp, pathname);
	dp += slen + 1;
	*dp++ = rdsize;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("read file", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len < 6) {
		*rdret = 0;
		return(0);
	}
	sz = rvi_msg[4];
	if (rvi_msg_len != sz + 6 || sz > rdsize) {
		printf("error: read file response has wrong length\n");
		return(ERROR_TARGET);
	}
	bcopy(rvi_msg + 5, databuf, sz);
	*rdret = sz;
	return(0);
}