Blame gfs2/libgfs2/gfs2l.c

Packit 6ef888
#include <fcntl.h>
Packit 6ef888
#include <unistd.h>
Packit 6ef888
#include <string.h>
Packit 6ef888
#include "libgfs2.h"
Packit 6ef888
Packit 6ef888
static void usage(const char *cmd)
Packit 6ef888
{
Packit 6ef888
	printf("A language for modifying and querying a gfs2 file system.\n");
Packit 6ef888
	printf("Usage: %s [options] <fs_path>\n", cmd);
Packit 6ef888
	printf("Available options:\n");
Packit 6ef888
	printf("  -h                Print this help message and exit\n");
Packit 6ef888
	printf("  -f <script_path>  Path to script file or '-' for stdin (the default)\n");
Packit 6ef888
	printf("  -T                Print a list of gfs2 structure types and exit\n");
Packit 6ef888
	printf("  -F <type>         Print a list of fields belonging to a type and exit\n");
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
struct cmdopts {
Packit 6ef888
	char *fspath;
Packit 6ef888
	FILE *src;
Packit 6ef888
	unsigned help:1;
Packit 6ef888
};
Packit 6ef888
Packit 6ef888
static int metastrcmp(const void *a, const void *b)
Packit 6ef888
{
Packit 6ef888
	const struct lgfs2_metadata *m1 = *(struct lgfs2_metadata **)a;
Packit 6ef888
	const struct lgfs2_metadata *m2 = *(struct lgfs2_metadata **)b;
Packit 6ef888
	return strcmp(m1->name, m2->name);
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
static void print_structs(void)
Packit 6ef888
{
Packit 6ef888
	const struct lgfs2_metadata *mlist[lgfs2_metadata_size];
Packit 6ef888
	int i;
Packit 6ef888
	for (i = 0; i < lgfs2_metadata_size; i++)
Packit 6ef888
		mlist[i] = &lgfs2_metadata[i];
Packit 6ef888
Packit 6ef888
	qsort(mlist, lgfs2_metadata_size, sizeof(struct lgfs2_metadata *), metastrcmp);
Packit 6ef888
	for (i = 0; i < lgfs2_metadata_size; i++)
Packit 6ef888
		if (mlist[i]->mh_type != GFS2_METATYPE_NONE)
Packit 6ef888
			printf("%s\n", mlist[i]->name);
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
static void print_fields(const char *name)
Packit 6ef888
{
Packit 6ef888
	const struct lgfs2_metadata *m = lgfs2_find_mtype_name(name, LGFS2_MD_GFS1|LGFS2_MD_GFS2);
Packit 6ef888
	if (m != NULL) {
Packit 6ef888
		const struct lgfs2_metafield *fields = m->fields;
Packit 6ef888
		const unsigned nfields = m->nfields;
Packit 6ef888
		int i;
Packit 6ef888
		for (i = 0; i < nfields; i++)
Packit 6ef888
			printf("0x%.4x %s\n", fields[i].offset, fields[i].name);
Packit 6ef888
	}
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
static int getopts(int argc, char *argv[], struct cmdopts *opts)
Packit 6ef888
{
Packit 6ef888
	int opt;
Packit 6ef888
	opts->src = stdin;
Packit 6ef888
	while ((opt = getopt(argc, argv, "F:f:hT")) != -1) {
Packit 6ef888
		switch (opt) {
Packit 6ef888
		case 'f':
Packit 6ef888
			if (strcmp("-", optarg)) {
Packit 6ef888
				opts->src = fopen(optarg, "r");
Packit 6ef888
				if (opts->src == NULL) {
Packit 6ef888
					perror("Failed to open source file");
Packit 6ef888
					return 1;
Packit 6ef888
				}
Packit 6ef888
			}
Packit 6ef888
			break;
Packit 6ef888
		case 'T':
Packit 6ef888
			print_structs();
Packit 6ef888
			exit(0);
Packit 6ef888
		case 'F':
Packit 6ef888
			print_fields(optarg);
Packit 6ef888
			exit(0);
Packit 6ef888
		case 'h':
Packit 6ef888
			opts->help = 1;
Packit 6ef888
			return 0;
Packit 6ef888
		default:
Packit 6ef888
			fprintf(stderr, "Use -h for help\n");
Packit 6ef888
			return 1;
Packit 6ef888
		}
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	if (argc - optind != 1) {
Packit 6ef888
		usage(argv[0]);
Packit 6ef888
		fprintf(stderr, "Missing file system path. Use -h for help.\n");
Packit 6ef888
		return 1;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	opts->fspath = strdup(argv[optind]);
Packit 6ef888
	if (opts->fspath == NULL) {
Packit 6ef888
		perror("getopts");
Packit 6ef888
		return 1;
Packit 6ef888
	}
Packit 6ef888
	return 0;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
static int openfs(const char *path, struct gfs2_sbd *sdp)
Packit 6ef888
{
Packit 6ef888
	int fd;
Packit 6ef888
	int ret;
Packit 6ef888
	int sane;
Packit 6ef888
	uint64_t count;
Packit 6ef888
Packit 6ef888
	fd = open(path, O_RDWR);
Packit 6ef888
	if (fd < 0) {
Packit 6ef888
		fprintf(stderr, "Failed to open %s\n", path);
Packit 6ef888
		return 1;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	memset(sdp, 0, sizeof(*sdp));
Packit 6ef888
	sdp->bsize = GFS2_BASIC_BLOCK;
Packit 6ef888
	sdp->device_fd = fd;
Packit 6ef888
	ret = compute_constants(sdp);
Packit 6ef888
	if (ret != 0) {
Packit 6ef888
		perror("Bad constants");
Packit 6ef888
		return 1;
Packit 6ef888
	}
Packit 6ef888
	ret = lgfs2_get_dev_info(fd, &sdp->dinfo);
Packit 6ef888
	if (ret != 0) {
Packit 6ef888
		perror("Failed to gather device info");
Packit 6ef888
		return 1;
Packit 6ef888
	}
Packit 6ef888
	fix_device_geometry(sdp);
Packit 6ef888
Packit 6ef888
	ret = read_sb(sdp);
Packit 6ef888
	if (ret != 0) {
Packit 6ef888
		perror("Could not read sb");
Packit 6ef888
		return 1;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	sdp->master_dir = lgfs2_inode_read(sdp, sdp->sd_sb.sb_master_dir.no_addr);
Packit 6ef888
	gfs2_lookupi(sdp->master_dir, "rindex", 6, &sdp->md.riinode);
Packit 6ef888
	sdp->fssize = sdp->device.length;
Packit 6ef888
	if (sdp->md.riinode) {
Packit 6ef888
		rindex_read(sdp, 0, &count, &sane);
Packit 6ef888
	} else {
Packit 6ef888
		perror("Failed to look up rindex");
Packit 6ef888
		return 1;
Packit 6ef888
	}
Packit 6ef888
	return 0;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
int main(int argc, char *argv[])
Packit 6ef888
{
Packit 6ef888
	int ret;
Packit 6ef888
	struct cmdopts opts = {NULL, NULL};
Packit 6ef888
	struct gfs2_sbd sbd;
Packit 6ef888
	struct lgfs2_lang_result *result;
Packit 6ef888
	struct lgfs2_lang_state *state;
Packit 6ef888
Packit 6ef888
	if (getopts(argc, argv, &opts)) {
Packit 6ef888
		exit(1);
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	if (opts.help) {
Packit 6ef888
		usage(argv[0]);
Packit 6ef888
		exit(0);
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	if (openfs(argv[optind], &sbd))
Packit 6ef888
		exit(1);
Packit 6ef888
Packit 6ef888
	state = lgfs2_lang_init();
Packit 6ef888
	if (state == NULL) {
Packit 6ef888
		perror("lgfs2_lang_init failed");
Packit 6ef888
		exit(1);
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	ret = lgfs2_lang_parsef(state, opts.src);
Packit 6ef888
	if (ret != 0) {
Packit 6ef888
		fprintf(stderr, "Parse failed\n");
Packit 6ef888
		free(opts.fspath);
Packit 6ef888
		return ret;
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	for (result = lgfs2_lang_result_next(state, &sbd;;
Packit 6ef888
	     result != NULL;
Packit 6ef888
	     result = lgfs2_lang_result_next(state, &sbd)) {
Packit 6ef888
		lgfs2_lang_result_print(result);
Packit 6ef888
		lgfs2_lang_result_free(&result);
Packit 6ef888
	}
Packit 6ef888
Packit 6ef888
	gfs2_rgrp_free(&sbd.rgtree);
Packit 6ef888
	inode_put(&sbd.md.riinode);
Packit 6ef888
	inode_put(&sbd.master_dir);
Packit 6ef888
	lgfs2_lang_free(&state);
Packit 6ef888
	free(opts.fspath);
Packit 6ef888
	return 0;
Packit 6ef888
}
Packit 6ef888
Packit 6ef888
// libgfs2 still requires an external print_it function
Packit 6ef888
void print_it(const char *label, const char *fmt, const char *fmt2, ...) { return; }