para_write: Return proper error code.
[paraslash.git] / file_write.c
1 /*
2 * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file file_write.c simple output plugin for testing purposes */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <sys/time.h>
13 #include <stdbool.h>
14
15 #include "para.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "write_common.h"
22 #include "string.h"
23 #include "fd.h"
24 #include "file_write.cmdline.h"
25 #include "error.h"
26
27 /** Data specific to the file writer. */
28 struct private_file_write_data {
29 /** The file descriptor of the output file. */
30 int fd;
31 };
32
33 /*
34 * Get a random filename.
35 *
36 * This is by no means a secure way to create temporary files in a hostile
37 * directory like \p /tmp. However, we use it only for creating temp files in
38 * ~/.paraslash, for which it is OK. Result must be freed by the caller.
39 */
40 __must_check __malloc static char *random_filename(void)
41 {
42 char *result, *home = para_homedir();
43 struct timeval tv;
44
45 gettimeofday(&tv, NULL);
46 srandom(tv.tv_usec);
47 result = make_message("%s/.paraslash/%08lu", home,
48 para_random(99999999));
49 free(home);
50 return result;
51 }
52
53 static int prepare_output_file(struct writer_node *wn)
54 {
55 struct file_write_args_info *conf = wn->conf;
56 char *filename;
57 int ret;
58 struct private_file_write_data *pfwd = para_calloc(sizeof(*pfwd));
59
60 if (conf->filename_given)
61 filename = conf->filename_arg;
62 else
63 filename = random_filename();
64 ret = para_open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
65 if (!conf->filename_given)
66 free(filename);
67 if (ret < 0)
68 goto out;
69 pfwd->fd = ret;
70 ret = mark_fd_blocking(pfwd->fd);
71 if (ret < 0)
72 goto out_close;
73 wn->private_data = pfwd;
74 return 1;
75 out_close:
76 close(pfwd->fd);
77 out:
78 free(pfwd);
79 return ret;
80 }
81
82 static void file_write_pre_select(struct sched *s, struct task *t)
83 {
84 struct writer_node *wn = container_of(t, struct writer_node, task);
85 struct private_file_write_data *pfwd = wn->private_data;
86 int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
87
88 if (ret == 0)
89 return;
90 if (ret < 0 || !pfwd)
91 return sched_min_delay(s);
92 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
93 }
94
95 static void file_write_close(struct writer_node *wn)
96 {
97 struct private_file_write_data *pfwd = wn->private_data;
98
99 if (!pfwd)
100 return;
101 close(pfwd->fd);
102 free(pfwd);
103 }
104
105 static void file_write_post_select(__a_unused struct sched *s,
106 struct task *t)
107 {
108 struct writer_node *wn = container_of(t, struct writer_node, task);
109 struct private_file_write_data *pfwd = wn->private_data;
110 struct btr_node *btrn = wn->btrn;
111 int ret;
112 char *buf;
113 size_t bytes;
114
115 t->error = 0;
116 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
117 if (ret <= 0)
118 goto out;
119 if (!pfwd) {
120 ret = prepare_output_file(wn);
121 goto out;
122 }
123 if (!FD_ISSET(pfwd->fd, &s->wfds))
124 return;
125 bytes = btr_next_buffer(btrn, &buf);
126 assert(bytes > 0);
127 //PARA_INFO_LOG("writing %zu\n", bytes);
128 ret = write(pfwd->fd, buf, bytes);
129 if (ret < 0)
130 goto out;
131 btr_consume(btrn, ret);
132 out:
133 if (ret < 0)
134 btr_remove_node(btrn);
135 t->error = ret;
136 }
137
138 __malloc static void *file_write_parse_config_or_die(const char *options)
139 {
140 struct file_write_args_info *conf = para_calloc(sizeof(*conf));
141
142 /* exits on errors */
143 file_cmdline_parser_string(options, conf, "file_write");
144 return conf;
145 }
146
147 static void file_write_free_config(void *conf)
148 {
149 file_cmdline_parser_free(conf);
150 }
151
152 /** the init function of the file writer */
153 void file_write_init(struct writer *w)
154 {
155 struct file_write_args_info dummy;
156
157 file_cmdline_parser_init(&dummy);
158 w->pre_select = file_write_pre_select;
159 w->post_select = file_write_post_select;
160 w->parse_config_or_die = file_write_parse_config_or_die;
161 w->free_config = file_write_free_config;
162 w->close = file_write_close;
163 w->shutdown = NULL; /* nothing to do */
164 w->help = (struct ggo_help) {
165 .short_help = file_write_args_info_help,
166 .detailed_help = file_write_args_info_detailed_help
167 };
168 file_cmdline_parser_free(&dummy);
169 }