#include <ftw.h>
#include <wait.h>
#include <assert.h>
+#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
.utimes = process_utimes,
};
-static int do_receive(struct btrfs_receive *r, const char *tomnt, int r_fd)
+static int do_receive(struct btrfs_receive *r, const char *tomnt, int r_fd,
+ u64 max_errors)
{
int ret;
char *dest_dir_full_path;
while (!end) {
ret = btrfs_read_and_process_send_stream(r_fd, &send_ops, r,
- r->honor_end_cmd);
+ r->honor_end_cmd,
+ max_errors);
if (ret < 0)
goto out;
if (ret)
return ret;
}
+static const struct option long_opts[] = {
+ { "max-errors", 1, NULL, 'E' },
+ { NULL, 0, NULL, 0 }
+};
+
int cmd_receive(int argc, char **argv)
{
int c;
char *fromfile = NULL;
struct btrfs_receive r;
int receive_fd = fileno(stdin);
-
+ u64 max_errors = 1;
int ret;
memset(&r, 0, sizeof(r));
r.write_fd = -1;
r.dest_dir_fd = -1;
- while ((c = getopt(argc, argv, "evf:")) != -1) {
+ while ((c = getopt_long(argc, argv, "evf:", long_opts, NULL)) != -1) {
switch (c) {
case 'v':
g_verbose++;
case 'e':
r.honor_end_cmd = 1;
break;
+ case 'E':
+ max_errors = arg_strtou64(optarg);
+ break;
case '?':
default:
fprintf(stderr, "ERROR: receive args invalid.\n");
}
}
- ret = do_receive(&r, tomnt, receive_fd);
+ ret = do_receive(&r, tomnt, receive_fd, max_errors);
return !!ret;
}
" in the data stream. Without this option,",
" the receiver terminates only if an error",
" is recognized or on EOF.",
+ "--max-errors <N> Terminate as soon as N errors happened while",
+ " processing commands from the send stream.",
+ " Default value is 1. A value of 0 means no limit.",
NULL
};
return ret;
}
+/*
+ * 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)
+ 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;
while (1) {
ret = read_and_process_cmd(&s);
- if (ret < 0)
- goto out;
- if (ret) {
+ 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;
}