view rvinterf/etmsync/fsbasics.c @ 525:98c6be4d3d8d

CONFIG_GSM vs CONFIG_INCLUDE_L1: a few changes in preparation
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 14 Jul 2014 18:42:38 +0000
parents 3dd74b16df82
children d1333db6385f
line wrap: on
line source

/*
 * Basic FFS2 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 "ffslimits.h"
#include "localtypes.h"
#include "localstruct.h"
#include "exitcodes.h"

extern u_char rvi_msg[];
extern int rvi_msg_len;

cmd_ffs2ver()
{
	u_char cmdpkt[4];
	int rc;

	cmdpkt[1] = ETM_FFS2;
	cmdpkt[2] = TMFFS_VERSION;
	rc = etm_pkt_exch(cmdpkt, 2);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		printf("FFS2 error %d\n", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 7) {
		printf("error: FFS2 version response has wrong length\n");
		return(ERROR_TARGET);
	}
	printf("FFS2 version: %02X.%02X\n", rvi_msg[5], rvi_msg[4]);
	return(0);
}

do_opendir(pathname, statertn, countrtn)
	char *pathname;
	u_char *statertn;
	int *countrtn;
{
	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_OPENDIR;
	*dp++ = slen + 1;
	strcpy(dp, pathname);
	dp += slen + 1;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("opendir", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 11 || rvi_msg[5] != 4) {
		printf("error: opendir response has wrong length\n");
		return(ERROR_TARGET);
	}
	*countrtn = rvi_msg[4];
	bcopy(rvi_msg + 6, statertn, 4);
	return(0);
}

do_readdir(state, namebuf, namebuflen)
	u_char *state;
	char *namebuf;
{
	u_char cmdpkt[10];
	int rc, slen;

	cmdpkt[1] = ETM_FFS2;
	cmdpkt[2] = TMFFS_READDIR;
	cmdpkt[3] = 4;
	bcopy(state, cmdpkt+4, 4);
	cmdpkt[8] = TMFFS_STRING_SIZE;
	rc = etm_pkt_exch(cmdpkt, 8);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("readdir", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len < 14) {
malformed:	printf("error: readdir response is malformed\n");
		return(ERROR_TARGET);
	}
	if (rvi_msg[5] != 4)
		goto malformed;
	slen = rvi_msg[10];
	if (slen < 2 || rvi_msg_len != slen + 12)
		goto malformed;
	if (slen > namebuflen) {
		printf("error: readdir response exceeds provided buffer\n");
		return(ERROR_TARGET);
	}
	if (rvi_msg[11 + slen - 1])	/* must be terminating NUL */
		goto malformed;
	bcopy(rvi_msg + 6, state, 4);
	strcpy(namebuf, rvi_msg + 11);
	return(0);
}

cmd_ls(argc, argv)
	char **argv;
{
	u_char state[4];
	char namebuf[256];
	int nument, i, rc;

	rc = do_opendir(argv[1], state, &nument);
	if (rc)
		return(rc);
	if (!nument) {
		printf("<empty dir>\n");
		return(0);
	}
	for (i = 0; i < nument; i++) {
		rc = do_readdir(state, namebuf, sizeof namebuf);
		if (rc)
			return(rc);
		printf("%s\n", namebuf);
	}
	return(0);
}

do_xlstat(pathname, result)
	char *pathname;
	struct stat_info *result;
{
	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_XLSTAT;
	*dp++ = slen + 1;
	strcpy(dp, pathname);
	dp += slen + 1;
	rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
	if (rc)
		return(rc);
	if (rvi_msg[3]) {
		report_ffs_err("xlstat", rvi_msg[3]);
		return(ERROR_TARGET);
	}
	if (rvi_msg_len != 30 || rvi_msg[4] != 24) {
		printf("error: xlstat response has wrong length\n");
		return(ERROR_TARGET);
	}
	result->type = rvi_msg[5];
	result->flags = rvi_msg[6];
	result->inode = rvi_msg[7] | rvi_msg[8] << 8;
	result->size = rvi_msg[9] | rvi_msg[10] << 8 | rvi_msg[11] << 16 |
			rvi_msg[12] << 24;
	result->space = rvi_msg[13] | rvi_msg[14] << 8 | rvi_msg[15] << 16 |
			rvi_msg[16] << 24;
	result->location = rvi_msg[17] | rvi_msg[18] << 8 | rvi_msg[19] << 16 |
				rvi_msg[20] << 24;
	result->block = rvi_msg[22];
	result->sequence = rvi_msg[23] | rvi_msg[24] << 8;
	result->updates = rvi_msg[25] | rvi_msg[26] << 8;
	return(0);
}

cmd_stat(argc, argv)
	char **argv;
{
	struct stat_info stat;
	int rc;
	char *type;

	rc = do_xlstat(argv[1], &stat);
	if (rc)
		return(rc);
	switch (stat.type) {
	case OT_FILE:
		type = "file";
		break;
	case OT_DIR:
		type = "directory";
		break;
	case OT_LINK:
		type = "symlink";
		break;
	default:
		type = "???";
	}
	printf("Type: %s%s\n", type,
		stat.flags & OF_READONLY ? ", read-only" : "");
	printf("inode %x\n", stat.inode);
	printf("size %u, space %u\n", stat.size, stat.space);
	printf("location=%x, block %d\n", stat.location, stat.block);
	return(0);
}