FreeCalypso > hg > freecalypso-sw
view rvinterf/etmsync/fileio.c @ 285:bb28ba9e82c5
fc-fsio: file read primitive and hd command implemented
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Fri, 28 Feb 2014 06:16:02 +0000 |
parents | 7b4d4e3e610a |
children | 146e7bf3fa4e |
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]) { printf("open fd: FFS error %d\n", 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) { fprintf(stderr, "BUG: attempt to read more than possible per TMFFS2 protocol\n"); exit(ERROR_BUG); } 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]) { printf("read fd: FFS error %d\n", 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]) { printf("close fd: FFS error %d\n", 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) { fprintf(stderr, "BUG: attempt to read more than possible per TMFFS2 protocol\n"); exit(ERROR_BUG); } 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]) { printf("read file: FFS error %d\n", 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); }