aacdec: detect buffer overrun and return an errror.
[paraslash.git] / file_writer.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file file_writer.c simple output plugin for testing purposes */
20
21 #include "para.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "write.h"
25 #include "string.h"
26 #include "fd.h"
27 #include "file_write.cmdline.h"
28 #include "error.h"
29
30 /** data specific to the file writer */
31 struct private_file_writer_data {
32 /** the file descriptor of the output file */
33 int fd;
34 /** non-zero if \a fd was added to the write fd set */
35 int check_fd;
36 };
37
38 static int file_writer_open(struct writer_node *wn)
39 {
40 struct private_file_writer_data *pfwd = para_calloc(
41 sizeof(struct private_file_writer_data));
42 struct file_write_args_info *conf = wn->conf;
43 char *filename;
44 if (conf->filename_given)
45 filename = conf->filename_arg;
46 else {
47 char *tmp = para_tmpname(), *home = para_homedir();
48 filename = make_message("%s/.paraslash/%s", home, tmp);
49 free(home);
50 free(tmp);
51 }
52 wn->private_data = pfwd;
53 pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
54 if (!conf->filename_given)
55 free(filename);
56 if (pfwd->fd >= 0)
57 return 8192;
58 free(pfwd);
59 return -E_FW_OPEN;
60 }
61
62 static int file_writer_write(char *data, size_t nbytes, struct writer_node *wn)
63 {
64 struct private_file_writer_data *pfwd = wn->private_data;
65 int ret = write(pfwd->fd, data, nbytes);
66 if (ret < 0)
67 ret = -E_FW_WRITE;
68 return ret;
69 }
70
71 static void file_writer_pre_select(struct sched *s, struct task *t)
72 {
73 struct writer_node *wn = t->private_data;
74 struct private_file_writer_data *pfwd = wn->private_data;
75 struct writer_node_group *wng = wn->wng;
76
77 // PARA_INFO_LOG("task %p check_fd: %d\n", t, pfwd->check_fd);
78 pfwd->check_fd = 0;
79 t->ret = -E_FW_NO_FILE;
80 if (pfwd->fd <= 0)
81 return;
82 t->ret = 0;
83 if (!*wng->loaded)
84 return;
85 t->ret = 1;
86 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
87 pfwd->check_fd = 1;
88 }
89
90 static void file_writer_post_select(struct sched *s, struct task *t)
91 {
92 struct writer_node *wn = t->private_data;
93 struct private_file_writer_data *pfwd = wn->private_data;
94 struct writer_node_group *wng = wn->wng;
95
96 t->ret = 0;
97 if (!pfwd->check_fd)
98 return;
99 if (!*wng->loaded)
100 return;
101 if (!FD_ISSET(pfwd->fd, &s->wfds))
102 return;
103 // PARA_INFO_LOG("writing %zd\n", *wng->loaded);
104 t->ret = write(pfwd->fd, wng->buf, *wng->loaded);
105 if (t->ret < 0)
106 t->ret = -E_FW_WRITE;
107 }
108
109 static void file_writer_close(struct writer_node *wn)
110 {
111 struct private_file_writer_data *pfwd = wn->private_data;
112 close(pfwd->fd);
113 free(pfwd);
114 }
115
116 __malloc void *file_writer_parse_config(char *options)
117 {
118 PARA_INFO_LOG("options: %s\n", options);
119 struct file_write_args_info *conf
120 = para_calloc(sizeof(struct file_write_args_info));
121 int ret = file_cmdline_parser_string(options, conf, "file_write");
122 PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
123 if (!ret)
124 return conf;
125 free(conf);
126 return NULL;
127 }
128
129 /** the init function of the file writer */
130 void file_writer_init(struct writer *w)
131 {
132 w->open = file_writer_open;
133 w->write = file_writer_write;
134 w->pre_select = file_writer_pre_select;
135 w->post_select = file_writer_post_select;
136 w->parse_config = file_writer_parse_config;
137 w->close = file_writer_close;
138 w->shutdown = NULL; /* nothing to do */
139 }