changeset 293:ffeea2f9d149

fc-fsio: fwrite from file implemented, but no 0 length file handling yet
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 01 Mar 2014 03:53:49 +0000
parents 3aa03b9519c0
children 797468042b32
files rvinterf/etmsync/fileio.c rvinterf/etmsync/fswrite.c
diffstat 2 files changed, 71 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/rvinterf/etmsync/fileio.c	Sat Mar 01 03:09:10 2014 +0000
+++ b/rvinterf/etmsync/fileio.c	Sat Mar 01 03:53:49 2014 +0000
@@ -89,6 +89,41 @@
 	return(0);
 }
 
+fd_write(fd, data, wrsize)
+	u_char *data;
+{
+	u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
+	int rc;
+
+	if (wrsize > MAX_PKT_TO_TARGET - 6) {
+		printf("error: fd write data fails to fit in the packet\n");
+		return(ERROR_USAGE);
+	}
+	dp = cmdpkt + 1;
+	*dp++ = ETM_FFS2;
+	*dp++ = TMFFS_WRITE;
+	*dp++ = fd;
+	*dp++ = wrsize;
+	bcopy(data, dp, wrsize);
+	dp += wrsize;
+	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
+	if (rc)
+		return(rc);
+	if (rvi_msg[3]) {
+		report_ffs_err("fd write", rvi_msg[3]);
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg_len != 6) {
+		printf("error: TMFFS_WRITE response has wrong length\n");
+		return(ERROR_TARGET);
+	}
+	if (rvi_msg[4] != wrsize) {
+		printf("fd write error: # of bytes written != requested\n");
+		return(ERROR_TARGET);
+	}
+	return(0);
+}
+
 fd_close(fd)
 {
 	u_char cmdpkt[5];
--- a/rvinterf/etmsync/fswrite.c	Sat Mar 01 03:09:10 2014 +0000
+++ b/rvinterf/etmsync/fswrite.c	Sat Mar 01 03:53:49 2014 +0000
@@ -140,8 +140,42 @@
 fwrite_from_file(pathname, srcfile)
 	char *pathname, *srcfile;
 {
-	fprintf(stderr, "This function is not yet implemented\n");
-	return(ERROR_BUG);
+	u_char buf[240];
+	FILE *srcf;
+	int rc, cc, first, tfd;
+
+	srcf = fopen(srcfile, "r");
+	if (!srcf) {
+		perror(srcfile);
+		return(ERROR_UNIX);
+	}
+	for (first = 1; cc = fread(buf, 1, sizeof buf, srcf); first = 0) {
+		if (first) {
+			if (cc < sizeof buf &&
+			    cc <= max_short_file_write(pathname)) {
+				fclose(srcf);
+				return do_short_fwrite(pathname, buf, cc);
+			}
+			rc = fd_open(pathname,
+				     FFS_O_WRONLY | FFS_O_CREATE | FFS_O_TRUNC,
+				     &tfd);
+			if (rc) {
+				fclose(srcf);
+				return(rc);
+			}
+		}
+		rc = fd_write(tfd, buf, cc);
+		if (rc) {
+			fclose(srcf);
+			fd_close(tfd);
+			return(rc);
+		}
+	}
+	fclose(srcf);
+	if (first)
+		return(0);
+	else
+		return fd_close(tfd);
 }
 
 cmd_fwrite(argc, argv)