alsa: Remove pointless initialization.
[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
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/%08lu", 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, struct task *t)
78 {
79 struct writer_node *wn = container_of(t, struct writer_node, task);
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,
101 struct task *t)
102 {
103 struct writer_node *wn = container_of(t, struct writer_node, task);
104 struct private_file_write_data *pfwd = wn->private_data;
105 struct btr_node *btrn = wn->btrn;
106 int ret;
107 char *buf;
108 size_t bytes;
109
110 ret = task_get_notification(t);
111 if (ret < 0)
112 goto out;
113 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
114 if (ret <= 0)
115 goto out;
116 if (!pfwd) {
117 ret = prepare_output_file(wn);
118 goto out;
119 }
120 if (!FD_ISSET(pfwd->fd, &s->wfds))
121 return 0;
122 bytes = btr_next_buffer(btrn, &buf);
123 assert(bytes > 0);
124 //PARA_INFO_LOG("writing %zu\n", bytes);
125 ret = xwrite(pfwd->fd, buf, bytes);
126 if (ret < 0)
127 goto out;
128 btr_consume(btrn, ret);
129 out:
130 if (ret < 0)
131 btr_remove_node(&wn->btrn);
132 return ret;
133 }
134
135 __malloc static void *file_write_parse_config_or_die(int argc, char **argv)
136 {
137 struct file_write_args_info *conf = para_calloc(sizeof(*conf));
138
139 /* exits on errors */
140 file_write_cmdline_parser(argc, argv, conf);
141 return conf;
142 }
143
144 static void file_write_free_config(void *conf)
145 {
146 file_write_cmdline_parser_free(conf);
147 }
148
149 /** the init function of the file writer */
150 void file_write_init(struct writer *w)
151 {
152 struct file_write_args_info dummy;
153
154 file_write_cmdline_parser_init(&dummy);
155 w->pre_select = file_write_pre_select;
156 w->post_select = file_write_post_select;
157 w->parse_config_or_die = file_write_parse_config_or_die;
158 w->free_config = file_write_free_config;
159 w->close = file_write_close;
160 w->help = (struct ggo_help)DEFINE_GGO_HELP(file_write);
161 file_write_cmdline_parser_free(&dummy);
162 }