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