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