Example #1
0
/*
 * If max_errors is 0, then don't stop processing the stream if one of the
 * callbacks in btrfs_send_ops structure returns an error. If greater than
 * zero, stop after max_errors errors happened.
 */
int btrfs_read_and_process_send_stream(int fd,
				       struct btrfs_send_ops *ops, void *user,
				       int honor_end_cmd,
				       u64 max_errors)
{
	int ret;
	struct btrfs_send_stream sctx;
	struct btrfs_stream_header hdr;
	u64 errors = 0;
	int last_err = 0;

	sctx.fd = fd;
	sctx.ops = ops;
	sctx.user = user;
	sctx.stream_pos = 0;

	ret = read_buf(&sctx, (char*)&hdr, sizeof(hdr));
	if (ret < 0)
		goto out;
	if (ret) {
		ret = 1;
		goto out;
	}

	if (strcmp(hdr.magic, BTRFS_SEND_STREAM_MAGIC)) {
		ret = -EINVAL;
		error("unexpected header");
		goto out;
	}

	sctx.version = le32_to_cpu(hdr.version);
	if (sctx.version > BTRFS_SEND_STREAM_VERSION) {
		ret = -EINVAL;
		error("stream version %d not supported, please use newer version",
				sctx.version);
		goto out;
	}

	while (1) {
		ret = read_and_process_cmd(&sctx);
		if (ret < 0) {
			last_err = ret;
			errors++;
			if (max_errors > 0 && errors >= max_errors)
				goto out;
		} else if (ret > 0) {
			if (!honor_end_cmd)
				ret = 0;
			goto out;
		}
	}

out:
	if (last_err && !ret)
		ret = last_err;

	return ret;
}
Example #2
0
/*
 * If max_errors is 0, then don't stop processing the stream if one of the
 * callbacks in btrfs_send_ops structure returns an error. If greater than
 * zero, stop after max_errors errors happened.
 */
int btrfs_read_and_process_send_stream(int fd,
				       struct btrfs_send_ops *ops, void *user,
				       int honor_end_cmd,
				       u64 max_errors)
{
	int ret;
	struct btrfs_send_stream s;
	struct btrfs_stream_header hdr;
	u64 errors = 0;
	int last_err = 0;

	s.fd = fd;
	s.ops = ops;
	s.user = user;

	ret = read_buf(&s, &hdr, sizeof(hdr));
	if (ret < 0)
		goto out;
	if (ret) {
		ret = 1;
		goto out;
	}

	if (strcmp(hdr.magic, BTRFS_SEND_STREAM_MAGIC)) {
		ret = -EINVAL;
		fprintf(stderr, "ERROR: Unexpected header\n");
		goto out;
	}

	s.version = le32_to_cpu(hdr.version);
	if (s.version > BTRFS_SEND_STREAM_VERSION) {
		ret = -EINVAL;
		fprintf(stderr, "ERROR: Stream version %d not supported. "
				"Please upgrade btrfs-progs\n", s.version);
		goto out;
	}

	while (1) {
		ret = read_and_process_cmd(&s);
		if (ret < 0) {
			last_err = ret;
			errors++;
			if (max_errors > 0 && errors >= max_errors)
				goto out;
		} else if (ret > 0) {
			if (!honor_end_cmd)
				ret = 0;
			goto out;
		}
	}

out:
	if (last_err && !ret)
		ret = last_err;

	return ret;
}
int btrfs_read_and_process_send_stream(int fd,
                                       struct btrfs_send_ops *ops, void *user)
{
    int ret;
    struct btrfs_send_stream s;
    struct btrfs_stream_header hdr;

    s.fd = fd;
    s.ops = ops;
    s.user = user;

    ret = read_buf(&s, &hdr, sizeof(hdr));
    if (ret < 0)
        goto out;
    if (ret) {
        ret = 1;
        goto out;
    }

    if (strcmp(hdr.magic, BTRFS_SEND_STREAM_MAGIC)) {
        ret = -EINVAL;
        fprintf(stderr, "ERROR: Unexpected header\n");
        goto out;
    }

    s.version = le32_to_cpu(hdr.version);
    if (s.version > BTRFS_SEND_STREAM_VERSION) {
        ret = -EINVAL;
        fprintf(stderr, "ERROR: Stream version %d not supported. "
                "Please upgrade btrfs-progs\n", s.version);
        goto out;
    }

    while (1) {
        ret = read_and_process_cmd(&s);
        if (ret < 0)
            goto out;
        if (ret) {
            ret = 0;
            goto out;
        }
    }

out:
    return ret;
}