aac_afh.c: Fix serious memory leak.
[paraslash.git] / file_write.c
1 /*
2 * Copyright (C) 2006-2007 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 "para.h"
10 #include "list.h"
11 #include "sched.h"
12 #include "write.h"
13 #include "string.h"
14 #include "fd.h"
15 #include "file_write.cmdline.h"
16 #include "error.h"
17
18 /** data specific to the file writer */
19 struct private_file_write_data {
20 /** the file descriptor of the output file */
21 int fd;
22 /** non-zero if \a fd was added to the write fd set */
23 int check_fd;
24 };
25
26 static int file_write_open(struct writer_node *wn)
27 {
28 struct private_file_write_data *pfwd = para_calloc(
29 sizeof(struct private_file_write_data));
30 struct file_write_args_info *conf = wn->conf;
31 char *filename;
32 if (conf->filename_given)
33 filename = conf->filename_arg;
34 else {
35 char *tmp = para_tmpname(), *home = para_homedir();
36 filename = make_message("%s/.paraslash/%s", home, tmp);
37 free(home);
38 free(tmp);
39 }
40 wn->private_data = pfwd;
41 pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
42 if (!conf->filename_given)
43 free(filename);
44 if (pfwd->fd >= 0)
45 return 8192;
46 free(pfwd);
47 return -E_FW_OPEN;
48 }
49
50 static int file_write_pre_select(struct sched *s, struct writer_node *wn)
51 {
52 struct private_file_write_data *pfwd = wn->private_data;
53 struct writer_node_group *wng = wn->wng;
54
55 pfwd->check_fd = 0;
56 if (pfwd->fd <= 0)
57 return -E_FW_NO_FILE;
58 if (!*wng->loaded)
59 return 1;
60 para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
61 pfwd->check_fd = 1;
62 return 1;
63 }
64
65 static int file_write_post_select(struct sched *s, struct writer_node *wn)
66 {
67 struct private_file_write_data *pfwd = wn->private_data;
68 struct writer_node_group *wng = wn->wng;
69 int ret;
70
71 if (!pfwd->check_fd)
72 return 1;
73 if (*wng->loaded <= wn->written)
74 return 1;
75 if (!FD_ISSET(pfwd->fd, &s->wfds))
76 return 1;
77 // PARA_INFO_LOG("writing %zd\n", *wng->loaded);
78 ret = write(pfwd->fd, wng->buf + wn->written,
79 *wng->loaded - wn->written);
80 if (ret < 0)
81 return -E_FW_WRITE;
82 wn->written += ret;
83 return 1;
84 }
85
86 static void file_write_close(struct writer_node *wn)
87 {
88 struct private_file_write_data *pfwd = wn->private_data;
89 close(pfwd->fd);
90 free(pfwd);
91 }
92
93 __malloc static void *file_write_parse_config(const char *options)
94 {
95 PARA_INFO_LOG("options: %s\n", options);
96 struct file_write_args_info *conf
97 = para_calloc(sizeof(struct file_write_args_info));
98 int ret = file_cmdline_parser_string(options, conf, "file_write");
99 PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
100 if (!ret)
101 return conf;
102 free(conf);
103 return NULL;
104 }
105
106 /** the init function of the file writer */
107 void file_write_init(struct writer *w)
108 {
109 w->open = file_write_open;
110 w->pre_select = file_write_pre_select;
111 w->post_select = file_write_post_select;
112 w->parse_config = file_write_parse_config;
113 w->close = file_write_close;
114 w->shutdown = NULL; /* nothing to do */
115 }