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