예제 #1
0
파일: config.c 프로젝트: inteos/WBSAirback
int read_config_file(struct config_tree *cft)
{
	struct cs *c = (struct cs *) cft;
	struct stat info;
	int r = 1;

	if (stat(c->filename, &info)) {
		log_sys_error("stat", c->filename);
		c->exists = 0;
		return 0;
	}

	if (!S_ISREG(info.st_mode)) {
		log_error("%s is not a regular file", c->filename);
		c->exists = 0;
		return 0;
	}

	c->exists = 1;

	if (info.st_size == 0) {
		log_verbose("%s is empty", c->filename);
		return 1;
	}

	if (!c->dev) {
		if (!(c->dev = dev_create_file(c->filename, NULL, NULL, 1)))
			return_0;

		if (!dev_open_readonly_buffered(c->dev)) {
			c->dev = 0;
			return_0;
		}
	}

	r = read_config_fd(cft, c->dev, 0, (size_t) info.st_size, 0, 0,
			   (checksum_fn_t) NULL, 0);

	if (!c->keep_open) {
		dev_close(c->dev);
		c->dev = 0;
	}

	c->timestamp = info.st_ctime;
	c->st_size = info.st_size;

	return r;
}
struct volume_group *text_vg_import_fd(struct format_instance *fid,
				       const char *file,
				       struct device *dev,
				       off_t offset, uint32_t size,
				       off_t offset2, uint32_t size2,
				       checksum_fn_t checksum_fn,
				       uint32_t checksum,
				       time_t *when, char **desc)
{
	struct volume_group *vg = NULL;
	struct config_tree *cft;
	struct text_vg_version_ops **vsn;

	_init_text_import();

	*desc = NULL;
	*when = 0;

	if (!(cft = create_config_tree(file, 0)))
		return_NULL;

	if ((!dev && !read_config_file(cft)) ||
	    (dev && !read_config_fd(cft, dev, offset, size,
				    offset2, size2, checksum_fn, checksum))) {
		log_error("Couldn't read volume group metadata.");
		goto out;
	}

	/*
	 * Find a set of version functions that can read this file
	 */
	for (vsn = &_text_vsn_list[0]; *vsn; vsn++) {
		if (!(*vsn)->check_version(cft))
			continue;

		if (!(vg = (*vsn)->read_vg(fid, cft, 0)))
			goto_out;

		(*vsn)->read_desc(vg->vgmem, cft, when, desc);
		break;
	}

      out:
	destroy_config_tree(cft);
	return vg;
}
const char *text_vgname_import(const struct format_type *fmt,
			       struct device *dev,
			       off_t offset, uint32_t size,
			       off_t offset2, uint32_t size2,
			       checksum_fn_t checksum_fn, uint32_t checksum,
			       struct id *vgid, uint64_t *vgstatus,
			       char **creation_host)
{
	struct config_tree *cft;
	struct text_vg_version_ops **vsn;
	const char *vgname = NULL;

	_init_text_import();

	if (!(cft = create_config_tree(NULL, 0)))
		return_NULL;

	if ((!dev && !read_config_file(cft)) ||
	    (dev && !read_config_fd(cft, dev, offset, size,
				    offset2, size2, checksum_fn, checksum)))
		goto_out;

	/*
	 * Find a set of version functions that can read this file
	 */
	for (vsn = &_text_vsn_list[0]; *vsn; vsn++) {
		if (!(*vsn)->check_version(cft))
			continue;

		if (!(vgname = (*vsn)->read_vgname(fmt, cft, vgid, vgstatus,
						   creation_host)))
			goto_out;

		break;
	}

      out:
	destroy_config_tree(cft);
	return vgname;
}