static int file_write_open(struct writer_node *wn)
{
- struct private_file_write_data *pfwd = para_calloc(
- sizeof(struct private_file_write_data));
+ struct private_file_write_data *pfwd = para_calloc(sizeof(*pfwd));
+
+ wn->private_data = pfwd;
+ pfwd->fd = -1;
+ return 0;
+}
+
+static int prepare_output_file(struct writer_node *wn)
+{
struct file_write_args_info *conf = wn->conf;
+ struct private_file_write_data *pfwd = wn->private_data;
char *filename;
int ret;
filename = conf->filename_arg;
else
filename = random_filename();
- wn->private_data = pfwd;
ret = para_open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (!conf->filename_given)
free(filename);
t->error = 0;
ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
- if (ret > 0)
+ if (ret > 0 && pfwd->fd >= 0)
para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
- else if (ret < 0)
+ else if (ret != 0) /* error or bos and fd not yet open */
sched_min_delay(s);
}
{
struct private_file_write_data *pfwd = wn->private_data;
- close(pfwd->fd);
+ if (pfwd->fd >= 0)
+ close(pfwd->fd);
free(pfwd);
}
ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
if (ret <= 0)
goto out;
+ if (pfwd->fd < 0) {
+ ret = prepare_output_file(wn);
+ if (ret < 0)
+ goto out;
+ }
if (!FD_ISSET(pfwd->fd, &s->wfds))
return;
bytes = btr_next_buffer(btrn, &buf);