a12867d5d24830e0b9b2050aff957a42eff3cbf0
[paraslash.git] / file_write.c
1 /*
2  * Copyright (C) 2006-2013 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
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "write.h"
19 #include "write_common.h"
20 #include "string.h"
21 #include "fd.h"
22 #include "file_write.cmdline.h"
23 #include "error.h"
24
25 /** Data specific to the file writer. */
26 struct private_file_write_data {
27         /** The file descriptor of the output file. */
28         int fd;
29 };
30
31 /*
32  * Get a random filename.
33  *
34  * This is by no means a secure way to create temporary files in a hostile
35  * directory like \p /tmp. However, we use it only for creating temp files in
36  * ~/.paraslash, for which it is OK. Result must be freed by the caller.
37  */
38 __must_check __malloc static char *random_filename(void)
39 {
40         char *result, *home = para_homedir();
41         struct timeval tv;
42
43         gettimeofday(&tv, NULL);
44         srandom(tv.tv_usec);
45         result = make_message("%s/.paraslash/%08lu", home,
46                 para_random(99999999));
47         free(home);
48         return result;
49 }
50
51 static int prepare_output_file(struct writer_node *wn)
52 {
53         struct file_write_args_info *conf = wn->conf;
54         char *filename;
55         int ret;
56         struct private_file_write_data *pfwd = para_calloc(sizeof(*pfwd));
57
58         if (conf->filename_given)
59                 filename = conf->filename_arg;
60         else
61                 filename = random_filename();
62         ret = para_open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
63         if (!conf->filename_given)
64                 free(filename);
65         if (ret < 0)
66                 goto out;
67         pfwd->fd = ret;
68         ret = mark_fd_blocking(pfwd->fd);
69         if (ret < 0)
70                 goto out_close;
71         wn->private_data = pfwd;
72         return 1;
73 out_close:
74         close(pfwd->fd);
75 out:
76         free(pfwd);
77         return ret;
78 }
79
80 static void file_write_pre_select(struct sched *s, struct task *t)
81 {
82         struct writer_node *wn = container_of(t, struct writer_node, task);
83         struct private_file_write_data *pfwd = wn->private_data;
84         int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
85
86         if (ret == 0)
87                 return;
88         if (ret < 0 || !pfwd)
89                 return sched_min_delay(s);
90         para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
91 }
92
93 static void file_write_close(struct writer_node *wn)
94 {
95         struct private_file_write_data *pfwd = wn->private_data;
96
97         if (!pfwd)
98                 return;
99         close(pfwd->fd);
100         free(pfwd);
101 }
102
103 static void file_write_post_select(__a_unused struct sched *s,
104                 struct task *t)
105 {
106         struct writer_node *wn = container_of(t, struct writer_node, task);
107         struct private_file_write_data *pfwd = wn->private_data;
108         struct btr_node *btrn = wn->btrn;
109         int ret;
110         char *buf;
111         size_t bytes;
112
113         ret = task_get_notification(t);
114         if (ret < 0)
115                 goto out;
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(&wn->btrn);
135         t->error = ret;
136 }
137
138 __malloc static void *file_write_parse_config_or_die(int argc, char **argv)
139 {
140         struct file_write_args_info *conf = para_calloc(sizeof(*conf));
141
142         /* exits on errors */
143         file_write_cmdline_parser(argc, argv, conf);
144         return conf;
145 }
146
147 static void file_write_free_config(void *conf)
148 {
149         file_write_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_write_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->help = (struct ggo_help) {
164                 .short_help = file_write_args_info_help,
165                 .detailed_help = file_write_args_info_detailed_help
166         };
167         file_write_cmdline_parser_free(&dummy);
168 }