view mpffs/ls.c @ 279:a7707c174510

pirelli/{rssi,txcal}: results of the Pirelli to CMU200 experiment
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 16 Feb 2019 18:30:11 +0000
parents 9bc7f0e03fa8
children
line wrap: on
line source

/*
 * This module contains the main function and other code specific to mpffs-ls
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include "types.h"
#include "struct.h"

extern char *imgfile;
extern struct objinfo root;
extern int verbose;

char workpath[512];

void
extra_chunk_callback(obj, opaque)
	struct objinfo *obj;
	u_long opaque;
{
	size_t *accump = (size_t *) opaque;
	struct chunkinfo chi;

	size_extra_chunk(obj, &chi);
	*accump += chi.len;
}

size_t
get_file_size(head)
	struct objinfo *head;
{
	struct chunkinfo chi;
	size_t accum;

	size_head_chunk(head, &chi);
	accum = chi.len;
	iterate_extra_chunks(head->descend, extra_chunk_callback,
				(u_long) &accum);
	return(accum);
}

dump_dir(firstent, path_prefix)
{
	int ent;
	struct objinfo obj;
	u_long size;

	for (ent = firstent; ent != 0xFFFF; ent = obj.sibling) {
		obj.entryno = ent;
		get_index_entry(&obj);
		if (!obj.type) /* skip deleted objects w/o further validation */
			continue;
		validate_chunk(&obj);
		validate_obj_name(&obj);
		if (path_prefix + strlen(obj.dataptr) + 2 > sizeof workpath) {
			fprintf(stderr,
	"handling object at index %x, name \"%s\": path buffer overflow\n",
				obj.entryno, (char *)obj.dataptr);
			exit(1);
		}
		sprintf(workpath + path_prefix, "/%s", (char *)obj.dataptr);
		switch (obj.type) {
		case 0xF2:
			/* directory */
			printf("d         %s\n", workpath);
			dump_dir(obj.descend, strlen(workpath));
			continue;
		case 0xF1:
			/* regular file */
			size = get_file_size(&obj);
			printf("- %7lu %s\n", size, workpath);
			continue;
		case 0xE1:
			/* special .journal file */
			printf("j %7lu %s\n", (u_long) obj.len, workpath);
			continue;
		default:
		printf("%s (index entry #%x): unexpected type %02X; skipping\n",
				workpath, obj.entryno, obj.type);
			continue;
		}
	}
}

usage()
{
	fprintf(stderr, "usage: mpffs-ls [options] ffs-image\n");
	exit(1);
}

main(argc, argv)
	char **argv;
{
	extern int optind;

	parse_cmdline_options(argc, argv);
	if (argc - optind != 1)
		usage();
	imgfile = argv[optind];
	preliminaries();
	dump_dir(root.descend, 0);
	exit(0);
}