예제 #1
0
파일: err.c 프로젝트: gdhh/ffs
int main (int argc, char * argv[]) {
	ERRNO(EINVAL);
	FOO_UNEXPECTED("cannot frob the ka-knob");

	goto error;

	if (false) {
		err_t * err = NULL;
error:
		while ((err = err_get()) != NULL) {
			switch (err_type(err)) {
			case ERR_VERSION:
				fprintf(stderr, "%s: %s : %s(%d) : v%d.%02d.%04d %.*s\n",
					basename((char *)argv[0]),
					err_type_name(err), basename(err_file(err)), err_line(err),
					VER_TO_MAJOR(err_code(err)), VER_TO_MINOR(err_code(err)),
					VER_TO_PATCH(err_code(err)),
					err_size(err), (char *)err_data(err));
				break;
			default:
				fprintf(stderr, "%s: %s : %s(%d) : (code=%d) %.*s\n",
					basename((char *)argv[0]),
					err_type_name(err), basename(err_file(err)), err_line(err),
					err_code(err), err_size(err), (char *)err_data(err));
			}
		}
	}

	return 0;
}
예제 #2
0
파일: vector.c 프로젝트: eddiejames/ffs
static void vector_4(void) {
    vector_t v;

    if (vector_init(&v, NULL, 0) < 0) {
        err_t * err = err_get();
        fprintf(stderr, "%s(%d): UNEXPECTED: %.*s\n",
                err_file(err), err_line(err), err_size(err),
		(const char *)err_data(err));
    }

    if (vector_init(&v, "my_vector", 0) < 0) {
	err_t * err = err_get();
        fprintf(stderr, "%s(%d): UNEXPECTED: %.*s\n",
                err_file(err), err_line(err), err_size(err),
		(const char *)err_data(err));
    }

    for (size_t i=VECTOR_ELEM_MIN; i<=VECTOR_ELEM_MAX; i+=3) {
        vector_init(&v, "my_vector", i);

        CU_ASSERT(vector_size(&v) == 0);
        CU_ASSERT(vector_pages(&v) == 0);
        CU_ASSERT(vector_capacity(&v) == 0);
        CU_ASSERT(vector_elem_size(&v) == i);
        CU_ASSERT(vector_elem_size(&v) * v.hdr.elem_num <= v.hdr.page_size);

        vector_size(&v, COUNT);

        CU_ASSERT(vector_size(&v) == COUNT);
        CU_ASSERT(vector_size(&v) <= vector_capacity(&v));
        CU_ASSERT(0 < vector_pages(&v));

        vector_delete(&v);
    }
}
예제 #3
0
파일: misc.c 프로젝트: whs1787/ffs
int dump_errors(const char * name, FILE * out)
{
	assert(name != NULL);

	if (out == NULL)
		out = stderr;

	err_t * err = NULL;

	while ((err = err_get()) != NULL) {
		switch (err_type(err)) {
		case ERR_VERSION:
			fprintf(out, "%s: %s : %s(%d) : v%d.%02d.%04d %.*s\n",
				basename((char *)name), err_type_name(err),
				basename(err_file(err)), err_line(err),
				VER_TO_MAJOR(err_code(err)),
				VER_TO_MINOR(err_code(err)),
				VER_TO_PATCH(err_code(err)),
				err_size(err), (char *)err_data(err));
			break;
		default:
			fprintf(out, "%s: %s : %s(%d) : (code=%d) %.*s\n",
				basename((char *)name), err_type_name(err),
				basename(err_file(err)), err_line(err),
				err_code(err), err_size(err),
				(char *)err_data(err));
		}
	}

	return 0;
}
예제 #4
0
파일: libffs2.c 프로젝트: gdhh/ffs
ffs_t *ffs_open(const char *path, off_t offset)
{
	FILE * file = fopen(path, "r+");
	if (file == NULL) {
		__error.errnum = -1;
		snprintf(__error.errstr, sizeof __error.errstr,
			 "%s: %s : %s(%d) : %s (errno=%d)\n",
			 program_invocation_short_name, "errno",
			 __FILE__, __LINE__, strerror(errno), errno);

		return NULL;
	}

	ffs_t *self = __ffs_fopen(file, offset);
	if (self == NULL) {
		fclose(file);

		err_t *err = err_get();
		assert(err != NULL);

		__error.errnum = err_code(err);
		snprintf(__error.errstr, sizeof __error.errstr,
			 "%s: %s : %s(%d) : (code=%d) %.*s\n",
			 program_invocation_short_name,
			 err_type_name(err), err_file(err), err_line(err),
			 err_code(err), err_size(err), (char *)err_data(err));
	}

	return self;
}
예제 #5
0
파일: libffs2.c 프로젝트: gdhh/ffs
int ffs_check(const char *path, off_t offset)
{
	RAII(FILE*, file, fopen(path, "r"), fclose);
	if (file == NULL) {
		__error.errnum = -1;
		snprintf(__error.errstr, sizeof __error.errstr,
			 "%s: %s : %s(%d) : %s (errno=%d)\n",
			 program_invocation_short_name, "errno",
			 __FILE__, __LINE__, strerror(errno), errno);

		return -1;
	}

	int rc = __ffs_fcheck(file, offset);
	if (rc < 0) {
		err_t *err = err_get();
		assert(err != NULL);

		__error.errnum = err_code(err);
		snprintf(__error.errstr, sizeof __error.errstr,
			 "%s: %s : %s(%d) : (code=%d) %.*s\n",
			 program_invocation_short_name,
			 err_type_name(err), err_file(err), err_line(err),
			 err_code(err), err_size(err), (char *)err_data(err));

		// __ffs_check will return FFS_CHECK_* const's
	}

	return rc;
}
예제 #6
0
파일: libffs2.c 프로젝트: gdhh/ffs
ffs_t *ffs_create(const char *path, off_t offset, uint32_t block_size,
		  uint32_t block_count)
{
	ffs_t *self = __ffs_create(path, offset, block_size, block_count);
	if (self == NULL) {
		err_t *err = err_get();
		assert(err != NULL);

		__error.errnum = err_code(err);
		snprintf(__error.errstr, sizeof __error.errstr,
			 "%s: %s : %s(%d) : (code=%d) %.*s\n",
			 program_invocation_short_name,
			 err_type_name(err), err_file(err), err_line(err),
			 err_code(err), err_size(err), (char *)err_data(err));
	}

	return self;
}
예제 #7
0
파일: libffs2.c 프로젝트: gdhh/ffs
ssize_t ffs_entry_list(ffs_t * self, ffs_entry_t ** list)
{
	ssize_t rc = __ffs_entry_list(self, list);
	if (rc < 0) {
		err_t *err = err_get();
		assert(err != NULL);

		__error.errnum = err_code(err);
		snprintf(__error.errstr, sizeof __error.errstr,
			 "%s: %s : %s(%d) : (code=%d) %.*s\n",
			 program_invocation_short_name,
			 err_type_name(err), err_file(err), err_line(err),
			 err_code(err), err_size(err), (char *)err_data(err));

		rc = -1;
	}

	return rc;
}
예제 #8
0
파일: libffs2.c 프로젝트: gdhh/ffs
int ffs_info(ffs_t *self, int name, uint32_t *value)
{
	int rc = __ffs_info(self, name, value);
	if (rc < 0) {
		err_t *err = err_get();
		assert(err != NULL);

		__error.errnum = err_code(err);
		snprintf(__error.errstr, sizeof __error.errstr,
			 "%s: %s : %s(%d) : (code=%d) %.*s\n",
			 program_invocation_short_name,
			 err_type_name(err), err_file(err), err_line(err),
			 err_code(err), err_size(err), (char *)err_data(err));

		rc = -1;
	}

	return rc;
}
예제 #9
0
파일: libffs2.c 프로젝트: gdhh/ffs
ssize_t ffs_entry_write(ffs_t * self, const char *path, const void *buf,
			off_t offset, size_t count)
{
	ssize_t rc = __ffs_entry_write(self, path, buf, offset, count);
	if (rc < 0) {
		err_t *err = err_get();
		assert(err != NULL);

		__error.errnum = err_code(err);
		snprintf(__error.errstr, sizeof __error.errstr,
			 "%s: %s : %s(%d) : (code=%d) %.*s\n",
			 program_invocation_short_name,
			 err_type_name(err), err_file(err), err_line(err),
			 err_code(err), err_size(err), (char *)err_data(err));

		rc = -1;
	}

	return rc;
}
예제 #10
0
파일: libffs2.c 프로젝트: gdhh/ffs
int ffs_entry_add(ffs_t * self, const char *path, off_t offset, size_t size,
		  ffs_type_t type, uint32_t flags)
{
	int rc = __ffs_entry_add(self, path, offset, size, type, flags);
	if (rc < 0) {
		err_t *err = err_get();
		assert(err != NULL);

		__error.errnum = err_code(err);
		snprintf(__error.errstr, sizeof __error.errstr,
			 "%s: %s : %s(%d) : (code=%d) %.*s\n",
			 program_invocation_short_name,
			 err_type_name(err), err_file(err), err_line(err),
			 err_code(err), err_size(err), (char *)err_data(err));

		rc = -1;
	}

	return rc;
}
예제 #11
0
파일: vector.c 프로젝트: eddiejames/ffs
static void vector_1(void) {
    vector_t v;

    CU_ASSERT(vector_init(&v, "my_vector", 0) == -1)
    err_t * err = err_get();
    fprintf(stderr, "%s(%d): %.*s\n",
            err_file(err), err_line(err), err_size(err),
            (const char *)err_data(err));

    for (size_t i=VECTOR_ELEM_MIN; i<=VECTOR_ELEM_MAX; i++) {
        CU_ASSERT(vector_init(&v, "my_vector", i) == 0);

        CU_ASSERT(vector_size(&v) == 0);
        CU_ASSERT(vector_pages(&v) == 0);
        CU_ASSERT(vector_capacity(&v) == 0);
        CU_ASSERT(vector_elem_size(&v) == i);
        CU_ASSERT(vector_elem_size(&v) * v.hdr.elem_num <= v.hdr.page_size);

        CU_ASSERT(vector_delete(&v) == 0);
    }
}
예제 #12
0
파일: main.c 프로젝트: whs1787/ffs
int main(int argc, char *argv[])
{
	static const struct option long_opts[] = {
		/* commands */
		{"inject", required_argument, NULL, c_INJECT},
		{"remove", required_argument, NULL, c_REMOVE},
		{"hexdump", required_argument, NULL, c_HEXDUMP},
		/* options */
		{"output", required_argument, NULL, o_OUTPUT},
		/* flags */
		{"force", no_argument, NULL, f_FORCE},
		{"p8", no_argument, NULL, f_P8},
		{"verbose", no_argument, NULL, f_VERBOSE},
		{"help", no_argument, NULL, f_HELP},
		{0, 0, 0, 0}
	};

	static const char *short_opts = "I:R:H:o:fpvh";

	int rc = EXIT_FAILURE;

	if (argc == 1)
		usage(args.short_name, false), exit(rc);

	int opt = 0, idx = 0;
	while ((opt = getopt_long(argc, argv, short_opts, long_opts,
				  &idx)) != -1)
		if (process_argument(&args, opt, optarg) < 0)
			goto error;

	/* getopt_long doesn't know what to do with orphans, */
	/* so we'll scoop them up here, and deal with them later */

	while (optind < argc)
		if (process_option(&args, argv[optind++]) < 0)
			goto error;

	if (args.verbose == f_VERBOSE)
		args_dump(&args);

	if (validate_args(&args) < 0)
		goto error;
	if (process_args(&args) < 0)
		goto error;

	rc = EXIT_SUCCESS;

	if (false) {
		err_t *err;
error:
		err = err_get();
		assert(err != NULL);

		fprintf(stderr, "%s: %s : %s(%d) : (code=%d) %.*s\n",
			program_invocation_short_name,
			err_type_name(err), err_file(err), err_line(err),
			err_code(err), err_size(err), (char *)err_data(err));
	}

	return rc;
}