Consolidate decoder code by introducing prepare_filter_node().
[paraslash.git] / file_write.c
1 /*
2  * Copyright (C) 2006-2009 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 <dirent.h>
12 #include <sys/time.h>
13 #include <stdbool.h>
14
15 #include "para.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "string.h"
22 #include "fd.h"
23 #include "file_write.cmdline.h"
24 #include "error.h"
25
26 /** data specific to the file writer */
27 struct private_file_write_data {
28         /** the file descriptor of the output file */
29         int fd;
30         /** non-zero if \a fd was added to the write fd set */
31         int check_fd;
32 };
33
34 /*
35  * Get a random filename.
36  *
37  * This is by no means a secure way to create temporary files in a hostile
38  * directory like \p /tmp. However, we use it only for creating temp files in
39  * ~/.paraslash, for which it is OK. Result must be freed by the caller.
40  */
41 __must_check __malloc static char *random_filename(void)
42 {
43         char *result, *home = para_homedir();
44         struct timeval tv;
45
46         gettimeofday(&tv, NULL);
47         srandom(tv.tv_usec);
48         result = make_message("%s/.paraslash/%08lu", home,
49                 para_random(99999999));
50         free(home);
51         return result;
52 }
53
54 static int file_write_open(struct writer_node *wn)
55 {
56         struct private_file_write_data *pfwd = para_calloc(
57                 sizeof(struct private_file_write_data));
58         struct file_write_args_info *conf = wn->conf;
59         char *filename;
60
61         if (conf->filename_given)
62                 filename = conf->filename_arg;
63         else
64                 filename = random_filename();
65         wn->private_data = pfwd;
66         pfwd->fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
67         if (!conf->filename_given)
68                 free(filename);
69         if (pfwd->fd >= 0)
70                 return 1;
71         free(pfwd);
72         return -E_FW_OPEN;
73 }
74
75 static int file_write_pre_select(struct sched *s, struct writer_node *wn)
76 {
77         struct private_file_write_data *pfwd = wn->private_data;
78         struct writer_node_group *wng = wn->wng;
79
80         pfwd->check_fd = 0;
81         if (pfwd->fd <= 0)
82                 return -E_FW_NO_FILE;
83         if (!*wng->loaded)
84                 return 1;
85         para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
86         pfwd->check_fd = 1;
87         return 1;
88 }
89
90 static int file_write_post_select(struct sched *s, struct writer_node *wn)
91 {
92         struct private_file_write_data *pfwd = wn->private_data;
93         struct writer_node_group *wng = wn->wng;
94         int ret;
95
96         if (!pfwd->check_fd)
97                 return 1;
98         if (*wng->loaded <= wn->written)
99                 return 1;
100         if (!FD_ISSET(pfwd->fd, &s->wfds))
101                 return 1;
102 //      PARA_INFO_LOG("writing %zd\n", *wng->loaded);
103         ret = write(pfwd->fd, *wng->bufp + wn->written,
104                 *wng->loaded - wn->written);
105         if (ret < 0)
106                 return -E_FW_WRITE;
107         wn->written += ret;
108         return 1;
109 }
110
111 static void file_write_close(struct writer_node *wn)
112 {
113         struct private_file_write_data *pfwd = wn->private_data;
114         close(pfwd->fd);
115         free(pfwd);
116 }
117
118 __malloc static void *file_write_parse_config(const char *options)
119 {
120         struct file_write_args_info *conf
121                 = para_calloc(sizeof(struct file_write_args_info));
122         int ret = file_cmdline_parser_string(options, conf, "file_write");
123
124         PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given);
125         if (!ret)
126                 return conf;
127         free(conf);
128         return NULL;
129 }
130
131 /** the init function of the file writer */
132 void file_write_init(struct writer *w)
133 {
134         struct file_write_args_info dummy;
135
136         file_cmdline_parser_init(&dummy);
137         w->open = file_write_open;
138         w->pre_select = file_write_pre_select;
139         w->post_select = file_write_post_select;
140         w->parse_config = file_write_parse_config;
141         w->close = file_write_close;
142         w->shutdown = NULL; /* nothing to do */
143         w->help = (struct ggo_help) {
144                 .short_help = file_write_args_info_help,
145                 .detailed_help = file_write_args_info_detailed_help
146         };
147         file_cmdline_parser_free(&dummy);
148 }